/* * Copyright (C) 2005-2009 Alfresco Software Limited. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * As a special exception to the terms and conditions of version 2.0 of * the GPL, you may redistribute this Program in connection with Free/Libre * and Open Source Software ("FLOSS") applications as described in Alfresco's * FLOSS exception. You should have recieved a copy of the text describing * the FLOSS exception, and it is also available here: * http://www.alfresco.com/legal/licensing" */ package org.alfresco.repo.cache.lookup; import java.io.Serializable; import org.alfresco.repo.cache.SimpleCache; import org.alfresco.util.Pair; import org.alfresco.util.ParameterCheck; /** * A cache for two-way lookups of database entities. These are characterized by having a unique * key (perhaps a database ID) and a separate unique key that identifies the object. If no cache * is given, then all calls are passed through to the backing DAO. *
* The keys must have good equals
and hashCode implementations and
* must respect the case-sensitivity of the use-case.
*
* All keys will be unique to the given cache region, allowing the cache to be shared * between instances of this class. *
* Generics: *
equals
and hashCode
.
*
* Were no adequate key can be generated for the value, then null can be returned.
* In this case, the {@link #findByValue(Object) findByValue} method might not even do a search
* and just return null itself i.e. if it is difficult to look the value up in storage
* then it is probably difficult to generate a cache key from it, too.. In this scenario, the
* cache will be purely for key-based lookups
*
* @param value the full value being keyed (never null)
* @return Returns the business key representing the entity, or null
* if an economical key cannot be generated.
*/
VK1 getValueKey(V1 value);
/**
* Find an entity for a given key.
*
* @param key the key (ID) used to identify the entity (never null)
* @return Return the entity or null if no entity is exists for the ID
*/
Pairequals
and hashCode
* methods of the value object should respect case-sensitivity in the same way that this
* lookup treats case-sensitivity i.e. if the equals
method is case-sensitive
* then this method should look the entity up using a case-sensitive search.
*
* Since this is a cache backed by some sort of database, null values are allowed by the
* cache. The implementation of this method can throw an exception if null is not
* appropriate for the use-case.
*
* If the search is impossible or expensive, this method should just return null. This
* would usually be the case if the {@link #getValueKey(Object) getValueKey} method also returned
* null i.e. if it is difficult to look the value up in storage then it is probably
* difficult to generate a cache key from it, too.
*
* @param value the value (business object) used to identify the entity (null allowed).
* @return Return the entity or null if no entity matches the given value
*/
Pairnull
value i.e. a value that has been persisted as null.
*/
private static final Serializable VALUE_NULL = "@@VALUE_NULL@@";
/**
* A value that was not found or persisted.
*/
private static final Serializable VALUE_NOT_FOUND = "@@VALUE_NOT_FOUND@@";
/**
* The cache region that will be used (see {@link CacheRegionKey}) in all the cache keys
*/
private static final String CACHE_REGION_DEFAULT = "DEFAULT";
private final SimpleCache
* All keys will be unique to the given cache region, allowing the cache to be shared
* between instances of this class.
*
* @param cache the cache that will back the two-way lookups; null to have no backing
* in a cache.
* @param cacheRegion the region within the cache to use.
* @param entityLookup the instance that is able to find and persist entities
*/
@SuppressWarnings("unchecked")
public EntityLookupCache(SimpleCache cache, String cacheRegion, EntityLookupCallbackDAO