Removed ActFmSyncQueue (going to redo that), refactored ChangesHappened constructor

pull/14/head
Sam Bosley 12 years ago
parent 5fb49bf8f0
commit 189acd1367

@ -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<? extends RemoteModel> modelType;
public Long id;
public SyncQueueEntry(Class<? extends RemoteModel> 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<SyncQueueEntry> queue;
private final HashSet<SyncQueueEntry> elements;
private ActFmSyncQueue() {
queue = new LinkedList<SyncQueueEntry>();
elements = new HashSet<SyncQueueEntry>();
}
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;
}
}

@ -24,19 +24,16 @@ public class ChangesHappened<TYPE extends RemoteModel, OE extends OutstandingEnt
private final long id;
private final String uuid;
private final List<OE> changes;
private long pushedAt;
private final long pushedAt;
public ChangesHappened(TYPE entity, RemoteModelDao<TYPE> modelDao,
public ChangesHappened(long id, Class<TYPE> modelClass, RemoteModelDao<TYPE> modelDao,
OutstandingEntryDao<OE> outstandingDao) {
this.modelClass = entity.getClass();
this.modelClass = modelClass;
this.outstandingClass = getOutstandingClass(modelClass);
this.id = entity.getId();
this.id = id;
this.changes = new ArrayList<OE>();
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<TYPE extends RemoteModel, OE extends OutstandingEnt
return changes;
}
public int numChanges() {
return changes.size();
}
public String getUUID() {
return uuid;
}
public long getPushedAt() {
return pushedAt;
}
private TYPE getEntity(long localId, RemoteModelDao<TYPE> modelDao) {
return modelDao.fetch(localId, RemoteModel.UUID_PROPERTY, RemoteModel.PUSHED_AT_PROPERTY);
}
private void populateChanges(OutstandingEntryDao<OE> outstandingDao) {
TodorooCursor<OE> cursor = outstandingDao.query(Query.select(getModelProperties(outstandingClass))
.where(OutstandingEntry.ENTITY_ID_PROPERTY.eq(id)).orderBy(Order.asc(OutstandingEntry.CREATED_AT_PROPERTY)));

@ -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<Task, TaskOutstanding> changes = new ChangesHappened<Task, TaskOutstanding>(t, taskDao, taskOutstandingDao);
assertTrue(changes.getChanges().size() > 0);
ChangesHappened<Task, TaskOutstanding> changes = new ChangesHappened<Task, TaskOutstanding>(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);
}

Loading…
Cancel
Save