/** * Creates a {@link Set} from an {@link Iterable}. * * @param iterable The items to create the set from. * @param <T> The generic type of items. * @return a set containing all items from the {@link Iterable}. */ public static <T> Set<T> asSet( Iterable<T> iterable ) { return addToCollection( iterable, new HashSet<>() ); }
/** * Creates a collection from an iterable. * * @param iterable The iterable to create the collection from. * @param <T> The generic type of both the iterable and the collection. * @return a collection containing all items from the iterable. */ public static <T> Collection<T> asCollection( Iterable<T> iterable ) { return addToCollection( iterable, new ArrayList<>() ); }
public static <T> List<T> asList( Iterable<T> iterator ) { return addToCollection( iterator, new ArrayList<>() ); }
@Test public void getAllRelationships() { Set<Relationship> existingRelationships = Iterables.addToCollection( getGraphDb().getAllRelationships(), new HashSet<>() ); Set<Relationship> createdRelationships = new HashSet<>(); Node node = getGraphDb().createNode(); for ( int i = 0; i < 100; i++ ) { createdRelationships.add( node.createRelationshipTo( getGraphDb().createNode(), TEST ) ); } newTransaction(); Set<Relationship> allRelationships = new HashSet<>(); allRelationships.addAll( existingRelationships ); allRelationships.addAll( createdRelationships ); int count = 0; for ( Relationship rel : getGraphDb().getAllRelationships() ) { assertTrue( "Unexpected rel " + rel + ", expected one of " + allRelationships, allRelationships.contains( rel ) ); count++; } assertEquals( allRelationships.size(), count ); }
@Test public void twoDynamicRecordsShouldShrinkToOneWhenRemoving() { // GIVEN // will occupy 61B of data, i.e. just two dynamic records NodeRecord node = nodeRecordWithDynamicLabels( nodeStore, oneByteLongs( 57 ) ); Collection<DynamicRecord> initialRecords = node.getDynamicLabelRecords(); NodeLabels nodeLabels = NodeLabelsField.parseLabelsField( node ); // WHEN List<DynamicRecord> changedDynamicRecords = Iterables.addToCollection( nodeLabels.remove( 255 /*Initial labels go from 255 and down to 255-58*/, nodeStore ), new ArrayList<>() ); // THEN assertEquals( initialRecords, changedDynamicRecords ); assertTrue( changedDynamicRecords.get( 0 ).inUse() ); assertFalse( changedDynamicRecords.get( 1 ).inUse() ); }
@Test public void twoDynamicRecordsShouldShrinkToOneWhenRemovingWithoutChangingItsOwner() { // GIVEN // will occupy 61B of data, i.e. just two dynamic records Long nodeId = 42L; NodeRecord node = nodeRecordWithDynamicLabels( nodeId, nodeStore, oneByteLongs( 57 ) ); NodeLabels nodeLabels = NodeLabelsField.parseLabelsField( node ); List<DynamicRecord> changedDynamicRecords = Iterables.addToCollection( nodeLabels.remove( 255 /*Initial labels go from 255 and down to 255-58*/, nodeStore ), new ArrayList<>() ); // WHEN Pair<Long,long[]> changedPair = DynamicNodeLabels.getDynamicLabelsArrayAndOwner( changedDynamicRecords, nodeStore.getDynamicLabelStore() ); // THEN assertEquals( nodeId, changedPair.first() ); }
@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; }
/** * Creates a {@link Set} from an {@link Iterable}. * * @param iterable The items to create the set from. * @param <T> The generic type of items. * @return a set containing all items from the {@link Iterable}. */ public static <T> Set<T> asSet( Iterable<T> iterable ) { return addToCollection( iterable, new HashSet<>() ); }
/** * Creates a collection from an iterable. * * @param iterable The iterable to create the collection from. * @param <T> The generic type of both the iterable and the collection. * @return a collection containing all items from the iterable. */ public static <T> Collection<T> asCollection( Iterable<T> iterable ) { return addToCollection( iterable, new ArrayList<>() ); }
public static <T> List<T> asList( Iterable<T> iterator ) { return addToCollection( iterator, new ArrayList<>() ); }
private static int countProperties(Set<String> ignoreProperties, PropertyContainer node) { final Collection<String> keys = Iterables.addToCollection(node.getPropertyKeys(), new HashSet<String>()); keys.removeAll(ignoreProperties); return keys.size(); }
private static void compareProperties(PropertyContainer pc1, PropertyContainer pc2, Set<String> ignoreProperties) { final Collection<String> keys1 = Iterables.addToCollection(pc1.getPropertyKeys(), new HashSet<String>()); final Collection<String> keys2 = Iterables.addToCollection(pc2.getPropertyKeys(), new HashSet<String>()); keys2.removeAll(ignoreProperties); keys1.removeAll(ignoreProperties); if (!keys1.equals(keys2)) { System.err.println("On " + pc1 + " != " + pc2 + " properties mismatch " + keys1 + " != " + keys2); } for (String prop : keys1) { final Object value1 = pc1.getProperty(prop); final Object value2 = pc2.getProperty(prop); if (!equals(value1, value2)) { System.err.println("On " + pc1 + " != " + pc2 + " property " + prop + " mismatch " + toString(value1) + " != " + toString(value2)); } } }
public Iterable<Person> getFriendRecommendation( int numberOfFriendsToReturn ) { HashSet<Person> friends = new HashSet<>(); addToCollection( getFriends(), friends ); HashSet<Person> friendsOfFriends = new HashSet<>(); addToCollection( getFriendsOfFriends(), friendsOfFriends ); friendsOfFriends.removeAll( friends ); ArrayList<RankedPerson> rankedFriends = new ArrayList<>(); for ( Person friend : friendsOfFriends ) { long rank = getNumberOfPathsToPerson( friend ); rankedFriends.add( new RankedPerson( friend, rank ) ); } Collections.sort( rankedFriends, new RankedComparer() ); trimTo( rankedFriends, numberOfFriendsToReturn ); return onlyFriend( rankedFriends ); }
public static void main(String[] args) { graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(new File(args[0])); long time = System.currentTimeMillis(); Map<Set<String>,Sample> statistics = new HashMap<Set<String>, Sample>(); int count = 0; for (Node node : graphDb.getAllNodes()) { final HashSet<String> keys = Iterables.addToCollection(node.getPropertyKeys(), new HashSet<String>()); Sample sample = statistics.get(keys); if (sample==null) { sample = new Sample(node); statistics.put(keys,sample); } else { sample.inc(); } count++; if (count % 100000 == 0) System.out.println("count = " + count + " sample "+sample); } time = System.currentTimeMillis() - time; System.out.println(" count " + count + " took " + time + " types " + statistics.size() + "\n" + statistics); graphDb.shutdown(); } }
private void assertPathIs( Iterable<Person> path, Person... expectedPath ) { ArrayList<Person> pathArray = new ArrayList<>(); Iterables.addToCollection( path, pathArray ); assertThat( pathArray.size(), equalTo( expectedPath.length ) ); for ( int i = 0; i < expectedPath.length; i++ ) { assertThat( pathArray.get( i ), equalTo( expectedPath[ i ] ) ); } }