001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.persistence;
020:
021: import java.util.Collections;
022: import javax.persistence.Query;
023:
024: import org.apache.openjpa.datacache.DelegatingQueryCache;
025: import org.apache.openjpa.datacache.QueryCache;
026: import org.apache.openjpa.datacache.QueryKey;
027: import org.apache.openjpa.datacache.TypesChangedEvent;
028:
029: /**
030: * Implements Query result cache via delegation to QueryCache.
031: *
032: * @author Abe White
033: * @since 0.4.1
034: * @nojavadoc
035: */
036: public class QueryResultCacheImpl implements QueryResultCache {
037:
038: private final DelegatingQueryCache _cache;
039:
040: /**
041: * Constructor; supply delegate.
042: */
043: public QueryResultCacheImpl(QueryCache cache) {
044: _cache = new DelegatingQueryCache(cache,
045: PersistenceExceptions.TRANSLATOR);
046: }
047:
048: /**
049: * Delegate.
050: */
051: public QueryCache getDelegate() {
052: return _cache.getDelegate();
053: }
054:
055: public void pin(Query q) {
056: if (_cache.getDelegate() != null)
057: _cache.pin(toQueryKey(q));
058: }
059:
060: public void unpin(Query q) {
061: if (_cache.getDelegate() != null)
062: _cache.unpin(toQueryKey(q));
063: }
064:
065: public void evict(Query q) {
066: if (_cache.getDelegate() != null)
067: _cache.remove(toQueryKey(q));
068: }
069:
070: public void evictAll() {
071: _cache.clear();
072: }
073:
074: public void evictAll(Class cls) {
075: _cache.onTypesChanged(new TypesChangedEvent(this , Collections
076: .singleton(cls)));
077: }
078:
079: /**
080: * Return a cache key for the given query.
081: */
082: private QueryKey toQueryKey(Query q) {
083: QueryImpl impl = (QueryImpl) q;
084: if (impl.hasPositionalParameters())
085: return QueryKey.newInstance(impl.getDelegate(), impl
086: .getPositionalParameters());
087: return QueryKey.newInstance(impl.getDelegate(), impl
088: .getNamedParameters());
089: }
090:
091: public int hashCode() {
092: return _cache.hashCode();
093: }
094:
095: public boolean equals(Object other) {
096: if (other == this )
097: return true;
098: if (!(other instanceof QueryResultCacheImpl))
099: return false;
100: return _cache.equals(((QueryResultCacheImpl) other)._cache);
101: }
102: }
|