IntegerGenotype extends ArrayList<Integer> implements ListGenotype<Integer> { public void init(Random random, int n) { try { getLowerBound(n - 1); getUpperBound(n - 1); } catch (IndexOutOfBoundsException outOfBoundException) { throw new IllegalArgumentException( "Cannot initialize a genotype with " + n + " entries with the specified bounds", outOfBoundException); } for (int i = 0; i < n; i++) { int lo = getLowerBound(i); int hi = getUpperBound(i); int value = lo + random.nextInt(hi - lo + 1); if (i >= size()) { add(value); } else { set(i, value); } } } IntegerGenotype(int lowerBound, int upperBound);  IntegerGenotype(Bounds<Integer> bounds); int getLowerBound(int index); int getUpperBound(int index); @SuppressWarnings("unchecked") @Override G newInstance(); void init(Random random, int n);  }
@Test(expected = IllegalArgumentException.class) public void testWrongBoundSettingList() { Random rand = new Random(); List<Integer> lowerBounds = new ArrayList<>(); lowerBounds.add(1); lowerBounds.add(2); lowerBounds.add(3); List<Integer> upperBounds = new ArrayList<>(); upperBounds.add(2); upperBounds.add(3); upperBounds.add(4); IntegerBounds bounds = new IntegerBounds(lowerBounds, upperBounds); IntegerGenotype listBoundGeno = new IntegerGenotype(bounds); assertTrue(listBoundGeno.isEmpty()); listBoundGeno.init(rand, 3); assertEquals(3, listBoundGeno.size()); listBoundGeno.init(rand, 4); }
@Test(expected = IllegalArgumentException.class) public void testWrongBoundSettingArray() { Random rand = new Random(); int[] lowerBounds = { 1, 2, 3 }; int[] upperBounds = { 2, 3, 4 }; IntegerBounds bounds = new IntegerBounds(lowerBounds, upperBounds); IntegerGenotype listBoundGeno = new IntegerGenotype(bounds); assertTrue(listBoundGeno.isEmpty()); listBoundGeno.init(rand, 3); assertEquals(3, listBoundGeno.size()); listBoundGeno.init(rand, 4); }