org.greenrobot.daocompat.identityscope.IdentityScopeLong Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of objectbox-daocompat Show documentation
Show all versions of objectbox-daocompat Show documentation
ObjectBox is a fast NoSQL database for Objects
The newest version!
/*
* Copyright (C) 2011-2017 Markus Junginger, greenrobot (http://greenrobot.org)
* 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://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.greenrobot.daocompat.identityscope;
import org.greenrobot.essentials.collections.LongHashMap;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.concurrent.locks.ReentrantLock;
/**
* The context for entity identities. Provides the scope in which entities will be tracked and managed.
*
* @author Markus
* @param
* Entity
*/
public class IdentityScopeLong implements IdentityScope {
private final LongHashMap> map;
private final ReentrantLock lock;
public IdentityScopeLong() {
map = new LongHashMap<>();
lock = new ReentrantLock();
}
@Override
public T get(Long key) {
return get2(key);
}
@Override
public T getNoLock(Long key) {
return get2NoLock(key);
}
public T get2(long key) {
lock.lock();
Reference ref;
try {
ref = map.get(key);
} finally {
lock.unlock();
}
if (ref != null) {
return ref.get();
} else {
return null;
}
}
public T get2NoLock(long key) {
Reference ref = map.get(key);
if (ref != null) {
return ref.get();
} else {
return null;
}
}
@Override
public void put(Long key, T entity) {
put2(key, entity);
}
@Override
public void putNoLock(Long key, T entity) {
put2NoLock(key, entity);
}
public void put2(long key, T entity) {
lock.lock();
try {
map.put(key, new WeakReference<>(entity));
} finally {
lock.unlock();
}
}
public void put2NoLock(long key, T entity) {
map.put(key, new WeakReference<>(entity));
}
@Override
public boolean detach(Long key, T entity) {
lock.lock();
try {
if (get(key) == entity && entity != null) {
remove(key);
return true;
} else {
return false;
}
} finally {
lock.unlock();
}
}
@Override
public void remove(Long key) {
lock.lock();
try {
map.remove(key);
} finally {
lock.unlock();
}
}
@Override
public void remove(Iterable keys) {
lock.lock();
try {
for (Long key : keys) {
map.remove(key);
}
} finally {
lock.unlock();
}
}
@Override
public void clear() {
lock.lock();
try {
map.clear();
} finally {
lock.unlock();
}
}
@Override
public void lock() {
lock.lock();
}
@Override
public void unlock() {
lock.unlock();
}
@Override
public void reserveRoom(int count) {
map.reserveRoom(count);
}
}