Figure implements Iterable<Shape> { public Figure add(final Shape shape) { final Optional<String> name = shape.name(); Preconditions.checkArgument( !(name.isPresent() && this.shaps.containsKey(name.get())), "A shape with name [%s] already exists in the figure", name ); if (shape.anonymous()) { this.anonymous.add(shape); } else { this.shaps.put(name.get(), shape); } return this; }  Figure add(final Shape shape); Figure add(final Renderable shape); Figure add(final Renderable shape, final String name); Optional<Shape> shape(final String name); @Override Iterator<Shape> iterator();  }
@Test public void addsAnonymousShape() { final Shape shape = new Shape(Mockito.mock(Renderable.class)); MatcherAssert.assertThat( new Figure().add(shape), Matchers.contains(shape) ); }
@Test public void addsNamedShape() { final Shape shape = new Shape(Mockito.mock(Renderable.class), "name"); MatcherAssert.assertThat( new Figure().add(shape), Matchers.contains(shape) ); }
@Test public void errorsWhenShapeAlreadyExists() { this.thrown.expect(IllegalArgumentException.class); final String name = "twice"; new Figure().add( new Shape(Mockito.mock(Renderable.class), name) ).add(new Shape(Mockito.mock(Renderable.class), name)); }