text:
package org.brunel.data.modify;

import org.brunel.data.Data;
import org.brunel.data.Dataset;
import org.brunel.data.Field;
import org.brunel.data.summary.FieldRowComparison;
import org.brunel.data.Fields;
import org.brunel.data.util.MapInt;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This transform sorts a dataset into an order determined bya  set of fields.
 * Each field can be defined as "increasing" or "decreasing" and the order of fields is important!
 *
 * This class may sort categories of data if the data type allows it (i.e. it is categorical, not ordinal)
 * It will always sort the rows of the data (so a PATH element will use that order, for example)
 */
public class Sort extends DataOperation {

    public static Dataset transform(Dataset base, String command, boolean sortCategories) {
        String[] sortFields = strings(command, ';');
        if (sortFields.length == 0) return base;
        // Build the dimensional information
        Field[] dimensions = getFields(base, sortFields);
        boolean[] ascending = getAscending(dimensions, sortFields);

        // Sort the rows to get the new row order
        int[] rowOrder = new FieldRowComparison(dimensions, ascending, true).makeSortedOrder();

        // Ensure that any data binned to the "..." catch-all category is moved to the end
        for (int i = base.fields.length - 1; i >= 0; i--) {
            Field f = base.fields[i];
            if (f.isBinned() && f.preferCategorical()) rowOrder = moveCatchAllToEnd(rowOrder, f);
        }

        Field[] fields = new Field[base.fields.length];
        for (int i = 0; i < fields.length; i++) {
            Field field = base.fields[i];
            fields[i] = Fields.permute(field, rowOrder, true);
            if (!field.ordered() && sortCategories) {
                Object[] newCategoryOrder = makeOrder(field, dimensions, ascending);
                fields[i].setCategories(newCategoryOrder);
            }
        }
        return base.replaceFields(fields);
    }

    private static Object[] makeOrder(Field field, Field[] dimensions, boolean[] ascending) {

        // Map from field categories to rows for that field
        Map<Object, List<Integer>> categorySums = new HashMap<>();
        for (int i = 0; i < field.rowCount(); i++) {
            Object category = field.value(i);
            if (category == null) continue;
            List<Integer> value = categorySums.get(category);
            if (value == null) {
                value = new ArrayList<>();
                categorySums.put(category, value);
            }
            value.add(i);
        }

        // For each row, create a value
        Object[] categories = field.categories();
        int n = categories.length;
        Object[][] dimensionData = new Object[dimensions.length][n];
        for (int r = 0; r < n; r++) {
            List<Integer> value = categorySums.get(categories[r]);
            for (int i = 0; i < dimensions.length; i++) {
                if (dimensions[i].isNumeric())
                    dimensionData[i][r] = sum(dimensions[i], value);
                else
                    dimensionData[i][r] = mode(dimensions[i], value);
            }
        }
        Field[] summaries = new Field[dimensions.length];
        for (int i = 0; i < dimensions.length; i++) {
            summaries[i] = Fields.makeColumnField("", null, dimensionData[i]);
            if (dimensions[i].isNumeric()) summaries[i].setNumeric();
        }
        int[] order = new FieldRowComparison(summaries, ascending, true).makeSortedOrder();
        Object[] result = new Object[n];
        for (int i = 0; i < n; i++) result[i] = categories[order[i]];
        return result;

    }

    private static Object sum(Field field, List<Integer> rows) {
        double sum = 0;
        for (int i : rows) {
            Double v = Data.asNumeric(field.value(i));
            if (v != null) sum += v;
        }
        return sum;
    }

    private static Object mode(Field field, List<Integer> rows) {
        Object mode = null;
        int max = 0;
        MapInt count = new MapInt();
        for (int i : rows) count.increment(field.value(i));
        return count.mode();
    }

    /* Convert an order (possibly with ties) into a ranking for the original rows */
    static double[] makeRowRanking(int[] order, FieldRowComparison comparison) {
        double[] ranks = new double[order.length];
        int runStart = 0;
        while (runStart < order.length) {
            // Create a run [runStart, runEnd-1] of identical values
            int runEnd = runStart + 1;
            while (runEnd < order.length && comparison.compare(order[runStart], order[runEnd]) == 0) runEnd++;
            double v = (runEnd + runStart + 1) / 2.0;

            // Set the ranks in the result array, incrementing runStart to the runEnd
            while (runStart < runEnd) ranks[order[runStart++]] = v;
        }
        return ranks;
    }

    private static Field[] getFields(Dataset base, String[] names) {
        Field[] fields = new Field[names.length];
        for (int i = 0; i < fields.length; i++) {
            String name = names[i].split(":")[0];
            fields[i] = base.field(name.trim());
            if (fields[i] == null)
                throw new IllegalArgumentException("Could not find field: " + name);
        }
        return fields;
    }

    private static boolean[] getAscending(Field[] dimensions, String[] names) {
        boolean[] ascending = new boolean[dimensions.length];
        for (int i = 0; i < ascending.length; i++) {
            String[] parts = names[i].split(":");
            if (parts.length > 1) {
                String p = parts[1].trim();
                if (p.equalsIgnoreCase("ascending")) ascending[i] = true;
                else if (p.equalsIgnoreCase("descending")) ascending[i] = false;
                else throw new IllegalArgumentException("Sort options must be 'ascending' or 'descending'");
            } else {
                // Dates and categories default to ascending. General numeric values default to descending
                ascending[i] = dimensions[i].isDate() || !dimensions[i].isNumeric();
            }
        }
        return ascending;
    }

    private static int[] moveCatchAllToEnd(int[] order, Field f) {
        int[] result = new int[order.length];
        List<Integer> atEnd = new ArrayList<>();
        int at = 0;
        for (int j : order) {
            if ("\u2026".equals(f.value(j))) atEnd.add(j);
            else result[at++] = j;
        }
        for (Integer i : atEnd) result[at++] = i;
        return result;
    }

    // Each row gets the sum rank
    static Object[] categoriesFromRanks(Field field, double[] rowRanking) {
        double n = field.rowCount();                                                // # rows
        Object[] categories = field.categories();                                   // categories
        int[] counts = (int[]) field.property("categoryCounts");                    // category counts
        Double[] means = new Double[counts.length];                                 // sums of ranks
        for (int i = 0; i < means.length; i++) means[i] = i / 100.0 / means.length; // Bias towards current order

        // Map categories to an index
        MapInt index = new MapInt().index(categories);

        // Sum ranks for this category
        for (int i = 0; i < n; i++) {
            Object o = field.value(i);
            if (o == null) continue;
            int idx = index.get(o);
            means[idx] += rowRanking[i] / counts[idx];
        }

        // Get the order (biggest first)
        Integer[] which = Data.order(means, true);

        // Build the sorted category list
        Object[] cats = new Object[which.length];
        for (int i = 0; i < which.length; i++)
            cats[i] = categories[which[i]];

        return cats;
    }
}


text:
<?php

namespace Creios\Routerunner\Components;

/**
 * Class Call
 * @package Creios\Routerunner
 */
class Call
{

    /**
     * @var string
     */
    private $controller;
    /**
     * @var string
     */
    private $method;

    /**
     * Call constructor.
     * @param string $controller
     * @param string $method
     */
    public function __construct($controller, $method)
    {
        $this->controller = $controller;
        $this->method = $method;
    }

    /**
     * @return string
     */
    public function getController()
    {
        return $this->controller;
    }

    /**
     * @return string
     */
    public function getMethod()
    {
        return $this->method;
    }

}

redpajama_set_name: RedPajamaGithub


text:
import { localize } from 'vs/nls';
import { IQuickPickSeparator } from 'vs/platform/quickinput/common/quickInput';
import { IPickerQuickAccessItem, PickerQuickAccessProvider, TriggerAction } from 'vs/platform/quickinput/browser/pickerQuickAccess';
import { matchesFuzzy } from 'vs/base/common/filters';
import { ITerminalService } from 'vs/workbench/contrib/terminal/browser/terminal';
import { ICommandService } from 'vs/platform/commands/common/commands';
import { TerminalCommandId } from 'vs/workbench/contrib/terminal/common/terminal';
import { ThemeIcon } from 'vs/platform/theme/common/themeService';
import { killTerminalIcon, renameTerminalIcon } from 'vs/workbench/contrib/terminal/browser/terminalIcons';

export class TerminalQuickAccessProvider extends PickerQuickAccessProvider<IPickerQuickAccessItem> {

	static PREFIX = 'term ';

	constructor(
		@ITerminalService private readonly _terminalService: ITerminalService,
		@ICommandService private readonly _commandService: ICommandService,
	) {
		super(TerminalQuickAccessProvider.PREFIX, { canAcceptInBackground: true });
	}

	protected _getPicks(filter: string): Array<IPickerQuickAccessItem | IQuickPickSeparator> {
		const terminalPicks: Array<IPickerQuickAccessItem | IQuickPickSeparator> = [];

		const terminalGroups = this._terminalService.terminalGroups;
		for (let groupIndex = 0; groupIndex < terminalGroups.length; groupIndex++) {
			const terminalGroup = terminalGroups[groupIndex];
			for (let terminalIndex = 0; terminalIndex < terminalGroup.terminalInstances.length; terminalIndex++) {
				const terminal = terminalGroup.terminalInstances[terminalIndex];
				const label = `$(${terminal.icon?.id}) ${groupIndex + 1}.${terminalIndex + 1}: ${terminal.title}`;

				const highlights = matchesFuzzy(filter, label, true);
				if (highlights) {
					terminalPicks.push({
						label,
						highlights: { label: highlights },
						buttons: [
							{
								iconClass: ThemeIcon.asClassName(renameTerminalIcon),
								tooltip: localize('renameTerminal', "Rename Terminal")
							},
							{
								iconClass: ThemeIcon.asClassName(killTerminalIcon),
								tooltip: localize('killTerminal', "Kill Terminal Instance")
							}
						],
						trigger: buttonIndex => {
							switch (buttonIndex) {
								case 0:
									this._commandService.executeCommand(TerminalCommandId.Rename, terminal);
									return TriggerAction.NO_ACTION;
								case 1:
									terminal.dispose(true);
									return TriggerAction.REMOVE_ITEM;
							}

							return TriggerAction.NO_ACTION;
						},
						accept: (keyMod, event) => {
							this._terminalService.setActiveInstance(terminal);
							this._terminalService.showPanel(!event.inBackground);
						}
					});
				}
			}
		}

		if (terminalPicks.length > 0) {
			terminalPicks.push({ type: 'separator' });
		}

		const createTerminalLabel = localize("workbench.action.terminal.newplus", "Create New Terminal");
		terminalPicks.push({
			label: `$(plus) ${createTerminalLabel}`,
			ariaLabel: createTerminalLabel,
			accept: () => this._commandService.executeCommand(TerminalCommandId.New)
		});
		const createWithProfileLabel = localize("workbench.action.terminal.newWithProfilePlus", "Create New Terminal With Profile");
		terminalPicks.push({
			label: `$(plus) ${createWithProfileLabel}`,
			ariaLabel: createWithProfileLabel,
			accept: () => this._commandService.executeCommand(TerminalCommandId.NewWithProfile)
		});

		return terminalPicks;

	}
}


