[core] Forbid ForwardingSet

Removes CopyOnWriteHashSet, our only usage of ForwardingSet. We weren't
using it.

Related to #13224
This commit is contained in:
Nik Everett 2015-09-22 12:31:08 -04:00
parent 4d47015a0c
commit e284210652
3 changed files with 1 additions and 236 deletions

View File

@ -1,109 +0,0 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.collect;
import com.google.common.collect.ForwardingSet;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Set;
/**
* {@link Set} implementation based on {@link CopyOnWriteHashMap}.
* Null values are not supported.
*/
public class CopyOnWriteHashSet<T> extends ForwardingSet<T> {
/**
* Return a copy of the provided set.
*/
public static <T> CopyOnWriteHashSet<T> copyOf(Collection<? extends T> set) {
if (set instanceof CopyOnWriteHashSet) {
// no need to copy in that case
@SuppressWarnings("unchecked")
final CopyOnWriteHashSet<T> cowSet = (CopyOnWriteHashSet<T>) set;
return cowSet;
} else {
return new CopyOnWriteHashSet<T>().copyAndAddAll(set);
}
}
private final CopyOnWriteHashMap<T, Boolean> map;
/** Create a new empty set. */
public CopyOnWriteHashSet() {
this(new CopyOnWriteHashMap<T, Boolean>());
}
private CopyOnWriteHashSet(CopyOnWriteHashMap<T, Boolean> map) {
this.map = map;
}
@Override
protected Set<T> delegate() {
return map.keySet();
}
/**
* Copy the current set and return a copy that contains or replaces <code>entry</code>.
*/
public CopyOnWriteHashSet<T> copyAndAdd(T entry) {
return new CopyOnWriteHashSet<>(map.copyAndPut(entry, true));
}
/**
* Copy the current set and return a copy that is the union of the current
* set and <code>entries</code>, potentially replacing existing entries in
* case of equality.
*/
public CopyOnWriteHashSet<T> copyAndAddAll(Collection<? extends T> entries) {
CopyOnWriteHashMap<T, Boolean> updated = this.map.copyAndPutAll(entries.stream().map(
p -> new AbstractMap.SimpleImmutableEntry<T, Boolean>(p, true)
));
return new CopyOnWriteHashSet<>(updated);
}
/**
* Copy the current set and return a copy that removes <code>entry</code>
* if it exists.
*/
public CopyOnWriteHashSet<T> copyAndRemove(Object entry) {
final CopyOnWriteHashMap<T, Boolean> updated = map.copyAndRemove(entry);
if (updated == map) {
return this;
} else {
return new CopyOnWriteHashSet<>(updated);
}
}
/**
* Copy the current set and return a copy that is the difference of the current
* set and <code>entries</code>.
*/
public CopyOnWriteHashSet<T> copyAndRemoveAll(Collection<?> entries) {
CopyOnWriteHashMap<T, Boolean> updated = this.map.copyAndRemoveAll(entries);
if (updated == map) {
return this;
} else {
return new CopyOnWriteHashSet<>(updated);
}
}
}

View File

@ -1,127 +0,0 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.common.collect;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.test.ESTestCase;
import java.util.HashSet;
import java.util.Set;
public class CopyOnWriteHashSetTests extends ESTestCase {
private static class O {
private final int value, hashCode;
O(int value, int hashCode) {
super();
this.value = value;
this.hashCode = hashCode;
}
@Override
public int hashCode() {
return hashCode;
}
@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof O)) {
return false;
}
return value == ((O) obj).value;
}
}
public void testDuel() {
final int iters = scaledRandomIntBetween(2, 5);
for (int iter = 0; iter < iters; ++iter) {
final int valueBits = randomIntBetween(1, 30);
final int hashBits = randomInt(valueBits);
// we compute the total number of ops based on the bits of the hash
// since the test is much heavier when few bits are used for the hash
final int numOps = randomInt(10 + hashBits * 100);
Set<O> ref = new HashSet<>();
CopyOnWriteHashSet<O> set = new CopyOnWriteHashSet<>();
assertEquals(ref, set);
final int hashBase = randomInt();
for (int i = 0; i < numOps; ++i) {
final int v = randomInt(1 << valueBits);
final int h = (v & ((1 << hashBits) - 1)) ^ hashBase;
O key = new O(v, h);
Set<O> newRef = new HashSet<>(ref);
final CopyOnWriteHashSet<O> newSet;
if (randomBoolean()) {
// ADD
newRef.add(key);
newSet = set.copyAndAdd(key);
} else {
// REMOVE
final boolean modified = newRef.remove(key);
newSet = set.copyAndRemove(key);
if (!modified) {
assertSame(set, newSet);
}
}
assertEquals(ref, set); // make sure that the old copy has not been modified
assertEquals(newRef, newSet);
assertEquals(newSet, newRef);
assertEquals(ref.isEmpty(), set.isEmpty());
assertEquals(newRef.isEmpty(), newSet.isEmpty());
ref = newRef;
set = newSet;
}
assertEquals(ref, CopyOnWriteHashSet.copyOf(ref));
assertEquals(ImmutableSet.of(), CopyOnWriteHashSet.copyOf(ref).copyAndRemoveAll(ref));
}
}
public void testUnsupportedAPIs() {
try {
new CopyOnWriteHashSet<>().add("a");
fail();
} catch (UnsupportedOperationException e) {
// expected
}
try {
new CopyOnWriteHashSet<>().copyAndAdd("a").remove("a");
fail();
} catch (UnsupportedOperationException e) {
// expected
}
}
public void testUnsupportedValues() {
try {
new CopyOnWriteHashSet<>().copyAndAdd(null);
fail();
} catch (IllegalArgumentException e) {
// expected
}
}
}

View File

@ -96,6 +96,7 @@ com.google.common.base.Predicates
com.google.common.base.Strings
com.google.common.base.Throwables
com.google.common.collect.Maps
com.google.common.collect.ForwardingSet
com.google.common.collect.Sets
com.google.common.base.Preconditions
com.google.common.collect.ImmutableSortedSet