public class TestPrivateList
1: import java.util.Scanner;
2: import java.util.List;
4: /**
5: * A program to show the various ways of messing with private lists.
6: *
7: * @author Mark Young (A00000000)
8: */
9: public class TestPrivateList {
11: public static void main(String[] args) {
12: PrivateList one, two, three;
13: List<String> unsafe, copy, safe;
15: // introduce yourself
16: System.out.println("\n"
17: + "A PrivateList is a list of Strings that refuses to add "
18: + "nulls or words starting with F.\n");
19: pause();
21: // Using an unsafe getter
22: System.out.println("Let's make a PrivateList");
23: one = makeList();
24: System.out.println("\tone == " + one);
25: pause();
26: System.out.println("Let unsafe = one.getWordsUnsafe()");
27: unsafe = one.getWordsUnsafe();
28: System.out.println("Messing with unsafe");
29: messWith(unsafe);
30: System.out.println("After messing with it, unsafe == " + unsafe);
31: System.out.println("After messing with unsafe, one == " + one);
32: pause();
34: // Using a copy getter
35: System.out.println("Let's make a PrivateList");
36: two = makeList();
37: System.out.println("\ttwo == " + two);
38: pause();
39: System.out.println("Let copy = two.getWordsCopy()");
40: copy = two.getWordsCopy();
41: System.out.println("Messing with copy");
42: messWith(copy);
43: System.out.println("After messing with it, copy == " + copy);
44: System.out.println("After messing with copy, two == " + two);
45: pause();
47: // Using an immutableList getter
48: System.out.println("Let's make a PrivateList");
49: three = makeList();
50: System.out.println("\tthree == " + three);
51: pause();
52: System.out.println("Let safe = three.getWordsSafe()");
53: safe = three.getWordsSafe();
54: System.out.println("Messing with safe");
55: messWith(safe);
56: System.out.println("After messing with it, safe == " + safe);
57: System.out.println("After messing with safe, three == " + three);
58: pause();
60: // Updating the list behind the view
61: System.out.println("As a bonus, let's add to three...");
62: three.addWord("extended");
63: System.out.println("\tthree == " + three);
64: pause();
65: System.out.println("...and safe is ALSO updated...");
66: System.out.println("\tsafe == " + safe);
67: System.out.println("...because it's a VIEW of three!");
68: pause();
69: }
71: /**
72: * Make a PrivateList with several elements, reporting whether each item is added.
73: *
74: * @return a PrivateList containing multiple words.
75: */
76: private static PrivateList makeList() {
77: PrivateList result = new PrivateList();
79: String[] words = {"Here", "is", null, "my", "fecking", "list"};
80: for (String w : words) {
81: if (result.addWord(w)) {
82: System.out.println(" -- added " + w);
83: } else {
84: System.out.println(" -- FAILED to add " + w);
85: }
86: }
88: return result;
89: }
92: /**
93: * Try to add/change/delete words in a PrivateList. With proper
94: * encapsulation, none of these operations should modify the PrivateList
95: * object.
96: *
97: * @param list the list of words from the PrivateList.
98: */
99: private static void messWith(List<String> list) {
100: try {
101: if (list.add("for")) {
102: System.out.println("\tAdded for");
103: } else {
104: System.out.println("\tFailed to add for");
105: }
106: } catch (UnsupportedOperationException e) {
107: System.out.println("\tadd not supported");
108: }
109: try {
110: if (list.add(null)) {
111: System.out.println("\tAdded null");
112: } else {
113: System.out.println("Failed to add null");
114: }
115: } catch (UnsupportedOperationException e) {
116: System.out.println("\tadd not supported");
117: }
118: try {
119: String item = list.remove(0);
120: if (item != null) {
121: System.out.println("\tRemoved " + item + " from position 0");
122: } else {
123: System.out.println("Failed to remove from position 0");
124: }
125: } catch (UnsupportedOperationException e) {
126: System.out.println("\tremove not supported");
127: }
128: try {
129: String item = list.set(0, "CHANGED");
130: System.out.println("\tChanged position 0 from " + item
131: + " to CHANGED");
132: } catch (UnsupportedOperationException e) {
133: System.out.println("\tset not supported");
134: }
135: System.out.println();
136: }
138: private static final Scanner kbd = new Scanner(System.in);
140: /**
141: * Prompt the user and wait for them to press enter.
142: */
143: private static void pause() {
144: System.out.println();
145: System.out.print("...press enter...");
146: kbd.nextLine();
147: System.out.println();
148: }
150: }