text:
<bill session="115" type="h" number="3851" updated="2017-10-04T16:15:17Z">
  <state datetime="2017-09-27">REFERRED</state>
  <status>
    <introduced datetime="2017-09-27"/>
  </status>
  <introduced datetime="2017-09-27"/>
  <titles>
    <title type="short" as="introduced">War Crimes Rewards Expansion Act</title>
    <title type="short" as="introduced">War Crimes Rewards Expansion Act</title>
    <title type="official" as="introduced">To amend the State Department Basic Authorities Act of 1956 to provide for rewards for the arrest or conviction of certain foreign nationals who have committed genocide or war crimes, and for other purposes.</title>
    <title type="display">War Crimes Rewards Expansion Act</title>
  </titles>
  <sponsor bioguide_id="F000450"/>
  <cosponsors>
    <cosponsor bioguide_id="E000179" joined="2017-09-27"/>
    <cosponsor bioguide_id="H001059" joined="2017-09-27"/>
    <cosponsor bioguide_id="M000312" joined="2017-09-27"/>
  </cosponsors>
  <actions>
    <action datetime="2017-09-27">
      <text>Introduced in House</text>
    </action>
    <action datetime="2017-09-27" state="REFERRED">
      <text>Referred to the House Committee on Foreign Affairs.</text>
    </action>
  </actions>
  <committees>
    <committee subcommittee="" code="HSFA" name="House Foreign Affairs" activity="Referral"/>
  </committees>
  <relatedbills/>
  <subjects>
    <term name="International affairs"/>
    <term name="Criminal investigation, prosecution, interrogation"/>
    <term name="Evidence and witnesses"/>
    <term name="War crimes, genocide, crimes against humanity"/>
  </subjects>
  <amendments/>
  <committee-reports/>
</bill>


text:
# Centromere Documentation

