org.apache.openjpa.datacache.DelegatingDataCache Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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://www.apache.org/licenses/LICENSE-2.0
*
* 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.apache.openjpa.datacache;
import java.util.BitSet;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Objects;
import org.apache.openjpa.util.RuntimeExceptionTranslator;
/**
* Delegating data cache that can also perform exception translation for
* use in facades. This cache allows its delegate to be null, in which
* case it returns default values for all operations.
*
* @author Abe White
*/
public class DelegatingDataCache
implements DataCache {
private static final BitSet EMPTY_BITSET = new BitSet(0);
private final DataCache _cache;
private final DelegatingDataCache _del;
private final RuntimeExceptionTranslator _trans;
/**
* Constructor. Supply delegate.
*/
public DelegatingDataCache(DataCache cache) {
this(cache, null);
}
public DelegatingDataCache(DataCache cache,
RuntimeExceptionTranslator trans) {
_cache = cache;
_trans = trans;
if (cache instanceof DelegatingDataCache)
_del = (DelegatingDataCache) _cache;
else
_del = null;
}
/**
* Return the direct delegate.
*/
public DataCache getDelegate() {
return _cache;
}
/**
* Return the native delegate.
*/
public DataCache getInnermostDelegate() {
return (_del == null) ? _cache : _del.getInnermostDelegate();
}
public int hashCode() {
if (_cache == null)
return super.hashCode();
return getInnermostDelegate().hashCode();
}
public boolean equals(Object other) {
if (other == this)
return true;
if (other instanceof DelegatingDataCache)
other = ((DelegatingDataCache) other).getInnermostDelegate();
return Objects.equals(getInnermostDelegate(), other);
}
/**
* Translate the OpenJPA exception.
*/
protected RuntimeException translate(RuntimeException re) {
return (_trans == null) ? re : _trans.translate(re);
}
public String getName() {
if (_cache == null)
return null;
try {
return _cache.getName();
} catch (RuntimeException re) {
throw translate(re);
}
}
public void setName(String name) {
if (_cache == null)
return;
try {
_cache.setName(name);
} catch (RuntimeException re) {
throw translate(re);
}
}
public void initialize(DataCacheManager manager) {
if (_cache == null)
return;
try {
_cache.initialize(manager);
} catch (RuntimeException re) {
throw translate(re);
}
}
public void commit(Collection additions, Collection newUpdates,
Collection existingUpdates, Collection