1 | package ise.antelope.tasks.typedefs.string;
|
---|
2 |
|
---|
3 | import java.util.*;
|
---|
4 |
|
---|
5 | /**
|
---|
6 | * Sort a list of Strings.
|
---|
7 | *
|
---|
8 | * @version $Revision: 1.2 $
|
---|
9 | */
|
---|
10 | public class Sort implements StringOp {
|
---|
11 |
|
---|
12 | private String separator = null;
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * @param s the separator. The string representing the list to be sorted
|
---|
16 | * will be split on this separator. This string is passed to a
|
---|
17 | * StringTokenizer, so this parameter represents a list of separators,
|
---|
18 | * not necessarily a single separator.
|
---|
19 | */
|
---|
20 | public void setSeparator(String s) {
|
---|
21 | separator = s;
|
---|
22 | }
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Sort the string. The given string will first be split using either
|
---|
26 | * that string set in <code>setSeparator</code> or by the default delimiters
|
---|
27 | * for StringTokenizer. If the default delimiters for StringTokenizer are
|
---|
28 | * used, the returned string will be a list of comma separated values.
|
---|
29 | *
|
---|
30 | * @param s
|
---|
31 | * @return Description of the Returned Value
|
---|
32 | */
|
---|
33 | public String execute(String s) {
|
---|
34 | if (s == null)
|
---|
35 | return "";
|
---|
36 | if (separator != null && separator.length() == 0)
|
---|
37 | separator = null;
|
---|
38 | List list = new ArrayList();
|
---|
39 | StringTokenizer st = separator == null ? new StringTokenizer(s) : new StringTokenizer(s, separator);
|
---|
40 | while(st.hasMoreTokens()) {
|
---|
41 | list.add(st.nextToken());
|
---|
42 | }
|
---|
43 | Collections.sort(list);
|
---|
44 | StringBuffer sorted = new StringBuffer();
|
---|
45 | String sep = separator == null ? "," : separator.substring(0, 1);
|
---|
46 | for (Iterator it = list.iterator(); it.hasNext(); ) {
|
---|
47 | sorted.append((String)it.next());
|
---|
48 | if (it.hasNext())
|
---|
49 | sorted.append(sep);
|
---|
50 | }
|
---|
51 | return sorted.toString();
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 |
|
---|