Context set values should be lowercase'd too

This commit is contained in:
Luck
2018-04-26 19:51:05 +01:00
Unverified
parent 316e177c1d
commit fef6ebf793
5 changed files with 15 additions and 108 deletions
@@ -68,27 +68,6 @@ abstract class AbstractContextSet implements ContextSet {
return backing().containsEntry(sanitizeKey(key), sanitizeValue(value));
}
@Override
public boolean hasIgnoreCase(@Nonnull String key, @Nonnull String value) {
String v = sanitizeValue(value);
Collection<String> values = backing().asMap().get(sanitizeKey(key));
if (values == null || values.isEmpty()) {
return false;
}
if (values.contains(v)) {
return true;
}
for (String val : values) {
if (val.equalsIgnoreCase(v)) {
return true;
}
}
return false;
}
@Override
public boolean isEmpty() {
return backing().isEmpty();
@@ -134,7 +113,7 @@ abstract class AbstractContextSet implements ContextSet {
if (stringIsEmpty(value)) {
throw new IllegalArgumentException("value is (effectively) empty");
}
return value;
return value.toLowerCase();
}
private static boolean stringIsEmpty(String s) {
@@ -47,9 +47,8 @@ import javax.annotation.Nonnull;
* <p>Contexts can be combined with each other to form so called
* "context sets" - simply a collection of context pairs.</p>
*
* <p>Context keys are case-insensitive, and will be converted to
* {@link String#toLowerCase() lowercase} by all implementations.
* Values however are case-sensitive.</p>
* <p>Context keys and values are case-insensitive, and will be converted to
* {@link String#toLowerCase() lowercase} by all implementations.</p>
*
* <p>Context keys and values may not be null or empty. A key/value will be
* deemed empty if it's length is zero, or if it consists of only space
@@ -272,33 +271,16 @@ public interface ContextSet {
/**
* Returns if the {@link ContextSet} contains a given context pairing.
*
* <p>This lookup is case-sensitive on the value.</p>
*
* @param key the key to look for
* @param value the value to look for (case sensitive)
* @param value the value to look for
* @return true if the set contains the context pair
* @throws NullPointerException if the key or value is null
*/
boolean has(@Nonnull String key, @Nonnull String value);
/**
* Returns if the {@link ContextSet} contains a given context pairing,
* ignoring the case of values.
*
* <p>This lookup is case-insensitive on the value.</p>
*
* @param key the key to look for
* @param value the value to look for
* @return true if the set contains the context pair
* @throws NullPointerException if the key or value is null
*/
boolean hasIgnoreCase(@Nonnull String key, @Nonnull String value);
/**
* Returns if the {@link ContextSet} contains a given context pairing.
*
* <p>This lookup is case-sensitive on the value.</p>
*
* @param entry the entry to look for
* @return true if the set contains the context pair
* @throws NullPointerException if the key or value is null
@@ -308,21 +290,6 @@ public interface ContextSet {
return has(entry.getKey(), entry.getValue());
}
/**
* Returns if the {@link ContextSet} contains a given context pairing,
* ignoring the case of values.
*
* <p>This lookup is case-insensitive on the value.</p>
*
* @param entry the entry to look for
* @return true if the set contains the context pair
* @throws NullPointerException if the key or value is null
*/
default boolean hasIgnoreCase(@Nonnull Map.Entry<String, String> entry) {
Objects.requireNonNull(entry, "entry");
return hasIgnoreCase(entry.getKey(), entry.getValue());
}
/**
* Returns if this {@link ContextSet} is fully "satisfied" by another set.
*
@@ -331,31 +298,11 @@ public interface ContextSet {
*
* <p>Mathematically, this method returns true if this set is a <b>subset</b> of the other.</p>
*
* <p>This check is case-sensitive. For a case-insensitive check,
* use {@link #isSatisfiedBy(ContextSet, boolean)}.</p>
*
* @param other the other set to check
* @return true if all entries in this set are also in the other set
* @since 3.1
*/
default boolean isSatisfiedBy(@Nonnull ContextSet other) {
return isSatisfiedBy(other, true);
}
/**
* Returns if this {@link ContextSet} is fully "satisfied" by another set.
*
* <p>For a context set to "satisfy" another, it must itself contain all of
* the context pairings in the other set.</p>
*
* <p>Mathematically, this method returns true if this set is a <b>subset</b> of the other.</p>
*
* @param other the other set to check
* @param caseSensitive if the check should be case sensitive
* @return true if all entries in this set are also in the other set
* @since 3.4
*/
default boolean isSatisfiedBy(@Nonnull ContextSet other, boolean caseSensitive) {
if (this == other) {
return true;
}
@@ -373,14 +320,8 @@ public interface ContextSet {
} else {
// neither are empty, we need to compare the individual entries
for (Map.Entry<String, String> context : toSet()) {
if (caseSensitive) {
if (!other.has(context)) {
return false;
}
} else {
if (!other.hasIgnoreCase(context)) {
return false;
}
if (!other.has(context)) {
return false;
}
}
return true;
@@ -32,7 +32,6 @@ import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import java.util.Collection;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
@@ -282,28 +281,13 @@ public final class MutableContextSet extends AbstractContextSet implements Conte
* Removes a context from this set.
*
* @param key the key to remove
* @param value the value to remove (case sensitive)
* @param value the value to remove
* @throws NullPointerException if the key or value is null
*/
public void remove(@Nonnull String key, @Nonnull String value) {
this.map.remove(sanitizeKey(key), sanitizeValue(value));
}
/**
* Removes a context from this set. (case-insensitive)
*
* @param key the key to remove
* @param value the value to remove
* @throws NullPointerException if the key or value is null
*/
public void removeIgnoreCase(@Nonnull String key, @Nonnull String value) {
String v = sanitizeValue(value);
Collection<String> strings = this.map.asMap().get(sanitizeKey(key));
if (strings != null) {
strings.removeIf(e -> e.equalsIgnoreCase(v));
}
}
/**
* Removes all contexts from this set with the given key.
*