CachedIndex implements LookupIndexIF<K, E> { @Override public E get(K key) { Entry entry = data[(key.hashCode() & 0x7FFFFFFF) % data.length]; while (entry != null && !entry.key.equals(key)) entry = entry.next; lookups++; if (entry == null) { E result = fallback.get(key); if (result == null && !nulls) return null; entry = addEntry(new Entry(key, result)); } else { hits++; entry.hits++; } return (E) entry.value; } CachedIndex(LookupIndexIF<K, E> fallback);  CachedIndex(LookupIndexIF<K, E> fallback, boolean nulls);  CachedIndex(LookupIndexIF<K, E> fallback, int max, int size, boolean nulls); @Override E get(K key); @Override E put(K key, E value); @Override E remove(K key); int getKeyNumber(); void writeReport();  }
@Test public void testEmpty() { Assert.assertTrue("found key in empty index", index.get("larsga") == null); }
@Test public void testFindMore() { check("larsga", "Lars Marius Garshol"); check("grove", "Geir Ove Gronmo"); check("tine", "Tine Holst"); check("sylvias", "Sylvia Schwab"); check("pepper", "Steve Pepper"); check("hca", "Hans Christian Alsos"); check("niko", "Niko Schmuck"); check("pam", "Pamela Gennusa"); check("kal", "Kal Ahmed"); check("murray", "Murray Woodman"); lookfor("larsga", "Lars Marius Garshol"); lookfor("grove", "Geir Ove Gronmo"); lookfor("tine", "Tine Holst"); lookfor("sylvias", "Sylvia Schwab"); lookfor("pepper", "Steve Pepper"); lookfor("hca", "Hans Christian Alsos"); lookfor("niko", "Niko Schmuck"); lookfor("pam", "Pamela Gennusa"); lookfor("kal", "Kal Ahmed"); lookfor("murray", "Murray Woodman"); Assert.assertTrue("non-existent key found", index.get("dummy") == null); }
@Test public void testExpand() { index = new CachedIndex(new EmptyIndex(), 1000, 5, true); check("larsga", "Lars Marius Garshol"); check("grove", "Geir Ove Gronmo"); check("tine", "Tine Holst"); check("sylvias", "Sylvia Schwab"); check("pepper", "Steve Pepper"); check("hca", "Hans Christian Alsos"); check("niko", "Niko Schmuck"); check("pam", "Pamela Gennusa"); check("kal", "Kal Ahmed"); check("murray", "Murray Woodman"); lookfor("larsga", "Lars Marius Garshol"); lookfor("grove", "Geir Ove Gronmo"); lookfor("tine", "Tine Holst"); lookfor("sylvias", "Sylvia Schwab"); lookfor("pepper", "Steve Pepper"); lookfor("hca", "Hans Christian Alsos"); lookfor("niko", "Niko Schmuck"); lookfor("pam", "Pamela Gennusa"); lookfor("kal", "Kal Ahmed"); lookfor("murray", "Murray Woodman"); Assert.assertTrue("non-existent key found", index.get("dummy") == null); }