下面列出了怎么用com.google.common.collect.Iterators的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
default void browseShouldNotFailWhenConcurrentEnqueue() throws Exception {
enQueue(defaultMail()
.name("name1")
.build());
enQueue(defaultMail()
.name("name2")
.build());
enQueue(defaultMail()
.name("name3")
.build());
ManageableMailQueue.MailQueueIterator items = getManageableMailQueue().browse();
enQueue(defaultMail()
.name("name4")
.build());
assertThatCode(() -> Iterators.consumingIterator(items)).doesNotThrowAnyException();
}
@Override
public short[] getAdapterIds() {
final MetadataReader reader = getReader(false);
if (reader == null) {
return new short[0];
}
final CloseableIterator<GeoWaveMetadata> results =
reader.query(new MetadataQuery(null, EXTERNAL_TO_INTERNAL_ID));
try (CloseableIterator<Short> it =
new CloseableIteratorWrapper<>(
results,
Iterators.transform(
results,
input -> ByteArrayUtils.byteArrayToShort(input.getValue())))) {
return ArrayUtils.toPrimitive(Iterators.toArray(it, Short.class));
}
}
@Override
public Iterator<Result<Titan0Vertex, Titan0Edge>> vertices(int offset, int limit) {
Preconditions.checkArgument(offset >=0, "Index offset should be greater than or equals to 0");
Preconditions.checkArgument(limit >=0, "Index limit should be greater than or equals to 0");
Iterator<TitanIndexQuery.Result<Vertex>> results = wrappedIndexQuery
.offset(offset)
.limit(limit)
.vertices().iterator();
Function<TitanIndexQuery.Result<Vertex>, AtlasIndexQuery.Result<Titan0Vertex, Titan0Edge>> function =
new Function<TitanIndexQuery.Result<Vertex>, AtlasIndexQuery.Result<Titan0Vertex, Titan0Edge>>() {
@Override
public AtlasIndexQuery.Result<Titan0Vertex, Titan0Edge> apply(TitanIndexQuery.Result<Vertex> source) {
return new ResultImpl(source);
}
};
return Iterators.transform(results, function);
}
@Test
public void callbackCount() {
BulkImportStoredProcedureOptions options = null;
String bulkImportSproc = null;
DocumentClient client = Mockito.mock(DocumentClient.class);
List<List<String>> batchesToInsert = new ArrayList<>();
batchesToInsert.add(new ArrayList<>());
batchesToInsert.add(new ArrayList<>());
batchesToInsert.add(new ArrayList<>());
String partitionIndex = "0";
BatchInserter bi = new BatchInserter(partitionIndex, batchesToInsert, client, bulkImportSproc, options);
Iterator<Callable<InsertMetrics>> callbackIterator = bi.miniBatchInsertExecutionCallableIterator();
List<Callable<InsertMetrics>> list = new ArrayList<>();
Iterators.addAll(list, callbackIterator);
assertThat(list.size(), equalTo(3));
}
@Test
public void testExpansionWithOrder() {
final MutableOffsetMap<String, String> mutable = createMap().toModifiableMap();
mutable.remove("k1");
mutable.put("k3", "v3");
mutable.put("k1", "v1");
assertEquals(ImmutableMap.of("k1", "v1", "k3", "v3"), mutable.newKeys());
final Map<String, String> result = mutable.toUnmodifiableMap();
assertTrue(result instanceof ImmutableOffsetMap);
assertEquals(threeEntryMap, result);
assertEquals(result, threeEntryMap);
assertFalse(Iterators.elementsEqual(threeEntryMap.entrySet().iterator(), result.entrySet().iterator()));
}
private void merge(List<Profile.ProfileNode> flatNodes,
List<ProfileNode> destinationRootNodes) {
destinationStack.push(destinationRootNodes);
PeekingIterator<Profile.ProfileNode> i =
Iterators.peekingIterator(flatNodes.iterator());
while (i.hasNext()) {
Profile.ProfileNode flatNode = i.next();
int destinationDepth = destinationStack.size() - 1;
for (int j = 0; j < destinationDepth - flatNode.getDepth(); j++) {
// TODO optimize: faster way to pop multiple elements at once
destinationStack.pop();
}
ProfileNode destinationNode = mergeOne(flatNode, destinationStack.getFirst());
if (i.hasNext() && i.peek().getDepth() > flatNode.getDepth()) {
destinationStack.push(destinationNode.childNodes);
}
}
}
@Override
public Iterator<ResolutionState> innerStateIterator(AnswerPropagatorState parent, Set<ReasonerAtomicQuery> subGoals){
Iterator<AnswerState> dbIterator;
Iterator<AnswerPropagatorState> subGoalIterator;
if(!this.isRuleResolvable()) {
Set<Type> queryTypes = new HashSet<>(this.getVarTypeMap().values());
boolean fruitless = context().ruleCache().absentTypes(queryTypes);
if (fruitless) dbIterator = Collections.emptyIterator();
else {
dbIterator = traversalExecutor.traverse(getPattern())
.map(ans -> new ConceptMap(ans.map(), new JoinExplanation(this.splitToPartialAnswers(ans)), this.withSubstitution(ans).getPattern()))
.map(ans -> new AnswerState(ans, parent.getUnifier(), parent))
.iterator();
}
subGoalIterator = Collections.emptyIterator();
} else {
dbIterator = Collections.emptyIterator();
ResolutionQueryPlan queryPlan = new ResolutionQueryPlan(context().queryFactory(), this);
subGoalIterator = Iterators.singletonIterator(new JoinState(queryPlan.queries(), new ConceptMap(), parent.getUnifier(), parent, subGoals));
}
return Iterators.concat(dbIterator, subGoalIterator);
}
@Override
public int getOffset() {
Iterator<ILeafNode> leafIter = Iterators.filter(basicIterator(), ILeafNode.class);
int firstLeafOffset = -1;
while(leafIter.hasNext()) {
ILeafNode leaf = leafIter.next();
if (firstLeafOffset == -1) {
firstLeafOffset = leaf.getTotalOffset();
}
if (!leaf.isHidden())
return leaf.getTotalOffset();
}
if (firstLeafOffset != -1)
return firstLeafOffset;
return getTotalOffset();
}
/** Test getTimeline() with timestamp start/end instead of UUIDs. */
@Test
public void testGetTimelineRESTTimestampsForward() throws Exception {
Date start = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").parse("2012-03-15 16:12:34.567");
Date end = new Date();
UUID startUuid = TimeUUIDs.uuidForTimestamp(start);
UUID endUuid = TimeUUIDs.getPrevious(TimeUUIDs.uuidForTimeMillis(end.getTime() + 1));
when(_server.getTimeline("table-name", "row-key", true, false, startUuid, endUuid, false, 10, ReadConsistency.STRONG))
.thenReturn(Iterators.<Change>emptyIterator());
DateTimeFormatter format = DateTimeFormatter.ISO_INSTANT;
URI uri = UriBuilder.fromUri("/sor/1")
.segment("table-name", "row-key", "timeline")
.queryParam("start", format.format(start.toInstant()))
.queryParam("end", format.format(end.toInstant()))
.queryParam("reversed", "false")
.build();
_resourceTestRule.client().resource(uri)
.accept(MediaType.APPLICATION_JSON_TYPE)
.header(ApiKeyRequest.AUTHENTICATION_HEADER, APIKEY_TABLE)
.get(new GenericType<List<Change>>() {
});
verify(_server).getTimeline("table-name", "row-key", true, false, startUuid, endUuid, false, 10, ReadConsistency.STRONG);
verifyNoMoreInteractions(_server);
}
/**
* Populate {@link Credentials} for the URI's to access them from their {@link FileSystem}s
* @param uris URIs that need to be accessed
* @param credentials Credentials object into which to add the credentials
* @param conf Configuration to access the FileSystem
* @throws IOException
*/
public static void addFileSystemCredentialsFromURIs(Collection<URI> uris, Credentials credentials,
Configuration conf) throws IOException {
// Obtain Credentials for any paths that the user may have configured.
if (uris != null && !uris.isEmpty()) {
Iterator<Path> pathIter = Iterators.transform(uris.iterator(), new Function<URI, Path>() {
@Override
public Path apply(URI input) {
return new Path(input);
}
});
Path[] paths = Iterators.toArray(pathIter, Path.class);
TokenCache.obtainTokensForFileSystems(credentials, paths, conf);
}
}
@Override
public Set<Entry<K, V>> entrySet() {
// A call to "Sets.union(ks1, ks2)" does not work because of the equals() definition on Map.Entry.
// This equality test breaks the unicity of the keys over the resulting Set.
// In other words, "Sets.union(ks1, ks2)" replies all the entries that
// are different on their keys or values.
final Set<Entry<K, V>> diff = difference(this.left, this.right);
return new AbstractEarlyFailingSet<Entry<K, V>>() {
@SuppressWarnings({ "unchecked", "rawtypes", "synthetic-access" })
@Override
public Iterator<Entry<K, V>> iterator() {
return Iterators.unmodifiableIterator((Iterator) Iterators.concat(
UnmodifiableMergingMapView.this.right.entrySet().iterator(), diff.iterator()));
}
@Override
public int size() {
return Iterators.size(iterator());
}
};
}
@Override
protected List<Diagnostic> getScriptErrors(Script script) {
EcoreUtil.resolveAll(script.eResource());
List<Diagnostic> diagnostics = super.getScriptErrors(script);
Iterator<TypableElement> typableASTNodes = Iterators.filter(EcoreUtil2.eAll(script), TypableElement.class);
List<Diagnostic> result = Lists.<Diagnostic> newArrayList(Iterables.filter(diagnostics,
ExceptionDiagnostic.class));
while (typableASTNodes.hasNext()) {
TypableElement typableASTNode = typableASTNodes.next();
RuleEnvironment ruleEnvironment = RuleEnvironmentExtensions.newRuleEnvironment(typableASTNode);
try {
typeSystem.type(ruleEnvironment, typableASTNode);
} catch (Throwable cause) {
if (cause instanceof Exception) {
result.add(new ExceptionDiagnostic((Exception) cause));
} else {
throw new RuntimeException(cause);
}
}
}
validator.validate(script.eResource(), CheckMode.ALL, CancelIndicator.NullImpl);
return result;
}
@Override
public Iterator<byte[][]> rangeDateTimeAsIDs(
final Value[] query,
final Literal lower,
final boolean equalsLower,
final Literal upper,
final boolean equalsUpper,
final boolean reverse,
final int limit) throws DataAccessLayerException {
if (query == null || query.length != 2 || isVariable(query[1])) {
return Iterators.emptyIterator();
}
final long lowerBound = lower == null ? Long.MIN_VALUE : Util.parseXMLSchemaDateTimeAsMSecs(lower), upperBound = upper == null ? Long.MAX_VALUE
: Util.parseXMLSchemaDateTimeAsMSecs(upper);
return _rdfIndexDAO.dateRangeQuery(query, lowerBound, equalsLower, upperBound, equalsUpper, reverse, limit);
}
@Override
public void writeOutput(Iterable<DocumentResult> documentResults, File outputDirectory)
throws IOException {
// now we compute many "samples" of possible corpora based on our existing corpus. We score each of
// these samples and compute confidence intervals from them
final Random rng = new Random(bootstrapSeed);
final Iterator<Collection<DocumentResult>> bootstrappedResults =
Iterators.limit(BootstrapIterator.forData(documentResults, rng), numBootstrapSamples);
final List<Map<String, BrokenDownSummaryConfusionMatrix<Symbol>>> resultsForSamples =
Lists.newArrayList();
while (bootstrappedResults.hasNext()) {
resultsForSamples.add(combineBreakdowns(
transform(bootstrappedResults.next(), DocumentResult.GetBreakdownMatricesFunction)
.iterator()));
}
final ImmutableMultimap<String, BrokenDownSummaryConfusionMatrix<Symbol>>
resultsByBreakdownType =
combineMapsToMultimap(resultsForSamples);
writeSampledBreakdownsToFiles(resultsByBreakdownType, outputDirectory);
}
public Iterator<Either<Exception, P2<K,V>>> getStreaming(Iterator<K> keys, AtomicInteger progress, AtomicInteger skipped) {
log.info("starting store lookups");
final List<Either<Exception, P2<K,V>>> ret = Lists.newArrayList();
int notFound = 0;
while (keys.hasNext()) {
final K key = keys.next();
final V value;
try {
value = index.get(key);
} catch (IOException e) {
log.error("error", e);
return Iterators.singletonIterator(Left.<Exception, P2<K,V>>of(new IndexReadException(e)));
}
if (value != null) {
ret.add(Right.<Exception, P2<K,V>>of(P.p(key, value)));
} else {
notFound++;
}
}
if (progress != null) progress.addAndGet(notFound);
if (skipped != null) skipped.addAndGet(notFound);
log.info("store lookups complete");
return ret.iterator();
}
protected void validateWhereClauseForConditions() throws InvalidRequestException
{
Iterator<ColumnDefinition> iterator = Iterators.concat(cfm.partitionKeyColumns().iterator(), cfm.clusteringColumns().iterator());
while (iterator.hasNext())
{
ColumnDefinition def = iterator.next();
Restriction restriction = processedKeys.get(def.name);
if (restriction == null || !(restriction.isEQ() || restriction.isIN()))
{
throw new InvalidRequestException(
String.format("DELETE statements must restrict all PRIMARY KEY columns with equality relations in order " +
"to use IF conditions, but column '%s' is not restricted", def.name));
}
}
}
/**
* Gets an iterator representing an immutable snapshot of all subscribers to the given event at
* the time this method is called.
*/
Iterator<Subscriber> getSubscribers(Object event) {
ImmutableSet<Class<?>> eventTypes = flattenHierarchy(event.getClass());
List<Iterator<Subscriber>> subscriberIterators =
Lists.newArrayListWithCapacity(eventTypes.size());
for (Class<?> eventType : eventTypes) {
CopyOnWriteArraySet<Subscriber> eventSubscribers = subscribers.get(eventType);
if (eventSubscribers != null) {
// eager no-copy snapshot
subscriberIterators.add(eventSubscribers.iterator());
}
}
return Iterators.concat(subscriberIterators.iterator());
}
public MimeMessageHeaders(MimeMessage message) throws MessagingException {
ImmutableList<Pair<String, String>> headsAndLines = Streams.stream(Iterators.forEnumeration(message.getAllHeaderLines()))
.map(Throwing.function(this::extractHeaderLine).sneakyThrow())
.collect(Guavate.toImmutableList());
fields = headsAndLines
.stream()
.map(Pair::getKey)
.collect(Guavate.toImmutableList());
headers = headsAndLines
.stream()
.collect(Guavate.toImmutableListMultimap(
pair -> pair.getKey().toLowerCase(Locale.US),
Pair::getValue));
}
public static AbstractElement findFirstWithSameConflicts(final AbstractElement element, final Grammar grammar) {
final List<String> conflicting = getConflictingKeywords(element, grammar);
AbstractElement result = element;
Iterator<AbstractElement> iterator = Iterators.filter(
Iterators.filter(EcoreUtil.getAllContents(grammar, true), AbstractElement.class),
new Predicate<AbstractElement>() {
@Override
public boolean apply(AbstractElement param) {
final List<String> otherConflicting = getConflictingKeywords(param, grammar);
return otherConflicting != null && otherConflicting.equals(conflicting);
}
});
if (iterator.hasNext())
result = iterator.next();
return result;
}
@Test
public void testDirectoryAccessAndModifiedTimeUpdates() throws IOException {
Files.createDirectories(path("/foo/bar"));
FileTimeTester tester = new FileTimeTester(path("/foo/bar"));
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeDidNotChange();
// TODO(cgdecker): Use a Clock for file times so I can test this reliably without sleeping
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
Files.createFile(path("/foo/bar/baz.txt"));
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeChanged();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
// access time is updated by reading the full contents of the directory
// not just by doing a lookup in it
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path("/foo/bar"))) {
// iterate the stream, forcing the directory to actually be read
Iterators.advance(stream.iterator(), Integer.MAX_VALUE);
}
tester.assertAccessTimeChanged();
tester.assertModifiedTimeDidNotChange();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
Files.move(path("/foo/bar/baz.txt"), path("/foo/bar/baz2.txt"));
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeChanged();
Uninterruptibles.sleepUninterruptibly(1, MILLISECONDS);
Files.delete(path("/foo/bar/baz2.txt"));
tester.assertAccessTimeDidNotChange();
tester.assertModifiedTimeChanged();
}
/**
* see https://bugs.eclipse.org/bugs/show_bug.cgi?id=428525
*/
@Test
public void testInvalidUnicode() throws Exception {
final Resource resource = this.toResource("class C { def m() {\'\\u\'.toString}}");
EcoreUtil2.resolveAll(resource);
Assert.assertEquals(1, resource.getErrors().size());
final Resource.Diagnostic error = IterableExtensions.<Resource.Diagnostic>head(resource.getErrors());
Assert.assertNotNull(error);
Assert.assertEquals("Invalid unicode", error.getMessage());
final XStringLiteral literal = IteratorExtensions.<XStringLiteral>head(Iterators.<XStringLiteral>filter(resource.getAllContents(), XStringLiteral.class));
Assert.assertEquals("u", literal.getValue());
}
@Nonnull
@Override
public Iterable<? extends Method> getMethods() {
return new Iterable<Method>() {
@Nonnull
@Override
public Iterator<Method> iterator() {
return Iterators.concat(getDirectMethods().iterator(), getVirtualMethods().iterator());
}
};
}
/**
* Transform a RemoteIteartor based on a transformation function that is allowed to throw an IOException.
* @param iter The RemoteIterator to transform
* @param func The function to use for transforming the values.
* @return The new RemoteIterator.
*/
public static RemoteIterator<LocatedFileStatus> transform(RemoteIterator<LocatedFileStatus> iter, final FunctionWithIOException<LocatedFileStatus, LocatedFileStatus> func) {
return new RemoteIterators.IterToRemote(Iterators.transform(
new RemoteIterators.RemoteToIter(iter),
t -> {
try {
return func.apply(t);
} catch (IOException ex) {
throw new CaughtIO(ex);
}
}));
}
@Test
public void testWriteAndListLogEntriesAsync() throws ExecutionException, InterruptedException {
String logName = RemoteLoggingHelper.formatForTest("log_name");
String filter = "logName=projects/" + logging.getOptions().getProjectId() + "/logs/" + logName;
loggingSnippets.write(logName);
// flush all pending asynchronous writes
logging.flush();
Iterator<LogEntry> iterator =
loggingSnippets.listLogEntriesAsync(filter).iterateAll().iterator();
while (Iterators.size(iterator) < 2) {
Thread.sleep(500);
iterator = loggingSnippets.listLogEntriesAsync(filter).iterateAll().iterator();
}
assertTrue(loggingSnippets.deleteLogAsync(logName));
}
/**
* @author FalseHonesty
* @reason ChatTriggers
*/
@Overwrite
public <S> Iterable<S> getByClass(final Class<S> clazz) {
return () -> {
List<T> list = map.get(initializeClassLookup(clazz));
if (list == null) {
return (UnmodifiableListIterator<S>) Utils.EMPTY_ITERATOR;
} else {
Iterator<T> iterator = list.iterator();
return Iterators.filter(iterator, clazz);
}
};
}
private Iterator<FileSet<CopyEntity>> injectRequestor(Iterator<FileSet<CopyEntity>> iterator) {
return Iterators.transform(iterator, new Function<FileSet<CopyEntity>, FileSet<CopyEntity>>() {
@Override
public FileSet<CopyEntity> apply(FileSet<CopyEntity> input) {
input.setRequestor(CopyableDatasetRequestor.this);
return input;
}
});
}
@Override
public Iterator<Tree> childrenIterator() {
return Iterators.concat(
Iterators.singletonIterator(openParenthesis),
parameters != null ? parameters.elementsAndSeparators(Function.identity(), Function.identity()) : new ArrayList<Tree>().iterator(),
Iterators.singletonIterator(closeParenthesis));
}
private void assertPreOrdered(List<OrderedNode> nodes) {
Set<OrderedNode> visited = new HashSet<>();
assertEquals("missing nodes", new HashSet<>(nodes), g.vertices());
for (int i = 1; i < nodes.size(); i++) {
OrderedNode node = nodes.get(i);
visited.add(node);
OrderedNode prev = nodes.get(i - 1);
if (!Iterators.all(g.getSuccessors(prev).iterator(), Predicates.in(visited)))
assertTrue("unvisited pred", Iterators.contains(g.getPredecessors(node).iterator(), prev));
}
}
@Override
public Iterator<Tree> childrenIterator() {
return Iterators.concat(Iterators.forArray(castKeyword, openingParenthesis),
sourceExpressions.elementsAndSeparators(Functions.<ExpressionTree>identity()),
Iterators.forArray(asKeyword, dataType, ccsidKeyword, ccsidExpression, encodingKeyword,
encodingExpression, formatKeyword, formatExpression, defaultKeyword, defaultExpression,
closingParenthesis));
}
@Test
public void testOnlyRegisteredSerialization() {
TitanManagement mgmt = graph.openManagement();
PropertyKey time = mgmt.makePropertyKey("time").dataType(Integer.class).make();
PropertyKey any = mgmt.makePropertyKey("any").cardinality(Cardinality.LIST).dataType(Object.class).make();
mgmt.buildIndex("byTime",Vertex.class).addKey(time).buildCompositeIndex();
EdgeLabel knows = mgmt.makeEdgeLabel("knows").make();
VertexLabel person = mgmt.makeVertexLabel("person").make();
mgmt.commit();
TitanTransaction tx = graph.newTransaction();
TitanVertex v = tx.addVertex("person");
v.property("time", 5);
v.property("any", new Double(5.0));
v.property("any", new TClass1(5,1.5f));
v.property("any", TEnum.THREE);
tx.commit();
tx = graph.newTransaction();
v = tx.query().has("time",5).vertices().iterator().next();
assertEquals(5,(int)v.value("time"));
assertEquals(3, Iterators.size(v.properties("any")));
tx.rollback();
//Verify that non-registered objects aren't allowed
for (Object o : new Object[]{new TClass2("abc",5)}) {
tx = graph.newTransaction();
v = tx.addVertex("person");
try {
v.property("any", o); //Should not be allowed
tx.commit();
fail();
} catch (IllegalArgumentException e) {
} finally {
if (tx.isOpen()) tx.rollback();
}
}
}