Java Code Examples for java.util.stream.Stream#allMatch()

The following examples show how to use java.util.stream.Stream#allMatch() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImmutableAcknowledgements.java    From ditto with Eclipse Public License 2.0 6 votes vote down vote up
private static HttpStatusCode getCombinedStatusCode(final Collection<? extends Acknowledgement> acknowledgements) {
    final HttpStatusCode result;
    if (1 == acknowledgements.size()) {
        result = acknowledgements.stream()
                .findFirst()
                .map(Acknowledgement::getStatusCode)
                .orElse(HttpStatusCode.INTERNAL_SERVER_ERROR);
    } else {
        final Stream<? extends Acknowledgement> acknowledgementStream = acknowledgements.stream();
        final boolean allAcknowledgementsSuccessful = acknowledgementStream.allMatch(Acknowledgement::isSuccess);
        if (allAcknowledgementsSuccessful) {
            result = HttpStatusCode.OK;
        } else {
            result = HttpStatusCode.FAILED_DEPENDENCY;
        }
    }
    return result;
}
 
Example 2
Source File: AtomicUpdateDocumentMerger.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 *
 * @param fullDoc the full doc to  be compared against
 * @param partialDoc the sub document to be tested
 * @return whether partialDoc is derived from fullDoc
 */
public static boolean isDerivedFromDoc(SolrInputDocument fullDoc, SolrInputDocument partialDoc) {
  for(SolrInputField subSif: partialDoc) {
    Collection<Object> fieldValues = fullDoc.getFieldValues(subSif.getName());
    if (fieldValues == null) return false;
    if (fieldValues.size() < subSif.getValueCount()) return false;
    Collection<Object> partialFieldValues = subSif.getValues();
    // filter all derived child docs from partial field values since they fail List#containsAll check (uses SolrInputDocument#equals which fails).
    // If a child doc exists in partialDoc but not in full doc, it will not be filtered, and therefore List#containsAll will return false
    Stream<Object> nonChildDocElements = partialFieldValues.stream().filter(x -> !(isChildDoc(x) &&
        (fieldValues.stream().anyMatch(y ->
            (isChildDoc(x) &&
                isDerivedFromDoc((SolrInputDocument) y, (SolrInputDocument) x)
            )
        )
        )));
    if (!nonChildDocElements.allMatch(fieldValues::contains)) return false;
  }
  return true;
}
 
Example 3
Source File: AbstractSetMultimapProperties.java    From capsule with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Property(trials = DEFAULT_TRIALS)
public void entryIteratorAfterInsert(@Size(min = 0, max = 0) final CT emptyCollection,
    @Size(min = 1, max = MAX_SIZE) final java.util.HashSet<Map.Entry<K, V>> inputValues) {

  CT testCollection = emptyCollection;

  for (Map.Entry<K, V> newValueTuple : inputValues) {
    final CT tmpCollection =
        (CT) testCollection.__insert(newValueTuple.getKey(), newValueTuple.getValue());
    testCollection = tmpCollection;
  }

  final CT finalCollection = testCollection;

  final Spliterator<Map.Entry> entrySpliterator = Spliterators
      .spliterator(finalCollection.entryIterator(), finalCollection.size(), Spliterator.DISTINCT);
  final Stream<Map.Entry> entryStream = StreamSupport.stream(entrySpliterator, false);

  boolean containsInsertedValues = entryStream.allMatch(inputValues::contains);

  assertTrue("Must contain all inserted values.", containsInsertedValues);
}
 
Example 4
Source File: EventQueue.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * This is just for unit testing, don't use it for production code.
 * <p>
 * It waits for all messages to be processed. If one event handler invokes an
 * other one, the later one also should be finished.
 * <p>
 * Long counter overflow is not handled, therefore it's safe only for unit
 * testing.
 * <p>
 * This method is just eventually consistent. In some cases it could return
 * even if there are new messages in some of the handler. But in a simple
 * case (one message) it will return only if the message is processed and
 * all the dependent messages (messages which are sent by current handlers)
 * are processed.
 *
 * @param timeout Timeout in milliseconds to wait for the processing.
 */
@VisibleForTesting
public void processAll(long timeout) {
  long currentTime = Time.now();
  while (true) {

    if (!isRunning) {
      LOG.warn("Processing of event skipped. EventQueue is not running");
      return;
    }

    long processed = 0;

    Stream<EventExecutor> allExecutor = this.executors.values().stream()
        .flatMap(handlerMap -> handlerMap.keySet().stream());

    boolean allIdle =
        allExecutor.allMatch(executor -> executor.queuedEvents() == executor
            .successfulEvents() + executor.failedEvents());

    if (allIdle) {
      return;
    }

    try {
      Thread.sleep(100);
    } catch (InterruptedException e) {
      LOG.warn("Interrupted exception while sleeping.", e);
      Thread.currentThread().interrupt();
    }

    if (Time.now() > currentTime + timeout) {
      throw new AssertionError(
          "Messages are not processed in the given timeframe. Queued: "
              + queuedCount.get() + " Processed: " + processed);
    }
  }
}
 
Example 5
Source File: ArchUnitTestEngineTest.java    From ArchUnit with Apache License 2.0 5 votes vote down vote up
@Test
void a_class_with_simple_hierarchy__descriptor_types() {
    EngineDiscoveryTestRequest discoveryRequest = new EngineDiscoveryTestRequest().withClass(SimpleRuleLibrary.class);

    TestDescriptor descriptor = testEngine.discover(discoveryRequest, engineId);

    Stream<TestDescriptor> archRulesDescriptors = getArchRulesDescriptorsOfOnlyChild(descriptor);
    boolean allAreContainer = archRulesDescriptors.allMatch(d -> d.getType().equals(CONTAINER));
    assertThat(allAreContainer).as("all rules descriptor have type " + CONTAINER).isTrue();
}
 
Example 6
Source File: AbstractBinaryRelationProperties.java    From capsule with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Property(trials = DEFAULT_TRIALS)
public void inverse(CT input) {
  final BinaryRelation.Immutable<V, K> inverseInput =
      (BinaryRelation.Immutable<V, K>) input.inverse();
  final Stream<Map.Entry<K, V>> entryStream = input.entrySet().stream();

  boolean inverseContainsInversedTuples =
      entryStream.allMatch(tuple -> inverseInput.containsEntry(tuple.getValue(), tuple.getKey()));

  assertTrue(inverseContainsInversedTuples);
}
 
Example 7
Source File: MessageTypeMetricSet.java    From vespa with Apache License 2.0 4 votes vote down vote up
/**
 * Returns true if every error in a stream is a test and set condition failed
 */
private static boolean onlyTestAndSetConditionFailed(Stream<Error> errors) {
    return errors.allMatch(e -> e.getCode() == DocumentProtocol.ERROR_TEST_AND_SET_CONDITION_FAILED);
}
 
Example 8
Source File: SessionWithInitializedTablesFactory.java    From james-project with Apache License 2.0 4 votes vote down vote up
private boolean allOperationsAreFullyPerformed(Session session, CassandraModule module) {
    Stream<Boolean> operations = Stream.of(createTypes(session, module), createTables(session, module));
    return operations.allMatch(updated -> updated);
}
 
Example 9
Source File: CollectionUtils.java    From freecol with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Implementation of all().
 *
 * @param <T> The stream member type.
 * @param stream The {@code Stream} to test.
 * @param predicate The {@code Predicate} to test with.
 * @return True if all members pass the predicate test.
 */
private static <T> boolean all_internal(Stream<T> stream,
                                        Predicate<? super T> predicate) {
    return stream.allMatch(predicate);
}