[COLLECTIONS-710] Calling CompositeCollection.size() will cash if the

list contains null element.
This commit is contained in:
Gary Gregory 2019-02-09 17:52:29 -05:00
parent b97da105fb
commit 036bbf34d2
2 changed files with 38 additions and 15 deletions

View File

@ -252,6 +252,9 @@ public class CompositeSet<E> implements Set<E>, Serializable {
*/ */
@Override @Override
public boolean containsAll(final Collection<?> coll) { public boolean containsAll(final Collection<?> coll) {
if (coll == null) {
return false;
}
for (final Object item : coll) { for (final Object item : coll) {
if (contains(item) == false) { if (contains(item) == false) {
return false; return false;
@ -291,7 +294,7 @@ public class CompositeSet<E> implements Set<E>, Serializable {
*/ */
@Override @Override
public boolean removeAll(final Collection<?> coll) { public boolean removeAll(final Collection<?> coll) {
if (coll.size() == 0) { if (CollectionUtils.isEmpty(coll)) {
return false; return false;
} }
boolean changed = false; boolean changed = false;
@ -354,6 +357,7 @@ public class CompositeSet<E> implements Set<E>, Serializable {
* @see SetMutator * @see SetMutator
*/ */
public synchronized void addComposited(final Set<E> set) { public synchronized void addComposited(final Set<E> set) {
if (set != null) {
for (final Set<E> existingSet : getSets()) { for (final Set<E> existingSet : getSets()) {
final Collection<E> intersects = CollectionUtils.intersection(existingSet, set); final Collection<E> intersects = CollectionUtils.intersection(existingSet, set);
if (intersects.size() > 0) { if (intersects.size() > 0) {
@ -370,6 +374,7 @@ public class CompositeSet<E> implements Set<E>, Serializable {
} }
all.add(set); all.add(set);
} }
}
/** /**
* Add these Sets to the list of sets in this composite. * Add these Sets to the list of sets in this composite.
@ -388,10 +393,12 @@ public class CompositeSet<E> implements Set<E>, Serializable {
* @param sets the Sets to be appended to the composite * @param sets the Sets to be appended to the composite
*/ */
public void addComposited(final Set<E>... sets) { public void addComposited(final Set<E>... sets) {
if (sets != null) {
for (final Set<E> set : sets) { for (final Set<E> set : sets) {
addComposited(set); addComposited(set);
} }
} }
}
/** /**
* Removes a set from those being decorated in this composite. * Removes a set from those being decorated in this composite.

View File

@ -64,6 +64,18 @@ public class CompositeSetTest<E> extends AbstractSetTest<E> {
assertTrue(set.contains("1")); assertTrue(set.contains("1"));
} }
@SuppressWarnings("unchecked")
public void testContainsAll() {
final CompositeSet<E> set = new CompositeSet<>(new Set[]{ buildOne(), buildTwo() });
assertFalse(set.containsAll(null));
}
@SuppressWarnings("unchecked")
public void testRemoveAll() {
final CompositeSet<E> set = new CompositeSet<>(new Set[]{ buildOne(), buildTwo() });
assertFalse(set.removeAll(null));
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void testRemoveUnderlying() { public void testRemoveUnderlying() {
final Set<E> one = buildOne(); final Set<E> one = buildOne();
@ -132,6 +144,10 @@ public class CompositeSetTest<E> extends AbstractSetTest<E> {
final Set<E> two = buildTwo(); final Set<E> two = buildTwo();
final CompositeSet<E> set = new CompositeSet<>(); final CompositeSet<E> set = new CompositeSet<>();
set.addComposited(one, two); set.addComposited(one, two);
set.addComposited((Set<E>) null);
set.addComposited((Set<E>[]) null);
set.addComposited(null, null);
set.addComposited(null, null, null);
final CompositeSet<E> set2 = new CompositeSet<>(buildOne()); final CompositeSet<E> set2 = new CompositeSet<>(buildOne());
set2.addComposited(buildTwo()); set2.addComposited(buildTwo());
assertTrue(set.equals(set2)); assertTrue(set.equals(set2));