/** * Gets a list based on an iterator. * <p> * As the wrapped Iterator is traversed, an ArrayList of its values is * created. At the end, the list is returned. * * @param iterator the iterator to use, not null * @return a list of the iterator contents * @throws NullPointerException if iterator parameter is null */ public static List toList(Iterator iterator) { return toList(iterator, 10); }
return emptyIterator(); return singletonIterator(obj);
public void testRemoveFromFilteredIterator() { final Predicate myPredicate = new Predicate() { public boolean evaluate( Object object ) { Integer i = (Integer) object; if (i.compareTo(new Integer(4)) < 0) return true; return false; } }; List list1 = new ArrayList(); List list2 = new ArrayList(); list1.add(new Integer(1)); list1.add(new Integer(2)); list2.add(new Integer(3)); list2.add(new Integer(4)); // will be ignored by the predicate Iterator it1 = IteratorUtils.filteredIterator(list1.iterator(), myPredicate ); Iterator it2 = IteratorUtils.filteredIterator(list2.iterator(), myPredicate ); Iterator it = IteratorUtils.chainedIterator(it1, it2); while (it.hasNext()) { it.next(); it.remove(); } assertEquals( 0, list1.size() ); assertEquals( 1, list2.size() ); }
public List<String> getTypeNames() { final DataTypeAdapter<?>[] userAdapters = GeoWaveConfiguratorBase.getDataAdapters(CLASS, context); if ((userAdapters == null) || (userAdapters.length <= 0)) { return IteratorUtils.toList( IteratorUtils.transformedIterator(getAdapters(), new Transformer() { @Override public Object transform(final Object input) { if (input instanceof DataTypeAdapter) { return ((DataTypeAdapter) input).getTypeName(); } return input; } })); } else { final List<String> retVal = new ArrayList<>(userAdapters.length); for (final DataTypeAdapter<?> adapter : userAdapters) { retVal.add(adapter.getTypeName()); } return retVal; } }
@SuppressWarnings("unchecked") private Iterator<Resource> getResourceInheritanceChainInternal(final Collection<String> bucketNames, final String configName, final Iterator<String> paths, final ResourceResolver resourceResolver) { // find all matching items among all configured paths Iterator<Resource> matchingResources = IteratorUtils.transformedIterator(paths, new Transformer() { @Override public Object transform(Object input) { String path = (String)input; for (String bucketName : bucketNames) { final String name = bucketName + "/" + configName; final String configPath = buildResourcePath(path, name); Resource resource = resourceResolver.getResource(configPath); if (resource != null) { log.trace("+ Found matching config resource for inheritance chain: {}", configPath); return resource; } else { log.trace("- No matching config resource for inheritance chain: {}", configPath); } } return null; } }); Iterator<Resource> result = IteratorUtils.filteredIterator(matchingResources, PredicateUtils.notNullPredicate()); if (result.hasNext()) { return result; } return null; }
/** * Returns an iterator over parameter sets. Each element returned from the * iterator is a java.util.Map. */ @SuppressWarnings("unchecked") public Iterator<Map<String, ?>> parametersIterator() { return (parameters == null || parameters.length == 0) ? IteratorUtils.emptyIterator() : IteratorUtils .transformedIterator(IteratorUtils.arrayIterator(parameters), nullMapTransformer); }
if (relPath.indexOf('/') < 0) { Resource res = new FileResource(resolver, providerRoot, providerFile, contentFileExtensions, contentFileCache, fileStatCache); return IteratorUtils.singletonIterator(res); Iterator<File> children = IteratorUtils.filteredIterator(IteratorUtils.arrayIterator(files), new Predicate() { @Override public boolean evaluate(Object object) { return null; return IteratorUtils.transformedIterator(children, new Transformer() { @Override public Object transform(Object input) {
public LoopSearch(Digraph digraph) { this.digraph = digraph; vertexIterator = IteratorUtils.filteredIterator(digraph.vertexIterator(), new Predicate() { public boolean evaluate(Object vertex) { return LoopSearch.this.digraph.hasArc(vertex, vertex); } }); }
/** * Returns the value as an {@link Iterator}. If the value is itself an * iterator, an instance of {@link Iterable}, or is an array, the items in * the value will be returned as the list. Otherwise, if the value is not * <code>null</code>, it will be returned as the only item in the * iterator. If the value is <code>null</code>, an empty iterator is * returned. * * @param value The value to convert. * @return The iterator. */ @API public static Iterator<?> asIterator( Object value ) { if ( value instanceof Iterator ) { return (Iterator<?>) value; } else if ( value instanceof Iterable<?> ) { return ( (Iterable<?>) value ).iterator(); } else if ( value instanceof Enumeration ) { return IteratorUtils.asIterator( (Enumeration<?>) value ); } else if ( value != null ) { if ( value.getClass().isArray() ) { return IteratorUtils.arrayIterator( (Object[]) value ); } else { return Arrays.asList( value ).iterator(); } } else { return IteratorUtils.emptyIterator(); } }
+ " configured. Valid sorting policy has to be configured in" + " yarn.scheduler.capacity.<queue>.multi-node-sorting.policy"); return IteratorUtils.singletonIterator( nodes.iterator().next()); "MultiNode policy '" + policyName + "' is configured, however " + "yarn.scheduler.capacity.multi-node-placement-enabled is false"); return IteratorUtils.singletonIterator( nodes.iterator().next());
/** * Returns an iterator over a snapshot copy of this group's collected events., optionally sorted by arrival order. If you need * to iterate over the group and e.g. remove select events, do so via {@link #removeEvent(CoreEvent)}. If you need to do so * atomically in order to prevent e.g. concurrent reception/aggregation of the group during iteration, wrap the iteration in a * synchronized block on the group instance. * * @return an iterator over collected {@link CoreEvent}s. * @throws ObjectStoreException */ @SuppressWarnings("unchecked") public Iterator<CoreEvent> iterator(boolean sortByArrival) throws ObjectStoreException { synchronized (this) { if (eventsObjectStore.allKeys(eventsPartitionKey).isEmpty()) { return IteratorUtils.emptyIterator(); } else { return IteratorUtils.arrayIterator(this.toArray(sortByArrival)); } } }
childIterators.add(IteratorUtils.transformedIterator(parentContentFile.getContent().getChildren().keySet().iterator(), new Transformer() { @Override public Object transform(final Object input) { if (files != null) { Arrays.sort(files, FileNameComparator.INSTANCE); childIterators.add(IteratorUtils.transformedIterator(IteratorUtils.arrayIterator(files), new Transformer() { @Override public Object transform(final Object input) { Iterator children = IteratorUtils.chainedIterator(childIterators); if (!children.hasNext()) { return null;
/** * From a given node, refresh resources extracted out of it depending on configuration * @param node * @throws RepositoryException */ void refreshResourceIterator(Node node) throws RepositoryException { if (properties){ PropertyIterator it = nameGlobs != null ? node.getProperties(nameGlobs) : node.getProperties(); currentResources = IteratorUtils.transformedIterator(it, o -> { try { return resolver.getResource(((Property) o).getPath()); } catch (RepositoryException e) { LOGGER.error("unable to read property", e); } return null; }); } else { currentResources = IteratorUtils.singletonIterator(resolver.getResource(node.getPath())); } }
public void testArrayIterator() { Object[] objArray = {"a", "b", "c"}; ResettableIterator iterator = IteratorUtils.arrayIterator(objArray); assertTrue(iterator.next().equals("a")); assertTrue(iterator.next().equals("b")); iterator = IteratorUtils.arrayIterator(new Integer(0)); fail("Expecting IllegalArgumentException"); } catch (IllegalArgumentException ex) { iterator = IteratorUtils.arrayIterator(null); fail("Expecting NullPointerException"); } catch (NullPointerException ex) { iterator = IteratorUtils.arrayIterator(objArray, 1); assertTrue(iterator.next().equals("b")); iterator = IteratorUtils.arrayIterator(objArray, -1); fail("Expecting IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException ex) { iterator = IteratorUtils.arrayIterator(objArray, 3); assertTrue(!iterator.hasNext()); iterator.reset(); iterator = IteratorUtils.arrayIterator(objArray, 4); fail("Expecting IndexOutOfBoundsException"); } catch (IndexOutOfBoundsException ex) { iterator = IteratorUtils.arrayIterator(objArray, 2, 3);
@Override @SuppressWarnings("unchecked") public Iterator<String> getFieldNames() { return (Iterator<String>)IteratorUtils.transformedIterator( model.predicates().iterator(), org.apache.stanbol.entityhub.model.sesame.ModelUtils.VALUR_TO_STRING_TRANSFORMER); }
/** * Test empty iterator */ public void testEmptyIterator() { assertSame(EmptyIterator.INSTANCE, IteratorUtils.EMPTY_ITERATOR); assertSame(EmptyIterator.RESETTABLE_INSTANCE, IteratorUtils.EMPTY_ITERATOR); assertEquals(true, IteratorUtils.EMPTY_ITERATOR instanceof Iterator); assertEquals(true, IteratorUtils.EMPTY_ITERATOR instanceof ResettableIterator); assertEquals(false, IteratorUtils.EMPTY_ITERATOR instanceof OrderedIterator); assertEquals(false, (Object) IteratorUtils.EMPTY_ITERATOR instanceof ListIterator); assertEquals(false, (Object) IteratorUtils.EMPTY_ITERATOR instanceof MapIterator); assertEquals(false, IteratorUtils.EMPTY_ITERATOR.hasNext()); IteratorUtils.EMPTY_ITERATOR.reset(); assertSame(IteratorUtils.EMPTY_ITERATOR, IteratorUtils.EMPTY_ITERATOR); assertSame(IteratorUtils.EMPTY_ITERATOR, IteratorUtils.emptyIterator()); try { IteratorUtils.EMPTY_ITERATOR.next(); fail(); } catch (NoSuchElementException ex) {} try { IteratorUtils.EMPTY_ITERATOR.remove(); fail(); } catch (IllegalStateException ex) {} }
protected final Iterator getIterator(Object literal) { if (literal instanceof Collection) { return ((Collection) literal).iterator(); } else if (literal instanceof Iterator) { return (Iterator) literal; } else if (literal instanceof Enumeration) { return IteratorUtils.asIterator((Enumeration) literal); } else if (literal instanceof Object[]) { return IteratorUtils.arrayIterator((Object[]) literal); } else { throw new ClassCastException("Could not convert literal value to iterator!"); } }
private PDPResponseCollection executeSingleRequest(final PDPRequestCollection pPDPRequestCollection) { final ArrayList<PDPRequest> orgRequests = new ArrayList<>(); Iterator converter = IteratorUtils.transformedIterator(pPDPRequestCollection.iterator(), new Transformer() { public Object transform(final Object input) { PDPRequest request = (PDPRequest) input; orgRequests.add(request); try { return convert52nRequestToXACMLRequest(request); } catch (XmlException e) { throw new DecisionProcessingException("can't convert pdp request: " + e, e); } } }); RequestDocument[] requests = (RequestDocument[]) IteratorUtils.toArray(converter, RequestDocument.class); ResponseDocument[] responses = m_requester.executeRequest(requests); PDPResponseCollection responseCol = new PDPResponseCollection(); for (int i = 0, j = responses.length; i < j; ++i) { ResponseDocument response = responses[i]; PDPResponse pdpresponse = convertXACMLResponseToPDPResponse(response, orgRequests.get(i)); responseCol.add(pdpresponse); } return responseCol; }
/** * Returns an iterator that returns only elements of the specified type. * * @param <T> Class of collection elements. * @param <S> Subclass of T to be used by iterator. * @param iterator Iterator to wrap. * @param type Type of element to return. * @return An iterator. */ @SuppressWarnings("unchecked") public static <T, S extends T> Iterator<S> iteratorForType(Iterator<T> iterator, Class<S> type) { return iterator instanceof ListIterator ? IteratorUtils.filteredListIterator((ListIterator<T>) iterator, (element) -> { return type.isInstance(element); }) : IteratorUtils.filteredIterator(iterator, (element) -> { return type.isInstance(element); }); }
@SuppressWarnings("unchecked") @Override public Collection<Iterator<Resource>> getResourceCollectionInheritanceChain(@NotNull final Resource contentResource, @NotNull final Collection<String> bucketNames, @NotNull final String configName) { if (!isEnabledAndParamsValid(contentResource, bucketNames, configName)) { return null; } final ResourceResolver resourceResolver = contentResource.getResourceResolver(); final List<String> paths = IteratorUtils.toList(findConfigRefs(contentResource, bucketNames)); // get resource collection with respect to collection inheritance Collection<Resource> resourceCollection = getResourceCollectionInternal(bucketNames, configName, paths.iterator(), resourceResolver); // get inheritance chain for each item found // yes, this resolves the closest item twice, but is the easiest solution to combine both logic aspects Iterator<Iterator<Resource>> result = IteratorUtils.transformedIterator(resourceCollection.iterator(), new Transformer() { @Override public Object transform(Object input) { Resource item = (Resource)input; return getResourceInheritanceChainInternal(bucketNames, configName + "/" + item.getName(), paths.iterator(), resourceResolver); } }); if (result.hasNext()) { return IteratorUtils.toList(result); } else { return null; } }