use of java.util.function.BiPredicate in project ArachneCentralAPI by OHDSI.
the class AnalysisFilesSavingServiceImpl method saveFiles.
@PreAuthorize("hasPermission(#analysis, " + "T(com.odysseusinc.arachne.portal.security.ArachnePermission).UPLOAD_ANALYSIS_FILES)")
protected List<AnalysisFile> saveFiles(List<MultipartFile> multipartFiles, IUser user, A analysis, DataReference dataReference, BiPredicate<String, CommonAnalysisType> checkFileExecutabilityPredicate) {
List<MultipartFile> filteredFiles = multipartFiles.stream().filter(file -> !(CommonAnalysisType.COHORT.equals(analysis.getType()) && file.getName().endsWith(OHDSI_JSON_EXT))).filter(file -> !file.getName().startsWith(ANALYSIS_INFO_FILE_DESCRIPTION)).collect(Collectors.toList());
List<AnalysisFile> savedFiles = new ArrayList<>();
List<String> errorFileMessages = new ArrayList<>();
for (MultipartFile file : filteredFiles) {
try {
final boolean isExecutable = checkFileExecutabilityPredicate.test(file.getOriginalFilename(), analysis.getType());
savedFiles.add(saveFile(file, user, analysis, file.getName(), isExecutable, dataReference));
} catch (AlreadyExistException e) {
errorFileMessages.add(e.getMessage());
}
}
if (!errorFileMessages.isEmpty()) {
throw new ValidationRuntimeException("Failed to save files", ImmutableMap.of(dataReference.getGuid(), errorFileMessages));
}
return savedFiles;
}
use of java.util.function.BiPredicate in project SpongeCommon by SpongePowered.
the class SpongeKeyBuilder method build0.
@Override
public Key<V> build0() {
Objects.requireNonNull(this.valueType, "The value type must be set");
Objects.requireNonNull(this.elementType, "The element type must be set");
BiPredicate<? super E, ? super E> includesTester = this.includesTester;
if (includesTester == null) {
includesTester = (e, e2) -> false;
}
Comparator<? super E> comparator = this.comparator;
if (comparator == null) {
if (Comparable.class.isAssignableFrom(GenericTypeReflector.erase(this.elementType))) {
// noinspection unchecked
comparator = Comparator.comparing(o -> ((Comparable) o));
} else {
comparator = (o1, o2) -> {
if (o1.equals(o2))
return 0;
// There could be collisions, but yeah, what can you do about that..
if (o1.hashCode() > o2.hashCode())
return 1;
return -1;
};
}
}
Supplier<E> defaultValueSupplier = () -> null;
final Class<?> rawType = GenericTypeReflector.erase(this.valueType);
if (ListValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new ArrayList();
} else if (SetValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new HashSet();
} else if (WeightedCollectionValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new WeightedTable();
} else if (MapValue.class.isAssignableFrom(rawType)) {
defaultValueSupplier = () -> (E) new HashMap<>();
}
final SpongeKey<Value<E>, E> key = new SpongeKey<>(this.key, this.valueType, this.elementType, comparator, includesTester, defaultValueSupplier);
KeyProvider.INSTANCE.register(this.key, (Key<Value<?>>) (Object) key);
return (Key<V>) key;
}
use of java.util.function.BiPredicate in project sqlg by pietermartin.
the class ReplacedStep method passesLabelHasContainers.
private boolean passesLabelHasContainers(SqlgGraph sqlgGraph, boolean isVertex, String table) {
return this.labelHasContainers.stream().allMatch(h -> {
BiPredicate biPredicate = h.getBiPredicate();
Object predicateValue = h.getValue();
if (predicateValue instanceof Collection) {
Collection<String> tableWithPrefixes = new ArrayList<>();
Collection<String> edgeTableWithoutSchemaAndPrefixes = new ArrayList<>();
Collection<String> predicateValues = (Collection<String>) predicateValue;
SchemaTable schemaTableWithOutPrefix = SchemaTable.from(sqlgGraph, table).withOutPrefix();
for (String value : predicateValues) {
if (!isVertex && !value.contains(".")) {
// edges usually don't have schema, so we're matching any table with any schema if we weren't given any
edgeTableWithoutSchemaAndPrefixes.add(value);
} else {
SchemaTable predicateValueAsSchemaTableWithPrefix = SchemaTable.from(sqlgGraph, value).withPrefix(isVertex ? VERTEX_PREFIX : EDGE_PREFIX);
tableWithPrefixes.add(predicateValueAsSchemaTableWithPrefix.toString());
}
}
if (edgeTableWithoutSchemaAndPrefixes.isEmpty()) {
return biPredicate.test(table, tableWithPrefixes);
} else if (tableWithPrefixes.isEmpty()) {
return biPredicate.test(schemaTableWithOutPrefix.getTable(), edgeTableWithoutSchemaAndPrefixes);
} else {
return biPredicate.test(table, tableWithPrefixes) || biPredicate.test(schemaTableWithOutPrefix.getTable(), edgeTableWithoutSchemaAndPrefixes);
}
} else {
Preconditions.checkState(predicateValue instanceof String, "Label HasContainer's value must be an Collection of Strings or a String. Found " + predicateValue.getClass().toString());
if (!isVertex && !((String) predicateValue).contains(".")) {
// edges usually don't have schema, so we're matching any table with any schema if we weren't given any
SchemaTable schemaTableWithOutPrefix = SchemaTable.from(sqlgGraph, table).withOutPrefix();
return biPredicate.test(schemaTableWithOutPrefix.getTable(), predicateValue);
} else {
SchemaTable predicateValueAsSchemaTableWithPrefix = SchemaTable.from(sqlgGraph, (String) predicateValue).withPrefix(isVertex ? VERTEX_PREFIX : EDGE_PREFIX);
return biPredicate.test(table, predicateValueAsSchemaTableWithPrefix.toString());
}
}
});
}
use of java.util.function.BiPredicate in project sqlg by pietermartin.
the class ReplacedStep method groupIdsBySchemaTable.
/**
* Groups the idHasContainers by SchemaTable.
* Each SchemaTable has a list representing the idHasContainers with the relevant BiPredicate and RecordId
*
* @return
*/
private Map<SchemaTable, List<Multimap<BiPredicate, RecordId>>> groupIdsBySchemaTable() {
Map<SchemaTable, List<Multimap<BiPredicate, RecordId>>> result = new HashMap<>();
for (HasContainer idHasContainer : this.idHasContainers) {
Map<SchemaTable, Boolean> newHasContainerMap = new HashMap<>();
P<Object> idPredicate = (P<Object>) idHasContainer.getPredicate();
BiPredicate biPredicate = idHasContainer.getBiPredicate();
// This is statement is for g.V().hasId(Collection) where the logic is actually P.within not P.eq
if (biPredicate == Compare.eq && idPredicate.getValue() instanceof Collection && ((Collection) idPredicate.getValue()).size() > 1) {
biPredicate = Contains.within;
}
Multimap<BiPredicate, RecordId> biPredicateRecordIdMultimap;
if (idPredicate.getValue() instanceof Collection) {
Collection<Object> ids = (Collection<Object>) idPredicate.getValue();
for (Object id : ids) {
RecordId recordId = RecordId.from(id);
List<Multimap<BiPredicate, RecordId>> biPredicateRecordIdList = result.get(recordId.getSchemaTable());
Boolean newHasContainer = newHasContainerMap.get(recordId.getSchemaTable());
if (biPredicateRecordIdList == null) {
biPredicateRecordIdList = new ArrayList<>();
biPredicateRecordIdMultimap = LinkedListMultimap.create();
biPredicateRecordIdList.add(biPredicateRecordIdMultimap);
result.put(recordId.getSchemaTable(), biPredicateRecordIdList);
newHasContainerMap.put(recordId.getSchemaTable(), false);
} else if (newHasContainer == null) {
biPredicateRecordIdMultimap = LinkedListMultimap.create();
biPredicateRecordIdList.add(biPredicateRecordIdMultimap);
newHasContainerMap.put(recordId.getSchemaTable(), false);
}
biPredicateRecordIdMultimap = biPredicateRecordIdList.get(biPredicateRecordIdList.size() - 1);
biPredicateRecordIdMultimap.put(biPredicate, recordId);
}
} else {
Object id = idPredicate.getValue();
RecordId recordId = RecordId.from(id);
List<Multimap<BiPredicate, RecordId>> biPredicateRecordIdList = result.computeIfAbsent(recordId.getSchemaTable(), k -> new ArrayList<>());
biPredicateRecordIdMultimap = LinkedListMultimap.create();
biPredicateRecordIdList.add(biPredicateRecordIdMultimap);
biPredicateRecordIdMultimap.put(biPredicate, recordId);
}
}
return result;
}
use of java.util.function.BiPredicate in project sqlg by pietermartin.
the class ReplacedStep method getRootSchemaTableTrees.
/**
* Calculates the root labels from which to start the query construction.
* <p>
* The hasContainers at this stage contains the {@link TopologyStrategy} from or without hasContainer.
* After doing the filtering it must be removed from the hasContainers as it must not partake in sql generation.
*
* @return A set of SchemaTableTree. A SchemaTableTree for each root label.
*/
Set<SchemaTableTree> getRootSchemaTableTrees(SqlgGraph sqlgGraph, int replacedStepDepth) {
Preconditions.checkState(this.isGraphStep(), "ReplacedStep must be for a GraphStep!");
Set<SchemaTableTree> result = new HashSet<>();
final GraphStep graphStep = (GraphStep) this.step;
final boolean isVertex = graphStep.getReturnClass().isAssignableFrom(Vertex.class);
final boolean isEdge = !isVertex;
// RecordIds grouped by SchemaTable
Map<SchemaTable, List<Multimap<BiPredicate, RecordId>>> groupedIds = null;
if (!this.idHasContainers.isEmpty()) {
groupedIds = groupIdsBySchemaTable();
}
// All tables depending on the strategy, topology tables only or the rest.
Map<String, Map<String, PropertyType>> filteredAllTables = SqlgUtil.filterSqlgSchemaHasContainers(this.topology, this.hasContainers, false);
// Optimization for the simple case of only one label specified.
if (isVertex && this.labelHasContainers.size() == 1 && this.labelHasContainers.get(0).getBiPredicate() == Compare.eq) {
HasContainer labelHasContainer = this.labelHasContainers.get(0);
String table = (String) labelHasContainer.getValue();
SchemaTable schemaTableWithPrefix = SchemaTable.from(sqlgGraph, table).withPrefix(isVertex ? VERTEX_PREFIX : EDGE_PREFIX);
if (filteredAllTables.containsKey(schemaTableWithPrefix.toString())) {
collectSchemaTableTrees(sqlgGraph, replacedStepDepth, result, groupedIds, schemaTableWithPrefix.toString());
}
} else {
for (String table : filteredAllTables.keySet()) {
// if graphStep's return class is Vertex ignore all edges and vice versa.
if ((isVertex && table.substring(table.indexOf(".") + 1).startsWith(VERTEX_PREFIX)) || (isEdge && table.substring(table.indexOf(".") + 1).startsWith(EDGE_PREFIX))) {
if (passesLabelHasContainers(sqlgGraph, isVertex, table)) {
collectSchemaTableTrees(sqlgGraph, replacedStepDepth, result, groupedIds, table);
}
}
}
}
return result;
}
Aggregations