Store locality of a Sone in the Sone itself, remove related methods from Database.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
index d87a712..3c32f71 100644 (file)
@@ -48,48 +48,8 @@ public class MemoryDatabase implements Database {
         * {@inheritDoc}
         */
        @Override
-       public boolean isLocalSone(Sone sone) throws DatabaseException {
-               Validation.begin().isNotNull("Sone", sone).check();
-               return isLocalSone(sone.getId());
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public boolean isLocalSone(String id) throws DatabaseException {
-               Validation.begin().isNotNull("Sone ID", id).check();
-               synchronized (localSones) {
-                       return localSones.containsKey(id);
-               }
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public boolean isRemoteSone(Sone sone) throws DatabaseException {
-               Validation.begin().isNotNull("Sone", sone).check();
-               return isRemoteSone(sone.getId());
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
-       public boolean isRemoteSone(String id) throws DatabaseException {
-               Validation.begin().isNotNull("Sone ID", id).check();
-               synchronized (remoteSones) {
-                       return remoteSones.containsKey(id);
-               }
-       }
-
-       /**
-        * {@inheritDoc}
-        */
-       @Override
        public Sone getSone(String id, boolean create) throws DatabaseException {
-               if (isLocalSone(id)) {
+               if (getLocalSone(id, false) != null) {
                        return getLocalSone(id, create);
                }
                return getRemoteSone(id, create);
@@ -131,7 +91,7 @@ public class MemoryDatabase implements Database {
                Validation.begin().isNotNull("Sone ID", id).check();
                synchronized (localSones) {
                        if (!localSones.containsKey(id) && create) {
-                               localSones.put(id, new Sone(id));
+                               localSones.put(id, new Sone(id, true));
                        }
                        return localSones.get(id);
                }
@@ -145,7 +105,7 @@ public class MemoryDatabase implements Database {
                Validation.begin().isNotNull("Sone ID", id).check();
                synchronized (remoteSones) {
                        if (!remoteSones.containsKey(id) && create) {
-                               remoteSones.put(id, new Sone(id));
+                               remoteSones.put(id, new Sone(id, false));
                        }
                        return remoteSones.get(id);
                }
@@ -156,7 +116,7 @@ public class MemoryDatabase implements Database {
         */
        @Override
        public void saveSone(Sone sone) throws DatabaseException {
-               if (isLocalSone(sone)) {
+               if (sone.isLocal()) {
                        synchronized (localSones) {
                                localSones.put(sone.getId(), sone);
                        }
@@ -173,7 +133,7 @@ public class MemoryDatabase implements Database {
         */
        @Override
        public void removeSone(Sone sone) throws DatabaseException {
-               Map<String, Sone> sones = isLocalSone(sone) ? localSones : remoteSones;
+               Map<String, Sone> sones = sone.isLocal() ? localSones : remoteSones;
                synchronized (sones) {
                        sones.remove(sone.getId());
                }
@@ -184,7 +144,7 @@ public class MemoryDatabase implements Database {
         */
        @Override
        public void removeSone(String id) throws DatabaseException {
-               Map<String, Sone> sones = isLocalSone(id) ? localSones : remoteSones;
+               Map<String, Sone> sones = ((getSone(id, false) != null) && getSone(id, false).isLocal()) ? localSones : remoteSones;
                synchronized (sones) {
                        sones.remove(id);
                }