From 189acd1367e83075fef3a1182c2fc09be241411f Mon Sep 17 00:00:00 2001 From: Sam Bosley Date: Wed, 3 Oct 2012 13:50:24 -0700 Subject: [PATCH] Removed ActFmSyncQueue (going to redo that), refactored ChangesHappened constructor --- .../astrid/actfm/sync/ActFmSyncQueue.java | 83 ------------------- .../actfm/sync/messages/ChangesHappened.java | 29 +++++-- .../todoroo/astrid/sync/SyncMessageTest.java | 7 +- 3 files changed, 26 insertions(+), 93 deletions(-) delete mode 100644 astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncQueue.java diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncQueue.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncQueue.java deleted file mode 100644 index 4a3d0614b..000000000 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncQueue.java +++ /dev/null @@ -1,83 +0,0 @@ -package com.todoroo.astrid.actfm.sync; - -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; - -import com.todoroo.astrid.data.RemoteModel; - -public class ActFmSyncQueue { - - private static final ActFmSyncQueue INSTANCE = new ActFmSyncQueue(); - - public static class SyncQueueEntry { - public Class modelType; - public Long id; - - public SyncQueueEntry(Class modelType, Long id) { - this.modelType = modelType; - this.id = id; - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result - + ((modelType == null) ? 0 : modelType.hashCode()); - result = prime * result + ((id == null) ? 0 : id.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) - return true; - if (obj == null) - return false; - if (getClass() != obj.getClass()) - return false; - SyncQueueEntry other = (SyncQueueEntry) obj; - if (modelType == null) { - if (other.modelType != null) - return false; - } else if (!modelType.equals(other.modelType)) - return false; - if (id == null) { - if (other.id != null) - return false; - } else if (!id.equals(other.id)) - return false; - return true; - } - - } - - private final List queue; - private final HashSet elements; - - private ActFmSyncQueue() { - queue = new LinkedList(); - elements = new HashSet(); - } - - public synchronized void enqueue(SyncQueueEntry entry) { - if (!elements.contains(entry)) { - queue.add(entry); - elements.add(entry); - } - } - - public synchronized SyncQueueEntry dequeue() { - if (queue.isEmpty()) - return null; - SyncQueueEntry entry = queue.remove(0); - elements.remove(entry); - return entry; - } - - public static ActFmSyncQueue getInstance() { - return INSTANCE; - } - -} diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java index 00bf14055..30f9e6be7 100644 --- a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/messages/ChangesHappened.java @@ -24,19 +24,16 @@ public class ChangesHappened changes; - private long pushedAt; + private final long pushedAt; - public ChangesHappened(TYPE entity, RemoteModelDao modelDao, + public ChangesHappened(long id, Class modelClass, RemoteModelDao modelDao, OutstandingEntryDao outstandingDao) { - this.modelClass = entity.getClass(); + this.modelClass = modelClass; this.outstandingClass = getOutstandingClass(modelClass); - this.id = entity.getId(); + this.id = id; this.changes = new ArrayList(); - if (!entity.containsValue(RemoteModel.UUID_PROPERTY) - || !entity.containsValue(RemoteModel.PUSHED_AT_PROPERTY)) { - entity = modelDao.fetch(entity.getId(), getModelProperties(modelClass)); - } + TYPE entity = getEntity(id, modelDao); if (entity == null) { this.uuid = RemoteModel.NO_UUID; this.pushedAt = 0; @@ -55,6 +52,22 @@ public class ChangesHappened modelDao) { + return modelDao.fetch(localId, RemoteModel.UUID_PROPERTY, RemoteModel.PUSHED_AT_PROPERTY); + } + private void populateChanges(OutstandingEntryDao outstandingDao) { TodorooCursor cursor = outstandingDao.query(Query.select(getModelProperties(outstandingClass)) .where(OutstandingEntry.ENTITY_ID_PROPERTY.eq(id)).orderBy(Order.asc(OutstandingEntry.CREATED_AT_PROPERTY))); diff --git a/tests-sync/src/com/todoroo/astrid/sync/SyncMessageTest.java b/tests-sync/src/com/todoroo/astrid/sync/SyncMessageTest.java index 7fbd89450..5f99c05bc 100644 --- a/tests-sync/src/com/todoroo/astrid/sync/SyncMessageTest.java +++ b/tests-sync/src/com/todoroo/astrid/sync/SyncMessageTest.java @@ -1,6 +1,7 @@ package com.todoroo.astrid.sync; import com.todoroo.astrid.actfm.sync.messages.ChangesHappened; +import com.todoroo.astrid.data.RemoteModel; import com.todoroo.astrid.data.Task; import com.todoroo.astrid.data.TaskOutstanding; @@ -9,8 +10,10 @@ public class SyncMessageTest extends NewSyncTestCase { public void testTaskChangesHappenedConstructor() { Task t = createTask(); try { - ChangesHappened changes = new ChangesHappened(t, taskDao, taskOutstandingDao); - assertTrue(changes.getChanges().size() > 0); + ChangesHappened changes = new ChangesHappened(t.getId(), Task.class, taskDao, taskOutstandingDao); + assertTrue(changes.numChanges() > 0); + assertFalse(RemoteModel.NO_UUID.equals(changes.getUUID())); + assertEquals(t.getValue(Task.UUID), changes.getUUID()); } catch (Exception e) { fail("ChangesHappened constructor threw exception " + e); }