private int indexOfBatchChunkSizeParameter(Method method) { Annotation[][] parameterAnnotations = method.getParameterAnnotations(); return IntStream.range(0, parameterAnnotations.length) .filter(i -> Stream.of(parameterAnnotations[i]).anyMatch(BatchChunkSize.class::isInstance)) .findFirst() .orElse(-1); }
public static <T extends Comparable<? super T>> int sortedIndex(T[] arr, T el) { boolean isDescending = arr[0].compareTo(arr[arr.length - 1]) > 0; return IntStream.range(0, arr.length) .filter(i -> isDescending ? el.compareTo(arr[i]) >= 0 : el.compareTo(arr[i]) <= 0) .findFirst() .orElse(arr.length); }
/** * Creates a deterministic hash code for store file collection. */ public static OptionalInt getDeterministicRandomSeed(Collection<HStoreFile> files) { return files.stream().mapToInt(f -> f.getPath().getName().hashCode()).findFirst(); }
/** * Find index of element in the array. Return -1 in case element does not exist. * <p> * Uses IntStream.range().filter() to find index of the element in the array. * * @param elements input array * @param el element to find * @return index of the element */ public static int indexOf(int[] elements, int el) { return IntStream.range(0, elements.length) .filter(idx -> elements[idx] == el) .findFirst() .orElse(-1); }
@Override public OptionalInt execute() { try (final IntStream stream = buildPrevious()) { return stream.findFirst(); } } }
@Override public void saveProfile(Map<String, Object> profile) { int idx = IntStream.range(0, profiles.size()) .filter(i -> profile.get("name").equals(profiles.get(i).get("name"))) .findFirst().orElse(-1); if (idx == -1) { profiles.add(profile); } else { profiles.set(idx, profile); } saveSparkConf(profiles); }
static List<Map<String, Object>> frames(final ClassLoader loader, final SourceLocator locator, final Throwable cause) { List<StackTraceElement> stacktrace = Arrays.asList(cause.getStackTrace()); int limit = IntStream.range(0, stacktrace.size()) .filter(i -> stacktrace.get(i).getClassName().equals(HANDLER)).findFirst() .orElse(stacktrace.size()); return stacktrace.stream() // trunk stack at HttpHandlerImpl (skip servers stack) .limit(limit) .map(e -> frame(loader, locator, cause, e)) .collect(Collectors.toList()); }
public synchronized int getAvailablePort() { int result = IntStream.generate(supplier) .filter(port -> !usedPorts.contains(port)) .findFirst() .getAsInt(); usedPorts.add(result); return result; } }
private int driverVersion() { return Stream.of( driver(NEW_DRIVER), driver(OLD_DRIVER) ) .filter(Optional::isPresent) .map(Optional::get) .mapToInt(Driver::getMajorVersion) .findFirst() .orElse(5); // Fallback. There is no driver so we just make up a version } }
Random r = new Random(); int[] fiveRandomNumbers = r.ints(5, 0, 11).toArray(); int randomNumber = r.ints(1, 0, 11).findFirst().getAsInt();
public Optional<DocItem> pop(final String classname, String method, String pattern) { try { List<DocItem> javadoc = javadoc(classname); int pos = IntStream.range(0, javadoc.size()) .filter(i -> javadoc.get(i).matches(method, pattern)) .findFirst() .orElse(-1); if (pos >= 0) { return Optional.ofNullable(javadoc.remove(pos)); } } catch (Exception x) { log.debug("Parsing of javadoc resulted in exception: {}", classname, x); } return Optional.empty(); }
default OptionalInt findFirst(IntPipeline pipeline) { requireNonNull(pipeline); return optimize(pipeline).getAsIntStream().findFirst(); }
/** * Find last index of element in the array. Return -1 in case element does not exist. * <p> * Uses IntStream.iterate().limit().filter() to find index of the element in the array. * * @param elements input array * @param el element to find * @return index of the element */ public static int lastIndexOf(int[] elements, int el) { return IntStream.iterate(elements.length - 1, i -> i - 1) .limit(elements.length) .filter(idx -> elements[idx] == el) .findFirst() .orElse(-1); }
private static Integer token( String name, Map<Integer,String> tokens ) { IntSupplier allocateFromMap = () -> { int newIndex = tokens.size(); tokens.put( newIndex, name ); return newIndex; }; return tokens.entrySet().stream() .filter( entry -> entry.getValue().equals( name ) ) .mapToInt( Map.Entry::getKey ) .findFirst().orElseGet( allocateFromMap ); }
private static int findOpenPort(final int startInclusive, final int endExclusive) { return range(startInclusive, endExclusive) .filter(RedisRule::testPort) .findFirst() .orElseThrow(() ->new IllegalStateException(format( "No open port found in the range [%d, %d]", startInclusive, endExclusive))); }
public String name(final Parameter parameter) { String name = RequestParam.nameFor(parameter); if (name != null) { return name; } // asm Executable exec = parameter.getDeclaringExecutable(); Parameter[] params = exec.getParameters(); int idx = IntStream.range(0, params.length) .filter(i -> params[i].equals(parameter)) .findFirst() .getAsInt(); String[] names = nameProvider.names(exec); return names[idx]; }
/** * Parse application bytecode, build route methods from it and merge output with the given routes. * * @param application Application to parse. * @param routes Application routes. * @return List of route methods. * @throws Exception If something goes wrong. */ public List<RouteMethod> parseFully(String application, List<Route.Definition> routes) throws Exception { List<RouteMethod> methods = parse(application); BiFunction<String, String, OptionalInt> routeIndex = (verb, pattern) -> { RouteMethod it = new RouteMethod(verb, pattern, new RouteResponse(void.class)); return IntStream.range(0, methods.size()) .filter(i -> methods.get(i).equals(it)) .findFirst(); }; List<RouteMethod> result = new ArrayList<>(routes.size()); for (Route.Definition route : routes) { routeIndex.apply(route.method(), route.pattern()) .ifPresent(i -> result.add(complement(route, methods.remove(i)))); } customizer.entrySet().forEach(it -> result.stream() .filter(it.getKey()) .forEach(it.getValue()) ); return result; }
AtomicInteger count = new AtomicInteger(IntStream.range(0, args.size()) .filter(i -> !args.get(i).getClassName().equals(String.class.getName())) .findFirst() .orElse(0)); int from = 0;
/** */ private static int keyInSamePartition(Ignite ignite, String cacheName, int key) { Affinity<Object> affinity = ignite.affinity(cacheName); return IntStream.iterate(key + 1, i -> i + 1) .filter(i -> affinity.partition(i) == affinity.partition(key)) .findFirst().getAsInt(); }
/** */ private static int keyInDifferentPartition(Ignite ignite, String cacheName, int key) { Affinity<Object> affinity = ignite.affinity(cacheName); return IntStream.iterate(key + 1, i -> i + 1) .filter(i -> affinity.partition(i) != affinity.partition(key)) .findFirst().getAsInt(); } }