1 package net.pterodactylus.sone.database.memory;
3 import static com.google.common.base.Optional.fromNullable;
4 import static com.google.common.collect.Maps.newHashMap;
7 import java.util.concurrent.locks.ReadWriteLock;
9 import net.pterodactylus.sone.database.IdentityDatabase;
10 import net.pterodactylus.sone.freenet.wot.Identity;
12 import com.google.common.base.Optional;
15 * {@link IdentityDatabase} implementation that keeps all {@link Identity}s in
18 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
20 public class MemoryIdentityDatabase implements IdentityDatabase {
22 private final ReadWriteLock lock;
23 private final Map<String, Identity> identities = newHashMap();
25 public MemoryIdentityDatabase(ReadWriteLock readWriteLock) {
26 this.lock = readWriteLock;
30 public Optional<Identity> getIdentity(String identityId) {
31 lock.readLock().lock();
33 return fromNullable(identities.get(identityId));
35 lock.readLock().unlock();
40 public void storeIdentity(Identity identitiy) {
41 lock.writeLock().lock();
43 identities.put(identitiy.getId(), identitiy);
45 lock.writeLock().unlock();