| Strings.java |
1 package comtest;
2 import java.util.ArrayList;
3
4 /**
5 * Class for handling String lists
6 * @author vesal
7 * @version 22.9.2007
8 * @example
9 * <pre name="testStrings">
10 * Strings list = new Strings();
11 * list.size() === 0;
12 * list.add("$add"); list.size() === $size; list.get($index) === "$value";
13 *
14 * $add | $size | $index | $value
15 * -----------------------------------
16 * one | 1 | 0 | one
17 * two | 2 | 0 | one
18 * --- | --- | 1 | two
19 * </pre>
20 *
21 */
22 public class Strings extends ArrayList<String> implements Cloneable {
23 private static final long serialVersionUID = 1L;
24
25 /**
26 * Add an other list to the end of this list
27 * @param list what list to add
28 * @return this list reference
29 * @example
30 * <pre name="testAdd">
31 * Strings l1 = new Strings(), l2 = new Strings();
32 * l1.add("one"); l1.add("two");
33 * l2.add("three"); l2.add("four");
34 * l1.add(l2);
35 * l1.get(2) === "three"; l1.get(3) === "four";
36 * </pre>
37 */
38 public Strings add(Strings list) {
39 addAll(list);
40 return this;
41 }
42
43 /**
44 * Make a new instance from list with same strings inside
45 * @return new list
46 * @example
47 * <pre name="testClone">
48 * Strings l1 = new Strings(), l2;
49 * l1.add("one"); l1.add("two");
50 * l2 = l1.clone();
51 * l1.set(1,"three");
52 * l2.get(0) === "one"; l2.get(1) === "two"; l1.get(1) === "three";
53 * </pre>
54 */
55 @Override
56 public Strings clone() {
57 Strings list = new Strings();
58 list.add(this);
59 return list;
60 }
61
62 /**
63 * Replace from every line target by replacement.
64 * Current list is changed, not cloned!
65 * @param target The sequence of char values to be replaced
66 * @param replacement The replacement sequence of char values
67 * @return reference to the same list
68 * @example
69 * <pre name="testReplace">
70 * Strings l1 = new Strings();
71 * l1.add("$value"); $
72 *
73 * | $value |
74 * ----------
75 * | one |
76 * | two |
77 * | three |
78 * | four |
79 *
80 * l1.replace("o","p");
81 * l1.get($i) === "$value";
82 *
83 * $i | $value
84 * ----------------
85 * 0 | pne
86 * 1 | twp
87 * 2 | three
88 * 3 | fpur
89 * </pre>
90 */
91 public Strings replace(String target, String replacement) {
92 for (int i=0; i<size(); i++) {
93 set(i, get(i).replace(target, replacement));
94 }
95 return this;
96 }
97
98
99 /**
100 * Remove a part from list
101 * @param fromIndex index to start
102 * @param toIndex index to stop
103 */
104 @Override
105 public void removeRange(int fromIndex, int toIndex) {
106 super.removeRange(fromIndex, toIndex);
107 }
108
109
110 /**
111 * Returns the list as separated by separator;
112 * @param separator what to use as separator
113 * @param endSeparator what to use as separator after last line
114 * if null or "" then no separator after last line
115 * @return list separated by separator
116 *
117 * @example
118 * <pre name="test">
119 * Strings list = new Strings();
120 * list.toString("|","!") === "!";
121 * list.add("one");
122 * list.toString("|","!") === "one!";
123 * list.add("two");
124 * list.toString("|","!") === "one|two!";
125 * list.toString("|","") === "one|two";
126 * list.toString("|",null) === "one|two";
127 * </pre>
128 */
129 public String toString(String separator, String endSeparator) {
130 StringBuffer result = new StringBuffer();
131 String sep = "";
132 for ( String s : this) {
133 result.append(sep + s);
134 sep = separator;
135 }
136 if ( endSeparator != null ) result.append(endSeparator);
137 return result.toString();
138 }
139
140 /**
141 * Returns the list as separated by separator;
142 * @param separator what to use as separator
143 * @return list separated by separator
144 *
145 * @example
146 * <pre name="test">
147 * Strings list = new Strings();
148 * list.toString("|") === "";
149 * list.add("one");
150 * list.toString("|") === "one";
151 * list.add("two");
152 * list.toString("|") === "one|two";
153 * </pre>
154 */
155 public String toString(String separator) {
156 return toString(separator,null);
157 }
158
159 }
160