BoundedArchive extends AbstractArchive { @Override public boolean addCheckedIndividuals(Collection<? extends Individual> c) { Set<Individual> set = new LinkedHashSet<>(c); set.removeAll(this); if (individuals.size() + set.size() > capacity) { throw new IndexOutOfBoundsException(); } return super.addCheckedIndividuals(c); } @Inject  BoundedArchive(int capacity); void setCapacity(int capacity); int getCapacity(); @Override boolean addCheckedIndividual(Individual individual); @Override boolean addCheckedIndividuals(Collection<? extends Individual> c);  }
@Test public void addCheckedIndividuals() { Individual individual1 = mock(Individual.class); BoundedArchive archive = new BoundedArchive(1) { @Override protected boolean updateWithNondominated(Collection<Individual> candidates) { individuals.addAll(candidates); return true; } }; archive.addCheckedIndividuals(Collections.singleton(individual1)); Assert.assertTrue(archive.contains(individual1)); Assert.assertEquals(1, archive.size()); }
@Test(expected = IndexOutOfBoundsException.class) public void addCheckedIndividualsDespiteArchiveFull() { Individual individual1 = mock(Individual.class); Individual individual2 = mock(Individual.class); BoundedArchive archive = new BoundedArchive(1) { @Override protected boolean updateWithNondominated(Collection<Individual> candidates) { individuals.addAll(candidates); return true; } }; archive.addCheckedIndividuals(Collections.singleton(individual1)); Assert.assertTrue(archive.contains(individual1)); Assert.assertEquals(1, archive.size()); archive.addCheckedIndividuals(Collections.singleton(individual2)); }