T
- The type of the evaluator.public final class CachingEvaluator<T> extends Evaluator<T> implements ICachingEvaluator
IEvaluator1
that caches the last evaluated result. This way, if a
second evaluation is requested for the same date, the value is returned without recomputing it.
This class is intended to be returned by IEvaluator.getCachingWrapper()
.Constructor and Description |
---|
CachingEvaluator(IEvaluator1<T> evaluator)
Initializes a new instance.
|
Modifier and Type | Method and Description |
---|---|
void |
clearCache()
Clears any values cached by this evaluator.
|
Object |
clone(CopyContext context)
Clones this object using the specified context.
|
protected void |
dispose(boolean disposing)
Releases any resources associated with this instance.
|
T |
evaluate(JulianDate date)
Evaluates the function.
|
TimeIntervalCollection |
getAvailabilityIntervals(TimeIntervalCollection consideredIntervals)
Gets the intervals over which data is available.
|
IEvaluator |
getCachingWrapper()
Gets a version of this evaluator that caches the previously computed value so that if it is evaluated
twice at the same date the computation is done only once.
|
boolean |
getIsThreadSafe()
Gets a value indicating whether the methods on this instance are safe to call from
multiple threads simultaneously.
|
boolean |
getIsTimeVarying()
Gets a value indicating whether or not the value of this evaluator changes with time.
|
boolean |
isAvailable(JulianDate date)
Determines if valid data is available for the given
JulianDate . |
void |
updateEvaluatorReferences(CopyContext context)
Updates the evaluator references held by this object using the reference-to-reference
mapping in the specified
CopyContext . |
dispose, evaluate, evaluate, evaluate, evaluate, getAvailabilityIntervals, getGroup
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getGroup
close, dispose
public CachingEvaluator(@Nonnull IEvaluator1<T> evaluator)
evaluator
- The evaluator to be wrapped.public void updateEvaluatorReferences(CopyContext context)
CopyContext
.
The following example shows how to implement this method for an evaluator that contains a nested evaluator:
@Override
public final void updateEvaluatorReferences(CopyContext context) {
m_nestedEvaluator = context.updateReference(m_nestedEvaluator);
}
This method is called by EvaluatorGroup
and usually does not need to be
called directly by users. EvaluatorGroup
uses this method to replace references
to shared evaluators with references to caching versions of the evaluators.
To implement this method, call CopyContext.updateReference(T)
on each evaluator reference
held by your evaluator and assign the return value back to the field.
updateEvaluatorReferences
in interface IEvaluator
updateEvaluatorReferences
in class Evaluator<T>
context
- The context that specifies the reference mapping.public Object clone(CopyContext context)
This method should be implemented to call a copy constructor on the class of the
object being cloned. The copy constructor should take the CopyContext
as a parameter
in addition to the existing instance to copy. The copy constructor should first call
CopyContext.addObjectMapping(T, T)
to identify the newly constructed instance
as a copy of the existing instance. It should then copy all fields, using
CopyContext.updateReference(T)
to copy any reference fields.
A typical implementation of ICloneWithContext
:
public static class MyClass implements ICloneWithContext {
public MyClass(MyClass existingInstance, CopyContext context) {
context.addObjectMapping(existingInstance, this);
someReference = context.updateReference(existingInstance.someReference);
}
@Override
public final Object clone(CopyContext context) {
return new MyClass(this, context);
}
private Object someReference;
}
In general, all fields that are reference types should be copied with a call to
CopyContext.updateReference(T)
. There are a couple of exceptions:
If one of these exceptions applies, the CopyContext
should be given an opportunity
to update the reference before the reference is copied explicitly. Use
CopyContext.updateReference(T)
to update the reference. If CopyContext.updateReference(T)
returns
the original object, indicating that the context does not have a replacement registered,
then copy the object manually by invoking a Clone method, a copy constructor, or by manually
constructing a new instance and copying the values.
alwaysCopy = context.updateReference(existingInstance.alwaysCopy);
if (existingInstance.alwaysCopy != null && alwaysCopy == existingInstance.alwaysCopy) {
alwaysCopy = (AlwaysCopy) existingInstance.alwaysCopy.clone(context);
}
If you are implementing an evaluator (a class that implements IEvaluator
), the
IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)
method shares some responsibilities with the
copy context constructor. Code duplication can be avoided by doing the following:
CopyContext.updateReference(T)
. You should still call CopyContext.updateReference(T)
on any references to
non-evaluators.
IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)
as the last line in the constructor and pass it the
same CopyContext
passed to the constructor.
IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)
as normal. See the reference documentation for
IEvaluator.updateEvaluatorReferences(agi.foundation.infrastructure.CopyContext)
for more information on implementing that method.
public MyClass(MyClass existingInstance, CopyContext context) {
super(existingInstance, context);
someReference = context.updateReference(existingInstance.someReference);
evaluatorReference = existingInstance.evaluatorReference;
updateEvaluatorReferences(context);
}
@Override
public void updateEvaluatorReferences(CopyContext context) {
evaluatorReference = context.updateReference(evaluatorReference);
}
@Override
public Object clone(CopyContext context) {
return new MyClass(this, context);
}
private Object someReference;
private IEvaluator evaluatorReference;
clone
in interface ICloneWithContext
clone
in class Evaluator<T>
context
- The context to use to perform the copy.protected void dispose(boolean disposing)
public TimeIntervalCollection getAvailabilityIntervals(TimeIntervalCollection consideredIntervals)
getAvailabilityIntervals
in interface IAvailability
getAvailabilityIntervals
in class Evaluator<T>
consideredIntervals
- The intervals over which availability information is needed. Note that the returned availability
intervals may indicate availability outside of these intervals of consideration.public boolean isAvailable(@Nonnull JulianDate date)
JulianDate
.isAvailable
in interface IAvailability
isAvailable
in class Evaluator<T>
date
- The date for which to check availability.true
if valid data is available for this date; otherwise false
.public boolean getIsTimeVarying()
false
, the evaluator is assumed to return the same value for any
input JulianDate
.getIsTimeVarying
in interface IIsTimeVarying
getIsTimeVarying
in class Evaluator<T>
public boolean getIsThreadSafe()
If this property is true
, all methods and properties are guaranteed to be thread safe.
Conceptually, an object that returns true
for this method acts as if there is a lock
protecting each method and property such that only one thread at a time can be inside any method or
property in the class. In reality, such locks are generally not used and are in fact discouraged. However,
the user must not experience any exceptions or inconsistent behavior that would not be experienced if such
locks were used.
If this property is false
, the behavior when using this class from multiple threads
simultaneously is undefined and may include inconsistent results and exceptions. Clients wishing to use
multiple threads should call CopyForAnotherThread.copy(T)
to get a separate instance of the
object for each thread.
getIsThreadSafe
in interface IThreadAware
getIsThreadSafe
in class Evaluator<T>
public IEvaluator getCachingWrapper()
This method is called by EvaluatorGroup
to create a caching version of an evaluator
that is shared between multiple computations.
To implement this method in your own evaluator, construct and return a caching version of the evaluator's base class.
For example, if your evaluator implements IEvaluator1
directly, return an instance of
CachingEvaluator
. In many cases, such as when implementing a PointEvaluator
this method does not need to be overridden because the default implementation returns an appropriate
caching wrapper already. If you do not want the last value computed by your evaluator to ever be cached, or
if your evaluator does its own caching internally, this method can return this
.
Shows an example implementation in an evaluator that implements IEvaluator1
directly, where T is double.
@Override
public IEvaluator getCachingWrapper() {
return new CachingEvaluator<Double>(this);
}
getCachingWrapper
in interface IEvaluator
getCachingWrapper
in class Evaluator<T>
this
should be returned by this method.public T evaluate(@Nonnull JulianDate date)
public final void clearCache()
clearCache
in interface ICachingEvaluator