@Override public int getGraphCount() { return (graphIds != null) ? graphIds.size() : 0; }
/** * Checks if any of the specified ids is contained in the set. * * @param other the ids to look for * @return true, iff any of the specified ids is contained in the set */ public boolean containsAny(GradoopIdSet other) { // Algorithm: the sizes of both lists might be vastly different // to prevent the case of iterating multiple times over large collections // we make sure to always iterate over the smaller one Set<GradoopId> iterate = this.ids; Set<GradoopId> contains = other.ids; int thisSize = this.size(); int otherSize = other.size(); if (thisSize > otherSize) { iterate = other.ids; contains = this.ids; } for (GradoopId id : iterate) { if (contains.contains(id)) { return true; } } return false; }
/** * Checks if the specified ids are contained in the set. * * @param other the ids to look for * @return true, iff all specified ids are contained in the set */ public boolean containsAll(GradoopIdSet other) { if (other.size() > this.size()) { return false; } for (GradoopId id : other) { if (!this.contains(id)) { return false; } } return true; }
/** * Checks if any of the specified ids is contained in the set. * * @param other the ids to look for * @return true, iff any of the specified ids is contained in the set */ public boolean containsAny(Set<GradoopId> other) { Set<GradoopId> iterate = this.ids; Set<GradoopId> contains = other; int thisSize = this.size(); int otherSize = other.size(); if (thisSize > otherSize) { iterate = other; contains = this.ids; } for (GradoopId id : iterate) { if (contains.contains(id)) { return true; } } return false; }
/** * Checks if the specified ids are contained in the set. * * @param other the ids to look for * @return true, iff all specified ids are contained in the set */ @Override public boolean containsAll(Collection<?> other) { if (other instanceof Set && other.size() > this.size()) { return false; } for (Object id : other) { if (!this.contains(id)) { return false; } } return true; }
/** * Returns all vertices that belong to the given graphs. * * @param graphIds graph identifiers * @return vertices that are contained in the graphs */ public Collection<V> getVerticesByGraphIds(GradoopIdSet graphIds) { Collection<V> result = Sets.newHashSetWithExpectedSize(graphIds.size()); for (V vertex : vertices.values()) { if (vertex.getGraphIds().containsAny(graphIds)) { result.add(vertex); } } return result; }
/** * Returns all edges that belong to the given graphs. * * @param graphIds Graph identifiers * @return edges */ public Collection<E> getEdgesByGraphIds(GradoopIdSet graphIds) { Collection<E> result = Sets.newHashSetWithExpectedSize(graphIds.size()); for (E edge : edges.values()) { if (edge.getGraphIds().containsAny(graphIds)) { result.add(edge); } } return result; }
@Override public void write(DataOutputView out) throws IOException { if (isEmpty()) { out.writeInt(0); } else { out.writeInt(size()); out.write(writeIds(ids)); } }
@Test public void testSize() throws Exception { GradoopId id1 = GradoopId.get(); GradoopId id2 = GradoopId.get(); GradoopIdSet ids = new GradoopIdSet(); assertThat(ids.size(), is(0)); ids.add(id1); assertThat(ids.size(), is(1)); ids.add(id1); assertThat(ids.size(), is(1)); ids.add(id2); assertThat(ids.size(), is(2)); }
@Test public void testAddAllCollection() throws Exception { GradoopId id1 = GradoopId.get(); GradoopId id2 = GradoopId.get(); GradoopId id3 = GradoopId.get(); GradoopIdSet ids = new GradoopIdSet(); ids.addAll(Arrays.asList(id1, id2)); assertThat(ids.size(), is(2)); assertTrue(ids.contains(id1)); assertTrue(ids.contains(id2)); assertFalse(ids.contains(id3)); ids.addAll(Arrays.asList(id1, id2)); assertThat(ids.size(), is(2)); }
@Test public void testFromExisting() { GradoopId id1 = GradoopId.get(); GradoopId id2 = GradoopId.get(); GradoopId id3 = GradoopId.get(); GradoopIdSet ids = GradoopIdSet.fromExisting(id1, id2, id3); assertThat(ids.size(), is(3)); }
@Test public void testAdd() throws Exception { GradoopId id1 = GradoopId.get(); GradoopId id2 = GradoopId.get(); GradoopIdSet ids = new GradoopIdSet(); assertThat(ids.size(), is(0)); ids.add(id1); assertThat(ids.size(), is(1)); assertTrue(ids.contains(id1)); // must not change ids.add(id1); assertThat(ids.size(), is(1)); assertTrue(ids.contains(id1)); // must change ids.add(id2); assertThat(ids.size(), is(2)); assertTrue(ids.contains(id1)); assertTrue(ids.contains(id2)); }
@Test public void testAddAllGradoopIds() throws Exception { GradoopId id1 = GradoopId.get(); GradoopId id2 = GradoopId.get(); GradoopIdSet list1 = new GradoopIdSet(); list1.add(id1); list1.add(id2); GradoopIdSet list2 = new GradoopIdSet(); list2.addAll(list1); assertThat(list2.size(), is(2)); assertTrue(list2.contains(id1)); assertTrue(list2.contains(id2)); list2.addAll(list1); assertThat(list2.size(), is(2)); }
@Test public void testWriteAndReadFields() throws Exception { GradoopId id1 = GradoopId.get(); GradoopId id2 = GradoopId.get(); GradoopIdSet idsWrite = GradoopIdSet.fromExisting(id1, id2); // write to byte[] ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputView dataOutputView = new DataOutputViewStreamWrapper(out); idsWrite.write(dataOutputView); // read from byte[] GradoopIdSet idsRead = new GradoopIdSet(); ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); DataInputView dataInputView = new DataInputViewStreamWrapper(in); idsRead.read(dataInputView); assertThat(idsRead.size(), is(2)); assertTrue(idsRead.contains(id1)); assertTrue(idsRead.contains(id2)); }
@Test public void testContains() throws Exception { GradoopId id1 = GradoopId.get(); GradoopId id2 = GradoopId.get(); GradoopIdSet ids = new GradoopIdSet(); ids.add(id1); assertThat(ids.size(), is(1)); assertTrue(ids.contains(id1)); assertFalse(ids.contains(id2)); }