/** * This static factory constructor allows the user to specify a time of * creation. * * @param object The object that will be wrapped. * @param timestamp Specifies the time this object was created in milliseconds * since the epoch. * @return A new WithTimestamp instance. * @param <U> The type that will be stored. */ public static <U> WithTimestamp<U> create( final U object, final long timestamp ) { return new WithTimestamp<U>(object, timestamp); }
/** * Returns the current value of this reference. * May be an old value if the scheduler has not refreshed the value recently. * * @return The current value of this reference. */ public T get() { return mRef.get().getValue(); }
/** * Private constructor. * * @param refreshPeriod Configures the refresh rate for the scheduler. * @param timeUnit Specifies the unit of time for the refreshPeriod. * @param refreshingLoader Used to initialize and refresh the cached value. * @param builder Thread factory builder. Expects that the name format is already set. * @param loopController Used to deterministically control refresh cycles during testing. */ private RefreshingReference( final Long refreshPeriod, final TimeUnit timeUnit, final RefreshingLoader<T> refreshingLoader, final ThreadFactoryBuilder builder, final Optional<LoopController> loopController ) { mRef = new AtomicReference<>(WithTimestamp.create(refreshingLoader.initial())); mScheduler = Executors.newSingleThreadScheduledExecutor( builder .setDaemon(true) .build() ); mRefreshingLoader = refreshingLoader; final RefreshingRunnable runnable = new RefreshingRunnable(); mScheduler.scheduleAtFixedRate(runnable, refreshPeriod, refreshPeriod, timeUnit); mLoopController = loopController; ResourceTracker.get().registerResource(this); }
/** * This static factory constructor returns a new WithTimestamp instance * with the default time set to the system time (measured in milliseconds * since the epoch) when the function is called. * * @param object The object that will be wrapped. * @return A new WithTimestamp instance. * @param <U> The type that will be stored. */ public static <U> WithTimestamp<U> create( final U object ) { final long time = System.currentTimeMillis(); return new WithTimestamp<U>(object, time); }