diff --git a/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncQueue.java b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncQueue.java new file mode 100644 index 000000000..4a3d0614b --- /dev/null +++ b/astrid/plugin-src/com/todoroo/astrid/actfm/sync/ActFmSyncQueue.java @@ -0,0 +1,83 @@ +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; + } + +}