下面列出了java.util.stream.Stream#allMatch ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
/**
*
* @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;
}
@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);
}
/**
* 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);
}
}
}
@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();
}
@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);
}
/**
* 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);
}
private boolean allOperationsAreFullyPerformed(Session session, CassandraModule module) {
Stream<Boolean> operations = Stream.of(createTypes(session, module), createTables(session, module));
return operations.allMatch(updated -> updated);
}
/**
* 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);
}