use of java.util.function.Predicate in project archiva by apache.
the class Maven2RepositoryStorage method readArtifactsMetadata.
@Override
public Collection<ArtifactMetadata> readArtifactsMetadata(ReadMetadataRequest readMetadataRequest) throws RepositoryStorageRuntimeException {
Path dir = pathTranslator.toFile(getRepositoryBasedir(readMetadataRequest.getRepositoryId()), readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion());
if (!(Files.exists(dir) && Files.isDirectory(dir))) {
return Collections.emptyList();
}
// all files that are not metadata and not a checksum / signature are considered artifacts
final Predicate<Path> dFilter = new ArtifactDirectoryFilter(readMetadataRequest.getFilter());
try (Stream<Path> stream = Files.list(dir)) {
// Returns a map TRUE -> (success values), FALSE -> (Exceptions)
Map<Boolean, List<Try<ArtifactMetadata>>> result = stream.filter(dFilter).map(path -> {
try {
return Try.success(getArtifactFromFile(readMetadataRequest.getRepositoryId(), readMetadataRequest.getNamespace(), readMetadataRequest.getProjectId(), readMetadataRequest.getProjectVersion(), path));
} catch (Exception e) {
LOGGER.debug("Could not create metadata for {}: {}", path, e.getMessage(), e);
return Try.<ArtifactMetadata>failure(e);
}
}).collect(Collectors.groupingBy(Try::isSuccess));
if (result.containsKey(Boolean.FALSE) && result.get(Boolean.FALSE).size() > 0 && (!result.containsKey(Boolean.TRUE) || result.get(Boolean.TRUE).size() == 0)) {
LOGGER.error("Could not get artifact metadata. Directory: {}. Number of errors {}.", dir, result.get(Boolean.FALSE).size());
Try<ArtifactMetadata> failure = result.get(Boolean.FALSE).get(0);
LOGGER.error("Sample exception {}", failure.getError().getMessage(), failure.getError());
throw new RepositoryStorageRuntimeException(readMetadataRequest.getRepositoryId(), "Could not retrieve metadata of the files");
} else {
if (!result.containsKey(Boolean.TRUE) || result.get(Boolean.TRUE) == null) {
return Collections.emptyList();
}
return result.get(Boolean.TRUE).stream().map(tr -> tr.get()).collect(Collectors.toList());
}
} catch (IOException e) {
LOGGER.error("Could not read directory {}: {}", dir, e.getMessage(), e);
}
return Collections.emptyList();
}
use of java.util.function.Predicate in project archiva by apache.
the class Maven2RepositoryStorage method listProjects.
@Override
public Collection<String> listProjects(String repoId, String namespace, Filter<String> filter) throws RepositoryStorageRuntimeException {
Path dir = pathTranslator.toFile(getRepositoryBasedir(repoId), namespace);
if (!(Files.exists(dir) && Files.isDirectory(dir))) {
return Collections.emptyList();
}
// scan all directories in the namespace, and only include those that are known to be projects
final Predicate<Path> dFilter = new DirectoryFilter(filter);
try (Stream<Path> stream = Files.list(dir)) {
return stream.filter(dFilter).filter(path -> isProject(path, filter)).map(path -> path.getFileName().toString()).sorted().collect(Collectors.toList());
} catch (IOException e) {
LOGGER.error("Could not read directory {}: {}", dir, e.getMessage(), e);
return Collections.emptyList();
}
}
use of java.util.function.Predicate in project jackrabbit-oak by apache.
the class MongoDocumentTraverser method getAllDocuments.
public <T extends Document> CloseableIterable<T> getAllDocuments(Collection<T> collection, Predicate<String> filter) {
if (!disableReadOnlyCheck) {
checkState(mongoStore.isReadOnly(), "Traverser can only be used with readOnly store");
}
MongoCollection<BasicDBObject> dbCollection = mongoStore.getDBCollection(collection);
// TODO This may lead to reads being routed to secondary depending on MongoURI
// So caller must ensure that its safe to read from secondary
Iterable<BasicDBObject> cursor = dbCollection.withReadPreference(mongoStore.getConfiguredReadPreference(collection)).find();
CloseableIterable<BasicDBObject> closeableCursor = CloseableIterable.wrap(cursor);
cursor = closeableCursor;
@SuppressWarnings("Guava") Iterable<T> result = FluentIterable.from(cursor).filter(o -> filter.test((String) o.get(Document.ID))).transform(o -> {
T doc = mongoStore.convertFromDBObject(collection, o);
// TODO Review the cache update approach where tracker has to track *all* docs
if (collection == Collection.NODES) {
NodeDocument nodeDoc = (NodeDocument) doc;
getNodeDocCache().put(nodeDoc);
}
return doc;
});
return CloseableIterable.wrap(result, closeableCursor);
}
use of java.util.function.Predicate in project sonar-java by SonarSource.
the class BytecodeCFGBuilderTest method all_opcodes_should_be_visited.
@Test
public void all_opcodes_should_be_visited() throws Exception {
Instructions ins = new Instructions();
Predicate<Integer> filterReturnAndThrow = opcode -> !((Opcodes.IRETURN <= opcode && opcode <= Opcodes.RETURN) || opcode == Opcodes.ATHROW);
NO_OPERAND_INSN.stream().filter(filterReturnAndThrow).forEach(ins::visitInsn);
INT_INSN.forEach(i -> ins.visitIntInsn(i, 0));
VAR_INSN.forEach(i -> ins.visitVarInsn(i, 0));
TYPE_INSN.forEach(i -> ins.visitTypeInsn(i, "java/lang/Object"));
FIELD_INSN.forEach(i -> ins.visitFieldInsn(i, "java/lang/Object", "foo", "D(D)"));
METHOD_INSN.forEach(i -> ins.visitMethodInsn(i, "java/lang/Object", "foo", "()V", i == INVOKEINTERFACE));
JUMP_INSN.forEach(i -> {
Label jumpLabel = new Label();
ins.visitJumpInsn(i, jumpLabel);
ins.visitLabel(jumpLabel);
});
ins.visitLdcInsn("a");
ins.visitIincInsn(0, 1);
Handle handle = new Handle(H_INVOKESTATIC, "", "", "()V", false);
ins.visitInvokeDynamicInsn("sleep", "()V", handle);
ins.visitLookupSwitchInsn(new Label(), new int[] {}, new Label[] {});
ins.visitMultiANewArrayInsn("B", 1);
Label l0 = new Label();
Label dflt = new Label();
Label case0 = new Label();
ins.visitTableSwitchInsn(0, 1, dflt, case0);
ins.visitLabel(dflt);
ins.visitInsn(NOP);
ins.visitLabel(l0);
ins.visitInsn(NOP);
BytecodeCFG cfg = ins.cfg();
Multiset<String> cfgOpcodes = cfgOpcodes(cfg);
List<String> collect = Instructions.ASM_OPCODES.stream().filter(filterReturnAndThrow).map(op -> Printer.OPCODES[op]).collect(Collectors.toList());
assertThat(cfgOpcodes).containsAll(collect);
}
use of java.util.function.Predicate in project n4js by eclipse.
the class ScriptApiTracker method computeMissingApiMethods.
/**
* Looking for at.ProvidesDefaultImplementation on Methods. Normal method declarations are not taken into account,
* since they would not be executed on the interface level.
*
* Beware: also the inheritance in the original API will be taken into account since compiled client code will link
* against that.
*
* @param type
* type to search for apis.
* @return List of {@link VirtualApiTMethod}
*/
public List<TMethod> computeMissingApiMethods(TInterface type, EObject context) {
Optional<ProjectComparisonAdapter> optAdapt = firstProjectComparisonAdapter(context.eResource());
if (optAdapt.isPresent()) {
ProjectComparisonAdapter projectComparisonAdapter = optAdapt.get();
ProjectComparisonEntry compareEntry = projectComparisonAdapter.getEntryFor(EcoreUtil2.getContainerOfType(type, TModule.class));
ProjectComparisonEntry typeCompare = compareEntry.getChildForElementImpl(type);
if (typeCompare == null) {
// are we in a completely missing API implementation (super-interfaces not implemented ?)
typeCompare = compareEntry.getChildForElementAPI(type);
}
if (typeCompare == null) {
if (logger.isDebugEnabled()) {
logger.debug(" want to throw new IllegalstateException() --> comparison for implementation not found type=" + type.getTypeAsString() + " in Implementation " + compareEntry.getElementImpl()[0]);
}
return emptyList();
}
LinkedHashMultimap<TMethod, TInterface> lhmmMehodInterface = LinkedHashMultimap.<TMethod, TInterface>create();
Predicate<ProjectComparisonEntry> filter = (pce -> pce.getElementAPI() instanceof TMethod);
filter = filter.and(pce -> pce.getElementImpl()[0] == null).and(pce -> PROVIDES_DEFAULT_IMPLEMENTATION.hasAnnotation((TMethod) pce.getElementAPI()));
Function<TInterface, Consumer<? super ProjectComparisonEntry>> actionProvider = pivot -> pce -> {
TMethod method = ((TMethod) pce.getElementAPI());
lhmmMehodInterface.put(method, pivot);
};
if (!checkInterfaceImplementsInterface(type, typeCompare.getElementAPI())) {
return emptyList();
}
// Call the supertype iterations scaffolding:
interfaceApiSupertypeWalker(filter, actionProvider, projectComparisonAdapter, (TInterface) typeCompare.getElementAPI(), TInterface.class);
// out in the caller when processing our results...
return lhmmMehodInterface.keySet().stream().map(m -> new VirtualApiTMethod(m.getName(), TypeUtils.copyPartial(m, TypesPackage.Literals.SYNTAX_RELATED_TELEMENT__AST_ELEMENT))).collect(Collectors.toList());
}
return emptyList();
}
Aggregations