下面列出了com.google.common.collect.Sets#symmetricDifference ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Test
public void testTermsConsistency(){
Set<Term> interpretedFromTermUtils = Sets.newHashSet(TermUtils.interpretedTerms());
Set<Term> interpretedFromTerms = Sets.newHashSet(Terms.interpretedTerms());
Set<Term> diff = Sets.symmetricDifference(interpretedFromTermUtils, interpretedFromTerms);
assertEquals("TermUtils.interpretedTerms() and Terms.interpretedTerms() must use the same terms. Difference(s): " +
diff, 0, diff.size());
Set<Term> hdfsTerms = DownloadTerms.DOWNLOAD_INTERPRETED_TERMS_HDFS;
diff = Sets.newHashSet(Sets.symmetricDifference(interpretedFromTermUtils, hdfsTerms));
diff.remove(GbifTerm.gbifID);
diff.remove(GbifTerm.mediaType);
diff.remove(GbifTerm.numberOfOccurrences);
diff.remove(GbifTerm.verbatimScientificName);
assertEquals("TermUtils.interpretedTerms() and DownloadTerms.DOWNLOAD_INTERPRETED_TERMS_HDFS must use the same terms. Difference(s): " +
diff, 0, diff.size());
}
/**
* None of our {@link AppEngineResourceElement#reload()} currently return a different instance, so
* a configuration file change should not result in a change.
*/
@Test
public void testChildElementPreservedOnChange() throws AppEngineException {
List<IFile> files = new ArrayList<>();
IProject project = projectCreator.getProject();
files.add(ConfigurationFileUtils.createEmptyCronXml(project));
files.add(ConfigurationFileUtils.createEmptyDatastoreIndexesXml(project));
files.add(ConfigurationFileUtils.createEmptyDispatchXml(project));
files.add(ConfigurationFileUtils.createEmptyDosXml(project));
files.add(ConfigurationFileUtils.createEmptyQueueXml(project));
AppEngineProjectElement projectElement = AppEngineProjectElement.create(project);
files.add(projectElement.getDescriptorFile());
AppEngineResourceElement[] subElements = projectElement.getConfigurations();
for (IFile file : files) {
boolean changed = projectElement.resourcesChanged(Collections.singleton(file));
assertTrue(changed);
AppEngineResourceElement[] newSubElements = projectElement.getConfigurations();
Set<Object> difference =
Sets.symmetricDifference(Sets.newHashSet(subElements), Sets.newHashSet(newSubElements));
assertThat("all elements should have been preserved", difference, Matchers.hasSize(0));
}
}
private static void testSymmetricDifference() {
Collection<String> collection1 = Lists.newArrayList("a2", "a3");
Collection<String> collection2 = Lists.newArrayList("a8", "a3", "a5");
Set<String> set1 = Sets.newHashSet("a2", "a3");
Set<String> set2 = Sets.newHashSet("a8", "a3", "a5");
MutableSet<String> mutableSet1 = UnifiedSet.newSetWith("a2", "a3");
MutableSet<String> mutableSet2 = UnifiedSet.newSetWith("a8", "a3", "a5");
// Find symmetric difference (elements that are contained in either one collection or other but not in both
Set<String> intersect = new HashSet<>(set1); // using JDK
intersect.retainAll(set2);
Set<String> jdk = new HashSet<>(set1);
jdk.addAll(set2);
jdk.removeAll(intersect);
Set<String> guava = Sets.symmetricDifference(set1, set2); // using guava
Collection<String> apache = CollectionUtils.disjunction(collection1, collection2); // using Apache
Set<String> gs = mutableSet1.symmetricDifference(mutableSet2); // using GS
System.out.println("symmetricDifference = " + jdk + ":" + guava + ":" + apache + ":" + gs); // print symmetricDifference = [a2, a5, a8]:[a2, a5, a8]:[a2, a5, a8]:[a2, a5, a8]
}
private static void testSymmetricDifference() {
Collection<String> collection1 = Lists.newArrayList("a2", "a3");
Collection<String> collection2 = Lists.newArrayList("a8", "a3", "a5");
Set<String> set1 = Sets.newHashSet("a2", "a3");
Set<String> set2 = Sets.newHashSet("a8", "a3", "a5");
MutableSet<String> mutableSet1 = UnifiedSet.newSetWith("a2", "a3");
MutableSet<String> mutableSet2 = UnifiedSet.newSetWith("a8", "a3", "a5");
// Найти все различные элементы (symmetric difference) у двух коллекций
Set<String> intersect = new HashSet<>(set1); // c помощью JDK
intersect.retainAll(set2);
Set<String> jdk = new HashSet<>(set1);
jdk.addAll(set2);
jdk.removeAll(intersect);
Set<String> guava = Sets.symmetricDifference(set1, set2); // с помощью guava
Collection<String> apache = CollectionUtils.disjunction(collection1, collection2); // c помощью Apache
Set<String> gs = mutableSet1.symmetricDifference(mutableSet2); // c помощью GS
System.out.println("symmetricDifference = " + jdk + ":" + guava + ":" + apache + ":" + gs); // напечатает symmetricDifference = [a2, a5, a8]:[a2, a5, a8]:[a2, a5, a8]:[a2, a5, a8]
}
public static void warnOfDifferences(ReferencedState s1, ReferencedState s2) {
Set<String> locDiffs = Sets.symmetricDifference(s1.locations, s2.locations);
Set<String> enricherDiffs = Sets.symmetricDifference(s1.enrichers, s2.enrichers);
Set<String> policyDiffs = Sets.symmetricDifference(s1.policies, s2.policies);
Set<String> feedDiffs = Sets.symmetricDifference(s1.feeds, s2.feeds);
if (locDiffs.size() > 0) {
LOG.warn("Deletion of orphan state found unusually referenced locations (keeping): " + locDiffs);
}
if (enricherDiffs.size() > 0) {
LOG.warn("Deletion of orphan state found unusually referenced enrichers (keeping): " + enricherDiffs);
}
if (policyDiffs.size() > 0) {
LOG.warn("Deletion of orphan state found unusually referenced policies (keeping): " + policyDiffs);
}
if (feedDiffs.size() > 0) {
LOG.warn("Deletion of orphan state found unusually referenced feeds (keeping): " + feedDiffs);
}
}
static SystemOutputStore2016 open(File dir, final AssessmentSpecFormats.Format format)
throws IOException {
final Symbol systemID = Symbol.from(dir.getName());
final File argumentsDir = new File(dir, "arguments");
final File linkingDir = new File(dir, "linking");
final File corpusLinkingFile = new File(new File(dir, "corpusLinking"), "corpusLinking");
corpusLinkingFile.getParentFile().mkdirs();
final ArgumentStore argStore = AssessmentSpecFormats.openSystemOutputStore(argumentsDir,
format);
final LinkingStore linkingStore =
LinkingStoreSource.createFor2016().openLinkingStore(linkingDir);
if (argStore.docIDs().equals(linkingStore.docIDs())) {
return new SystemOutputStore2016(systemID, argStore, linkingStore, corpusLinkingFile);
} else {
throw new RuntimeException("Argument and linking store docIDs do not match, missing " + Sets
.symmetricDifference(argStore.docIDs(), linkingStore.docIDs()));
}
}
static SystemOutputStore2016 openOrCreate(File dir,
final AssessmentSpecFormats.Format format) throws IOException {
final Symbol systemID = Symbol.from(dir.getName());
final File argumentsDir = new File(dir, "arguments");
final File linkingDir = new File(dir, "linking");
final File corpusLinkingFile = new File(new File(dir, "corpusLinking"), "corpusLinking");
corpusLinkingFile.getParentFile().mkdirs();
final ArgumentStore argStore = AssessmentSpecFormats.openOrCreateSystemOutputStore(argumentsDir,
format);
final LinkingStore linkingStore =
LinkingStoreSource.createFor2016().openOrCreateLinkingStore(linkingDir);
if (argStore.docIDs().equals(linkingStore.docIDs())) {
return new SystemOutputStore2016(systemID, argStore, linkingStore, corpusLinkingFile);
} else {
throw new RuntimeException("Argument and linking store docIDs do not match, missing " + Sets
.symmetricDifference(argStore.docIDs(), linkingStore.docIDs()));
}
}
public static SystemOutputStore2017 open(File dir) throws IOException {
final Symbol systemID = Symbol.from(dir.getName());
final File argumentsDir = new File(dir, "arguments");
final File linkingDir = new File(dir, "linking");
final File corpusLinkingFile = new File(new File(dir, "corpusLinking"), "corpusLinking");
corpusLinkingFile.getParentFile().mkdirs();
final ArgumentStore argStore = AssessmentSpecFormats.openSystemOutputStore(argumentsDir,
AssessmentSpecFormats.Format.KBP2017);
final LinkingStore linkingStore =
LinkingStoreSource.createFor2016().openLinkingStore(linkingDir);
if (argStore.docIDs().equals(linkingStore.docIDs())) {
return new SystemOutputStore2017(systemID, argStore, linkingStore, corpusLinkingFile);
} else {
throw new RuntimeException("Argument and linking store docIDs do not match, missing " + Sets
.symmetricDifference(argStore.docIDs(), linkingStore.docIDs()));
}
}
static SystemOutputStore2017 openOrCreate(File dir,
final AssessmentSpecFormats.Format format) throws IOException {
final Symbol systemID = Symbol.from(dir.getName());
final File argumentsDir = new File(dir, "arguments");
final File linkingDir = new File(dir, "linking");
final File corpusLinkingFile = new File(new File(dir, "corpusLinking"), "corpusLinking");
corpusLinkingFile.getParentFile().mkdirs();
final ArgumentStore argStore = AssessmentSpecFormats.openOrCreateSystemOutputStore(argumentsDir,
format);
final LinkingStore linkingStore =
LinkingStoreSource.createFor2016().openOrCreateLinkingStore(linkingDir);
if (argStore.docIDs().equals(linkingStore.docIDs())) {
return new SystemOutputStore2017(systemID, argStore, linkingStore, corpusLinkingFile);
} else {
throw new RuntimeException("Argument and linking store docIDs do not match, missing " + Sets
.symmetricDifference(argStore.docIDs(), linkingStore.docIDs()));
}
}
/**
* Sets the packages that should be treated as deleted and ignored.
*/
@Override
@VisibleForTesting // productionVisibility = Visibility.PRIVATE
public void setDeletedPackages(Iterable<PackageIdentifier> pkgs) {
ImmutableSet<PackageIdentifier> newDeletedPackagesSet = ImmutableSet.copyOf(pkgs);
Set<PackageIdentifier> newlyDeletedOrNotDeletedPackages =
Sets.symmetricDifference(deletedPackages.get(), newDeletedPackagesSet);
if (!newlyDeletedOrNotDeletedPackages.isEmpty()) {
// PackageLookupValue is a HERMETIC node type, so we can't invalidate it.
memoizingEvaluator.delete(
k -> PackageLookupValue.appliesToKey(k, newlyDeletedOrNotDeletedPackages::contains));
}
deletedPackages.set(newDeletedPackagesSet);
}
public static List<Set<String>> diff(HeaderGroup expected, HeaderGroup actual) {
Set<String> expecetdKeys = keys(expected);
Set<String> actualKeys = keys(actual);
Set<String> common = Sets.intersection(expecetdKeys, actualKeys);
Set<String> symdiff = Sets.symmetricDifference(expecetdKeys, actualKeys);
Set<String> diffval = new HashSet<>();
for (String s : common)
if (! expected.getCondensedHeader(s).getValue().equals(actual.getCondensedHeader(s).getValue())) diffval.add(s);
return Arrays.asList(diffval, symdiff);
}
private void testDifferenceAndOrder(List<Term> fromTermUtils, List<Term> fromTerms) {
Set<Term> fromTermUtilsSet = Sets.newHashSet(fromTermUtils);
Set<Term> fromTermsSet = Sets.newHashSet(fromTerms);
Set<Term> diff = Sets.symmetricDifference(fromTermUtilsSet, fromTermsSet);
assertEquals("fromTerms and fromTermUtils must use the same terms. Difference(s): " +
diff, 0, diff.size());
int i = 0;
for (; i < fromTermUtils.size(); i++) {
assertEquals("Order is different at position "+i, fromTermUtils.get(i), fromTerms.get(i));
}
}
@Override
@SuppressWarnings("checkstyle:parameterName")
public int compareTo(final PathArgument o) {
if (!(o instanceof AugmentationIdentifier)) {
return -1;
}
AugmentationIdentifier other = (AugmentationIdentifier) o;
Set<QName> otherChildNames = other.getPossibleChildNames();
int thisSize = childNames.size();
int otherSize = otherChildNames.size();
if (thisSize == otherSize) {
// Quick Set-based comparison
if (childNames.equals(otherChildNames)) {
return 0;
}
// We already know the sets are not equal, but have equal size, hence the sets differ in their elements,
// but potentially share a common set of elements. The most consistent way of comparing them is using
// total ordering defined by QName's compareTo. Hence convert both sets to lists ordered
// by QName.compareTo() and decide on the first differing element.
final List<QName> diff = new ArrayList<>(Sets.symmetricDifference(childNames, otherChildNames));
verify(!diff.isEmpty(), "Augmentation identifiers %s and %s report no difference", this, o);
diff.sort(QName::compareTo);
return childNames.contains(diff.get(0)) ? -1 : 1;
} else if (thisSize < otherSize) {
return 1;
} else {
return -1;
}
}
@Override
public Object filter(
Object var,
JinjavaInterpreter interpreter,
Object[] args,
Map<String, Object> kwargs
) {
return new ArrayList<>(
Sets.symmetricDifference(
objectToSet(var),
objectToSet(parseArgs(interpreter, args))
)
);
}
private static boolean isListEquals(List<String> alist, List<String> blist) {
Set<String> differenceSet = Sets.symmetricDifference(Sets.newHashSet(alist), Sets.newHashSet(blist));
return CollectionUtils.isEmpty(differenceSet);
}
@Inject
public AstyanaxTableDAO(LifeCycleRegistry lifeCycle,
@SystemTableNamespace String systemTableNamespace,
@SystemTablePlacement String systemTablePlacement,
@ShardsPerTable int defaultNumShards,
@BootstrapTables Map<String, Long> bootstrapTables,
PlacementFactory placementFactory,
PlacementCache placementCache,
@CurrentDataCenter String selfDataCenter,
@Maintenance RateLimiterCache rateLimiterCache,
DataCopyDAO dataCopyDAO,
DataPurgeDAO dataPurgeDAO,
FullConsistencyTimeProvider fullConsistencyTimeProvider,
@TableChangesEnabled ValueStore<Boolean> tableChangesEnabled,
@CachingTableDAORegistry CacheRegistry cacheRegistry,
@PlacementsUnderMove Map<String, String> placementsUnderMove,
ObjectMapper objectMapper,
@Nullable Clock clock) {
_systemTablePlacement = checkNotNull(systemTablePlacement, "systemTablePlacement");
_bootstrapTables = HashBiMap.create(checkNotNull(bootstrapTables, "bootstrapTables"));
_reservedUuids = _bootstrapTables.inverse().keySet();
_placementFactory = checkNotNull(placementFactory);
_placementCache = checkNotNull(placementCache, "placementCache");
_selfDataCenter = checkNotNull(selfDataCenter, "selfDataCenter");
_defaultShardsLog2 = RowKeyUtils.computeShardsLog2(defaultNumShards, "default");
_rateLimiterCache = checkNotNull(rateLimiterCache, "rateLimiterCache");
_dataCopyDAO = checkNotNull(dataCopyDAO, "copyDataDAO");
_dataPurgeDAO = checkNotNull(dataPurgeDAO, "purgeDataDAO");
_fullConsistencyTimeProvider = checkNotNull(fullConsistencyTimeProvider, "fullConsistencyTimeProvider");
_tableChangesEnabled = checkNotNull(tableChangesEnabled, "tableChangesEnabled");
_tableCacheHandle = cacheRegistry.lookup("tables", true);
_placementsUnderMove = checkNotNull(placementsUnderMove, "placementsUnderMove");
_objectMapper = requireNonNull(objectMapper);
_clock = clock != null ? clock : Clock.systemUTC();
// There are two tables used to store metadata about all the other tables.
checkNotNull(systemTableNamespace, "systemTableNamespace");
_systemTable = systemTableNamespace + ":table";
_systemTableUuid = systemTableNamespace + ":table_uuid";
String systemDataCenterTable = systemTableNamespace + ":data_center";
_systemTableUnPublishedDatabusEvents = systemTableNamespace + ":table_unpublished_databus_events";
_systemTableEventRegistry = systemTableNamespace + ":table_event_registry";
// If this table DAO uses itself to store its table metadata (ie. it requires bootstrap tables) then make sure
// the right bootstrap tables are specified. This happens when "_dataStore" uses "this" for table metadata.
if (!_bootstrapTables.isEmpty()) {
Set<String> expectedTables = ImmutableSet.of(_systemTable, _systemTableUuid, systemDataCenterTable, _systemTableUnPublishedDatabusEvents, _systemTableEventRegistry);
Set<String> diff = Sets.symmetricDifference(expectedTables, bootstrapTables.keySet());
checkState(diff.isEmpty(), "Bootstrap tables map is missing tables or has extra tables: %s", diff);
}
lifeCycle.manage(this);
}
@ExpectWarning(value="GC", num=3)
public static void testSets(Set<String> s1, Set<Integer> s2) {
Sets.intersection(s1, s2);
Sets.difference(s1, s2);
Sets.symmetricDifference(s1, s2);
}
@Test
public void symmetric_set_difference_guava () {
Set<String> yourFriendsOrMyFriends = Sets.symmetricDifference(yourFriends, myFriends);
assertEquals(12, yourFriendsOrMyFriends.size());
}
/**
* set1, set2的补集(在set1或set2中,但不在交集中的对象,又叫反交集)的只读view,不复制产生新的Set对象.
*
* 如果尝试写入该View会抛出UnsupportedOperationException
*/
public static <E> Set<E> disjointView(final Set<? extends E> set1, final Set<? extends E> set2) {
return Sets.symmetricDifference(set1, set2);
}
/**
* set1, set2的补集(在set1或set2中,但不在交集中的对象,又叫反交集)的只读view,不复制产生新的Set对象.
*
* 如果尝试写入该View会抛出UnsupportedOperationException
*/
public static <E> Set<E> disjointView(final Set<? extends E> set1, final Set<? extends E> set2) {
return Sets.symmetricDifference(set1, set2);
}