diff --git a/opt4j-core/src/main/java/org/opt4j/core/IndividualSet.java b/opt4j-core/src/main/java/org/opt4j/core/IndividualSet.java index 6c1eca97..e0f07f9c 100644 --- a/opt4j-core/src/main/java/org/opt4j/core/IndividualSet.java +++ b/opt4j-core/src/main/java/org/opt4j/core/IndividualSet.java @@ -1,27 +1,23 @@ /******************************************************************************* * Copyright (c) 2014 Opt4J * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated + * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to the following conditions: * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. + * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the + * Software. * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE + * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR + * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *******************************************************************************/ package org.opt4j.core; +import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.LinkedHashSet; @@ -29,8 +25,8 @@ import java.util.concurrent.CopyOnWriteArraySet; /** - * The {@link IndividualSet} is a {@link Set} of {@link Individual}s. It allows - * to add and remove listeners, see {@link IndividualSetListener}. + * The {@link IndividualSet} is a {@link Set} of {@link Individual}s. It allows to add and remove listeners, see + * {@link IndividualSetListener}. * * @see org.opt4j.core.optimizer.Archive * @see org.opt4j.core.optimizer.Population @@ -154,13 +150,7 @@ public void clear() { public boolean addAll(Collection c) { boolean res = false; for (Individual individual : c) { - boolean b = individuals.add(individual); - if (b) { - for (IndividualSetListener listener : listeners) { - listener.individualAdded(this, individual); - } - } - res |= b; + res |= add(individual); } return res; } @@ -173,17 +163,7 @@ public boolean addAll(Collection c) { * @return true if at least one individual was added */ public boolean addAll(Individual... c) { - boolean res = false; - for (Individual individual : c) { - boolean b = individuals.add(individual); - if (b) { - for (IndividualSetListener listener : listeners) { - listener.individualAdded(this, individual); - } - } - res |= b; - } - return res; + return addAll(Arrays.asList(c)); } /* diff --git a/opt4j-core/src/test/java/org/opt4j/core/IndividiualSetTest.java b/opt4j-core/src/test/java/org/opt4j/core/IndividiualSetTest.java new file mode 100644 index 00000000..a449e3a7 --- /dev/null +++ b/opt4j-core/src/test/java/org/opt4j/core/IndividiualSetTest.java @@ -0,0 +1,257 @@ +package org.opt4j.core; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.Set; + +import org.junit.Assert; +import org.junit.Test; + +public class IndividiualSetTest { + + @Test + public void addTest() { + IndividualSet set = new IndividualSet(); + Individual individual = new Individual(); + + Assert.assertTrue(set.add(individual)); + + Assert.assertTrue(set.contains(individual)); + } + + private class AddIndividualSetListener implements IndividualSetListener { + IndividualSet collection; + Individual i; + + @Override + public void individualRemoved(IndividualSet collection, Individual i) { + Assert.assertTrue(false); // shall not happen in this testcase + } + + @Override + public void individualAdded(IndividualSet collection, Individual i) { + Assert.assertNull(this.collection); + Assert.assertNull(this.i); + this.collection = collection; + this.i = i; + } + }; + + @Test + public void addWithListenerTest() { + final IndividualSet set = new IndividualSet(); + final Individual individual = new Individual(); + + AddIndividualSetListener l = new AddIndividualSetListener(); + set.addListener(l); + + Assert.assertTrue(set.add(individual)); + + Assert.assertEquals(set, l.collection); + Assert.assertEquals(individual, l.i); + } + + public void iteratorTest() { + IndividualSet set = new IndividualSet(); + Individual individual = new Individual(); + set.add(individual); + + Iterator iterator = set.iterator(); + Assert.assertTrue(iterator.hasNext()); + Assert.assertEquals(individual, iterator.next()); + } + + public void iteratorRemoveTest() { + final IndividualSet set = new IndividualSet(); + final Individual individual = new Individual(); + set.add(individual); + + IndividualSetListener l = new IndividualSetListener() { + @Override + public void individualRemoved(IndividualSet collection, Individual i) { + Assert.assertEquals(set, collection); + Assert.assertEquals(individual, i); + Assert.assertTrue(collection.isEmpty()); + } + + @Override + public void individualAdded(IndividualSet collection, Individual individual) { + } + }; + set.addListener(l); + + Iterator iterator = set.iterator(); + Assert.assertTrue(iterator.hasNext()); + Assert.assertEquals(individual, iterator.next()); + iterator.remove(); + } + + @Test + public void sizeTest() { + IndividualSet set = new IndividualSet(); + Individual individual = new Individual(); + + Assert.assertEquals(0, set.size()); + set.add(individual); + Assert.assertEquals(1, set.size()); + } + + @Test + public void addListenerTest() { + IndividualSet set = new IndividualSet(); + IndividualSetListener l = new IndividualSetListener() { + @Override + public void individualRemoved(IndividualSet collection, Individual individual) { + } + + @Override + public void individualAdded(IndividualSet collection, Individual individual) { + } + }; + set.addListener(l); + + Assert.assertTrue(set.listeners.contains(l)); + } + + @Test + public void removeListenerTest() { + IndividualSet set = new IndividualSet(); + IndividualSetListener l = new IndividualSetListener() { + @Override + public void individualRemoved(IndividualSet collection, Individual individual) { + } + + @Override + public void individualAdded(IndividualSet collection, Individual individual) { + } + }; + + set.addListener(l); + Assert.assertTrue(set.listeners.contains(l)); + + set.removeListener(l); + Assert.assertFalse(set.listeners.contains(l)); + Assert.assertTrue(set.listeners.isEmpty()); + } + + @Test + public void addAllTest() { + IndividualSet set = new IndividualSet(); + Individual i1 = new Individual(); + Individual i2 = new Individual(); + Individual i3 = new Individual(); + + Assert.assertTrue(set.addAll(i1, i2)); + Assert.assertTrue(set.addAll(i3)); + Assert.assertFalse(set.addAll()); + Assert.assertFalse(set.addAll(i1, i2, i3)); + } + + @Test + public void containsAllTest() { + IndividualSet set = new IndividualSet(); + Individual i1 = new Individual(); + Individual i2 = new Individual(); + Individual i3 = new Individual(); + + Set is = new HashSet(); + is.add(i1); + is.add(i2); + + Assert.assertTrue(set.addAll(is)); + Assert.assertTrue(set.containsAll(is)); + + is.add(i3); + Assert.assertFalse(set.containsAll(is)); + } + + @Test + public void isEmptyTest() { + IndividualSet set = new IndividualSet(); + Individual i1 = new Individual(); + + Assert.assertTrue(set.isEmpty()); + set.add(i1); + Assert.assertFalse(set.isEmpty()); + } + + private class RemoveIndividualSetListener implements IndividualSetListener { + IndividualSet collection; + Individual individual; + + @Override + public void individualRemoved(IndividualSet collection, Individual individual) { + this.collection = collection; + this.individual = individual; + } + + @Override + public void individualAdded(IndividualSet collection, Individual individual) { + } + }; + + @Test + public void removeTest() { + final IndividualSet set = new IndividualSet(); + final Individual i = new Individual(); + set.add(i); + + RemoveIndividualSetListener l = new RemoveIndividualSetListener(); + set.addListener(l); + + Assert.assertTrue(set.remove(i)); + Assert.assertEquals(set, l.collection); + Assert.assertEquals(i, l.individual); + Assert.assertTrue(set.isEmpty()); + + Assert.assertFalse(set.remove(i)); + } + + @Test + public void removeAllTest() { + final IndividualSet set = new IndividualSet(); + final Individual i1 = new Individual(); + final Individual i2 = new Individual(); + set.add(i1); + set.add(i2); + + Set rem = new HashSet(); + rem.add(i1); + rem.add(i2); + + Assert.assertTrue(set.removeAll(rem)); + Assert.assertTrue(set.isEmpty()); + + Assert.assertFalse(set.removeAll(rem)); + } + + @Test + public void retainAllTest() { + final IndividualSet set = new IndividualSet(); + final Individual i1 = new Individual(); + final Individual i2 = new Individual(); + set.add(i1); + set.add(i2); + + Set rem = new HashSet(); + rem.add(i2); + + Assert.assertTrue(set.retainAll(rem)); + Assert.assertTrue(set.contains(i2)); + Assert.assertEquals(1, set.size()); + + Assert.assertFalse(set.retainAll(rem)); + } + + @Test + public void toArrayTest() { + IndividualSet set = new IndividualSet(); + Individual individual = new Individual(); + Assert.assertTrue(set.add(individual)); + + Individual[] array = { individual }; + set.toArray(array); + Assert.assertEquals(1, array.length); + Assert.assertEquals(individual, array[0]); + } +}