@Test public void subsetEmpty() { MutableSet<String> emptySet = mSet(); MutableSet<String> singletonSet = mSet("Bertha"); Assert.assertTrue(Sets.isSubsetOf(emptySet, singletonSet)); Assert.assertFalse(Sets.isSubsetOf(singletonSet, emptySet)); }
private boolean isInDistanceInternal( int distance, NetworkNode from, NetworkNode to, TwoNetworkNodes cachePairKey) { if (from.equals(to)) return true; if (distance == 0) return false; if (SimpleNetwork.areNodesConnecting(from, to)) return true; // Breadth-first search of the network Set<NetworkNode> visitedNodes = Sets.newHashSet(); visitedNodes.add(from); Set<NetworkNode> networkingNodesToTest = Sets.newHashSet(); listConnectedNotVisitedNetworkingNodes(visitedNodes, from, networkingNodesToTest); int distanceSearched = 1; while (distanceSearched < distance) { distanceSearched++; for (NetworkNode nodeToTest : networkingNodesToTest) { if (SimpleNetwork.areNodesConnecting(nodeToTest, to)) { distanceCache.put(cachePairKey, distanceSearched); return true; } visitedNodes.add(nodeToTest); } Set<NetworkNode> nextNetworkingNodesToTest = Sets.newHashSet(); for (NetworkNode nodeToTest : networkingNodesToTest) listConnectedNotVisitedNetworkingNodes(visitedNodes, nodeToTest, nextNetworkingNodesToTest); networkingNodesToTest = nextNetworkingNodesToTest; } return false; }
@Test public void properSubsetEmpty() { MutableSet<String> emptySet = mSet(); MutableSet<String> singletonSet = UnifiedSet.newSetWith("Bertha"); Assert.assertTrue(Sets.isProperSubsetOf(emptySet, singletonSet)); Assert.assertFalse(Sets.isProperSubsetOf(singletonSet, emptySet)); }
@Test public void properSubsetEqual() { MutableSet<String> setA = UnifiedSet.newSetWith("Bertha", null, "Myra"); MutableSet<String> setB = UnifiedSet.newSetWith("Myra", "Bertha", null); Assert.assertFalse(Sets.isProperSubsetOf(setA, setB)); Assert.assertFalse(Sets.isProperSubsetOf(setB, setA)); }
@Test public void properSubsetNotEmpty() { MutableSet<String> singletonSet = UnifiedSet.newSetWith("Bertha"); MutableSet<String> doubletonSet = UnifiedSet.newSetWith("Bertha", "Myra"); Assert.assertTrue(Sets.isProperSubsetOf(singletonSet, doubletonSet)); Assert.assertFalse(Sets.isProperSubsetOf(doubletonSet, singletonSet)); }
public void testContainsAll() { ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers()); for (Set<Integer> subset : Sets.powerSet(ImmutableSet.of(1, 2, 3))) { assertTrue(set.containsAll(subset)); } for (Set<Integer> subset : Sets.powerSet(ImmutableSet.of(1, 2, 3))) { assertFalse(set.containsAll(Sets.union(subset, ImmutableSet.of(9)))); } assertFalse(set.containsAll(ImmutableSet.of("blah"))); }
/** * expand super types after scanning, for super types that were not scanned. this is helpful in * finding the transitive closure without scanning all 3rd party dependencies. it uses {@link * ReflectionUtils#getSuperTypes(Class)}. * * <p>for example, for classes A,B,C where A supertype of B, B supertype of C: * * <ul> * <li>if scanning C resulted in B (B->C in store), but A was not scanned (although A supertype * of B) - then getSubTypes(A) will not return C * <li>if expanding supertypes, B will be expanded with A (A->B in store) - then getSubTypes(A) * will return C * </ul> */ public void expandSuperTypes() { if (store.keySet().contains(index(SubTypesScanner.class))) { Multimap<String, String> mmap = store.get(index(SubTypesScanner.class)); Sets.SetView<String> keys = Sets.difference(mmap.keySet(), Sets.newHashSet(mmap.values())); Multimap<String, String> expand = HashMultimap.create(); for (String key : keys) { expandSupertypes(expand, key, forName(key)); } mmap.putAll(expand); } }
@Test public void differenceAllUnique() { MutableSet<String> names = Sets.differenceAll(this.uniqueSets.get(0), this.uniqueSets.get(1), this.uniqueSets.get(2)); Assert.assertEquals(UnifiedSet.newSetWith("Harry", "Tom", "Dick", null), names); Verify.assertSetsEqual( names, Sets.difference( Sets.difference(this.uniqueSets.get(0), this.uniqueSets.get(1)), this.uniqueSets.get(2))); }
@Test public void differenceAllIdentical() { MutableSet<String> names = Sets.differenceAll( this.identicalSets.get(0), this.identicalSets.get(1), this.identicalSets.get(2)); Assert.assertEquals(UnifiedSet.newSetWith(), names); Verify.assertSetsEqual( names, Sets.difference( Sets.difference(this.identicalSets.get(0), this.identicalSets.get(1)), this.identicalSets.get(2))); }
/** * get types annotated with a given annotation, both classes and annotations, including annotation * member values matching * * <p>{@link Inherited} is honored according to given honorInherited * * <p>depends on TypeAnnotationsScanner configured */ public Set<Class<?>> getTypesAnnotatedWith(final Annotation annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.annotationType().getName()); Iterable<Class<?>> filter = filter(forNames(annotated, loaders()), withAnnotation(annotation)); Iterable<String> classes = getAllAnnotated( names(filter), annotation.annotationType().isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet( concat(filter, forNames(filter(classes, not(in(Sets.newHashSet(annotated)))), loaders()))); }
@Test public void differenceAllOverlapping() { MutableSet<String> names = Sets.differenceAll( this.overlappingSets.get(0), this.overlappingSets.get(1), this.overlappingSets.get(2)); Assert.assertEquals(UnifiedSet.newSetWith("Harry"), names); Verify.assertSetsEqual( names, Sets.difference( Sets.difference(this.overlappingSets.get(0), this.overlappingSets.get(1)), this.overlappingSets.get(2))); }
@Test public void intersectAllIdentical() { MutableSet<String> names = Sets.intersectAll( this.identicalSets.get(0), this.identicalSets.get(1), this.identicalSets.get(2)); Assert.assertEquals(UnifiedSet.newSetWith("Tom", "Dick", "Harry", null), names); }
@Test public void intersectAllOverlapping() { MutableSet<String> names = Sets.intersectAll( this.overlappingSets.get(0), this.overlappingSets.get(1), this.overlappingSets.get(2)); Assert.assertEquals(UnifiedSet.newSetWith("Dick"), names); }
@Test public void testCollectionInstance() { Maps.newLinkedHashMap(); Lists.newArrayList(); Sets.newHashSet(); ObjectArrays.newArray(Integer.class, 10); }
/** * Assigns sequential identifiers to the provided <code>clusters</code> (and their sub-clusters). * If a cluster already has an identifier, the identifier will not be changed. * * @param clusters Clusters to assign identifiers to. * @throws IllegalArgumentException if the provided clusters contain non-unique identifiers */ public static void assignClusterIds(Collection<Cluster> clusters) { final ArrayList<Cluster> flattened = Lists.newArrayListWithExpectedSize(clusters.size()); flatten(flattened, clusters); synchronized (clusters) { final HashSet<Integer> ids = Sets.newHashSet(); // First, find the start value for the id and check uniqueness of the ids // already provided. int maxId = Integer.MIN_VALUE; for (final Cluster cluster : flattened) { if (cluster.id != null) { if (!ids.add(cluster.id)) { throw new IllegalArgumentException("Non-unique cluster id found: " + cluster.id); } maxId = Math.max(maxId, cluster.id); } } // We'd rather start with 0 maxId = Math.max(maxId, -1); // Assign missing ids for (final Cluster c : flattened) { if (c.id == null) { c.id = ++maxId; } } } }
/** * get all fields annotated with a given annotation * * <p>depends on FieldAnnotationsScanner configured */ public Set<Field> getFieldsAnnotatedWith(final Class<? extends Annotation> annotation) { final Set<Field> result = Sets.newHashSet(); for (String annotated : store.get(index(FieldAnnotationsScanner.class), annotation.getName())) { result.add(getFieldFromString(annotated, loaders())); } return result; }
private Variance calculateArgumentProjectionKindFromSuper( @NotNull TypeProjection argument, @NotNull List<TypeProjectionAndVariance> projectionsFromSuper) { Set<Variance> projectionKindsInSuper = Sets.newLinkedHashSet(); for (TypeProjectionAndVariance projectionAndVariance : projectionsFromSuper) { projectionKindsInSuper.add(projectionAndVariance.typeProjection.getProjectionKind()); } Variance defaultProjectionKind = argument.getProjectionKind(); if (projectionKindsInSuper.size() == 0) { return defaultProjectionKind; } else if (projectionKindsInSuper.size() == 1) { Variance projectionKindInSuper = projectionKindsInSuper.iterator().next(); if (defaultProjectionKind == INVARIANT || defaultProjectionKind == projectionKindInSuper) { return projectionKindInSuper; } else { reportError( "Incompatible projection kinds in type arguments of super methods' return types: " + projectionsFromSuper + ", defined in current: " + argument); return defaultProjectionKind; } } else { reportError( "Incompatible projection kinds in type arguments of super methods' return types: " + projectionsFromSuper); return defaultProjectionKind; } }
public static TileEntity getTileEntity(IBlockAccess blockaccess, int x, int y, int z) { HashSet<ChunkCoordinates> visited = Sets.newHashSet(); Block block = blockaccess.getBlock(x, y, z); while (block != NailedBlocks.portalController) { if (isValidLinkPortalBlock(block) == 0) { return null; } ChunkCoordinates pos = new ChunkCoordinates(x, y, z); if (!visited.add(pos)) { return null; } int meta = blockaccess.getBlockMetadata(x, y, z); if (meta == 0) { return null; } if (meta == 1) { y--; } else if (meta == 2) { y++; } else if (meta == 3) { z--; } else if (meta == 4) { z++; } else if (meta == 5) { x--; } else if (meta == 6) { x++; } else { return null; } block = blockaccess.getBlock(x, y, z); } return blockaccess.getTileEntity(x, y, z); }
@NonNull public IAndroidTarget[] getMissingTargets() { synchronized (mLocalPackages) { if (mCachedMissingTargets == null) { Map<MissingTarget, MissingTarget> result = Maps.newHashMap(); Set<ISystemImage> seen = Sets.newHashSet(); for (IAndroidTarget target : getTargets()) { Collections.addAll(seen, target.getSystemImages()); } for (LocalPkgInfo local : getPkgsInfos(PkgType.PKG_ADDON_SYS_IMAGE)) { LocalAddonSysImgPkgInfo info = (LocalAddonSysImgPkgInfo) local; ISystemImage image = info.getSystemImage(); if (!seen.contains(image)) { addOrphanedSystemImage(image, info.getDesc(), result); } } for (LocalPkgInfo local : getPkgsInfos(PkgType.PKG_SYS_IMAGE)) { LocalSysImgPkgInfo info = (LocalSysImgPkgInfo) local; ISystemImage image = info.getSystemImage(); if (!seen.contains(image)) { addOrphanedSystemImage(image, info.getDesc(), result); } } mCachedMissingTargets = result.keySet(); } return mCachedMissingTargets.toArray(new IAndroidTarget[mCachedMissingTargets.size()]); } }
public void testValues() { ImmutableBiMap<String, Integer> bimap = ImmutableBiMap.copyOf(ImmutableMap.of("one", 1, "two", 2, "three", 3, "four", 4)); Set<Integer> values = bimap.values(); assertEquals(Sets.newHashSet(1, 2, 3, 4), values); assertThat(values).containsExactly(1, 2, 3, 4).inOrder(); }
// Loop through all changed classes, adding their parents (and their // parents) // to another set of changed classes public Set<JavaClass> findChangedParents(Set<JavaClass> classes) { Set<JavaClass> changedParents = Sets.newHashSet(classes); for (JavaClass jclass : classes) { findParents(jclass, changedParents); } return changedParents; }
public void testKeySet() { ImmutableBiMap<String, Integer> bimap = ImmutableBiMap.copyOf(ImmutableMap.of("one", 1, "two", 2, "three", 3, "four", 4)); Set<String> keys = bimap.keySet(); assertEquals(Sets.newHashSet("one", "two", "three", "four"), keys); ASSERT.that(keys).hasContentsInOrder("one", "two", "three", "four"); }
public void testKeySet() { ImmutableBiMap<String, Integer> bimap = ImmutableBiMap.copyOf(ImmutableMap.of("one", 1, "two", 2, "three", 3, "four", 4)); Set<String> keys = bimap.keySet(); assertEquals(Sets.newHashSet("one", "two", "three", "four"), keys); assertThat(keys).containsExactly("one", "two", "three", "four").inOrder(); }
public void testValues() { ImmutableBiMap<String, Integer> bimap = ImmutableBiMap.copyOf(ImmutableMap.of("one", 1, "two", 2, "three", 3, "four", 4)); Set<Integer> values = bimap.values(); assertEquals(Sets.newHashSet(1, 2, 3, 4), values); ASSERT.that(values).hasContentsInOrder(1, 2, 3, 4); }
/** * get types annotated with a given annotation, both classes and annotations * * <p>{@link Inherited} is honored according to given honorInherited. * * <p>when honoring @Inherited, meta-annotation should only effect annotated super classes and * it's sub types * * <p>when not honoring @Inherited, meta annotation effects all subtypes, including annotations * interfaces and classes * * <p><i>Note that this (@Inherited) meta-annotation type has no effect if the annotated type is * used for anything other then a class. Also, this meta-annotation causes annotations to be * inherited only from superclasses; annotations on implemented interfaces have no effect.</i> * * <p>depends on TypeAnnotationsScanner and SubTypesScanner configured */ public Set<Class<?>> getTypesAnnotatedWith( final Class<? extends Annotation> annotation, boolean honorInherited) { Iterable<String> annotated = store.get(index(TypeAnnotationsScanner.class), annotation.getName()); Iterable<String> classes = getAllAnnotated(annotated, annotation.isAnnotationPresent(Inherited.class), honorInherited); return Sets.newHashSet(concat(forNames(annotated, loaders()), forNames(classes, loaders()))); }
/** * Builds an "Other Topics" cluster that groups those documents from <code>allDocument</code> that * were not referenced in any cluster in <code>clusters</code>. * * @param allDocuments all documents to check against * @param clusters list of clusters with assigned documents * @param label label for the "Other Topics" group * @return the "Other Topics" cluster */ public static Cluster buildOtherTopics( List<Document> allDocuments, List<Cluster> clusters, String label) { final Set<Document> unclusteredDocuments = Sets.newLinkedHashSet(allDocuments); final Set<Document> assignedDocuments = Sets.newHashSet(); for (Cluster cluster : clusters) { collectAllDocuments(cluster, assignedDocuments); } unclusteredDocuments.removeAll(assignedDocuments); final Cluster otherTopics = new Cluster(label); otherTopics.addDocuments(unclusteredDocuments); otherTopics.setOtherTopics(true); return otherTopics; }
@Test public void whenCalculatingSetIntersection_thenCorrect() { final Set<Character> first = ImmutableSet.of('a', 'b', 'c'); final Set<Character> second = ImmutableSet.of('b', 'c', 'd'); final Set<Character> intersection = Sets.intersection(first, second); assertThat(intersection, containsInAnyOrder('b', 'c')); }
@Test public void whenCalculateUnionOfSets_thenCorrect() { final Set<Character> first = ImmutableSet.of('a', 'b', 'c'); final Set<Character> second = ImmutableSet.of('b', 'c', 'd'); final Set<Character> union = Sets.union(first, second); assertThat(union, containsInAnyOrder('a', 'b', 'c', 'd')); }
@Test public void unionAllOverlapping() { MutableSet<String> names = Sets.unionAll( this.overlappingSets.get(0), this.overlappingSets.get(1), this.overlappingSets.get(2)); Assert.assertEquals( UnifiedSet.newSetWith("Tom", "Dick", "Harry", "Larry", "Paul", null), names); }
@Override protected void assertMoreInvariants(Map<K, V> map) { BiMap<K, V> bimap = (BiMap<K, V>) map; for (Entry<K, V> entry : map.entrySet()) { assertEquals(entry.getKey() + "=" + entry.getValue(), entry.toString()); assertEquals(entry.getKey(), bimap.inverse().get(entry.getValue())); } assertEquals("{" + joiner.join(map.entrySet()) + "}", map.toString()); assertEquals("[" + joiner.join(map.entrySet()) + "]", map.entrySet().toString()); assertEquals("[" + joiner.join(map.keySet()) + "]", map.keySet().toString()); assertEquals("[" + joiner.join(map.values()) + "]", map.values().toString()); assertEquals(Sets.newHashSet(map.entrySet()), map.entrySet()); assertEquals(Sets.newHashSet(map.keySet()), map.keySet()); }