ByteArrayFilter { public boolean containsAndAdd(byte[] id) { HashCode code = HASH_FUNC.hashBytes(id); int index = Math.abs(code.asInt()) & sizeMask; byte[] oldId = array.getAndSet(index, id); return Arrays.equals(id, oldId); } ByteArrayFilter(int size); boolean containsAndAdd(byte[] id); int getSize();  }
@Test public void testTheBasics() { ByteArrayFilter filter = new ByteArrayFilter(2); byte[] twentyNineId = new byte[]{27, 28, 29}; byte[] thirtyId = new byte[]{27, 28, 30}; byte[] thirtyThreeId = new byte[]{27, 28, 33}; assertFalse("nothing should be contained at all", filter.containsAndAdd(twentyNineId)); assertTrue("now it should", filter.containsAndAdd(twentyNineId)); assertFalse("false unless the hash collides", filter.containsAndAdd(thirtyId)); assertTrue("original should still return true", filter.containsAndAdd(twentyNineId)); assertTrue("new array should still return true", filter.containsAndAdd(thirtyId)); assertFalse("colliding array returns false", filter.containsAndAdd(thirtyThreeId)); assertTrue( "colliding array returns true in second call", filter.containsAndAdd(thirtyThreeId)); assertFalse("original colliding array returns false", filter.containsAndAdd(thirtyId)); assertTrue("original colliding array returns true", filter.containsAndAdd(thirtyId)); assertFalse("colliding array returns false", filter.containsAndAdd(thirtyThreeId)); }