/** * Get a value from the registry - without casting {@code NotFoundException}. * * @param <T> desired type of value. * @param key the key whose associated value is to be returned. * @return a value or {@code null} if it does not exist */ public static <T> T getIfExists(String key) { @SuppressWarnings("unchecked") T obj = (T) sp.rdp(key); return obj; }
@Override public boolean existAny (K[] keys) { for (K key : keys) { if (rdp(key) != null) return true; } return false; }
@Override public synchronized void nrd (Object key) { while (rdp (key) != null) { try { this.wait (NRD_RESOLUTION); } catch (InterruptedException ignored) { } } }
@Override public synchronized V rd (Object key) { Object obj; while ((obj = rdp (key)) == null) { try { this.wait (); } catch (InterruptedException e) { } } return (V) obj; }
@Override public synchronized V rd (Object key, long timeout) { Object obj; long now = System.currentTimeMillis(); long end = now + timeout; while ((obj = rdp (key)) == null && (now = System.currentTimeMillis()) < end) { try { this.wait (end - now); } catch (InterruptedException e) { } } return (V) obj; }
@Override public synchronized V nrd (Object key, long timeout) { Object obj; long now = System.currentTimeMillis(); long end = now + timeout; while ((obj = rdp (key)) != null && (now = System.currentTimeMillis()) < end) { try { this.wait (Math.min(NRD_RESOLUTION, end - now)); } catch (InterruptedException ignored) { } } return (V) obj; }
/** * @return a copy of the NameRegistrar's entries as a Map */ public static Map<String,Object> getAsMap() { Map<String,Object> map = new HashMap<String,Object>(); for (String k : sp.getKeySet()) { Object v = sp.rdp(k); if (v != null) map.put(k,v); } return map; }
/** * Get a value from the registry. * * @param <T> desired type of entry value. * @param key the key whose associated value is to be returned. * @return a value * @throws NotFoundException if key not present in registrar */ public static <T> T get(String key) throws NotFoundException { @SuppressWarnings("unchecked") T obj = (T) sp.rdp(key); if (obj == null) { throw new NotFoundException(key); } return obj; }
public void dump(PrintStream p, String indent, boolean detail) { String inner = indent + " "; p.println(indent + "name-registrar:"); for (String key : sp.getKeySet()) { Object obj = sp.rdp(key); String objectClassName = obj == null ? "<NULL>" : obj.getClass().getName(); p.println(inner + key + ": " + objectClassName); if (detail && obj instanceof Loggeable) { ((Loggeable) obj).dump(p, inner + " "); } } } }
private void gc (int generation) { Set<K> exps; synchronized (this) { exps = expirables[generation]; expirables[generation] = new HashSet<K>(); } for (K k : exps) { if (rdp(k) != null) { synchronized (this) { expirables[generation].add(k); } } Thread.yield (); } if (sl != null) { synchronized (this) { if (sl != null && sl.isEmpty()) sl = null; } } }
@Test public void testOutRdpInpRdp() throws Exception { Object o = Boolean.TRUE; String k = "testOutRdpInpRdp_Key"; sp.out(k, o); assertTrue(o.equals(sp.rdp(k))); assertTrue(o.equals(sp.rd(k))); assertTrue(o.equals(sp.rd(k, 1000))); assertTrue(o.equals(sp.inp(k))); assertNull(sp.rdp(k)); assertNull(sp.rd(k, 100)); }
@Test public void testSimpleOut() { sp.out("testSimpleOut_Key", "ABC"); sp.out("testSimpleOut_Key", "XYZ"); assertEquals("ABC", sp.rdp("testSimpleOut_Key")); assertEquals("ABC", sp.inp("testSimpleOut_Key")); assertEquals("XYZ", sp.rdp("testSimpleOut_Key")); assertEquals("XYZ", sp.inp("testSimpleOut_Key")); assertNull(sp.rdp("Test")); assertNull(sp.inp("Test")); }
@Test public void testMD5Template() throws Exception { final String KEY = "TestMD5Template_Key"; sp.out(KEY, "123"); sp.out(KEY, "456"); sp.out(KEY, "789"); Template tmpl = new MD5Template(KEY, "456"); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.rdp(tmpl), "456"); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.inp(tmpl), "456"); assertNull(sp.rdp(tmpl)); assertNull(sp.inp(tmpl)); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.inp(KEY), "123"); assertEquals(sp.rdp(KEY), "789"); assertEquals(sp.inp(KEY), "789"); assertNull(sp.rdp(KEY)); assertNull(sp.inp(KEY)); }
@Test public void testTemplate() throws Exception { final String KEY = "TestTemplate_Key"; sp.out(KEY, "123"); sp.out(KEY, "456"); sp.out(KEY, "789"); Template tmpl = new ObjectTemplate(KEY, "456"); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.rdp(tmpl), "456"); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.inp(tmpl), "456"); assertNull(sp.rdp(tmpl)); assertNull(sp.inp(tmpl)); assertEquals(sp.rdp(KEY), "123"); assertEquals(sp.inp(KEY), "123"); assertEquals(sp.rdp(KEY), "789"); assertEquals(sp.inp(KEY), "789"); assertNull(sp.rdp(KEY)); assertNull(sp.inp(KEY)); }
@Test public void testSingleKeyLoad() throws Exception { String s = "The quick brown fox jumped over the lazy dog"; String k = "testSingleKeyLoad_SingleKey"; Profiler prof = new Profiler(); for (int i = 0; i < COUNT; i++) { sp.out(k, s, 60000); } // prof.dump (System.err, "SingleKeyLoad out >"); prof = new Profiler(); for (int i = 0; i < COUNT; i++) { assertTrue(s.equals(sp.in(k))); } // prof.dump (System.err, "SingleKeyLoad in >"); assertNull(sp.rdp(k)); }
@Test public void testNullEntry() { try { sp.out("testNull", null); fail("NullPointerException should have been called"); } catch (NullPointerException e) { assertNull("Verify null entry (rdp)", sp.rdp("testNull")); assertNull("Verify null entry (inp)", sp.inp("testNull")); } }
@Test public void testGC() throws Exception { sp.out("testGC_Key", "ABC", 50); sp.out("testGC_Key", "XYZ", 50); assertEquals("ABC", sp.rdp("testGC_Key")); try { Thread.sleep(60); } catch (InterruptedException e) { } assertEquals("testGC_Key", sp.getKeysAsString()); sp.gc(); assertEquals("", sp.getKeysAsString()); sp.gc(); }
@Test public void testPush() { sp.push("PUSH", "ONE"); sp.push("PUSH", "TWO"); sp.push("PUSH", "THREE"); sp.out("PUSH", "FOUR"); assertEquals("THREE", sp.rdp("PUSH")); assertEquals("THREE", sp.inp("PUSH")); assertEquals("TWO", sp.inp("PUSH")); assertEquals("ONE", sp.inp("PUSH")); assertEquals("FOUR", sp.inp("PUSH")); assertNull(sp.rdp("PUSH")); }