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