/** * Returns an {@link Optional} containing the first element in this fluent iterable that satisfies * the given predicate, if such an element exists. * * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code null}. If {@code null} * is matched in this fluent iterable, a {@link NullPointerException} will be thrown. * * <p><b>{@code Stream} equivalent:</b> {@code stream.filter(predicate).findFirst()}. */ public final Optional<E> firstMatch(Predicate<? super E> predicate) { return Iterables.tryFind(getDelegate(), predicate); }
private Annotation cacheKeyAnnotation() { return Iterables.tryFind(annotations, new Predicate<Annotation>() { @Override public boolean apply(Annotation input) { return input.annotationType().equals(CacheKey.class); } }).orNull(); }
private boolean isCachedInTenantKVCache(final String key) { return Iterables.tryFind(CACHED_TENANT_KEY, new Predicate<TenantKey>() { @Override public boolean apply(final TenantKey input) { return key.startsWith(input.toString()); } }).orNull() != null; } }
/** * Returns an {@link Optional} containing the first element in this fluent iterable that satisfies * the given predicate, if such an element exists. * * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code null}. If {@code null} * is matched in this fluent iterable, a {@link NullPointerException} will be thrown. * * <p><b>{@code Stream} equivalent:</b> {@code stream.filter(predicate).findFirst()}. */ public final Optional<E> firstMatch(Predicate<? super E> predicate) { return Iterables.tryFind(getDelegate(), predicate); }
/** * Returns an {@link Optional} containing the first element in this fluent iterable that satisfies * the given predicate, if such an element exists. * * <p><b>Warning:</b> avoid using a {@code predicate} that matches {@code null}. If {@code null} * is matched in this fluent iterable, a {@link NullPointerException} will be thrown. * * <p><b>{@code Stream} equivalent:</b> {@code stream.filter(predicate).findFirst()}. */ public final Optional<E> firstMatch(Predicate<? super E> predicate) { return Iterables.tryFind(getDelegate(), predicate); }
private boolean isSystemKey(final String key) { return Iterables.tryFind(ImmutableList.copyOf(TenantKey.values()), new Predicate<TenantKey>() { @Override public boolean apply(final TenantKey input) { return key.startsWith(input.toString()); } }).orNull() != null; }
private boolean isSingleValueKey(final String key) { return Iterables.tryFind(ImmutableList.copyOf(TenantKey.values()), new Predicate<TenantKey>() { @Override public boolean apply(final TenantKey input) { return input.isSingleValue() && key.startsWith(input.toString()); } }).orNull() != null; }
/** * Tests a string against a set of valid choices to see if it is a * case-insensitive match. * * @param choices The choices available to match against * @param potentialChoice The potential choice * @return If matched, an {@link Optional} containing the matched value */ protected Optional<Object> getExactMatch(final Iterable<String> choices, final String potentialChoice) { return Iterables.tryFind(choices, potentialChoice::equalsIgnoreCase).toJavaUtil().map(this::getValue); }
@Override public Extractor getExtractor(final Input input, final String extractorId) throws NotFoundException { final Optional<Extractor> extractor = Iterables.tryFind(this.getExtractors(input), new Predicate<Extractor>() { @Override public boolean apply(Extractor extractor) { return extractor.getId().equals(extractorId); } }); if (!extractor.isPresent()) { LOG.error("Extractor <{}> not found.", extractorId); throw new NotFoundException("Couldn't find extractor " + extractorId); } return extractor.get(); }
public NodeCommandMetadata deserializeNodeCommand(final String nodeCommand, final String type) throws IOException { final SystemNodeCommandType systemType = Iterables.tryFind(ImmutableList.copyOf(SystemNodeCommandType.values()), new Predicate<SystemNodeCommandType>() { @Override public boolean apply(final SystemNodeCommandType input) { return input.name().equals(type); } }).orNull(); return (systemType != null) ? mapper.readValue(nodeCommand, systemType.getCommandMetadataClass()) : mapper.readValue(nodeCommand, DefaultNodeCommandMetadata.class); }
private void validateApplicableObjectTypes(final UUID tagDefinitionId, final ObjectType objectType) throws TagApiException { final ControlTagType controlTagType = Iterables.tryFind(ImmutableList.<ControlTagType>copyOf(ControlTagType.values()), new Predicate<ControlTagType>() { @Override public boolean apply(final ControlTagType input) { return input.getId().equals(tagDefinitionId); } }).orNull(); if (controlTagType != null && !controlTagType.getApplicableObjectTypes().contains(objectType)) { // TODO Add missing ErrorCode.TAG_NOT_APPLICABLE // throw new TagApiException(ErrorCode.TAG_NOT_APPLICABLE); throw new IllegalStateException(String.format("Invalid control tag '%s' for object type '%s'", controlTagType.name(), objectType)); } }
public void testTryFind() { Iterable<String> list = newArrayList("cool", "pants"); assertThat(Iterables.tryFind(list, Predicates.equalTo("cool"))).hasValue("cool"); assertThat(Iterables.tryFind(list, Predicates.equalTo("pants"))).hasValue("pants"); assertThat(Iterables.tryFind(list, Predicates.alwaysTrue())).hasValue("cool"); assertThat(Iterables.tryFind(list, Predicates.alwaysFalse())).isAbsent(); assertCanIterateAgain(list); }
private Iterable<Session> toUniqueSessions(final Session targetSession, final ImmutableList<Session> sessions) { Optional<Session> session = tryFind(sessions, isForRequest(targetSession.getRequest())); if (session.isPresent()) { return from(sessions).filter(not(isForRequest(targetSession.getRequest()))); } return sessions; }
@Override public HttpResponse failover(final HttpRequest request) { ImmutableList<Session> sessions = restoreSessions(this.file); final Optional<Session> session = tryFind(sessions, isForRequest(request)); if (session.isPresent()) { return session.get().getResponse(); } logger.error("No match request found: {}", request); throw new MocoException("no failover response found"); }
@Override public TaskLocation getTaskLocation(final String id) { Preconditions.checkNotNull(id, "id"); Optional<TaskRunner> taskRunner = taskMaster.getTaskRunner(); if (taskRunner.isPresent()) { Optional<? extends TaskRunnerWorkItem> item = Iterables.tryFind( taskRunner.get().getRunningTasks(), (Predicate<TaskRunnerWorkItem>) taskRunnerWorkItem -> id.equals(taskRunnerWorkItem.getTaskId()) ); if (item.isPresent()) { return item.get().getLocation(); } } else { log.error("Failed to get task runner because I'm not the leader!"); } return TaskLocation.unknown(); }
private <T> T find(final Iterable<T> all, @Nullable final String name, final String what, final Predicate<T> predicate) { if (name == null) { return null; } final T result = Iterables.tryFind(all, predicate).orNull(); if (result == null) { throw new IllegalStateException(String.format("%s : cannot find entry %s", what, name)); } return result; } }
private static boolean isAnnotationPresent(CacheInvocationParameter parameter, final Class<?> annotation) { return Iterables.tryFind(parameter.getAnnotations(), new Predicate<Annotation>() { @Override public boolean apply(Annotation input) { return input.annotationType().equals(annotation); } }).isPresent(); } }
@CacheResult(cacheKeyMethod = "getUserByEmailCacheKey") @HystrixCommand public User getUserByEmail(final String email) { return Iterables.tryFind(storage.values(), new Predicate<User>() { @Override public boolean apply(User input) { return input.getProfile().getEmail().equalsIgnoreCase(email); } }).orNull(); }
protected MockFlowFile findFlowFile(List<MockFlowFile> flowFilesForRelationship, String attributeName, String attributeValue) { Optional<MockFlowFile> optional = Iterables.tryFind(flowFilesForRelationship, ff -> ff.getAttribute(attributeName).equals(attributeValue)); Assert.assertTrue(optional.isPresent()); return optional.get(); } }
protected MockFlowFile findFlowFile(List<MockFlowFile> flowFilesForRelationship, String attributeName, String attributeValue) { Optional<MockFlowFile> optional = Iterables.tryFind(flowFilesForRelationship, ff -> ff.getAttribute(attributeName).equals(attributeValue)); Assert.assertTrue(optional.isPresent()); return optional.get(); }