obj = iter.next(); if (obj instanceof Expirable) { obj = ((Expirable) obj).getValue(); if (obj == null) { iter.remove();
@Override public void push (K key, V value, long timeout) { if (key == null || value == null) throw new NullPointerException ("key=" + key + ", value=" + value); Object v = value; if (timeout > 0) { v = new Expirable (value, System.currentTimeMillis() + timeout); } synchronized (this) { List l = getList(key); boolean wasEmpty = l.isEmpty(); l.add (0, v); if (wasEmpty) this.notifyAll (); if (timeout > 0) { registerExpirable(key, timeout); } } if (sl != null) notifyListeners(key, value); }
@Override public String toString() { return getClass().getName() + "@" + Integer.toHexString(hashCode()) + ",value=" + value.toString() + ",expired=" + isExpired (); }
private Object getHead (Object key, boolean remove) { Object obj = null; List l = (List) entries.get (key); boolean wasExpirable = false; while (obj == null && l != null && l.size() > 0) { obj = l.get(0); if (obj instanceof Expirable) { obj = ((Expirable) obj).getValue(); wasExpirable = true; } if (obj == null) { l.remove (0); if (l.isEmpty()) { entries.remove (key); } } } if (l != null) { if (remove && obj != null) l.remove (0); if (l.isEmpty()) { entries.remove (key); if (wasExpirable) unregisterExpirable(key); } } return obj; }
public void run() { Object[] listeners = null; synchronized (this) { if (sl == null) return; List l = (List) sl.entries.get (key); if (l != null) listeners = l.toArray(); } if (listeners != null) { for (int i=0; i<listeners.length; i++) { Object o = listeners[i]; if (o instanceof TSpace.Expirable) { o = ((TSpace.Expirable)o).getValue(); } if (o instanceof SpaceListener) { ((SpaceListener) o).notify(key, value); } } } } }.start();
@Override public void put (K key, V value, long timeout) { if (key == null || value == null) throw new NullPointerException ("key=" + key + ", value=" + value); Object v = value; if (timeout > 0) { v = new Expirable (value, System.currentTimeMillis() + timeout); } synchronized (this) { List l = new LinkedList(); l.add (v); entries.put (key, l); this.notifyAll (); if (timeout > 0) { registerExpirable(key, timeout); } } if (sl != null) notifyListeners(key, value); }
@Override public void out (K key, V value, long timeout) { if (key == null || value == null) throw new NullPointerException ("key=" + key + ", value=" + value); Object v = value; if (timeout > 0) { v = new Expirable (value, System.currentTimeMillis() + timeout); } synchronized (this) { List l = getList(key); l.add(v); if (l.size() == 1) this.notifyAll (); if (timeout > 0) { registerExpirable(key, timeout); } } if (sl != null) notifyListeners(key, value); }
public void notifyListeners (Object key, Object value) { Object[] listeners = null; synchronized (this) { if (sl == null) return; List l = (List) sl.entries.get (key); if (l != null) listeners = l.toArray(); } if (listeners != null) { for (Object listener : listeners) { Object o = listener; if (o instanceof Expirable) o = ((Expirable) o).getValue(); if (o instanceof SpaceListener) ((SpaceListener) o).notify(key, value); } } }
public Object getValue() { return isExpired() ? null : value; }
@Test public void testExpirableIsExpired1() throws Throwable { boolean result = new TSpace.Expirable(new Object(), 100L).isExpired(); assertTrue("result", result); }
@Test public void testExpirableIsExpired() throws Throwable { boolean result = new TSpace.Expirable("", 9184833384926L).isExpired(); assertFalse("result", result); }
@Test public void testExpirableGetValue1() throws Throwable { Object result = new TSpace.Expirable(null, 9184833384926L).getValue(); assertNull("result", result); }
@Test public void testExpirableGetValue() throws Throwable { String result = (String) new TSpace.Expirable("", 9184833384926L).getValue(); assertEquals("result", "", result); }
@Test public void testExpirableToStringThrowsNullPointerException() throws Throwable { try { new TSpace.Expirable(null, 100L).toString(); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { assertNull("ex.getMessage()", ex.getMessage()); } }
@Test public void testExpirableGetValue2() throws Throwable { Object result = new TSpace.Expirable(new Object(), 100L).getValue(); assertNull("result", result); }
@Test public void testExpirableCompareTo2() throws Throwable { int result = new TSpace.Expirable(null, 0L).compareTo(new TSpace.Expirable(new Object(), 1L)); assertEquals("result", -1, result); }
@Test public void testExpirableCompareTo() throws Throwable { int result = new TSpace.Expirable(Integer.valueOf(0), 1L).compareTo(new TSpace.Expirable(new Object(), 0L)); assertEquals("result", 1, result); }
@Test public void testExpirableToString() throws Throwable { new TSpace.Expirable(";\"i", 100L).toString(); assertTrue("Test completed without Exception", true); }
@Test public void testExpirableConstructor() throws Throwable { Object value = new Object(); TSpace.Expirable expirable = new TSpace.Expirable(value, 100L); assertEquals("expirable.expires", 100L, expirable.expires); assertSame("expirable.value", value, expirable.value); }
@Test public void testExpirableCompareToThrowsNullPointerException() throws Throwable { try { new TSpace.Expirable(new Object(), 100L).compareTo(null); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { assertNull("ex.getMessage()", ex.getMessage()); } }