1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*
* $Id: ItemList.java,v 1.1 2006/04/15 14:10:48 platform Exp $
* Created on 2006-3-24
*/
package org.json.simple;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* |a:b:c| => |a|,|b|,|c| |:| => ||,|| |a:| => |a|,||
*
* @author FangYidong<fangyidong@yahoo.com.cn>
*/
public class ItemList {
private String sp = ",";
@SuppressWarnings("rawtypes")
List items = new ArrayList();
public ItemList() {
}
public ItemList(final String s) {
this.split(s, this.sp, this.items);
}
public ItemList(final String s, final String sp) {
this.sp = s;
this.split(s, sp, this.items);
}
public ItemList(final String s, final String sp, final boolean isMultiToken) {
this.split(s, sp, this.items, isMultiToken);
}
@SuppressWarnings("rawtypes")
public List getItems() {
return this.items;
}
public String[] getArray() {
return (String[]) this.items.toArray();
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void split(final String s, final String sp, final List append, final boolean isMultiToken) {
if ((s == null) || (sp == null)) {
return;
}
if (isMultiToken) {
final StringTokenizer tokens = new StringTokenizer(s, sp);
while (tokens.hasMoreTokens()) {
append.add(tokens.nextToken().trim());
}
} else {
this.split(s, sp, append);
}
}
@SuppressWarnings({ "rawtypes", "unchecked" })
public void split(final String s, final String sp, final List append) {
if ((s == null) || (sp == null)) {
return;
}
int pos = 0;
int prevPos = 0;
do {
prevPos = pos;
pos = s.indexOf(sp, pos);
if (pos == -1) {
break;
}
append.add(s.substring(prevPos, pos).trim());
pos += sp.length();
} while (pos != -1);
append.add(s.substring(prevPos).trim());
}
public void setSP(final String sp) {
this.sp = sp;
}
@SuppressWarnings("unchecked")
public void add(final int i, final String item) {
if (item == null) {
return;
}
this.items.add(i, item.trim());
}
@SuppressWarnings("unchecked")
public void add(final String item) {
if (item == null) {
return;
}
this.items.add(item.trim());
}
@SuppressWarnings("unchecked")
public void addAll(final ItemList list) {
this.items.addAll(list.items);
}
public void addAll(final String s) {
this.split(s, this.sp, this.items);
}
public void addAll(final String s, final String sp) {
this.split(s, sp, this.items);
}
public void addAll(final String s, final String sp, final boolean isMultiToken) {
this.split(s, sp, this.items, isMultiToken);
}
/**
* @param i
* 0-based
* @return
*/
public String get(final int i) {
return (String) this.items.get(i);
}
public int size() {
return this.items.size();
}
@Override
public String toString() {
return this.toString(this.sp);
}
public String toString(final String sp) {
final StringBuffer sb = new StringBuffer();
for (int i = 0; i < this.items.size(); i++) {
if (i == 0) {
sb.append(this.items.get(i));
} else {
sb.append(sp);
sb.append(this.items.get(i));
}
}
return sb.toString();
}
public void clear() {
this.items.clear();
}
public void reset() {
this.sp = ",";
this.items.clear();
}
}
|