Created
February 24, 2014 11:19
-
-
Save novotnyr/9186632 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package sk.upjs.ics.novotnyr.taskr; | |
import java.util.Iterator; | |
import java.util.LinkedList; | |
import java.util.List; | |
public class TaskDao { | |
public static final TaskDao INSTANCE = new TaskDao(); | |
private List<Task> tasks = new LinkedList<Task>(); | |
private long idGenerator = 0; | |
public TaskDao() { | |
Task zubar = new Task(); | |
zubar.setName("Zubár"); | |
saveOrUpdate(zubar); | |
Task obed = new Task(); | |
obed.setName("Obed"); | |
obed.setDone(true); | |
saveOrUpdate(obed); | |
} | |
public void saveOrUpdate(Task task) { | |
if(task.getId() == null) { | |
task.setId(idGenerator++); | |
tasks.add(task); | |
} else { | |
Iterator<Task> iterator = tasks.iterator(); | |
while(iterator.hasNext()) { | |
Task t = iterator.next(); | |
if(t.getId() == task.getId()) { | |
iterator.remove(); | |
break; | |
} | |
} | |
tasks.add(task); | |
} | |
} | |
public List<Task> list() { | |
return new LinkedList<Task>(this.tasks); | |
} | |
public Task getTask(long taskId) { | |
for (Task task : this.tasks) { | |
if(task.getId() == taskId) { | |
return task; | |
} | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment