All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dev.marksman.collectionviews.SetHelpers Maven / Gradle / Ivy

There is a newer version: 1.2.3
Show newest version
package dev.marksman.collectionviews;

/**
 * Helper methods for implementers of custom {@link Set}s.
 * 

* If you are implementing a custom {@link Set}, your {@code equals} and {@code hashCode} * methods SHOULD delegate to {@code setEquals} and {@code setHashCode}, respectively. This will * ensure that equality works correctly with the built-in {@link Set} types. *

* Your {@code toString} method MAY delegate to {@code setToString}. */ @SuppressWarnings("WeakerAccess") public final class SetHelpers { private SetHelpers() { } @SuppressWarnings("unchecked") public static boolean setEquals(Set set, Set other) { if (set == null || other == null) { return false; } if (other == set) { return true; } if (other.size() != set.size()) { return false; } return containsAllElements((Set) other, (Iterable) set); } public static int setHashCode(Set set) { int result = 0; for (Object obj : set) { if (obj != null) { result += obj.hashCode(); } } return result; } public static String setToString(Set set) { return Util.iterableToString("Set", set); } private static boolean containsAllElements(Set set, Iterable elements) { for (Object elem : elements) { if (!set.contains(elem)) { return false; } } return true; } }