public void add(T element) {
    int newIndex = (int) (++writeIndex) % size;
    elements[newIndex] = element;
    int nextIndex = (newIndex + 1) % size;
    if (elements[nextIndex] != null) {
      readIndex = nextIndex;
    }
    if (++length > size) {
      length = size;
    }
  }
@Test
  public void testAdd() {
    CircularBuffer<Integer> cb = new CircularBuffer<>(2);

    cb.add(1);
    assertEquals(1, cb.getLength());
    final List<Integer> elements = new ArrayList<>();
    cb.forEach(
        new Function<Integer, Boolean>() {
          @Override
          public Boolean apply(Integer value) {
            elements.add(value);
            return true;
          }
        });
    assertEquals(0, cb.getLength());
    assertEquals(Arrays.asList(1), elements);
  }