#### [Introduction](#introduction-1)
- [About](#about)
- [Demo](#demo)
- [License](#license)
- [Requirements](#requirements)
- [Maven Artifacts](#maven-artifacts)

#### [Getting Started](#getting-started-1)
- [Data Models](#data-models)
- [Repositories](#repositories)
- [Data Import](#data-import)
- [Web Service Controllers](#web-service-controllers)

#### [REST API](#rest-api-1)

#### [Modules](#modules-1)
- [Centromere Core](#centromere-core)
  - [Customizing Model Classes](#customizing-model-classes)
- [Centromere MongoDB](#centromere-mongodb)
  - [MongoDB Repositories](#mongodb-repositories)
- [Centromere SQL](#centromere-sql)
  - [SQL Repositories](#sql-repositories)
- [Centromere JPA](#centromere-jpa)
  - [JPA Repositories](#jpa-repositories)
- [Centromere Data Import CLI](#centromere-data-import-cli)
  - [Import Configuration](#import-configuration)
  - [Running the Import](#running-the-import)
- [Centromere Web](#centromere-web)
  - [Web Service Configuration](#web-service-configuration)

# Introduction

## About

Centromere is a set of tools for developing scalable data warehouses and RESTful web services.  It is designed to tackle the problems inherent in developing data warehouses for genomic data, where data structures can vary greatly and scale fast, and where use-cases must be flexible to accommodate shifting business needs.  Centromere is developed in Java using the open-source, enterprise-grade Spring Framework, and supports integration with multiple database technologies.  You can use Centromere to create a new data warehouse from scratch, or bootstrap one or more existing databases, and make your data available via a customizable REST API.

Here are a few ways Centromere can help make your data warehouse and REST API better:
- Support classes for quickly creating data models and data access objects (DAOs).
- Components for reading standard column- and row-based data files, and rapidly developing extract-transform-load (ETL) pipelines.
- Spring Boot initializer for exposing your data repository as a REST web service with:
  - A flexible and easy-to-use query API
  - Simple token-based security
  - Automatic API documentation with [Swagger](https://swagger.io/)
  - Support for exporting data in JSON, XML and tab-delimited-table formats.

Centromere does _**not**_ contain a pre-defined data model, but rather provides you the tools to quickly generate your own model and expose your data through scalable repositories and web services.

## Requirements

These are the current requirements for developing data warehouses with Centromere:

- Java JDK 8+
- Maven 3
- MongoDB 3+

\* Support for additional database technologies is coming soon.

## Maven Artifacts

Artifacts for Centromere release builds are available from the Maven Central Repository:

```xml
COMING SOON
```

## License

Centromere is licensed under the Apache License, version 2.0:

> Copyright 2018 William Oemler, Blueprint Medicines

> Licensed under the Apache License, Version 2.0 (the "License");
> you may not use this file except in compliance with the License.
> You may obtain a copy of the License at

> http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

# Getting Started

### Data Models

The core of every Centromere data warehouse are the data model classes, which are represented as implementations of the `Model` interface.  The entities serve as data transfer objects (DTOs) to move data between application layers, and define how data is queried and represented in the web services.  While models can themselves represent aggregate data or database views, they should be considered atomic entities when reading and serving data through the web API.  Each model will have its own data access object (DAO) repository class and REST endpoint controller class.

The `Model` interface looks like this:

```java
@Filterable
public interface Model<ID extends Serializable> {
	ID getId();
}
```

A basic `Model` implementation looks like a plain old java object (POJO):

```java
/* Simple database-agnostic model */
public class Gene implements Model<String> {

	private String id;
	private Long publicId;
	private String geneSymbol;
	private String species;
	private String chromosome;
	private Set<String> aliases;
	private Map<String,String> attributes;

	public String getId() { return id; }

	/* Getters and Setters */
}
```

The `Model` interface defines three thing about all model classes:

- They must have an identifying attribute, which is intended to serve as the primary key ID (in this case, a `String` representation of a MongoDB `ObjectID`).
- This identifier must be a serializable object, for the sake of representation in a URL.
- The attributes in the object may be filtered in web service responses, using object transformers like Jackson.

Model object persistence will be handled depending on the database back-end and integration module being used (eg. `centromere-mongodb` or `centromere-jpa`).  For some implementations, the above `Model` example is all you need to get started, but sometimes additional customization is required to get the maximum utility:

```java
/* MongoDB model */
@Document(collection = "genes")
public class Gene implements Model<String> {

	@Id private String id;
	@Indexed private Long publicId;
	@Indexed private String geneSymbol;
	private String species;
	private String chromosome;
	private Set<String> aliases;
	private Map<String,String> attributes;

	public String getId() { return id; }

	/* Getters and Setters */
}
```

### Repositories

All Centromere repository classes implement the base `RepositoryOperations` interface, which defines all of the basic CRUD operations that all Centromere repositories should dataSetSupport.  This interface is based on Spring Data's `PagingAndSortingRepository`, but with some additional method definitions for dynamic query operations.  The database-specific implementations also include several methods specific to those data stores.  A MongoDB repository implementation for the above `Gene` model class might look like this:

```java
@ModelRepository(Gene.class)
public class GeneRepository extends GenericMongoRepository<Gene, String> {
	@Autowired
	public GeneRepository(MongoTemplate mongoTemplate) {
		super(mongoTemplate);
	}
}
```

Dynamic repository queries in Centromere are created by chaining a series of query operations, represented by the `QueryCriteria` class.  These operations are defined by the field to be queried, the value of the field, and the operator to be used to make the evaluation.  For example:

```java
QueryCriteria criteria = new QueryCriteria("publicId", 1L, Evaluation.EQUALS)

/*
Will be translated based upon the database implementation to:
   WHERE publicId = 1 # for SQL
   or
   {"publicId": 1} # for MongoDB
*/

```

### Data Import

The `centromere-core` module contains a number of simple classes intended to aid in the development of data import pipelines.  The core of this are four basic interfaces: `RecordReader`, `RecordWriter`, `RecordImporter`, and Spring's `Validator`.  When combined with a `RecordProcessor`, these components create a utility for importing a specific data type input into database records.  For example, components for importing Entrez Gene records into a MongoDB database might look like this:

```java
/* RecordReaders take an input data source and return Model objects.*/
public class GeneInfoReader extends AbstractRecordFileReader<Gene> {

	@Override
	public Gene readRecord()  {
		Gene gene = null;
		String line;
		try {
			boolean flag = true;
			while(flag) {
				line = this.getReader().readLine();
				if (line == null || !line.startsWith("#Format: tax_id GeneID")) {
					flag = false;
					if (line != null && !line.equals("")) gene = getRecordFromLine(line);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return gene;
	}

	private Gene getRecordFromLine(String line) {
		String[] bits = line.split("\\t");
		Gene gene = new Gene();
		gene.setSpecies(Integer.parseInt(bits[0]));
		gene.setPublicId(Long.parseLong(bits[1]));
		gene.setGeneSymbol(bits[2]);
		gene.setAliases(new HashSet<>(Arrays.asList(bits[3].split("\\|"))));
		gene.setChromosome(bits[5]);
		return gene;
	}

}

/* Validators asses whether a Model object has been correctly constructed.*/
public class GeneValidator implements Validator {

	public boolean supports(Class<?> aClass) {
		return aClass.equals(Gene.class);
	}

	public void validate(Object o, Errors errors) {
		Gene gene = (Gene) o;
		ValidationUtils.rejectIfEmptyOrWhitespace(errors,  "geneSymbol", "symbol.empty");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors,  "publicId", "publicId.empty");
	}
}

/* RecordWriters take Model objects and write them to temporary files or directly to a database.*/
@Component
public class GeneRepositoryWriter extends RepositoryRecordWriter<Gene> {
	@Autowired
	public GeneRepositoryWriter(GeneRepository repository) {
		super(repository);
	}
}

/* RecordProcessors tie all of these components together to import a specific data type. */
@Component
public class GeneInfoProcessor extends GenericRecordProcessor<Gene> {
	@Autowired
	public GeneInfoProcessor(GeneRepositoryWriter writer) {
		super(new GeneInfoReader(), new GeneValidator(), writer);
	}
}
```

### Web Service Controllers

The web services controller layer handles HTTP requests and routes them to the appropriate repository implementation.  Web service controllers dataSetSupport standard CRUD operations via `GET`, `POST`, `PUT`, and `DELETE` methods. `HEAD` and `OPTIONS` methods are also supported for the purpose of exposing additional resource endpoint information.

```java
@Controller
@ExposesResourcesFor(Gene.class)
@RequestMapping(value = "/api/genes")
public class GeneController extends CrudApiController<Gene, String> {

	@Autowired
	public GeneController(GeneRepository repository, EntityLinks entityLinks) {
		super(repository, Gene.class,
		new ModelResourceAssembler(GeneController.class, Gene.class, entityLinks));
	}

}
```

# REST API

Once your application is up-and-running, you can reach your data using the relative root URLs specified in your controller classes.  For example:

Method | URI | Description
-------|-----|------------
`GET` | `/genes` | Fetches all Genes
`GET` | `/genes/{id}` | Fetch a single Gene by ID
`POST` | `/genes` | Creates a new Gene record
`PUT` | `/genes/{id}` | Updates an existing Gene
`DELETE` | `/genes/{id}` | Deletes an existing Gene
`OPTIONS` | `/genes` | Fetches info about the available Gene operations

#### Media Types

Centromere supports the following media types for `GET` requests: `application/json`, `application/hal+json`, `application/xml`, `application/hal+xml`, and `plain/text`.  If no return type is specified, `application/json` will be used by default.  The media type `plain/text` returns the response data as a tab-delimited text table.  Only requests for the media types `application/hal+json` and `application/hal+xml` will return HATEOAS links.

#### Searching

Centromere supports dynamic query operations using query string parameters.  The available query parameters for each resource are defined in the model class (see the documentation for the `centromere-core` module). You can perform queries using one or more entity attributes in the standard way:

```
GET /genes?name=akt,mtor,braf
GET /cnv?valueBetween=-0.5,0.5
GET /samples?tissue=lung&type=cellLine
```

#### Paging and Sorting

Requests can return results that are both paginated and sorted.  Centromere uses the default Spring Data URI query parameter syntax:

```
GET /genes?size=100&page=2&sort=entrezGeneId,asc
```

Page numbering starts from zero, and the default page size is 1000 records.  If sorting by multiple fields, use multiple `sort` parameters in your request.  The sorts will be applied in the order given:

```
# Will first sort by `type`, then by `entrezGeneId`
GET /genes?sort=type,desc&sort=entrezGeneId,asc
```

#### Field Filtering

Requests can specify which entity fields will be returned, or excluded:

```
GET /genes?fields=entrezGeneId,primaryGeneSymbol
GET /genes?excludedFields=description,links
```

#### Hypermedia

For hypermedia dataSetSupport, use the `application/hal+json` or `application/hal+xml` media types to include embedded HAL-formatted links to related entities, allowing for easy resource discovery:

```
Request:
GET /hgu133/rma

Response:
200 OK
[
	{
		sampleId: 123,
		entrezGeneId: 207,
		dataSetId: 43,
		value: 103.12
		links: [
			{ rel: "self", href: "http://myapp/hgu133/rma?sampleId=123&entrezGeneId=207&dataSetId=43"  },
			{ rel: "sample", href: "http://myapp/samples/123"  },
			{ rel: "gene", href: "http://myapp/genes/207"  },
			{ rel: "data_file", href: "http://myapp/datafiles/43"  }
		]
	}, ...
]
```

#### Compression

All responses can be GZIP compressed by including the `Accept-Encoding: gzip,deflate` header:

```
curl -s -H "Accept-Encoding: gzip,deflate" http://myserver/api/genes > genes.gz
```

# Modules

## Centromere Core

### Customizing Model Classes

By default, all fields in classes that implement `Model` are exposed as valid query string parameters in the web services layer.  You can further customize a resource's query parameters by applying several annotations:

```java
public class Gene implements Model<String> {

	private String id;
	private Long entrezGeneId;
	@Alias("symbol") private String primaryGeneSymbol;
	private Integer taxId;
	@Ignored private String locusTag;
	private String chromosome;
	@Ignored private String chromosomeLocation;
	@Ignored private String description;
	private String geneType;
	@Alias("name") private Set<String> aliases;
	@Aliases({
		@Alias(value = "isKinase", fieldName = "attributes.kinase"),
		@Alias(value = "isCgcGene", fieldName = "attributes.cgcGene")
	})
	private Map<String,String> attributes;

	/* Getters and Setters */
}
```

The `Alias` annotation allows query string parameters to map to entity fields of a different name, or to nested fields, with an optional value of `Evaluation`, different from the standard equality test.

It is also possible to customize HATEOAS link generation in the web service layer by using the `ForeignKey` annotation:

```java
public class CopyNumber implements Model<String> {

	private String id;

	@ForeignKey(model = Sample.class, relationship = ForeignKey.Relationship.MANY_TO_ONE, rel = "sample")
	private String sampleId;

	@ForeignKey(model = Gene.class, relationship = ForeignKey.Relationship.MANY_TO_ONE,
			rel = "gene", field = "entrezGeneId")
	private String geneId;

	@Aliases({
			@Alias(value = "signalGreaterThan", evaluation = Evaluation.GREATER_THAN),
			@Alias(value = "signalLessThan", evaluation = Evaluation.LESS_THAN),
			@Alias(value = "signalBetween", evaluation = Evaluation.BETWEEN),
			@Alias(value = "signalOutside", evaluation = Evaluation.OUTSIDE_INCLUSIVE)
	})
	private Double signal;

	/* Getters and Setters */

}
```

All model classes will have `self` links created when HATEOAS-supported media types are requested in the web services layer.  Classes with `ForeignKey`-annotated field will also get links generated based upon the relationship described in the annotation parameters:

```javascript
{
	"id": "123",
	"sampleId": "456",
	"geneId": 789,
	"signal": 2.45,
	"links": [
		{ "rel": "self", "href": "/api/cnv/123" },
		{ "rel": "sample", "href": "/api/samples/456" },
		{ "rel": "gene", "href": "/api/genes?entrezGeneId=789" }
	]
}
```

## Centromere MongoDB

### MongoDB Repositories

The `GenericMongoRepository` is the MongoDB implementation of `RepositoryOperations`, utilizing Spring Data MongoDB's `MongoTemplate` for query execution and object mapping.  Before creating repository classes, you should configure your database connection:

```java
/* Example configuration for a MongoDB 3.x instance */
@Configuration
@PropertySource({ "classpath:mongodb-data-source.properties" })
public class MongoConfig extends AbstractMongoConfiguration {

	@Autowired private Environment env;

	@Override
	public String getDatabaseName() {
		return env.getRequiredProperty("mongo.name");
	}

	@Override
	@Bean
	public Mongo mongo() throws Exception {
		ServerAddress serverAddress = new ServerAddress(env.getRequiredProperty("mongo.host"));
		List<MongoCredential> credentials = new ArrayList<>();
		credentials.add(MongoCredential.createScramSha1Credential(
				env.getRequiredProperty("mongo.username"),
				env.getRequiredProperty("mongo.name"),
				env.getRequiredProperty("mongo.password").toCharArray()
		));
		return new MongoClient(serverAddress, credentials);
	}

}
```

A simple implementation of a MongoDB repository class looks like this:

```java
@Repository
public class GeneRepository extends GenericMongoRepository<Gene, String> {
	@Autowired
	public GeneRepository(MongoTemplate mongoTemplate) {
		super(mongoTemplate, Gene.class);
	}
}
```

You can also expand on the basic operation set of the repository class by defining your own methods:

```java

@Repository
public class GeneRepository extends GenericMongoRepository<Gene, String> {

    @Autowired
	public GeneRepository(MongoTemplate mongoTemplate) {
		super(mongoTemplate, Gene.class);
	}

    /* Using the MongoDB driver `Query` class and API */

    public List<Gene> findByEntrezGeneId(Long entrezGeneId) {
        return this.getMongoOperations()
            .find(new Query(Criteria.where("entrezGeneId").is(entrezGeneId));
    }

    /* Using `QueryCriteria` and the Centromere repository API */

    public List<Gene> findByGeneSymbolAlias(String name) {
        return this.find(new QueryCriteria("aliases", name, Evaluation.EQUALS));
    }

}

```

## Centromere SQL

### SQL Repositories

The `GenericJdbcRepository` is the JDBC SQL database implementation of `RepositoryOperations`.  This repository implementation is based on  `com.nurkiewicz.jdbcrepository.JdbcRepository`, but uses a more complex version of the `TableDescription` class, and a custom SQL generation class, `SqlBuilder`.  Much like with `JdbcRepository`, you define a `GenericJdbcRepository` using a `ComplexTableDescription`, `RowMapper`, and optional `RowUnmapper`.  For example:

```java
/* Subject data stored in a single table */
@Repository
public SubjectRepository extends GenericJdbcRepository<Subject, Integer> {

	@Autowired
	public SubjectRepository(DataSource dataSource) {
	    super(dataSource, new SubjectTableDescription(), new SubjectMapper(), new SubjectUnmapper());
	}

	public static class SubjectTableDescription extends ComplexTableDescription {
		public SubjectTableDescription() {
			super(
				"subjects", // table name
				Arrays.asList("subject_id") // primary key ID columns
			);
		}
	}

	public static class SubjectMapper implements RowMapper<Subject> {
		@Override
		public Subject mapRow(ResultSet rs, int i) {
			Subject subject = new Subject();
			subject.setId(rs.getInt("subject_id"));
			subject.setName(rs.getString("name"));
			subject.setAge(rs.getInt("age"));
			subject.setGender(rs.getString("gender"));
			List<Attributes> attributes = new ArrayList();
			if (rs.getString("attributes") != null) {
				for (String attribute: rs.getString("attributes").split(":::")) {
					String[] bits = attributes.split("::");
					attributes.add(new Attribute(bits[0], bits[1]));
				}
			}
			subject.setAttributes(attributes);
			return subject;
		}
	}

	public static class SubjectUnmapper implements RowUnmapper<Subject> {
		@Override
		public Map<String,Object> mapColumns(Subject subject) {
			Map<String,Object> map = new HashMap();
			map.put("subject_id", subject.getId());
			map.put("name", subject.getName());
			map.put("age", subject.getAge());
			map.put("gender", subject.getGender());
			boolean flag = false;
			StringBuilder sb = new StringBuilder();
			for (Attribute attribute: subject.getAttributes()) {
				if (flag) {
					sb.append(":::");
				}
				flag = true;
				sb.append(attribute.getName()).append("::").append(attribute.getValue());
			}
			map.put("attributes", sb.toString());
			return map;
		}
	}

}
```

If the `RowUnmapper` is left out, the repository will be read-only, and all `insert` or `update` method calls will result in an exception.

Storing `Subject` records with their `Attributes` in a single table is simple enough, but it is not ideal.  The better solution would be to create a separate table, `subject_attributes`, with a many-to-one relationship with the `subjects` table.  This would allow you to index the attribute names for easier queries:

```java
/* Subject data stored in two MySQL tables */
@Repository
public SubjectRepository extends GenericJdbcRepository<Subject, Integer> {

	@Autowired
	public SubjectRepository(DataSource dataSource) {
	    super(dataSource, new SubjectTableDescription(), new SubjectMapper());
	}

	@Override
	public <S extends Subject> S insert(S entity) {

    		KeyHolder keyHolder = new GeneratedKeyHolder();
    		this.getJdbcTemplate().update(
    				new PreparedStatementCreator() {
    					@Override
    					@SuppressWarnings("JpaQueryApiInspection")
    					public PreparedStatement createPreparedStatement(Connection connection) throws
    							SQLException {
    						PreparedStatement ps = connection.prepareStatement(
    								"INSERT INTO `subjects` (name, gender, age) VALUES (?, ?, ?);",
    								new String[] {"id"}
    						);
    						ps.setString(1, entity.getName());
    						ps.setString(2, entity.getGender());
    						ps.setInt(3, entity.getAge());
    						return ps;
    					}
    				},
    				keyHolder
    		);
    		Integer subjectId = keyHolder.getKey().intValue();
    		entity.setId(Integer.toString(geneId));

    		if (entity.getAttributes() != null) {
    			for (Attribute attribute : entity.getAttributes()) {
    				this.getJdbcTemplate().update(
    						"INSERT INTO `subject_attributes` (subject_id, name, value) VALUES (?, ?, ?)",
    						subjectId, attribute.getName(), attribute.getValue());
    			}
    		}

    		return entity;

    	}

	public static class SubjectTableDescription extends ComplexTableDescription {
		public SubjectTableDescription() {
			super(
				"subjects", // table name
				Arrays.asList("s.subject_id"), // primary key IDs
				"s.*, GROUP_CONCAT(CONCAT(a.name, '::', a.value) SEPARATOR ':::') as attributes", // SELECT statement
				"subjects s left join subject_attributes a on s.subject_id = a.subject_id", // FROM statement
				"s.subject_id" // GROUP BY statement
			);
		}
	}

	public static class SubjectMapper implements RowMapper<Subject> {
		@Override
		public Subject mapRow(ResultSet rs, int i) {
			Subject subject = new Subject();
			subject.setId(rs.getInt("subject_id"));
			subject.setName(rs.getString("name"));
			subject.setAge(rs.getInt("age"));
			subject.setGender(rs.getString("gender"));
			List<Attributes> attributes = new ArrayList();
			if (rs.getString("attributes") != null) {
				for (String attribute: rs.getString("attributes").split(":::")) {
					String[] bits = attributes.split("::");
					attributes.add(new Attribute(bits[0], bits[1]));
				}
			}
			subject.setAttributes(attributes);
			return subject;
		}
	}

}
```

Now we have a CRUD repository that pulls data from two tables into a single model class.

## Centromere JPA

### JPA Repositories

The `GenericMongoRepository` is the MongoDB implementation of `RepositoryOperations`, utilizing Spring Data MongoDB's `MongoTemplate` for query execution and object mapping.  Before creating repository classes, you should configure your database connection:

```java
/* Example configuration for a MongoDB 3.x instance */
@Configuration
@PropertySource({ "classpath:mongodb-data-source.properties" })
public class MongoConfig extends AbstractMongoConfiguration {

	@Autowired private Environment env;

	@Override
	public String getDatabaseName() {
		return env.getRequiredProperty("mongo.name");
	}

	@Override
	@Bean
	public Mongo mongo() throws Exception {
		ServerAddress serverAddress = new ServerAddress(env.getRequiredProperty("mongo.host"));
		List<MongoCredential> credentials = new ArrayList<>();
		credentials.add(MongoCredential.createScramSha1Credential(
				env.getRequiredProperty("mongo.username"),
				env.getRequiredProperty("mongo.name"),
				env.getRequiredProperty("mongo.password").toCharArray()
		));
		return new MongoClient(serverAddress, credentials);
	}

}
```

A simple implementation of a MongoDB repository class looks like this:

```java
@Repository
public class GeneRepository extends GenericMongoRepository<Gene, String> {
	@Autowired
	public GeneRepository(MongoTemplate mongoTemplate) {
		super(mongoTemplate, Gene.class);
	}
}
```

You can also expand on the basic operation set of the repository class by defining your own methods:

```java

@Repository
public class GeneRepository extends GenericMongoRepository<Gene, String> {

    @Autowired
	public GeneRepository(MongoTemplate mongoTemplate) {
		super(mongoTemplate, Gene.class);
	}

    /* Using the MongoDB driver `Query` class and API */

    public List<Gene> findByEntrezGeneId(Long entrezGeneId) {
        return this.getMongoOperations()
            .find(new Query(Criteria.where("entrezGeneId").is(entrezGeneId));
    }

    /* Using `QueryCriteria` and the Centromere repository API */

    public List<Gene> findByGeneSymbolAlias(String name) {
        return this.find(new QueryCriteria("aliases", name, Evaluation.EQUALS));
    }

}
```

## Centromere Data Import CLI

### Import Configuration

The `centromere-data-import-cli` module makes use of the component classes defined in the `centromere-core` module.  To create a command line import tool for your data import components, you must first define a configuration class that extends `DataImportConfigurer`:

```java
@Configuration
@ComponentScan(basePackages = { "me.woemler.dataimport" })
public class ImportConfig extends DataImportConfigurer {

}
```

The `DataImportConfigurer` will initialize several beans that help manage the import process, including the `DataImportManager`, which handles mapping your implemented `RecordProcessor` classes to user-inputted data files on the command line.  By default, the `DataImportManager` will pick up on all `RecordProcessor` instances that are annotated with the `@DataTypes` annotation and create an association between the provided data type labels and their processor classes.  You can append-to or overwrite the default data type and data set mapping behavior by overriding the xxx methods:

```java
@Configuration
@ComponentScan(basePackages = { "me.woemler.dataimport" })
public class ImportConfig extends DataImportConfigurer {

    @Autowired private ApplicationContext context;

    @Override
    public Map<String, DataSetMetadata> configureDataSetMappings(Map<String, DataSetMetadata> dataSetMap) {
        dataSetMap.put("test", new BasicDataSetMetadata(xxx));
        return dataSetMap;
    }

    @Override
    public Map<String, RecordProcessor> configureDataTypeMappings(Map<String, RecordProcessor> dataTypeMap) {
        dataTypeMap.put("mutations", context.getBean(MutationProcessor.class));
        return dataTypeMap;
    }

}
```

The `DataImportConfigurer` also creates an instance of a `CommandLineRunner` bean, which will accept and parse command line arguments, and then execute the appropriate actions.  This module utilizes [JCommander](http://jcommander.org/) to define and parse command line arguments, and the command line arguments are defined within the `ImportCommandArguments` and `AddCommandArguments` classes.  To utilize the default command line behavior, make use of the `CommandLineRunner` instance in your main class:

```java
public class Main {
    public static void main(String[] args) throws Exception {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ImportConfig.class);
        CommandLineRunner runner = context.getBean(CommandLineRunner.class);
        runner.run(args);
    }
}
```

### Running the Import

Data import tools tools built with `centromere-data-import-cli` can run as executable JAR files on command line.  When run with the default arguments and configuration, such as is described above, the command line tool syntax is as follows:

```
# Import command
Usage: import [options]
  Options:
    -d, --data-set
       Data set label or JSON representation.  If not provided, no data set will
       be associated with the file.
  * -t, --data-type
       Data type label for the target file.  Required.
  * -i, --input
       File to be imported.  Required.
    --skip-invalid-data-sets
       When true, records and files associated with invalid or existing data
       sets will be skipped, rather than throw an exception.
       Default: false
    --skip-invalid-genes
       When true, records that do not match a valid gene will be skipped, rather
       than throw an exception.
       Default: false
    --skip-invalid-records
       When true, records that fail validation will be skipped, rather than
       throwing an exception.
       Default: false
    --skip-invalid-samples
       When true, records that do not match valid samples will be skipped,
       rather than throw an exception.
       Default: false
    -T, --temp-dir
       Directory to write temporary files to.  Defaults to '/tmp'.
       Default: /tmp

# Add command
Usage: add category label body
  Positional arguments:
    category
      Used to specify the record type to be added.  Currently supports 'data_type'
      and 'data_set'.
    label
      Unique string identifier to associate with the record to be added.  Used
      for identification in the import command.
    body
      Content of the record.  For the 'data_type' category, this should be the
      name of a RecordProcessor class or existing bean.  For the 'data_set'
      category, this should be a JSON representation of the data set metadata.
```

## Centromere Web

### Web Service Configuration

The easiest way to configure Centromere is to use the available auto-configuration annotations in a Spring Boot application class or supporting configuration class: `@AutoConfigureWebServices`, `@AutoConfigureWebSecurity`, and `@AutoConfigureApiDocumentation`.  This will handle all of the required web context configuration and bean registration for the web services, API documentation, and security features:

```java
@Configuration
@AutoConfigureWebServices
@AutoConfigureWebSecurity
@AutoConfigureApiDocumentation
@ComponentScan(basePackages = { "me.woemler.myapp" })
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

	@Override
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
		return application.sources(Application.class);
	}

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}
```


text:
@interface IQURLConnection ()<NSURLSessionDataDelegate>
{
    NSError *_customError;
    NSMutableData *_data;
}

@property(nonatomic, strong) NSURLSession *session;

@end

@implementation IQURLConnection

#pragma mark - Initializers

+ (nonnull instancetype)sendAsynchronousRequest:(nonnull NSMutableURLRequest *)request
                                  responseBlock:(void (^ __nullable)(NSHTTPURLResponse* _Nullable response))responseBlock
                            uploadProgressBlock:(void (^ __nullable)(CGFloat progress))uploadProgress
                          downloadProgressBlock:(void (^ __nullable)(CGFloat progress))downloadProgress
                              completionHandler:(void (^ __nullable)(NSData* _Nullable result, NSError* _Nullable error))completion
{
    IQURLConnection *asyncRequest = [[IQURLConnection alloc] initWithRequest:request isBackgroundTask:NO resumeData:nil responseBlock:responseBlock uploadProgressBlock:uploadProgress downloadProgressBlock:downloadProgress completionBlock:completion];
    [asyncRequest start];

    return asyncRequest;
}

- (instancetype _Nonnull )initWithRequest:(NSMutableURLRequest *_Nonnull)request
                         isBackgroundTask:(BOOL)isBackgroundTask
                               resumeData:(NSData*_Nullable)dataToResume
                            responseBlock:(void (^ __nullable)(NSHTTPURLResponse* _Nullable response))responseBlock
                      uploadProgressBlock:(void (^ __nullable)(CGFloat progress))uploadProgress
                    downloadProgressBlock:(void (^ __nullable)(CGFloat progress))downloadProgress
                          completionBlock:(void (^ __nullable)(NSData* _Nullable result, NSError* _Nullable error))completion
{
    if ([dataToResume length])
    {
        [request addValue:[NSString stringWithFormat: @"bytes=%lu-",(unsigned long)[dataToResume length]] forHTTPHeaderField:@"Range"];
    }

    if (self = [super init])
    {
        _isBackgroundTask = isBackgroundTask;

        if (isBackgroundTask && request.HTTPBody)
        {
            //Creating directory for saving file
            NSString *documentsDirectory = [[self class] backgroundSessionFilesDirectory];

            NSString *fileName = [[NSUUID UUID] UUIDString];
            NSString *filePath = [documentsDirectory stringByAppendingPathComponent:fileName];
            NSURL *fileURL = [NSURL fileURLWithPath:filePath];

            [request.HTTPBody writeToURL:fileURL atomically:YES];

            //If file created successfully, then uploading using background session
            if ([[NSFileManager defaultManager] fileExistsAtPath:filePath])
            {
                static int i = 0;
                i++;
                NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
                NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[bundleIdentifier stringByAppendingFormat:@".background%d",i]];
                _session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];

                _task = [_session uploadTaskWithRequest:request fromFile:fileURL];
            }
            //else uploading through default session
            else
            {
                NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
                _session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
                _task = [_session uploadTaskWithRequest:request fromData:request.HTTPBody];
            }
        }
        else
        {
            NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
            _session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
            _task = [_session dataTaskWithRequest:request];
        }

        _uploadProgressBlock = uploadProgress;
        _downloadProgressBlock = downloadProgress;
        _dataCompletionBlock = completion;
        _responseBlock = responseBlock;

        _data = [[NSMutableData alloc] initWithData:dataToResume];
    }
    return self;
}

#pragma mark - Getters

+(NSString*)backgroundSessionFilesDirectory
{
    //Creating directory for saving file
    NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
    NSString *backgroundDirectory = [documentsDirectory stringByAppendingPathComponent:[[[NSBundle mainBundle] bundleIdentifier] stringByAppendingPathComponent:@"background"]];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    if ([fileManager fileExistsAtPath:backgroundDirectory] == NO)
    {
        [fileManager createDirectoryAtPath:backgroundDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    }

    return backgroundDirectory;
}

-(NSURLRequest *)originalRequest
{
    return _task.originalRequest;
}

-(NSHTTPURLResponse *)response
{
    if ([_task.response isKindOfClass:[NSHTTPURLResponse class]])
    {
        return (NSHTTPURLResponse*)_task.response;
    }
    else
    {
        return nil;
    }
}

-(CGFloat)downloadProgress
{
    if (_task.countOfBytesExpectedToReceive != 0)
    {
        return ((CGFloat)_task.countOfBytesReceived/(CGFloat)_task.countOfBytesExpectedToReceive);
    }
    else
    {
        return 0;
    }
}

-(CGFloat)uploadProgress
{
    if (_task.countOfBytesExpectedToSend != 0)
    {
        return ((CGFloat)_task.countOfBytesSent/(CGFloat)_task.countOfBytesExpectedToSend);
    }
    else
    {
        return 0;
    }
}

-(NSError *)error
{
    if (_customError)
    {
        return _customError;
    }
    else
    {
        return _task.error;
    }
}

-(NSCachedURLResponse *)cachedURLResponse
{
    return [[NSURLCache sharedURLCache] cachedResponseForRequest:self.originalRequest];
}

-(NSDictionary *)cachedDictionaryResponse
{
    NSData *data = [[self cachedURLResponse] data];

    if (data)
    {
        return [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
    }
    else
    {
        return nil;
    }
}

-(NSData *)responseData
{
    return _data;
}

-(void)sendDownloadProgress:(CGFloat)progress
{
    if (_downloadProgressBlock && progress > 0)
    {
        void (^progressBlock)(CGFloat progress) = _downloadProgressBlock;

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            if (progressBlock)
            {
                progressBlock(progress);
            }
        }];
    }
}

-(void)sendUploadProgress:(CGFloat)progress
{
    if (_uploadProgressBlock && progress > 0)
    {
        void (^progressBlock)(CGFloat progress) = _uploadProgressBlock;

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            if (progressBlock)
            {
                progressBlock(progress);
            }
        }];
    }
}

void (^_backgroundSessionCompletionHandler)(void);

+(void)setBackgroundSessionCompletionHandler:(void (^)(void))backgroundSessionCompletionHandler
{
    _backgroundSessionCompletionHandler = backgroundSessionCompletionHandler;
}

+(void (^)(void))backgroundSessionCompletionHandler
{
    return _backgroundSessionCompletionHandler;
}

-(void)sendCompletionData:(NSData*)data error:(NSError*)error
{
    if (_dataCompletionBlock)
    {
        void (^completionBlock)(NSData* _Nullable result, NSError* _Nullable error) = _dataCompletionBlock;

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            if (completionBlock)
            {
                completionBlock(data,error);
            }

            if ([[self class] backgroundSessionCompletionHandler])
            {
                [[self class] backgroundSessionCompletionHandler]();
                [self class].backgroundSessionCompletionHandler = nil;
            }
        }];
    }
}

-(void)sendResponse:(NSHTTPURLResponse*)response
{
    if (_responseBlock)
    {
        void (^responseBlock)(NSHTTPURLResponse* _Nullable response) = _responseBlock;

        [[NSOperationQueue mainQueue] addOperationWithBlock:^{

            if (responseBlock)
            {
                responseBlock(response);
            }
        }];
    }
}

#pragma mark - NSURLSessionTaskDelegate

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler
{
    NSURLComponents *requestURLComponent = [[NSURLComponents alloc] initWithURL:task.originalRequest.URL resolvingAgainstBaseURL:NO];

    NSURLProtectionSpace *protectionSpace = [challenge protectionSpace];

    if ([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
    {
        NSURLCredential *credential = [NSURLCredential credentialForTrust:protectionSpace.serverTrust];

        completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
    }
    else
    {
        completionHandler(NSURLSessionAuthChallengePerformDefaultHandling,nil);
    }
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
{
    [self sendUploadProgress:self.uploadProgress];
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(nullable NSError *)error
{
    [self sendCompletionData:_data error:error];
}

#pragma mark - NSURLSessionDataDelegate

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSHTTPURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler
{
    NSDictionary *headers = [response allHeaderFields];
    if (headers)
    {
        if (_data == nil)
        {
            _data = [[NSMutableData alloc] init];
        }
    }

    [self sendResponse:response];
    completionHandler(NSURLSessionResponseAllow);
}

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
{
    [_data appendData:data];

    [self sendDownloadProgress:self.downloadProgress];
}

#pragma mark - Other methods

-(void)start
{
    _customError = nil;
    [self.task resume];
}

-(void)cancel
{
    _customError = [NSError errorWithDomain:NSStringFromClass([self class]) code:NSURLErrorCancelled userInfo:nil];

    [self sendCompletionData:_data error:_customError];

    [self.task cancel];

    _responseBlock = NULL;
    _uploadProgressBlock = NULL;
    _downloadProgressBlock = NULL;
    _dataCompletionBlock = NULL;
}

-(void)dealloc
{
    _data = nil;
    _uploadProgressBlock = NULL;
    _downloadProgressBlock = NULL;
    _responseBlock = NULL;
    _dataCompletionBlock = NULL;
}

@end


text:
package com.philosophicalhacker.philhackernews.ui.storydetail;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;

import com.philosophicalhacker.philhackernews.R;
import com.philosophicalhacker.philhackernews.model.Item;
import com.philosophicalhacker.philhackernews.ui.commentslist.CommentsFragment;
import com.philosophicalhacker.philhackernews.ui.refresh.RefreshableFragmentHostingActivity;

import butterknife.ButterKnife;

public class StoryDetailActivity extends RefreshableFragmentHostingActivity {

    private static final String EXTRA_STORY = "com.philosophicalhacker.philhackernews.EXTRA_STORY";
    private static final String STORY_FRAG_TAG = "story";
    private static final String COMMENT_FRAG_TAG = "comments";
    private static final String SAVE_STATE_DETAIL_FRAG_SHOW = "SAVE_STATE_DETAIL_FRAG_SHOW";
    private StoryDetailFragment mStoryDetailFragment;
    private CommentsFragment mCommentsFragment;
    private Item mStoryItem;

    public static Intent getStartIntent(Context context, Item item) {
        Intent intent = new Intent(context, StoryDetailActivity.class);
        intent.putExtra(EXTRA_STORY, item);
        return intent;
    }

    //----------------------------------------------------------------------------------
    // Lifecycle Methods
    //----------------------------------------------------------------------------------
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        mStoryItem = getIntent().getParcelableExtra(EXTRA_STORY);
        if (shouldShowStoryDetailFragment(savedInstanceState)) {
            showStoryDetailFragment(mStoryItem);
        } else {
            showCommentsFragment(mStoryItem);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        boolean storyDetailFragShowing = getSupportFragmentManager().findFragmentByTag(STORY_FRAG_TAG) != null;
        outState.putBoolean(SAVE_STATE_DETAIL_FRAG_SHOW, storyDetailFragShowing);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_view_comments:
                showCommentsFragment(mStoryItem);
                break;
            case R.id.action_view_story:
                showStoryDetailFragment(mStoryItem);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    //----------------------------------------------------------------------------------
    // Helpers
    //----------------------------------------------------------------------------------
    private boolean shouldShowStoryDetailFragment(Bundle savedInstanceState) {
        return savedInstanceState == null ||
                savedInstanceState.getBoolean(SAVE_STATE_DETAIL_FRAG_SHOW);
    }

    private void showStoryDetailFragment(Item item) {
        StoryDetailFragment storyDetailFragment = getStoryDetailFragment(item);
        configureRefreshableFragment(storyDetailFragment);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, storyDetailFragment, STORY_FRAG_TAG)
                .commit();
    }

    private void showCommentsFragment(Item item) {
        CommentsFragment commentsFragment = getCommentsFragment(item);
        configureRefreshableFragment(commentsFragment);
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.container, commentsFragment, COMMENT_FRAG_TAG)
                .commit();
    }

    private CommentsFragment getCommentsFragment(Item item) {
        mCommentsFragment = (CommentsFragment) getSupportFragmentManager().findFragmentByTag(COMMENT_FRAG_TAG);
        if (mCommentsFragment == null) {
            mCommentsFragment = CommentsFragment.newInstance(item);
        }
        return mCommentsFragment;
    }

    private StoryDetailFragment getStoryDetailFragment(Item item) {
        mStoryDetailFragment = (StoryDetailFragment) getSupportFragmentManager().findFragmentByTag(STORY_FRAG_TAG);
        if (mStoryDetailFragment == null) {
            mStoryDetailFragment = StoryDetailFragment.newInstance(item);
        }
        return mStoryDetailFragment;
    }

}


text:
require 'rails_helper'

RSpec.describe ListItem, :type => :model do
  let(:list_item) { FactoryGirl.build(:list_item) }

  it { should have_one(:search).dependent(:destroy) }
  it { should belong_to(:user) }
  it { should belong_to(:item) }

  it { should validate_presence_of(:name) }

  it_behaves_like 'it can be converted to an item type',
    FactoryGirl.build(:list_item)

  describe '#before_update' do
    before(:each) {
      list_item.save!
      FactoryGirl.create(:search, list_item: list_item)
    }

    context 'item has not been updated' do
      it 'does not delete its search or item' do
        list_item.name = "Just Updating The Name"
        list_item.save!

        expect(list_item.search).to_not be_nil
        expect(list_item.item).to_not be_nil
      end
    end

    context 'item has been updated to a real media item' do
      it 'deletes its search' do
        list_item.item = FactoryGirl.create(:movie)
        list_item.save!

        requested_items_assigned_to_list_item = RequestedItem.
          select { |item|
          item.list_item == list_item
        }

        expect(list_item.search).to be_nil
        expect(Search.where(list_item: list_item)).to be_empty
        expect(requested_items_assigned_to_list_item).to be_empty
      end
    end
  end
end


text:
/*****************************************************************************

  test.cpp --

  Original Author: Martin Janssen, Synopsys, Inc., 2002-02-15

 *****************************************************************************/

/*****************************************************************************

  MODIFICATION LOG - modifiers, enter your name, affiliation, date and
  changes you are making here.

      Name, Affiliation, Date:
  Description of Modification:

 *****************************************************************************/

/*
Please compile the program below with SC5.0. The output of the program is:

sc_bit(1).to_char() = `

where the last char in the line is a caro in my shell. It should be

sc_bit(1).to_char() = 1
*/

#include <systemc.h>

int sc_main(int argc, char* arg[])
{
    sc_report_handler::set_actions("/IEEE_Std_1666/deprecated", SC_DO_NOTHING);
    cout << "sc_bit(1).to_char() = " << sc_bit(1).to_char() << endl;

    return 0;
}


text:
//-----------------------------------------------------------------------------------
//    The confidential and proprietary information contained in this file may
//    only be used by a person authorised under and to the extent permitted
//    by a subsisting licensing agreement from ARM Limited or its affiliates
//    or between you and a party authorised by ARM
//
//           (C) COPYRIGHT [2016] ARM Limited or its affiliates
//               ALL RIGHT RESERVED
//
//    This entire notice must be reproduced on all copies of this file
//    and copies of this files may only be made by a person if such person is
//    permitted to do so under the terms of a subsisting license agreement
//    from ARM Limited or its affiliates or between you and a party authorised by ARM
//-----------------------------------------------------------------------------------

#ifndef _CRYS_DH_H
#define _CRYS_DH_H

#include "crys_rsa_types.h"
#include "crys_kdf.h"
#include "crys_rnd.h"

#ifdef __cplusplus
extern "C"
{
#endif

/*!
@file
@brief This module defines the API that supports Diffie-Hellman key exchange, as defined in [PKCS3] and in [X9.42] (key lengths 1024 and 2048 bits).
*/

/************************ Defines ******************************/

#define CRYS_DHPubKey_t  CRYSRSAPubKey_t

#define CRYS_DH_MAX_VALID_KEY_SIZE_VALUE_IN_BITS  CRYS_RSA_MAX_VALID_KEY_SIZE_VALUE_IN_BITS /*!< \internal RL restrict to 2048 */

#define CRYS_DH_MIN_VALID_KEY_SIZE_VALUE_IN_BITS  1024 /*!< Size limitation according to ANSI standard */

#define CRYS_DH_MAX_MOD_SIZE_IN_BYTES   (CRYS_DH_MAX_VALID_KEY_SIZE_VALUE_IN_BITS / 8)
#define CRYS_DH_MAX_MOD_SIZE_IN_WORDS   (CRYS_DH_MAX_MOD_SIZE_IN_BYTES/sizeof(uint32_t))

#define CRYS_DH_MAX_MOD_BUFFER_SIZE_IN_WORDS (CRYS_DH_MAX_MOD_SIZE_IN_WORDS + 2)
#define CRYS_DH_DOMAIN_GENERATION_MAX_SIZE_BITS   CRYS_RSA_MAX_KEY_GENERATION_SIZE_BITS /*!< \internal RL restrict to 2048 */

/* The following defines are for the readability of the user */
#define CRYS_DHPrimeData_t   CRYS_RSAPrimeData_t
#define CRYS_DHUserPubKey_t  CRYS_RSAUserPubKey_t
#define CRYS_DH_OtherInfo_t  CRYS_KDF_OtherInfo_t
#define CRYS_DH_COUNT_OF_OTHER_INFO_ENTRIES  CRYS_KDF_COUNT_OF_OTHER_INFO_ENTRIES
#define CRYS_DH_MAX_SIZE_OF_OTHER_INFO_ENTRY  CRYS_KDF_MAX_SIZE_OF_OTHER_INFO_ENTRY /*!< Size is in bytes */
#define CRYS_DH_MAX_SIZE_OF_KEYING_DATA  CRYS_KDF_MAX_SIZE_OF_KEYING_DATA /*!< Size is in bytes*/

/************************ Enums ********************************/

/* DH operations mode (used in some functions of DH) */
typedef enum
{
   CRYS_DH_PKCS3_mode  = 0,
   CRYS_DH_ANSI_X942_mode = 1,
   CRYS_DH_NumOfModes,

   CRYS_DH_OpModeLast    = 0x7FFFFFFF,

}CRYS_DH_OpMode_t;

/* HASH operation modes, used in DH */
typedef enum
{
	CRYS_DH_HASH_SHA1_mode		= CRYS_HASH_SHA1_mode,
	CRYS_DH_HASH_SHA224_mode	= CRYS_HASH_SHA224_mode,
	CRYS_DH_HASH_SHA256_mode	= CRYS_HASH_SHA256_mode,
	CRYS_DH_HASH_SHA384_mode	= CRYS_HASH_SHA384_mode,
	CRYS_DH_HASH_SHA512_mode	= CRYS_HASH_SHA512_mode,
	CRYS_DH_HASH_MD5_mode	        = CRYS_HASH_MD5_mode, /*!< \internal not used in DH */

	CRYS_DH_HASH_NumOfModes		= CRYS_HASH_MD5_mode,
	CRYS_DH_HASH_OperationModeLast  = 0x7FFFFFFF,

}CRYS_DH_HASH_OpMode_t;

/* key derivation modes, used in DH */
typedef enum
{
	CRYS_DH_ASN1_Der_mode    = CRYS_KDF_ASN1_DerivMode,
	CRYS_DH_Concat_Der_mode  = CRYS_KDF_ConcatDerivMode,
	CRYS_DH_X963_DerMode    = CRYS_KDF_ConcatDerivMode,

	CRYS_DH_DerivationFunc_ModeLast= 0x7FFFFFFF,

}CRYS_DH_DerivationFunc_Mode;

/************************ Typedefs  *************************************/
/* temp buffer structure used for exponentiation in DH functions */
typedef struct
{
	CRYS_DHPrimeData_t PrimeData;
	CRYS_DHPubKey_t    PubKey;
	uint32_t TempBuff[CRYS_DH_MAX_MOD_BUFFER_SIZE_IN_WORDS];
} CRYS_DH_ExpTemp_t;

/* temp buffer structure used for DH functions (non hybrid) */
typedef struct
{
	CRYS_DHPrimeData_t PrimeData;
	CRYS_DHUserPubKey_t    UserPubKey;
	uint32_t TempBuff[CRYS_DH_MAX_MOD_BUFFER_SIZE_IN_WORDS];
} CRYS_DH_Temp_t;

/* temp buffer structure used for DH hybrid functions */
typedef struct
{
	CRYS_DHPrimeData_t PrimeData;
	CRYS_DHUserPubKey_t    UserPubKey;
	uint32_t TempBuff[2*CRYS_DH_MAX_MOD_BUFFER_SIZE_IN_WORDS];
} CRYS_DH_HybrTemp_t;

/* Buffer used for FIPS KAT */
typedef struct
{
	CRYS_DHUserPubKey_t pubKey;
	CRYS_DHPrimeData_t  primeData;
	uint8_t secretBuff[CRYS_DH_MIN_VALID_KEY_SIZE_VALUE_IN_BITS/SASI_BITS_IN_BYTE];  // KAT tests uses 1024 bit key
} CRYS_DH_FipsKat_t;

/************************ Structs  **************************************/

/************************ Public Variables ******************************/

/************************ Public Functions ******************************/

/*******************************************************************************************/

/*!
@brief This function has two purposes:

 Randomly generate the client private key according to the choosen version [PKCS3] or [ANSI X9.42].

 Computes the client public key as follows:	ClientPub = Generator^Prv mod Prime, where '^' is the symbol of exponentiation.

This function should not be called directly. Instead, use the macros ::CRYS_DH_PKCS3_GeneratePubPrv and ::CRYS_DH_ANSI_X942_GeneratePubPrv.
\note
All buffer parameters should be in Big-Endian form.

@return CRYS_OK on success.
@return A non-zero value on failure as defined crys_dh_error.h
 */
CIMPORT_C CRYSError_t _DX_DH_GeneratePubPrv(
                    CRYS_RND_Context_t *rndContext_ptr,             /*!< [in/out] Pointer to the RND context buffer. */
                    uint8_t *Generator_ptr,                         /*!< [in]  Pointer to the Generator octet string. */
                    uint16_t GeneratorSize,                         /*!< [in]  The size of the Generator string (in bytes). */
                    uint8_t *Prime_ptr,                             /*!< [in]  Pointer to the Prime octet string P (used as modulus in the algorithm). */
                    uint16_t PrimeSize,                             /*!< [in]  The size of the Prime string in bytes. */
                    uint16_t L,                                     /*!< [in]  Exact size in bits of the Prime to be generated (relevant only for [PKCS3]):

 If L!=0, force the private key to be [2^(L-1) ? Prv < 2^L], where '^'
									        indicates exponentiation.

 If L = 0 then [0 < Prv < P-1].

 */
                    uint8_t *Q_ptr,                                 /*!< [in]  Relevant only for [ANSI X9.42] - Pointer to the Q octet string in the range:
                                                                                1 <= Prv <= Q-1 or 1 < Prv < Q-1. */
                    uint16_t QSize,                                 /*!< [in]  Relevant only for [ANSI X9.42] - Size of the Q string (in bytes). */
                    CRYS_DH_OpMode_t DH_mode,                       /*!< [in]  An enumerator declaring whether this is [PKCS3] or [ANSI X9.42] mode. */
                    CRYS_DHUserPubKey_t *tmpPubKey_ptr,             /*!< [in]  Pointer to a temporary buffer for public key structure. Used for the
							                       exponentiation function. */
                    CRYS_DHPrimeData_t  *tmpPrimeData_ptr,          /*!< [in]  Pointer to a structure holding internal temporary buffers. */
                    uint8_t *ClientPrvKey_ptr,                      /*!< [out] Pointer to the Private key Prv. This buffer should be at least the following
									       size (in bytes):

 If L is provided: (L+7)/8.

 If L is NULL: \p PrimeSize.

 */
                    uint16_t *ClientPrvKeySize_ptr,                 /*!< [in/out] Pointer to the Private key size:

 Input - size of the given buffer.

 Output - actual size of the generated private key.

 */
                    uint8_t *ClientPub1_ptr,                        /*!< [out] Pointer to the Public key. This buffer should be at least \p PrimeSize bytes. */
                    uint16_t *ClientPubSize_ptr                     /*!< [in/out] Pointer to the Public key size:

 Input - size of the given buffer.

 Output - actual size of the generated public key.

 */
);

/* macro for calling the GeneratePubPrv function on PKCS#3 mode:  Q is irrelevant */
/*--------------------------------------------------------------------------------*/
/*!
This macro is used to generate the public and private DH keys according to [PKCS3]. For a description of the parameters see ::_DX_DH_GeneratePubPrv.
*/
#define CRYS_DH_PKCS3_GeneratePubPrv(rndContext_ptr, Generator_ptr,GeneratorSize,\
				Prime_ptr,PrimeSize,\
				L,\
				tmpPubKey_ptr,tmpPrimeData_ptr,\
				ClientPrvKey_ptr,ClientPrvKeySize_ptr,\
				ClientPub_ptr,ClientPubSize_ptr)\
		_DX_DH_GeneratePubPrv(rndContext_ptr, (Generator_ptr),(GeneratorSize),\
				(Prime_ptr),(PrimeSize),\
				(L),\
				(uint8_t *)NULL,(uint16_t)0,\
				CRYS_DH_PKCS3_mode,\
				(tmpPubKey_ptr),(tmpPrimeData_ptr),\
				(ClientPrvKey_ptr),(ClientPrvKeySize_ptr),\
				(ClientPub_ptr),(ClientPubSize_ptr))

/*!
This macro is used to generate the public and private DH keys according to [ANSI X9.42]. For a description of the parameters see ::_DX_DH_GeneratePubPrv.
*/
#define CRYS_DH_ANSI_X942_GeneratePubPrv(rndContext_ptr, Generator_ptr,GeneratorSize,Prime_ptr,PrimeSize,\
				Q_ptr,QSize,\
				tmpPubKey_ptr,tmpPrimeData_ptr,\
				ClientPrvKey_ptr,ClientPrvKeySize_ptr,\
				ClientPub_ptr,ClientPubSize_ptr)\
		_DX_DH_GeneratePubPrv(rndContext_ptr, (Generator_ptr),(GeneratorSize),\
				(Prime_ptr),(PrimeSize),\
				(uint16_t)0,\
				(Q_ptr),(QSize),\
				CRYS_DH_ANSI_X942_mode,\
				(tmpPubKey_ptr),(tmpPrimeData_ptr),\
				(ClientPrvKey_ptr),(ClientPrvKeySize_ptr),\
				(ClientPub_ptr),(ClientPubSize_ptr))

/*******************************************************************************************/
/*!
@brief This function computes the shared secret key (value) accordng to [ANSI X9.42], 7.5.1:
          SecretKey = ServerPubKey ^ ClientPrvKey mod Prime.
\note

 All buffer parameters should be in Big-Endian form.

The user must obtain assurance of validity of the public key, using one of methods,
described in [ANSI X9.42] paragraph 7.4.

The actual size of the private key (in bits) must be not less than 2 and not greater than the actual
size of the Prime (modulus in bits).

@return CRYS_OK on success.
@return A non-zero value on failure as defined in crys_dh_error.h.
*/
CIMPORT_C CRYSError_t CRYS_DH_GetSecretKey(
					uint8_t *ClientPrvKey_ptr,                  /*!< [in]  Pointer to the Private key octet string Prv < Prime. */
					uint16_t ClientPrvKeySize,                  /*!< [in]  The Private key Size (in bytes). */
					uint8_t *ServerPubKey_ptr,                  /*!< [in]  Pointer to the Server public key octet string. */
					uint16_t ServerPubKeySize,                  /*!< [in]  The Server Public key Size (in bytes). */
					uint8_t *Prime_ptr,                         /*!< [in]  Pointer to the Prime octet string. */
					uint16_t PrimeSize,                         /*!< [in]  The size of the Prime string. */
					CRYS_DHUserPubKey_t *tmpPubKey_ptr,         /*!< [in]  Pointer to the public key structure. Used for the exponentiation
											 operation function. Need not be initialized. */
					CRYS_DHPrimeData_t  *tmpPrimeData_ptr,      /*!< [in]  Pointer to a structure containing internal temp buffers. */
					uint8_t *SecretKey_ptr,                     /*!< [out] Pointer to the secret key octet string. This buffer should be at
											least PrimeSize bytes. */
					uint16_t *SecretKeySize_ptr                 /*!< [in/out] Pointer to the secret key Buffer Size. This buffer should be at
											least of PrimeSize bytes:

 Input  - size of the given buffer.

 Output - actual size.

*/
);

/******************************************************************************************/
/*!
@brief This function extracts the shared secret keying data from the shared secret value. It should be called by using
macros ::CRYS_DH_X942_GetSecretDataAsn1 and ::CRYS_DH_X942_GetSecretDataConcat.

\note

 The "other info" argument and its AlgorithmID entry are mandatory only for ASN1 key derivation, and optional for
the other derivation modes.

If used, all entries of the structure should be initialized with relevant data and size, prior to calling this function
(entry size of empty fields must be set to 0).

All buffers arguments are represented in Big-Endian form.

@return CRYS_OK on success.
@return A non-zero value on failure as defined in crys_dh_error.h.
*/
 CIMPORT_C CRYSError_t CRYS_DH_X942_GetSecretData(
                    uint8_t                  *ClientPrvKey_ptr,        /*!< [in] Pointer to the Private key octet string. */
                    uint16_t                  ClientPrvKeySize,         /*!< [in] The Private key size (in bytes). */
                    uint8_t                  *ServerPubKey_ptr,         /*!< [in] Pointer to the Server public key octet string. */
		    uint16_t                  ServerPubKeySize,         /*!< [in] The Server Public key size (in bytes). */
                    uint8_t                  *Prime_ptr,                /*!< [in] Pointer to the Prime octet string. */
                    uint16_t                  PrimeSize,                /*!< [in] The size of the Prime string. */
                    CRYS_DH_OtherInfo_t        *otherInfo_ptr,          /*!< [in] Pointer to structure containing other data, shared by two entities
										  sharing the secret keying data.
                                                                                The Maximal size of each data entry of "other info" is limited - see crys_kdf.h
										  for the defined value. */
                    CRYS_DH_HASH_OpMode_t       hashMode,               /*!< [in] One of the supported SHA-x HASH modes. The supported modes are according to the supported
								        	  HASH modes for the product (and MD5 is not supported). */
                    CRYS_DH_DerivationFunc_Mode DerivFunc_mode,         /*!< [in] The enumerator ID of key derivation function mode. ASN1 or Concatenation
										  modes are supported. */
                    CRYS_DH_Temp_t             *tmpBuff_ptr,            /*!< [in] A pointer to the DH temp buffer structure. Not initialized. */
                    uint8_t                  *SecretKeyingData_ptr,     /*!< [out] Pointer to the secret key octet string. This buffer should be at least
										   PrimeSize bytes. */
                    uint16_t                  SecretKeyingDataSize      /*!< [in] The required Secret Keying data size (in bytes). Must be larger than 0,
										  and smaller than the maximal - CRYS_DH_MAX_SIZE_OF_KEYING_DATA. */
);

/****************************************************************/
/*!
This macro implements the DH [ANSI X9.42] standard. It derives a secret key using the Derivation function based on ASN.1. For a
description of the parameters see ::CRYS_DH_X942_GetSecretData.*/
#define CRYS_DH_X942_GetSecretDataAsn1(ClientPrvKey_ptr,ClientPrvKeySize,ServerPubKey_ptr,ServerPubKeySize,Prime_ptr,PrimeSize,otherInfo_ptr,hashMode,tmpBuff_ptr,SecretKeyingData_ptr,SecretKeyingDataSize)\
	CRYS_DH_X942_GetSecretData((ClientPrvKey_ptr),(ClientPrvKeySize),(ServerPubKey_ptr),(ServerPubKeySize),(Prime_ptr),(PrimeSize),(otherInfo_ptr),(hashMode),(CRYS_DH_ASN1_Der_mode),(tmpBuff_ptr),(SecretKeyingData_ptr),(SecretKeyingDataSize))
/*!
This macro implements the DH [ANSI X9.42] standard. It derives a secret key using the Derivation function based on concatenation of HASHed data.
For a description of the parameters see ::CRYS_DH_X942_GetSecretData.*/
#define CRYS_DH_X942_GetSecretDataConcat(ClientPrvKey_ptr,ClientPrvKeySize,ServerPubKey_ptr,ServerPubKeySize,Prime_ptr,PrimeSize,otherInfo_ptr,hashMode,tmpBuff_ptr,SecretKeyingData_ptr,SecretKeyingDataSize)\
	CRYS_DH_X942_GetSecretData((ClientPrvKey_ptr),(ClientPrvKeySize),(ServerPubKey_ptr),(ServerPubKeySize),(Prime_ptr),(PrimeSize),(otherInfo_ptr),(hashMode),(CRYS_DH_Concat_Der_mode),(tmpBuff_ptr),(SecretKeyingData_ptr),(SecretKeyingDataSize))

/****************************************************************/
/*!
@brief The function computes shared secret data using two pairs of public and private keys:

 SecretKey1 = ServerPubKey1^ClientPrvKey1 mod Prime.

 SecretKey2 = ServerPubKey2^ClientPrvKey2 mod Prime.

It uses the Derivation function to derive secret keying data from the two secret keys (values).
This function may be called directly, or by using macros ::CRYS_DH_X942_HybridGetSecretDataAsn1 and ::CRYS_DH_X942_HybridGetSecretDataConcat
described above.

\note

 The "other info" argument and its AlgorithmID entry are mandatory only for ASN1 key derivation, and optional for the other derivation modes.

If used, all entries of the structure should be initialized with relevant data and size, prior to calling this function
(entry size of empty fields must be set to 0).

 All buffers arguments are represented in Big-Endian form.

@return CRYS_OK on success.
@return A non-zero value on failure as defined in crys_dh_error.h or crys_kdf_error.h.
*/
CIMPORT_C CRYSError_t CRYS_DH_X942_HybridGetSecretData(
                uint8_t            *ClientPrvKey_ptr1,          /*!< [in]  Pointer to the First Private key octet string number. */
                uint16_t            ClientPrvKeySize1,          /*!< [in]  The First Private key Size (in bytes). */
                uint8_t            *ClientPrvKey_ptr2,          /*!< [in]  Pointer to the Second Private key octet string. */
		uint16_t            ClientPrvKeySize2,          /*!< [in]  The Second Private key Size (in bytes). */
                uint8_t            *ServerPubKey_ptr1,          /*!< [in]  Pointer to the First Server public key octet string. */
                uint16_t            ServerPubKeySize1,          /*!< [in]  The First Server Public key Size (in bytes). */
                uint8_t            *ServerPubKey_ptr2,          /*!< [in]  Pointer to the Second Server public key octet string. */
                uint16_t            ServerPubKeySize2,          /*!< [in]  The Second Server Public key Size (in bytes). */
                uint8_t            *Prime_ptr,                  /*!< [in]  Pointer to the Prime octet string. */
                uint16_t            PrimeSize,                  /*!< [in]  The size of the Prime string. */
                CRYS_DH_OtherInfo_t  *otherInfo_ptr,            /*!< [in]  Pointer to structure containing optional other data, shared by two entities
								 sharing the secret keying data. */
                CRYS_DH_HASH_OpMode_t hashMode,                 /*!< [in]  One of the supported SHA-x HASH modes. The supported modes are according to the supported
								           HASH modes for the product (and MD5 is not supported). */
                CRYS_DH_DerivationFunc_Mode DerivFunc_mode,     /*!< [in]  The type of function to use to derive the secret key to the key data.
								 ASN.1 or Concatenation modes are supported. */
                CRYS_DH_HybrTemp_t   *tmpDhHybr_ptr,            /*!< [in]  Pointer to a CRYS_DH_Temp_t structure that contains temp buffers for
								 internal operations. */
                uint8_t            *SecretKeyingData_ptr,       /*!< [out] Pointer to the secret key octet string. This buffer should be at least
								 of size PrimeSize bytes. */
                uint16_t            SecretKeyingDataSize        /*!< [in]  The required Secret Keying data size (in bytes). Must be larger than 0,
								 and smaller than CRYS_DH_MAX_SIZE_OF_KEYING_DATA. */
);

/****************************************************************/
/*!
This macro implements the DH [X9.42] standard deriving a hybrid secret key from two public-private pair of keys using the Derivation function based on ASN.1.
For a description of the parameters see ::CRYS_DH_X942_HybridGetSecretData.*/
#define CRYS_DH_X942_HybridGetSecretDataAsn1(ClientPrvKey_ptr1,ClientPrvKeySize1,ClientPrvKey_ptr2,ClientPrvKeySize2,ServerPubKey_ptr1,ServerPubKeySize1,ServerPubKey_ptr2,ServerPubKeySize2,Prime_ptr,PrimeSize,otherInfo_ptr,hashFunc,tmpDhHybr_ptr,SecretKeyingData_ptr,SecretKeyingDataSize)\
	CRYS_DH_X942_HybridGetSecretData((ClientPrvKey_ptr1),(ClientPrvKeySize1),(ClientPrvKey_ptr2),(ClientPrvKeySize2),(ServerPubKey_ptr1),(ServerPubKeySize1),(ServerPubKey_ptr2),(ServerPubKeySize2),(Prime_ptr),(PrimeSize),(otherInfo_ptr),(hashFunc),CRYS_DH_ASN1_Der_mode,(tmpDhHybr_ptr),(SecretKeyingData_ptr),(SecretKeyingDataSize))

/*!
This macro implements the DH [X9.42] standard, deriving a hybrid secret key from two pairs of public-private keys, using the Derivation
function based on concatenation using SHA-x HASH. For a description of the parameters see ::CRYS_DH_X942_HybridGetSecretData.
*/
#define CRYS_DH_X942_HybridGetSecretDataConcat(ClientPrvKey_ptr1,ClientPrvKeySize1,ClientPrvKey_ptr2,ClientPrvKeySize2,ServerPubKey_ptr1,ServerPubKeySize1,ServerPubKey_ptr2,ServerPubKeySize2,Prime_ptr,PrimeSize,otherInfo_ptr,hashFunc,tmpDhHybr_ptr,SecretKeyingData_ptr,SecretKeyingDataSize)\
	CRYS_DH_X942_HybridGetSecretData((ClientPrvKey_ptr1),(ClientPrvKeySize1),(ClientPrvKey_ptr2),(ClientPrvKeySize2),(ServerPubKey_ptr1),(ServerPubKeySize1),(ServerPubKey_ptr2),(ServerPubKeySize2),(Prime_ptr),(PrimeSize),(otherInfo_ptr),(hashFunc),CRYS_DH_Concat_Der_mode,(tmpDhHybr_ptr),(SecretKeyingData_ptr),(SecretKeyingDataSize))

/******************************************************************************************/
/*!
@brief The function checks the obtained DH public key according to its domain parameters [ANSI X9.42-2001]

\note
Assuming: The DH domain parameters are valid.

@return CRYS_OK on success.
@return A non-zero value on failure as defined in crys_dh_error.h.
*/
CIMPORT_C CRYSError_t CRYS_DH_CheckPubKey(
					uint8_t              *modP_ptr,            /*!< [in] The pointer to the modulus (prime) P. */
					uint32_t              modPsizeBytes,        /*!< [in]  The modulus size in bytes. */
					uint8_t              *orderQ_ptr,           /*!< [in]  The pointer to the prime order Q of generator. */
					uint32_t              orderQsizeBytes,      /*!< [in]  The size of order of generator in bytes. */
					uint8_t              *pubKey_ptr,           /*!< [in]  The pointer to the public key to be validated. */
					uint32_t              pubKeySizeBytes,      /*!< [in]  The public key size in bytes. */
					CRYS_DH_Temp_t       *tempBuff_ptr         /*!< [in]  The temp buffer for internal calculations. */
);

#ifdef __cplusplus
}
#endif

#endif


text:
//
//  NativeObjectHolder.m
//  KirinKit
//
//  Created by James Hugman on 29/01/2012.
//  Copyright (c) 2012 Future Platforms. All rights reserved.
//

#import "NativeObjectHolder.h"

#import "KirinExtensionOnMainThread.h"
#import "KirinSerialExecute.h"
#import <UIKit/UIViewController.h>

#import <objc/runtime.h>

@interface NativeObjectHolder ()

@property(nonatomic, retain) NSDictionary* methodsMap;
@property(nonatomic, copy) NSString* name;

- (void) initializeMethodsMap;
- (NSString*) camelCasedMethodName: (NSString*) methodName;
- (NSString*) underscoredMethodName: (NSString*) methodName;

@end

@implementation NativeObjectHolder

static dispatch_queue_t serialDispatchQueue = nil;

@synthesize nativeObject = nativeObject_;
@synthesize dispatchQueue = dispatchQueue_;
@synthesize methodsMap = methodsMap_;

+ (NativeObjectHolder*) holderForObject: (NSObject*) object withName:(NSString *)name {
    NativeObjectHolder* holder = [[NativeObjectHolder alloc] init];
    holder.name = name;
    holder.nativeObject = object;
    return holder;
}

+ (dispatch_queue_t) globalSerialQueue {
    if (!serialDispatchQueue) {
        serialDispatchQueue = dispatch_queue_create("com.futureplatforms.kirin", DISPATCH_QUEUE_SERIAL);
    }
    NSLog(@"SERIAL DISPATCH QUEUE");
    return serialDispatchQueue;
}

- (void) setNativeObject:(NSObject*)nativeObject {
    nativeObject_ = nativeObject;
    if (!nativeObject_) {
        self.dispatchQueue = nil;
        return;
    }

    if (
            [nativeObject isKindOfClass:[UIViewController class]] ||
            [nativeObject conformsToProtocol:@protocol(KirinExtensionOnMainThread)]
        ) {
        // Dispatch to main/UI thread
        self.dispatchQueue = nil;
    } else if ([nativeObject conformsToProtocol:@protocol(KirinSerialExecute)]) {
        // Dispatch to custom serial queue
        NSString * queueName = [NSString stringWithFormat:@"com.futureplatforms.kirin.%@", self.name];
        self.dispatchQueue = dispatch_queue_create([queueName UTF8String], DISPATCH_QUEUE_SERIAL);
    } else {
        SEL getter = @selector(dispatchQueue);

        // See if this object provides its own dispatch queue
        if ([nativeObject_ respondsToSelector:getter]) {
            if (!nativeObject) {
                return;
            }

            IMP imp = [nativeObject methodForSelector:getter];
            dispatch_queue_t (*func)(id, SEL) = (void *) imp;
            self.dispatchQueue = (dispatch_queue_t) func(nativeObject, getter);
            NSLog(@"setNativeObject responds to dispatch queue getter: %@, %p", self.name, self.dispatchQueue);

        }

        if (!self.dispatchQueue) {
            // This object does not provide its own dispatch queue, so use a default global concurrent queue
            self.dispatchQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        }

    }

    // generate the method names will use to call selectors on this object.
    self.methodsMap = nil;
    [self initializeMethodsMap];
}

- (NSString*) underscoredMethodName: (NSString*) methodName {
    return [[methodName componentsSeparatedByString:@":"] componentsJoinedByString:@"_"];
}

- (NSString*) camelCasedMethodName: (NSString*) methodName {
    NSMutableString *output = [NSMutableString string];
    BOOL makeNextCharacterUpperCase = NO;
    for (NSInteger idx = 0; idx < [methodName length]; idx += 1) {
        unichar c = [methodName characterAtIndex:idx];
        if (c == ':') {
            makeNextCharacterUpperCase = YES;
        } else if (makeNextCharacterUpperCase) {
            [output appendString:[[NSString stringWithCharacters:&c length:1] uppercaseString]];
            makeNextCharacterUpperCase = NO;
        } else {
            [output appendFormat:@"%C", c];
        }
    }
    return output;
}

- (void) initializeMethodsMap {
    if (self.methodsMap) {
        return;
    }
    NSMutableDictionary* methods = [NSMutableDictionary dictionary];

    self.methodsMap = methods;

    Class class = object_getClass(self.nativeObject);
    while (class) {
        int i=0;
        unsigned int mc = 0;
        Method * mlist = class_copyMethodList(class, &mc);

        for(i=0;i<mc;i++) {
            Method method = mlist[i];
            SEL selector = method_getName(method);
            NSString* realMethodName = NSStringFromSelector(selector);

            methods[[self underscoredMethodName:realMethodName]] = realMethodName;
            methods[[self camelCasedMethodName: realMethodName]] = realMethodName;
        }

        /* note mlist needs to be freed */
        free(mlist);

        class = class_getSuperclass(class);
    }
}

- (NSArray*) methodNames {
    NSMutableArray* methodNames = [NSMutableArray array];
    for (NSString* methodName in [self.methodsMap keyEnumerator]) {
        [methodNames addObject:methodName];
    }
    return methodNames;

}

- (SEL) findSelectorFromString: methodName {
    return NSSelectorFromString(self.methodsMap[methodName]);
}

@end


text:
require_relative '../../lib/schedule_maker'
require 'yaml'

module ScheduleMaker
  class Spec
    def self.load_rotation
      rotation_file = File.join(File.expand_path('../..', __FILE__), 'fixture', 'rotation.yaml')
      YAML.load_file(rotation_file)
    end

    def self.load_schedule
      rotation_file = File.join(File.expand_path('../..', __FILE__), 'fixture', 'schedule.yaml')
      YAML.load_file(rotation_file)
    end

    def self.create_schedule(schedule)
      raise "Invalid schedule (#{schedule.class} => #{schedule.inspect})" unless schedule.is_a?(Array)
      result = []
      schedule.each do |period|
        result << ScheduleMaker::Period.new(period.keys[0], period.values[0])
      end
      result
    end

    def self.include_shift_for(rotation, participant)
      rotation.each do |period|
        return true if period.participant == participant
      end
      false
    end

    def self.sparse_schedule(schedule_in, start, options = {})
      day_length = options.fetch(:day_length, 86400.0)
      safety_max = options.fetch(:safety_max, 365) # Prevent this from entering infinite loop
      participants = options.fetch(:participants, {})
      schedule = schedule_in.dup
      end_date = Util.dateparse(options.fetch(:end_date, schedule[schedule.size - 1][:end]))
      init_sched = []
      schedule_zero_start = ScheduleMaker::Util.dateparse(schedule[0][:start])
      iter = start.dup
      while iter < end_date
        if iter >= schedule_zero_start
          the_shift = schedule.shift
          date_diff = ScheduleMaker::Util.dateparse(the_shift[:end]) - ScheduleMaker::Util.dateparse(the_shift[:start])
          len = (1.0 * date_diff / day_length).to_i
          participants[the_shift[:assignee]] ||= { 'period_length' => len, 'timezone' => 'UTC' }
          len.times { |_c| init_sched << ScheduleMaker::Period.new(the_shift[:assignee], 1) }
          iter += day_length * len
          schedule_zero_start = schedule.empty? ? end_date : ScheduleMaker::Util.dateparse(schedule[0][:start])
        else
          participants['**unassigned**'] ||= { 'period_length' => 1, 'timezone' => 'UTC' }
          init_sched << ScheduleMaker::Period.new('**unassigned**', 1)
          iter += day_length
        end
      end
      fail 'ScheduleMaker::Spec::sparse_schedule failed to converge schedule!' unless schedule.empty?
      rotation = ScheduleMaker::Rotation.new(participants, start: start, init_sched: init_sched)
      ScheduleMaker::Schedule.new(participants, start: start, rotation: rotation)
    end
  end
end

describe ScheduleMaker::Spec do
  describe '#include_shift_for' do
    it 'Should return true when a shift for someone is included' do
      rotation = [
        ScheduleMaker::Period.new('alice', 1),
        ScheduleMaker::Period.new('bob', 1),
        ScheduleMaker::Period.new('charles', 1)
      ]
      expect(ScheduleMaker::Spec.include_shift_for(rotation, 'alice')).to be true
    end

    it 'Should return false when a shift for someone is included' do
      rotation = [
        ScheduleMaker::Period.new('alice', 1),
        ScheduleMaker::Period.new('bob', 1),
        ScheduleMaker::Period.new('charles', 1)
      ]
      expect(ScheduleMaker::Spec.include_shift_for(rotation, 'douglas')).to be false
    end
  end

  describe '#sparse_schedule' do
    it 'Should generate a correct sparse schedule with no specified end date' do
      date = ScheduleMaker::Util.dateparse('2016-01-01T00:00:00')
      sparse_schedule = [
        { start: '2016-01-13T00:00:00+00:00', end: '2016-01-14T00:00:00+00:00', length: 1, assignee: 'bob' },
        { start: '2016-01-29T00:00:00+00:00', end: '2016-01-30T00:00:00+00:00', length: 1, assignee: 'bob' }
      ]
      participants = { 'bob' => { 'period_length' => 1, 'timezone' => 'America/Chicago' } }
      schedule = ScheduleMaker::Spec.sparse_schedule(sparse_schedule, date, participants: participants)
      expect(schedule.rotation.rotation.size).to eq(29)
    end

    it 'Should generate a correct sparse schedule with a specified end date' do
      date = ScheduleMaker::Util.dateparse('2016-01-01T00:00:00')
      end_date = ScheduleMaker::Util.dateparse('2016-02-01T00:00:00')
      sparse_schedule = [
        { start: '2016-01-13T00:00:00+00:00', end: '2016-01-14T00:00:00+00:00', length: 1, assignee: 'bob' },
        { start: '2016-01-29T00:00:00+00:00', end: '2016-01-30T00:00:00+00:00', length: 1, assignee: 'bob' }
      ]
      participants = { 'bob' => { 'period_length' => 1, 'timezone' => 'America/Chicago' } }
      schedule = ScheduleMaker::Spec.sparse_schedule(sparse_schedule, date, participants: participants, end_date: end_date)
      expect(schedule.rotation.rotation.size).to eq(31)
    end

    it 'Should generate a correct sparse schedule with multiple participants' do
      date = ScheduleMaker::Util.dateparse('2016-01-01T00:00:00')
      sparse_schedule = [
        { start: '2016-01-13T00:00:00+00:00', end: '2016-01-14T00:00:00+00:00', length: 1, assignee: 'alice' },
        { start: '2016-01-29T00:00:00+00:00', end: '2016-01-30T00:00:00+00:00', length: 1, assignee: 'bob' }
      ]
      participants = { 'bob' => { 'period_length' => 1, 'timezone' => 'America/Chicago' } }
      schedule = ScheduleMaker::Spec.sparse_schedule(sparse_schedule, date, participants: participants)
      expect(schedule.rotation.rotation.size).to eq(29)
    end

    it 'Should generate a correct sparse schedule with no participants hash' do
      date = ScheduleMaker::Util.dateparse('2016-01-01T00:00:00')
      sparse_schedule = [
        { start: '2016-01-13T00:00:00+00:00', end: '2016-01-14T00:00:00+00:00', length: 1, assignee: 'alice' },
        { start: '2016-01-29T00:00:00+00:00', end: '2016-01-30T00:00:00+00:00', length: 1, assignee: 'bob' }
      ]
      schedule = ScheduleMaker::Spec.sparse_schedule(sparse_schedule, date)
      expect(schedule.rotation.rotation.size).to eq(29)
    end
  end
end


text:
package timetakers.repository.converter;

import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

/**
 * @author David Liebl
 */

@Converter(autoApply = true)
public class LocalDateTimeConverter implements AttributeConverter<LocalDateTime, Long> {

    @Override
    public Long convertToDatabaseColumn(LocalDateTime entityValue) {
        if (entityValue==null)
            return null;
        return entityValue.toEpochSecond(ZoneOffset.UTC);
    }

    @Override
    public LocalDateTime convertToEntityAttribute(Long dbValue) {
        if(dbValue==null)
            return null;
        return LocalDateTime.ofEpochSecond(dbValue, 0, ZoneOffset.UTC);
    }
}


text:
import logging

from flask_appbuilder.models.sqla import Model
from flask_appbuilder.security.sqla.models import User

from superset.dao.exceptions import DAODeleteFailedError
from superset.dashboards.filter_sets.commands.base import BaseFilterSetCommand
from superset.dashboards.filter_sets.commands.exceptions import (
    FilterSetDeleteFailedError,
    FilterSetForbiddenError,
    FilterSetNotFoundError,
)
from superset.dashboards.filter_sets.dao import FilterSetDAO

logger = logging.getLogger(__name__)

class DeleteFilterSetCommand(BaseFilterSetCommand):
    def __init__(self, user: User, dashboard_id: int, filter_set_id: int):
        super().__init__(user, dashboard_id)
        self._filter_set_id = filter_set_id

    def run(self) -> Model:
        try:
            self.validate()
            return FilterSetDAO.delete(self._filter_set, commit=True)
        except DAODeleteFailedError as err:
            raise FilterSetDeleteFailedError(str(self._filter_set_id), "") from err

    def validate(self) -> None:
        self._validate_filterset_dashboard_exists()
        try:
            self.validate_exist_filter_use_cases_set()
        except FilterSetNotFoundError as err:
            if FilterSetDAO.find_by_id(self._filter_set_id):  # type: ignore
                raise FilterSetForbiddenError(
                    'the filter-set does not related to dashboard "%s"'
                    % str(self._dashboard_id)
                ) from err
            raise err