public static <T> List<T> asList( Iterator<T> iterator ) { return addToCollection( iterator, new ArrayList<>() ); }
public static <T> Collection<T> asCollection( Iterator<T> iterable ) { return addToCollection( iterable, new ArrayList<>() ); }
public static <T> Set<T> asSet( Iterator<T> iterator ) { return addToCollection( iterator, new HashSet<>() ); }
/** * Adds all the items in {@code iterator} to {@code collection}. * @param <C> the type of {@link Collection} to add to items to. * @param <T> the type of items in the collection and iterator. * @param iterable the {@link Iterator} to grab the items from. * @param collection the {@link Collection} to add the items to. * @return the {@code collection} which was passed in, now filled * with the items from {@code iterator}. */ public static <C extends Collection<T>,T> C addToCollection( Iterable<T> iterable, C collection ) { return Iterators.addToCollection( iterable.iterator(), collection ); }
private void addDefinitions( List<IndexDefinition> definitions, final TokenRead tokenRead, Iterator<IndexReference> indexes ) { addToCollection( map( index -> descriptorToDefinition( tokenRead, index ), indexes ), definitions ); }
if ( isImplicit() && peek == null ) /* JSON parse error */ addToCollection( statements.errors(), errors );
addToCollection( statements.errors(), errors );
@Test public void getAllNodesIteratorShouldPickUpHigherIdsThanHighIdWhenStarted() throws Exception { // GIVEN { Transaction tx = db.beginTx(); db.createNode(); db.createNode(); tx.success(); tx.close(); } // WHEN iterator is started Transaction transaction = db.beginTx(); Iterator<Node> allNodes = db.getAllNodes().iterator(); allNodes.next(); // and WHEN another node is then added Thread thread = new Thread( () -> { Transaction newTx = db.beginTx(); assertThat( newTx, not( instanceOf( PlaceboTransaction.class ) ) ); db.createNode(); newTx.success(); newTx.close(); } ); thread.start(); thread.join(); // THEN the new node is picked up by the iterator assertThat( addToCollection( allNodes, new ArrayList<>() ).size(), is( 2 ) ); transaction.close(); }
@Test public void getAllRelationshipsIteratorShouldPickUpHigherIdsThanHighIdWhenStarted() throws Exception { // GIVEN Transaction tx = db.beginTx(); createRelationshipAssumingTxWith( "key", 1 ); createRelationshipAssumingTxWith( "key", 2 ); tx.success(); tx.close(); // WHEN tx = db.beginTx(); Iterator<Relationship> allRelationships = db.getAllRelationships().iterator(); Thread thread = new Thread( () -> { Transaction newTx = db.beginTx(); assertThat( newTx, not( instanceOf( PlaceboTransaction.class ) ) ); createRelationshipAssumingTxWith( "key", 3 ); newTx.success(); newTx.close(); } ); thread.start(); thread.join(); // THEN assertThat( addToCollection( allRelationships, new ArrayList<>() ).size(), is(3) ); tx.success(); tx.close(); }
@SuppressWarnings("unchecked") private List convertToList(Object list) { if (list == null) return null; else if (list instanceof List) return (List) list; else if (list instanceof Collection) return new ArrayList((Collection)list); else if (list instanceof Iterable) return Iterables.addToCollection((Iterable)list,(List)new ArrayList<>(100)); else if (list instanceof Iterator) return Iterators.addToCollection((Iterator)list,(List)new ArrayList<>(100)); else if (list.getClass().isArray() && !list.getClass().getComponentType().isPrimitive()) { List result = new ArrayList<>(100); Collections.addAll(result, ((Object[]) list)); return result; } return null; }
private Future<List<Map<String, Object>>> submit(GraphDatabaseService db, String statement, Map<String, Object> params, String key, List<Object> partition) { return POOL.submit(() -> Iterators.addToCollection(db.execute(statement, parallelParams(params, key, partition)), new ArrayList<>(partition.size()))); }
@Procedure @Description("apoc.cypher.mapParallel(fragment, params, list-to-parallelize) yield value - executes fragment in parallel batches with the list segments being assigned to _") public Stream<MapResult> mapParallel(@Name("fragment") String fragment, @Name("params") Map<String, Object> params, @Name("list") List<Object> data) { final String statement = withParamsAndIterator(fragment, params.keySet(), "_"); db.execute("EXPLAIN " + statement).close(); return Util.partitionSubList(data, PARTITIONS,null) .flatMap((partition) -> Iterators.addToCollection(db.execute(statement, parallelParams(params, "_", partition)), new ArrayList<>(partition.size())).stream()) .map(MapResult::new); } @Procedure
public static <T> Set<T> asSet( Iterator<T> iterator ) { return addToCollection( iterator, new HashSet<>() ); }
public static <T> List<T> asList( Iterator<T> iterator ) { return addToCollection( iterator, new ArrayList<>() ); }
public static <T> Collection<T> asCollection( Iterator<T> iterable ) { return addToCollection( iterable, new ArrayList<>() ); }
/** * Adds all the items in {@code iterator} to {@code collection}. * @param <C> the type of {@link Collection} to add to items to. * @param <T> the type of items in the collection and iterator. * @param iterable the {@link Iterator} to grab the items from. * @param collection the {@link Collection} to add the items to. * @return the {@code collection} which was passed in, now filled * with the items from {@code iterator}. */ public static <C extends Collection<T>,T> C addToCollection( Iterable<T> iterable, C collection ) { return Iterators.addToCollection( iterable.iterator(), collection ); }
private <T> ArrayList<T> fromIterableToArrayList( Iterator<T> iterable ) { ArrayList<T> collection = new ArrayList<>(); addToCollection( iterable, collection ); return collection; }
private void addDefinitions( List<IndexDefinition> definitions, final TokenRead tokenRead, Iterator<IndexReference> indexes ) { addToCollection( map( index -> descriptorToDefinition( tokenRead, index ), indexes ), definitions ); }
private Person getRandomFriendOf( Person p ) { ArrayList<Person> friends = new ArrayList<>(); addToCollection( p.getFriends().iterator(), friends ); return friends.get( r.nextInt( friends.size() ) ); }
private <T> List<T> toList( Result result, String column ) { List<T> results = new ArrayList<>(); Iterators.addToCollection( result.columnAs( column ), results ); return results; }