Misc cleanup

This commit is contained in:
Luck
2017-12-17 12:17:46 +00:00
Unverified
parent ad5299d0cd
commit f86bdb7619
28 changed files with 79 additions and 178 deletions
@@ -39,7 +39,6 @@ abstract class AbstractContextSet implements ContextSet {
protected abstract Multimap<String, String> backing();
@Nonnull
@Override
public boolean containsKey(@Nonnull String key) {
return backing().containsKey(sanitizeKey(key));
@@ -52,13 +51,11 @@ abstract class AbstractContextSet implements ContextSet {
return values != null ? ImmutableSet.copyOf(values) : ImmutableSet.of();
}
@Nonnull
@Override
public boolean has(@Nonnull String key, @Nonnull String value) {
return backing().containsEntry(sanitizeKey(key), sanitizeValue(value));
}
@Nonnull
@Override
public boolean hasIgnoreCase(@Nonnull String key, @Nonnull String value) {
String v = sanitizeValue(value);
@@ -90,6 +87,28 @@ abstract class AbstractContextSet implements ContextSet {
return backing().size();
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof ContextSet)) return false;
final ContextSet other = (ContextSet) o;
final Multimap<String, String> otherContexts;
if (other instanceof MutableContextSet) {
otherContexts = ((MutableContextSet) other).backing();
} else {
otherContexts = other.toMultimap();
}
return backing().equals(otherContexts);
}
@Override
public int hashCode() {
return backing().hashCode();
}
static String sanitizeKey(String key) {
return checkNotNull(key, "key is null").toLowerCase().intern();
}
@@ -277,7 +277,7 @@ public interface ContextSet {
* @since 3.1
*/
default boolean isSatisfiedBy(@Nonnull ContextSet other) {
return isSatisfiedBy(other, true);
return this == other || isSatisfiedBy(other, true);
}
/**
@@ -289,6 +289,10 @@ public interface ContextSet {
* @since 3.4
*/
default boolean isSatisfiedBy(@Nonnull ContextSet other, boolean caseSensitive) {
if (this == other) {
return true;
}
Preconditions.checkNotNull(other, "other");
if (this.isEmpty()) {
// this is empty, so is therefore always satisfied.
@@ -40,9 +40,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* An immutable implementation of {@link ContextSet}.
*
* <p>On construction, all keys/values are {@link String#intern()}ed, in order to increase
* comparison speed.</p>
*
* @since 2.16
*/
public final class ImmutableContextSet extends AbstractContextSet implements ContextSet {
@@ -164,9 +161,11 @@ public final class ImmutableContextSet extends AbstractContextSet implements Con
}
private final ImmutableSetMultimap<String, String> map;
private final int hashCode;
ImmutableContextSet(ImmutableSetMultimap<String, String> contexts) {
this.map = contexts;
this.hashCode = map.hashCode();
}
@Override
@@ -216,26 +215,9 @@ public final class ImmutableContextSet extends AbstractContextSet implements Con
return map;
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof ContextSet)) return false;
final ContextSet other = (ContextSet) o;
final Multimap<String, String> otherContexts;
if (other instanceof MutableContextSet) {
otherContexts = ((MutableContextSet) other).backing();
} else {
otherContexts = other.toMultimap();
}
return this.map.equals(otherContexts);
}
@Override
public int hashCode() {
return map.hashCode();
return hashCode;
}
@Override
@@ -45,9 +45,6 @@ import static com.google.common.base.Preconditions.checkNotNull;
/**
* A mutable implementation of {@link ContextSet}.
*
* <p>On construction, all keys/values are {@link String#intern()}ed, in order to increase
* comparison speed.</p>
*
* @since 2.16
*/
public final class MutableContextSet extends AbstractContextSet implements ContextSet {
@@ -338,28 +335,6 @@ public final class MutableContextSet extends AbstractContextSet implements Conte
map.clear();
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof ContextSet)) return false;
final ContextSet other = (ContextSet) o;
final Multimap<String, String> otherContexts;
if (other instanceof MutableContextSet) {
otherContexts = ((MutableContextSet) other).map;
} else {
otherContexts = other.toMultimap();
}
return this.map.equals(otherContexts);
}
@Override
public int hashCode() {
return map.hashCode();
}
@Override
public String toString() {
return "MutableContextSet(contexts=" + this.map + ")";