| 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 public Strings clone() {
56 Strings list = new Strings();
57 list.add(this);
58 return list;
59 }
60
61 /**
62 * Replace from every line target by replacement.
63 * Current list is changed, not cloned!
64 * @param target The sequence of char values to be replaced
65 * @param replacement The replacement sequence of char values
66 * @return reference to the same list
67 * @example
68 * <pre name="testReplace">
69 * Strings l1 = new Strings();
70 * l1.add("$value"); $
71 *
72 * | $value |
73 * ----------
74 * | one |
75 * | two |
76 * | three |
77 * | four |
78 *
79 * l1.replace("o","p");
80 * l1.get($i) === "$value";
81 *
82 * $i | $value
83 * ----------------
84 * 0 | pne
85 * 1 | twp
86 * 2 | three
87 * 3 | fpur
88 * </pre>
89 */
90 public Strings replace(String target, String replacement) {
91 for (int i=0; i<size(); i++) {
92 set(i, get(i).replace(target, replacement));
93 }
94 return this;
95 }
96
97
98 /**
99 * Remove a part from list
100 * @param fromIndex index to start
101 * @param toIndex index to stop
102 */
103 public void removeRange(int fromIndex, int toIndex) {
104 super.removeRange(fromIndex, toIndex);
105 }
106
107
108 /**
109 * Returns the list as separated by separator;
110 * @param separator what to use as separator
111 * @param endSeparator what to use as separator after last line
112 * if null or "" then no separator after last line
113 * @return list separated by separator
114 *
115 * @example
116 * <pre name="test">
117 * Strings list = new Strings();
118 * list.toString("|","!") === "!";
119 * list.add("one");
120 * list.toString("|","!") === "one!";
121 * list.add("two");
122 * list.toString("|","!") === "one|two!";
123 * list.toString("|","") === "one|two";
124 * list.toString("|",null) === "one|two";
125 * </pre>
126 */
127 public String toString(String separator, String endSeparator) {
128 StringBuffer result = new StringBuffer();
129 String sep = "";
130 for ( String s : this) {
131 result.append(sep + s);
132 sep = separator;
133 }
134 if ( endSeparator != null ) result.append(endSeparator);
135 return result.toString();
136 }
137
138 /**
139 * Returns the list as separated by separator;
140 * @param separator what to use as separator
141 * @return list separated by separator
142 *
143 * @example
144 * <pre name="test">
145 * Strings list = new Strings();
146 * list.toString("|") === "";
147 * list.add("one");
148 * list.toString("|") === "one";
149 * list.add("two");
150 * list.toString("|") === "one|two";
151 * </pre>
152 */
153 public String toString(String separator) {
154 return toString(separator,null);
155 }
156
157 }
158