DataKeeper.java
01 /*
02  *
03  * All content copyright Terracotta, Inc., unless otherwise indicated. All rights reserved.
04  *
05  */
06 package demo.tasklist.service;
07 
08 import java.util.ArrayList;
09 import java.util.Iterator;
10 import java.util.List;
11 
12 /**
13  * DataKeeper keeps track of the current state of the task list. All
14  * modifications to the task list are made by calling DataKeeper's methods.
15  */
16 public class DataKeeper implements java.io.Serializable {
17    private static final long serialVersionUID = 5664956817748843899L;
18    private final List<String> userList;
19 
20    public DataKeeper() {
21       userList = new ArrayList<String>();
22    }
23 
24    public void addListItem(String newListItem) {
25       if (newListItem != null) {
26          userList.add(newListItem);
27       }
28    }
29 
30    public void deleteListItems(String[] itemsForDelete) {
31       if (itemsForDelete != null) {
32          for (int i = 0; i < itemsForDelete.length; i++) {
33             userList.remove(itemsForDelete[i]);
34          }
35       }
36    }
37 
38    public int getListSize() {
39       if (userList == null) {
40          return 0;
41       }
42       return userList.size();
43    }
44 
45    public String getListItem(int index) {
46       return userList.get(index);
47    }
48 
49    public List<String> getList() {
50       return userList;
51    }
52 
53    @Override
54    public String toString() {
55       StringBuilder sb = new StringBuilder();
56       boolean startedSeps = false;
57       for (Iterator<String> iter = getList().iterator(); iter.hasNext();) {
58          if (startedSeps) {
59             sb.append(", ");
60          else {
61             startedSeps = true;
62          }
63          sb.append(iter.next());
64       }
65       return sb.toString();
66    }
67 }