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