@Override public Stream<Entry> entries() { List<Entry> entries = new ArrayList<>(); try { /* * This code should be revisited to avoid buffering of the entries. * 1) Do we really need sorting classes? This force buffering of entries. * libs, cmds and configs are not sorted. * 2) I/O streams should be concatenated instead of buffering into * entries list. * 3) Close I/O streams in a close handler. */ if (classes != null) { try (Stream<Path> stream = Files.walk(classes)) { entries.addAll( stream .filter( p -> !Files.isDirectory(p) && !classes.relativize(p).toString().startsWith("_the.") && !classes.relativize(p).toString().equals("javac_state")) .sorted() .map(p -> toEntry(p, classes, EntryType.CLASS_OR_RESOURCE)) .collect(Collectors.toList())); } } if (cmds != null) { try (Stream<Path> stream = Files.walk(cmds)) { entries.addAll( stream .filter(p -> !Files.isDirectory(p)) .map(p -> toEntry(p, cmds, EntryType.NATIVE_CMD)) .collect(Collectors.toList())); } } if (libs != null) { try (Stream<Path> stream = Files.walk(libs)) { entries.addAll( stream .filter(p -> !Files.isDirectory(p)) .map(p -> toEntry(p, libs, EntryType.NATIVE_LIB)) .collect(Collectors.toList())); } } if (configs != null) { try (Stream<Path> stream = Files.walk(configs)) { entries.addAll( stream .filter(p -> !Files.isDirectory(p)) .map(p -> toEntry(p, configs, EntryType.CONFIG)) .collect(Collectors.toList())); } } } catch (IOException ioe) { throw new UncheckedIOException(ioe); } return entries.stream(); }
public void resolve( String identifier, TableReference container, EReference reference, int position, boolean resolveFuzzy, final ISqlReferenceResolveResult<TableDefinition> result) { SQLScript sqlScript = (SQLScript) EcoreUtil.getRootContainer(container); String catalogName = container.getCatalogName(); Predicate<TableDefinition> filter = !Helper.isEmpty(catalogName) ? table -> catalogName.equals(table.getSchemaQualifiedName().getCatalogName()) : table -> true; String schemaName = container.getSchemaName(); Predicate<TableDefinition> filter2 = !Helper.isEmpty(schemaName) ? table -> schemaName.equals(table.getSchemaQualifiedName().getSchemaName()) : table -> true; Stream<TableDefinition> tables = sqlScript .getStatements() .stream() .filter(stmt -> stmt instanceof TableDefinition) .map(table -> (TableDefinition) table) .filter(filter.and(filter2)); Consumer<TableDefinition> addMapping = table -> result.addMapping(table.getSchemaQualifiedName().getName(), table); if (resolveFuzzy) { tables .filter( table -> table.getSchemaQualifiedName() != null && table.getSchemaQualifiedName().getName() != null && table .getSchemaQualifiedName() .getName() .toUpperCase() .startsWith(identifier.toUpperCase())) .forEach(addMapping); } else { tables .filter( table -> table.getSchemaQualifiedName() != null && table.getSchemaQualifiedName().getName() != null && table.getSchemaQualifiedName().getName().equals(identifier)) .findFirst() .ifPresent(addMapping); } }
@Test public void findChangeLocationUsingTagName() { WebDriver driver = Browsers.CHROME; driver.get("http://www.ticketfly.com"); Stream<WebElement> links = driver.findElements(By.tagName("a")).stream(); links.filter((WebElement e) -> e.getText().equals("change location")).findFirst().get().click(); }
public static void main(String[] args) { // An Operation on a stream that returns a stream is an intermediary operation Stream<String> stream1 = Stream.of("one", "two", "three", "four", "five"); Predicate<String> p1 = Predicate.isEqual("two"); stream1.filter(p1); // This is an intermediary operation and it does nothing! /* The below piece of code does nothing! peek() and filter() methods are intermediary operations as they return a stream. Hence, this code does not prints anything. It is just a simple declaration. */ List<String> result1 = new ArrayList<>(); Stream<String> stream2 = Stream.of("one", "two", "three", "four", "five"); Predicate<String> p2 = Predicate.isEqual("two"); stream2.peek(System.out::println).filter(p2).peek(result1::add); System.out.println("size -> " + result1.size()); System.out.println("******************************************"); /* Using Final method (forEach()) Only the Final operation triggers the processing of the data the stream is connected to. */ List<String> result2 = new ArrayList<>(); Stream<String> stream3 = Stream.of("one", "two", "three", "four", "five"); Predicate<String> p3 = Predicate.isEqual("two"); Predicate<String> p4 = Predicate.isEqual("three"); stream3.peek(System.out::println).filter(p3.or(p4)).forEach(result2::add); System.out.println("size -> " + result2.size()); }
public void testWalkOneLevel() { try (Stream<Path> s = Files.walk(testFolder, 1)) { Object[] actual = s.filter(path -> !path.equals(testFolder)).sorted().toArray(); assertEquals(actual, level1); } catch (IOException ioe) { fail("Unexpected IOException"); } }
public Stream<AbstractEvent> filter(Stream<AbstractEvent> stream, String interestingEvents) { Set<Predicate<AbstractEvent>> wantedEvents = Arrays.stream(interestingEvents.split(",")) .map(str -> predicateMatch(str)) .collect(Collectors.toSet()); return stream.filter(ev -> wantedEvents.stream().anyMatch(pred -> pred.test(ev))); }
@Nullable public static <T, U> U findInstance(Stream<T> stream, Class<U> clazz) { return stream .filter(SdcctStreamUtils.<T, U>instances(clazz)) .findFirst() .map(clazz::cast) .orElse(null); }
@Parameters(name = "{0}") public static Object[] getAutomata() throws IOException { try (Stream<Path> configFiles = Files.walk(Paths.get("config/specification"))) { return configFiles .filter(path -> path.getFileName().toString().endsWith(".spc")) .sorted() .toArray(); } }
public static void main(String[] args) throws IOException { try (Stream<Path> stream = Files.list(Paths.get(""))) { String joined = stream .map(String::valueOf) .filter(path -> !path.startsWith(".")) .sorted() .collect(Collectors.joining("; ")); System.out.println("List: " + joined); } Path start = Paths.get(""); int maxDepth = 5; try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr) -> String.valueOf(path).endsWith(".java"))) { String joined = stream.sorted().map(String::valueOf).collect(Collectors.joining("; ")); System.out.println("Found: " + joined); } try (Stream<Path> stream = Files.walk(start, maxDepth)) { String joined = stream .map(String::valueOf) .filter(path -> path.endsWith(".java")) .sorted() .collect(Collectors.joining("; ")); System.out.println("walk(): " + joined); } List<String> lines = Files.readAllLines(Paths.get("src/golf.sh")); lines.add("puts 'foobar'"); Path path = Paths.get("src/golf-modified.sh"); Files.write(path, lines); try (Stream<String> stream = Files.lines(path)) { stream.filter(line -> line.contains("puts")).map(String::trim).forEach(System.out::println); } System.out.println("a" == "a"); System.out.println("a" != new String("a")); System.out.println(null != "a"); System.out.println("a".equals("a")); try (BufferedReader reader = Files.newBufferedReader(path)) { while (reader.ready()) System.out.println(reader.readLine()); } try (BufferedWriter writer = Files.newBufferedWriter(Paths.get("hello-world.sh"))) { writer.write("puts 'Hello world'"); } try (BufferedReader reader = Files.newBufferedReader(path)) { long countPuts = reader.lines().filter(line -> line.contains("put")).count(); System.out.println(countPuts); } }
protected void updateEntitiesList() { this.entities = getAll(); if (this.searchCriteria != null && this.searchCriteria.size() > 0) { Stream<T> stream = this.entities.stream(); for (FieldSearchCriterion<T> fieldSearchCriterion : this.searchCriteria) { stream = stream.filter(fieldSearchCriterion.buildPredicate()); } this.entities = stream.collect(Collectors.toList()); } this.notifyObservers(null); }
@Test public void testInvalidDatasetTypesFail() throws AssertionError { final Stream<DatasetType> allTypes = Arrays.stream(DatasetType.values()); allTypes .filter(t -> t != DatasetType.BIT) .forEach( type -> { dataset = datasetCreator.createDataset(type); final boolean result = (boolean) ij.op().run(DatasetIsBinary.class, dataset); final String typeClassName = dataset.getType().getClass().getName(); assertFalse("A Dataset of type " + typeClassName + " should not be binary", result); }); }
public void loadServerConfigFiles() { if (!Files.exists(serverStorageDir)) { LOGGER.info("Server storage directory doesn't exist, not loading anything"); return; } try (Stream<Path> files = Files.list(serverStorageDir)) { files .filter(p -> p.getFileName().toString().endsWith(".json")) .forEach(this::loadServerConfig); } catch (IOException e) { LOGGER.warn("Unable to load server storage files", e); return; } }
static boolean hasEntry(Path path) { boolean hasChild; try (Stream<Path> pathsStream = Files.walk(path)) { hasChild = pathsStream .filter(Files::isRegularFile) .map(FileSystemUtils::toFolderEntry) .findAny() .isPresent(); } catch (IOException e) { throw new TDPException(UNABLE_TO_DELETE_FOLDER, e, build().put("path", path)); } return hasChild; }
@Override public void addContent(PdfPTable table, Font font, IWeaponStats weapon) { if (weapon == null) { table.addCell(createEmptyNameCell(font)); } else { Stream<Identifier> tags = weapon.getTags().stream(); List<String> values = tags.filter(printedTags::contains) .map(input -> resources.getString("Weapons.Tags." + input.getId() + ".Short")) .collect(toList()); String valueString = values.isEmpty() ? " " : Joiner.on(",").join(values); table.addCell(createFilledContentCell(font, valueString)); } }
private Stream<PostponingReasons> getPostponingReasonsStream( String sql, boolean doDistinctAfterFirstStream) { final AccumulationContext context = new AccumulationContext(); final LazyResultSetIterator<Pair> iterator = LazyResultSetIterator.<Pair>of( Common.getRsSupplier(jdbcConnector, sql, "streamAllPostponingReasons"), Common.getMappingSqlFunction( rs -> new Pair( sqlDialect.translateFromDb(rs.getObject(1), UtcDay.class), CurrencyUnit.ofNumericCode(rs.getInt(2))), sql, "streamAllPostponingReasons"), sql); Stream<Pair> stream = iterator.stream(); if (doDistinctAfterFirstStream) { stream = stream.distinct(); } return stream .filter( pair -> { if (context.builder == null) { context.builder = ImmutableSet.builder(); context.currentDay = pair.day; context.builder.add(pair.unit); return false; } if (!pair.day.equals(context.currentDay)) { return true; } else { context.builder.add(pair.unit); return false; } }) .map( pair -> { final PostponingReasons postponingReasons = new PostponingReasons(context.currentDay, context.builder.build()); if (iterator.hasNext()) { context.builder = ImmutableSet.builder(); context.currentDay = pair.day; context.builder.add(pair.unit); } return postponingReasons; }); }
@Test(dependsOnMethods = "testLog4j2ConfigUpdate") public void testAuditLog() throws IOException, ConfigurationException { PrivilegedCarbonContext carbonContext = PrivilegedCarbonContext.getCurrentContext(); Principal principal = () -> "Banda"; carbonContext.setUserPrincipal(principal); Logger audit = org.wso2.carbon.kernel.Constants.AUDIT_LOG; audit.info("Attempting to test the audit logs."); Path auditLog = Paths.get(System.getProperty("carbon.home"), "logs", "audit.log"); Assert.assertTrue(Files.exists(auditLog), "audit.log does not exist."); try (Stream<String> stream = Files.lines(auditLog)) { Optional<String> match = stream.filter(line -> line.contains("user-name=Banda")).findAny(); Assert.assertTrue(match.isPresent(), "user-name=Banda is not found in the audit.log"); } }
// TODO get action should be able to return futures with sub-types of entity // wrapper public CompletableFuture<EntityWrapper<?>> get(String username) { // yes this is very inefficient. The plan is to get it working and then // get it right Stream<EntityRelationship> children; children = getEntities().stream(); Stream<EntityRelationship> filtered = children.filter( child -> { String childUsername = child.resolve(Account.class).join().getProperties().getUsername(); return childUsername == null && username == null || childUsername.equals(username); }); EntityRelationship entityRelationship = filtered.findAny().get(); return (CompletableFuture) entityRelationship.resolve(Account.class); }
@Override protected EntityLivingBase findNewTarget() { double maxDist = getTargetDistance(); List<T> entities = EntitySelector.type( taskOwner.worldObj, targetClass, taskOwner.boundingBox.expand(maxDist, maxDist * 0.5D, maxDist)); Stream<T> stream = entities .stream() .filter(entity -> entity.getDistanceSqToEntity(taskOwner) <= maxDist * maxDist); if (predicate != null) stream = stream.filter(predicate); entities = stream.collect(Collectors.toList()); if (entities.isEmpty()) return null; return entities.get(taskOwner.worldObj.rand.nextInt(entities.size())); }
private Stream<? extends Entity> validate( Stream<? extends Entity> entities, ValidationResource validationResource, ValidationMode validationMode) { if (ENTITIES_THAT_DO_NOT_NEED_VALIDATION.contains(getName())) { return entities; } // prepare validation initValidation(validationResource, validationMode); // add validation operation to stream return entities.filter( entity -> { validationResource.incrementRow(); validateEntityValueTypes(entity, validationResource); // other validation steps might not be able to handle invalid data types, stop here if (validationResource.hasViolations()) { throw new MolgenisValidationException(validationResource.getViolations()); } validateEntityValueRequired(entity, validationResource); validateEntityValueUniqueness(entity, validationResource, validationMode); validateEntityValueReferences(entity, validationResource); if (validationMode == ValidationMode.UPDATE) { validateEntityValueReadOnly(entity, validationResource); } if (validationResource.hasViolations()) { throw new MolgenisValidationException(validationResource.getViolations()); } return true; }); }
public static JsonObject invoke(JsonObject json, String area) { JsonObject responseData = json.getObject("ResponseData"); if (responseData == null) { return new JsonObject(); } if (Server.l != null) { Server.l.info(getLatestUpdate(json) + " - " + getExecutionTime(json)); } Predicate<String> isJsonArray = name -> responseData.getField(name) instanceof JsonArray; Function<String, List<JsonObject>> jsonArrayToList = name -> { Stream<Object> stream = stream(responseData.<JsonArray>getField(name).spliterator(), false); Stream<JsonObject> objectStream = stream.filter(getFilterFor(area)); return objectStream.collect(toList()); }; Predicate<Map.Entry<String, List<JsonObject>>> nonEmpty = entry -> !entry.getValue().isEmpty(); Stream<Map.Entry<String, List<JsonObject>>> entryStream = responseData .getFieldNames() .stream() .filter(isJsonArray) .collect(toMap(String::toLowerCase, jsonArrayToList)) .entrySet() .stream() .filter(nonEmpty); JsonObject result = toJsonObject(entryStream); setUniqueKeysOn(result.getArray("trains")); writeHeaderFields(result); return result; }
public Analyze() throws IOException { Stream<String> lines = Files.lines(Paths.get(Break.LOCATION + "/result")); Stream<Result> s = lines.map(this::parseLine); System.out.println("Searching ..."); FrequencyAnalysis frequencyAnalysis = new FrequencyAnalysis(FrequencyAnalysis.simple(), 0.8); Map<Break.Setup, String> answers = s.filter(result -> frequencyAnalysis.analyse(result.line)) // .filter(result -> // frequencyAnalysis.containsKeywords(result.line)) .collect(Collectors.toMap(r -> r.setup, r -> r.line)); System.out.println("Found " + answers.values().size()); answers.forEach( (k, v) -> { System.out.println(k + " -> " + v); }); if (answers.isEmpty()) { throw new IllegalStateException("Did not find anything ..."); } }
/** * Gets an iterator over those vertices/edges that have the specific IDs wanted, or those that * have indexed properties with the wanted values, or failing that by just getting all of the * vertices or edges. * * @param getElementsByIds Function that will return an iterator over all the vertices/edges in * the graph that have the specific IDs * @param getElementsByIndex Function that returns a stream of all the vertices/edges in the graph * that have an indexed property with a specific value * @param getAllElements Function that returns an iterator of all the vertices or all the edges * (i.e. full scan) * @return An iterator for all the vertices/edges for this step */ private <ElementType extends Element> Iterator<? extends ElementType> elements( BiFunction<OrientGraph, Object[], Iterator<ElementType>> getElementsByIds, TriFunction<OrientGraph, OIndex<Object>, Iterator<Object>, Stream<? extends ElementType>> getElementsByIndex, Function<OrientGraph, Iterator<ElementType>> getAllElements) { final OrientGraph graph = getGraph(); if (this.ids != null && this.ids.length > 0) { /** Got some element IDs, so just get the elements using those */ return this.iteratorList(getElementsByIds.apply(graph, this.ids)); } else { /** Have no element IDs. See if there's an indexed property to use */ Set<OrientIndexQuery> indexQueryOptions = findIndex(); if (!indexQueryOptions.isEmpty()) { List<ElementType> elements = new ArrayList<>(); indexQueryOptions.forEach( indexQuery -> { OLogManager.instance().debug(this, "using " + indexQuery); Stream<? extends ElementType> indexedElements = getElementsByIndex.apply(graph, indexQuery.index, indexQuery.values); elements.addAll( indexedElements .filter(element -> HasContainer.testAll(element, this.hasContainers)) .collect(Collectors.<ElementType>toList())); }); return elements.iterator(); } else { OLogManager.instance() .warn( this, "scanning through all elements without using an index for Traversal " + getTraversal()); return this.iteratorList(getAllElements.apply(graph)); } } }
@Override public void logout() { final HttpUriRequest logoutGet = RequestBuilder.get().setUri(NYT_LOGOUT_URL).build(); try (final CloseableHttpResponse getResponse = this.getHttpClient().execute(logoutGet)) { // successful NYT logout should give 200 status final int responseStatus = getResponse.getStatusLine().getStatusCode(); if (responseStatus != 200) { final String errorMessage = String.format("did not detect expected 200, got %d instead", responseStatus); throw new LogoutException(errorMessage); } // successful NYT logout should delete a few cookies like this: // Set-Cookie: NYT-S=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/; // domain=.nytimes.com final Header[] cookies = getResponse.getHeaders("Set-Cookie"); if (cookies.length < 1) { throw new LogoutException("no cookie deletions detected, logout might have failed"); } final Stream<Header> cookieStream = Arrays.stream(cookies); final Predicate<Header> deletedCheck = c -> c.getValue().contains("deleted"); if (!cookieStream.allMatch(deletedCheck)) { final List<Header> unexpectedCookies = cookieStream.filter(deletedCheck).collect(Collectors.toList()); LOG.error("unexpected cookies={}", unexpectedCookies); throw new LogoutException("unexpected cookie(s) set, loguout might have failed"); } LOG.info("successfully logged out of nyt"); } catch (IOException | LogoutException e) { LOG.error("error while logging out of nyt, e={}", e); } }
private static String getClassName(File inputFile) throws FileNotFoundException { String className; try { Path myPath = Paths.get(inputFile.toURI()); Stream<String> lines = Files.lines(myPath); String line = lines.filter(s -> s.startsWith(".class ")).findFirst().get(); lines.close(); if (null == line) { throw new RuntimeException("Missing class directive in " + inputFile); } Matcher m = CLASS_PATTERN.matcher(line); if (!m.find()) { throw new RuntimeException("Strange class directive: " + line); } className = m.group(1); } catch (IOException e) { throw new RuntimeException("Unable to read class name in " + inputFile, e); } return className; }
private static int positiveSumStreamApproach(List<Integer> list) { Stream<Integer> stream = list.stream(); int sum = stream.filter(i -> i > 0).mapToInt(i -> i).sum(); return sum; }
public static <T, U> Stream<U> asInstances(Stream<T> stream, Class<U> clazz) { return stream.filter(SdcctStreamUtils.<T, U>instances(clazz)).map(clazz::cast); }
public static <T, U> Stream<T> filterInstances(Stream<T> stream, Class<U> clazz) { return stream.filter(clazz::isInstance); }
@Override public Object filter(Stream t, Predicate p) { return t.filter(p); }
static void filesTest() { // Files.list try { try (Stream<Path> stream = Files.list(Paths.get("/opt"))) { String joined = stream .map(String::valueOf) .filter(path -> !path.startsWith(".")) .sorted() .collect(Collectors.joining("; ")); System.out.println("List path /opt : " + joined); } } catch (IOException e) { e.printStackTrace(); } // Files.find Path start = Paths.get("/Users/alibaba/Downloads/2016113"); int maxDepth = 5; try (Stream<Path> stream = Files.find(start, maxDepth, (path, attr) -> String.valueOf(path).endsWith(".js"))) { String joined = stream.sorted().map(String::valueOf).collect(Collectors.joining("; ")); System.out.println("Files find : " + joined); } catch (IOException e) { e.printStackTrace(); } // Files.walk try (Stream<Path> stream = Files.walk(start, maxDepth)) { String joined = stream .map(String::valueOf) .filter(path -> path.endsWith(".js")) .sorted() .collect(Collectors.joining("; ")); System.out.println("Files walk : " + joined); } catch (IOException e) { e.printStackTrace(); } // Files.readAllLines try { String p = "/Users/alibaba/linuxsir.txt"; List<String> lines = Files.readAllLines(Paths.get(p)); lines.add("print('foobar');"); Files.write(Paths.get(p), lines); lines.remove(lines.size() - 1); System.out.println("readAllLines " + lines); Files.write(Paths.get(p), lines); } catch (IOException e) { e.printStackTrace(); } // Files.lines try (Stream<String> stream = Files.lines(Paths.get("/Users/alibaba/linuxsir.txt"))) { stream.filter(line -> line.contains("w")).map(String::valueOf).forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } // Files.newBufferedReader&Files.newBufferedWriter Path path = Paths.get("/Users/alibaba/linuxsir.txt"); try (BufferedReader reader = Files.newBufferedReader(path)) { System.out.println(reader.readLine()); } catch (IOException e) { e.printStackTrace(); } path = Paths.get("/Users/alibaba/output.txt"); try (BufferedWriter writer = Files.newBufferedWriter(path)) { writer.write("print('Hello World')"); } catch (IOException e) { e.printStackTrace(); } }
/** * Convert a stream of objects into a set of strings. * * @param stream stream of objects * @return set of strings */ private Set<String> streamToStrings(Stream<Object> stream) { return stream .filter(String.class::isInstance) .map(String.class::cast) .collect(Collectors.toSet()); }