RingBuffer { synchronized Object[] snapshot() { if (_snapshot != null) { return _snapshot; } final Object[] result = new Object[_size]; if (_size < _buffer.length) { System.arraycopy(_buffer, 0, result, 0, _size); } else { System.arraycopy( _buffer, _cursor + 1, result, 0, _buffer.length - _cursor - 1 ); System.arraycopy( _buffer, 0, result, _buffer.length - _cursor - 1, _cursor + 1 ); } return _snapshot = result; } RingBuffer(final int size);   }
@Test(dataProvider = "maxSizes") public void snapshot(final int max) { final RingBuffer ints = new RingBuffer(max); for (int i = 0; i < 33; ++i) { ints.add(i); final Object[] snapshot = ints.snapshot(); final int size = Math.min(i + 1, max); Assert.assertEquals(snapshot.length, size); final Object[] expected = new Object[size]; for (int j = 0; j < size; ++j) { expected[size - j - 1] = i - j; } Assert.assertEquals(snapshot, expected); } }