org.codehaus.cake.cache.service.loading.CacheLoadingConfiguration Maven / Gradle / Ivy
Show all versions of cake-cache-api Show documentation
/*
* Copyright 2008 Kasper Nielsen.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://cake.codehaus.org/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package org.codehaus.cake.cache.service.loading;
import java.util.Collection;
import org.codehaus.cake.attribute.BooleanAttribute;
import org.codehaus.cake.cache.Cache;
import org.codehaus.cake.cache.CacheEntry;
import org.codehaus.cake.ops.Ops.Op;
import org.codehaus.cake.ops.Ops.Predicate;
/**
* This class is used to configure the loading service prior to usage.
*
* If the specified loader is null
no loader will be used for loading new key-value bindings. And the
* {@link CacheLoadingService} will not be available at runtime. All values must then put into the cache by using
* {@link Cache#put(Object, Object)}, {@link Cache#putAll(java.util.Map)} or some of the other put operations.
*
* @author Kasper Nielsen
* @version $Id: CacheLoadingConfiguration.java 529 2007-12-27 17:09:26Z kasper $
* @param
* the type of keys maintained by the cache
* @param
* the type of mapped values
*/
public class CacheLoadingConfiguration {
/** The needs reload predicate. */
private Predicate super CacheEntry> needsReloadSelector;
/** The cache loader. */
private Object loader;
/**
* Returns the cache loader that has been set using either {@link #setLoader(Op)} or
* {@link #setLoader(SimpleCacheLoader)}.
*
* @return the loader that the cache should use for loading elements.
* @see #setLoader(Op)
* @see #setLoader(SimpleCacheLoader)
*/
public Object getLoader() {
return loader;
}
/**
* Sets a loader that should be used for loading new elements into the cache.
*
* @param loader
* the cache loader to set
* @return this configuration
*/
public CacheLoadingConfiguration setLoader(Op super K, ? extends V> loader) {
this.loader = loader;
return this;
}
/**
* Sets a loader that should be used for loading new elements into the cache.
*
* @param loader
* the cache loader to set
* @return this configuration
*/
public CacheLoadingConfiguration setLoader(SimpleCacheLoader super K, ? extends V> loader) {
this.loader = loader;
return this;
}
/**
* Returns the configured needs reload filter.
*
* @return the configured needs reload filter
* @see #setNeedsReloadFilter(Predicate)
*/
public Predicate super CacheEntry> getNeedsReloadFilter() {
return needsReloadSelector;
}
/**
* Sets a filter ({@link Predicate}) that is used for determining if an element needs to be reloaded. The
* predicate is checked when various load methods in {@link CacheLoadingService} are called.
*
* Some implementations might also check the predicate on calls to
* {@link org.codehaus.cake.cache.Cache#getAll(Collection)}, {@link org.codehaus.cake.cache.Cache#getEntry(Object)},
* but this is not required.
*
* @param selector
* the needs reload predicate
* @return this configuration
* @see #getNeedsReloadFilter()
*/
public CacheLoadingConfiguration setNeedsReloadFilter(Predicate super CacheEntry> selector) {
needsReloadSelector = selector;
return this;
}
/**
* The Hits attribute indicates the number of hits for a cache element. The mapped value must be of a
* type long between 0 and {@link Long#MAX_VALUE}.
*/
static final class IsLoadingForcedAttribute extends BooleanAttribute {
/** serialVersionUID. */
private static final long serialVersionUID = -2353351535602223603L;
/** Creates a new SizeAttribute. */
IsLoadingForcedAttribute() {
super("LoadingForced");
}
/** @return Preserves singleton property */
private Object readResolve() {
return CacheLoadingService.IS_FORCED;
}
}
}