@Test public void iteratesOverCorrectClustersWithPartialMatch() throws Exception { List<Statement> elements = ImmutableList.of( LDFusionToolTestUtils.createHttpStatement("s3", "p", "o1"), LDFusionToolTestUtils.createHttpStatement("s1", "p", "o2"), LDFusionToolTestUtils.createHttpStatement("s2", "p", "o3"), LDFusionToolTestUtils.createHttpStatement("s1", "p", "o4"), LDFusionToolTestUtils.createHttpStatement("s2", "p", "o5"), LDFusionToolTestUtils.createHttpStatement("s1", "p", "o6")); Comparator<Statement> comparator = new Comparator<Statement>() { @Override public int compare(Statement o1, Statement o2) { return o1.getSubject().stringValue().compareTo(o2.getSubject().stringValue()); } }; ClusterIterator<Statement> clusterIterator = new ClusterIterator<Statement>(elements, comparator); Map<URI, List<Statement>> expectedClusters = new HashMap<>(); expectedClusters.put(LDFusionToolTestUtils.createHttpUri("s1"), ImmutableList.of( LDFusionToolTestUtils.createHttpStatement("s1", "p", "o2"), LDFusionToolTestUtils.createHttpStatement("s1", "p", "o4"), LDFusionToolTestUtils.createHttpStatement("s1", "p", "o6"))); expectedClusters.put(LDFusionToolTestUtils.createHttpUri("s2"), ImmutableList.of(LDFusionToolTestUtils.createHttpStatement("s2", "p", "o3"), LDFusionToolTestUtils.createHttpStatement("s2", "p", "o5"))); expectedClusters.put(LDFusionToolTestUtils.createHttpUri("s3"), ImmutableList.of(LDFusionToolTestUtils.createHttpStatement("s3", "p", "o1"))); for (int i = 0; i < 3; i++) { List<Statement> actualCluster = clusterIterator.next(); List<Statement> expectedCluster = expectedClusters.get(actualCluster.get(0).getSubject()); assertThat(actualCluster, containsInAnyOrder(expectedCluster.toArray())); } }
@Override public List<T> next() { int fromIndex = cursor; T first = nextElement(); while (hasNextElement()) { T next = peekNextElement(); if (comparator.compare(first, next) == 0) { nextElement(); } else { break; } } return new SubList(fromIndex, cursor); }
ClusterIterator implements Iterator<List<T>> { @Override public List<T> next() { int fromIndex = cursor; T first = nextElement(); while (hasNextElement()) { T next = peekNextElement(); if (comparator.compare(first, next) == 0) { nextElement(); } else { break; } } return new SubList(fromIndex, cursor); } }
ClusterIterator implements Iterator<List<T>> { @Override public List<T> next() { int fromIndex = cursor; T first = nextElement(); while (hasNextElement()) { T next = peekNextElement(); if (comparator.compare(first, next) == 0) { nextElement(); } else { break; } } return new SubList(fromIndex, cursor); } @SuppressWarnings("unchecked")  ClusterIterator(List<T> elements, Comparator<T> comparator); }
ClusterIterator implements Iterator<List<T>> { @Override public List<T> next() { int fromIndex = cursor; T first = nextElement(); while (hasNextElement()) { T next = peekNextElement(); if (comparator.compare(first, next) == 0) { nextElement(); } else { break; } } return new SubList(fromIndex, cursor); } @SuppressWarnings("unchecked")  ClusterIterator(List<T> elements, Comparator<T> comparator); @Override boolean hasNext(); @Override List<T> next(); @Override void remove(); }
ClusterIterator implements Iterator<List<T>> { @Override public List<T> next() { int fromIndex = cursor; T first = nextElement(); while (hasNextElement()) { T next = peekNextElement(); if (comparator.compare(first, next) == 0) { nextElement(); } else { break; } } return new SubList(fromIndex, cursor); } @SuppressWarnings("unchecked")  ClusterIterator(List<T> elements, Comparator<T> comparator); @Override boolean hasNext(); @Override List<T> next(); @Override void remove();  }