@SuppressWarnings("unchecked") @Test void testGet() { LongSupplier currentTimeSupplier = Mockito.mock(LongSupplier.class); Mockito.when(currentTimeSupplier.getAsLong()) .thenReturn(0L, toMillis(5), toMillis(15), toMillis(25), toMillis(30)); Supplier<String> refreshFunction = Mockito.mock(Supplier.class); Mockito.when(refreshFunction.get()) .thenReturn("a", "b"); CachedObject<String> cachedName = new CachedObject<>(10, currentTimeSupplier); assertEquals("a", cachedName.get(refreshFunction)); assertEquals("a", cachedName.get(refreshFunction)); assertEquals("b", cachedName.get(refreshFunction)); assertEquals("b", cachedName.get(refreshFunction)); assertEquals("b", cachedName.get(refreshFunction)); }
public synchronized T get(Supplier<T> refreshFunction) { long currentTime = currentTimeSupplier.getAsLong(); long millisecondsSinceLastRefresh = currentTime - lastRefreshTime; long secondsSinceLastRefresh = TimeUnit.MILLISECONDS.toSeconds(millisecondsSinceLastRefresh); if (object == null || secondsSinceLastRefresh > expirationTimeInSeconds) { this.object = refreshFunction.get(); this.lastRefreshTime = currentTimeSupplier.getAsLong(); } return object; }
CachedObject { public synchronized T get(Supplier<T> refreshFunction) { long currentTime = currentTimeSupplier.getAsLong(); long millisecondsSinceLastRefresh = currentTime - lastRefreshTime; long secondsSinceLastRefresh = TimeUnit.MILLISECONDS.toSeconds(millisecondsSinceLastRefresh); if (object == null || secondsSinceLastRefresh > expirationTimeInSeconds) { this.object = refreshFunction.get(); this.lastRefreshTime = currentTimeSupplier.getAsLong(); } return object; } }
CachedObject { public synchronized T get(Supplier<T> refreshFunction) { long currentTime = currentTimeSupplier.getAsLong(); long millisecondsSinceLastRefresh = currentTime - lastRefreshTime; long secondsSinceLastRefresh = TimeUnit.MILLISECONDS.toSeconds(millisecondsSinceLastRefresh); if (object == null || secondsSinceLastRefresh > expirationTimeInSeconds) { this.object = refreshFunction.get(); this.lastRefreshTime = currentTimeSupplier.getAsLong(); } return object; } CachedObject(long expirationTimeInSeconds);  CachedObject(long expirationTimeInSeconds, LongSupplier currentTimeSupplier); }
CachedObject { public synchronized T get(Supplier<T> refreshFunction) { long currentTime = currentTimeSupplier.getAsLong(); long millisecondsSinceLastRefresh = currentTime - lastRefreshTime; long secondsSinceLastRefresh = TimeUnit.MILLISECONDS.toSeconds(millisecondsSinceLastRefresh); if (object == null || secondsSinceLastRefresh > expirationTimeInSeconds) { this.object = refreshFunction.get(); this.lastRefreshTime = currentTimeSupplier.getAsLong(); } return object; } CachedObject(long expirationTimeInSeconds);  CachedObject(long expirationTimeInSeconds, LongSupplier currentTimeSupplier); synchronized T get(Supplier<T> refreshFunction); synchronized T forceRefresh(Supplier<T> refreshFunction); }
CachedObject { public synchronized T get(Supplier<T> refreshFunction) { long currentTime = currentTimeSupplier.getAsLong(); long millisecondsSinceLastRefresh = currentTime - lastRefreshTime; long secondsSinceLastRefresh = TimeUnit.MILLISECONDS.toSeconds(millisecondsSinceLastRefresh); if (object == null || secondsSinceLastRefresh > expirationTimeInSeconds) { this.object = refreshFunction.get(); this.lastRefreshTime = currentTimeSupplier.getAsLong(); } return object; } CachedObject(long expirationTimeInSeconds);  CachedObject(long expirationTimeInSeconds, LongSupplier currentTimeSupplier); synchronized T get(Supplier<T> refreshFunction); synchronized T forceRefresh(Supplier<T> refreshFunction);  }