/** * <p> * Creates a new cache based on the given {@link Retriever} and with given maximum size. * </p> * * @param retriever * object which can retrieve values for keys * @param maxEntries * maximum number of entries the cache will store before evicting some */ public Cache(Retriever<? super K,? extends V> retriever, int maxEntries) { Preconditions.checkArgument(retriever != null, "retriever is null"); Preconditions.checkArgument(maxEntries >= 1, "maxEntries must be at least 1"); cache = new FastMap<K, V>(11, maxEntries); this.retriever = retriever; }
/** * <p> * Creates a new cache based on the given {@link Retriever} and with given maximum size. * </p> * * @param retriever * object which can retrieve values for keys * @param maxEntries * maximum number of entries the cache will store before evicting some */ public Cache(Retriever<? super K,? extends V> retriever, int maxEntries) { Preconditions.checkArgument(retriever != null, "retriever is null"); Preconditions.checkArgument(maxEntries >= 1, "maxEntries must be at least 1"); cache = new FastMap<K, V>(11, maxEntries); this.retriever = retriever; }
/** * <p> * Creates a new cache based on the given {@link Retriever} and with given maximum size. * </p> * * @param retriever * object which can retrieve values for keys * @param maxEntries * maximum number of entries the cache will store before evicting some */ public Cache(Retriever<? super K,? extends V> retriever, int maxEntries) { Preconditions.checkArgument(retriever != null, "retriever is null"); Preconditions.checkArgument(maxEntries >= 1, "maxEntries must be at least 1"); cache = new FastMap<>(11, maxEntries); this.retriever = retriever; }
@Test(expected = NullPointerException.class) public void testNull2() { Map<String, String> map = new FastMap<String, String>(); map.put("foo", null); }
@Test(expected = NullPointerException.class) public void testNull1() { Map<String, String> map = new FastMap<String, String>(); assertNull(map.get(null)); map.put(null, "bar"); }
@Test public void testClear() { Map<String, String> map = new FastMap<String, String>(); map.put("foo", "bar"); map.clear(); assertEquals(0, map.size()); assertTrue(map.isEmpty()); assertNull(map.get("foo")); }
@Test public void testGrow() { Map<String, String> map = new FastMap<String, String>(1, FastMap.NO_MAX_SIZE); map.put("foo", "bar"); map.put("baz", "bang"); assertEquals("bar", map.get("foo")); assertEquals("bang", map.get("baz")); }
@Test public void testPutAndGet() { Map<String, String> map = new FastMap<String, String>(); assertNull(map.get("foo")); map.put("foo", "bar"); assertEquals("bar", map.get("foo")); }
@Test public void testRemove() { Map<String, String> map = new FastMap<String, String>(); map.put("foo", "bar"); map.remove("foo"); assertEquals(0, map.size()); assertTrue(map.isEmpty()); assertNull(map.get("foo")); }
@Test public void testMaxSize() { Map<String, String> map = new FastMap<String, String>(1, 1); map.put("foo", "bar"); assertEquals(1, map.size()); map.put("baz", "bang"); assertEquals(1, map.size()); assertNull(map.get("foo")); map.put("baz", "buzz"); assertEquals(1, map.size()); assertEquals("buzz", map.get("baz")); }
@Test public void testSizeEmpty() { Map<String, String> map = new FastMap<String, String>(); assertEquals(0, map.size()); assertTrue(map.isEmpty()); map.put("foo", "bar"); assertEquals(1, map.size()); assertFalse(map.isEmpty()); map.remove("foo"); assertEquals(0, map.size()); assertTrue(map.isEmpty()); }
@Test public void testVersusHashMap() { Map<Integer, String> actual = new FastMap<Integer, String>(1, 1000000); Map<Integer, String> expected = Maps.newHashMapWithExpectedSize(1000000); Random r = RandomUtils.getRandom(); for (int i = 0; i < 1000000; i++) { double d = r.nextDouble(); Integer key = r.nextInt(100); if (d < 0.4) { assertEquals(expected.get(key), actual.get(key)); } else { if (d < 0.7) { assertEquals(expected.put(key, "foo"), actual.put(key, "foo")); } else { assertEquals(expected.remove(key), actual.remove(key)); } assertEquals(expected.size(), actual.size()); assertEquals(expected.isEmpty(), actual.isEmpty()); } } }
private static FastMap<String, String> buildTestFastMap() { FastMap<String, String> map = new FastMap<String, String>(); map.put("foo", "bar"); map.put("baz", "bang"); map.put("alpha", "beta"); return map; }