001/* Copyright 2006 FangYidong
002
003   Licensed under the Apache License, Version 2.0 (the "License");
004   you may not use this file except in compliance with the License.
005   You may obtain a copy of the License at
006
007       http://www.apache.org/licenses/LICENSE-2.0
008
009   Unless required by applicable law or agreed to in writing, software
010   distributed under the License is distributed on an "AS IS" BASIS,
011   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012   See the License for the specific language governing permissions and
013   limitations under the License. */
014package org.json.simple;
015
016import java.util.ArrayList;
017import java.util.List;
018import java.util.StringTokenizer;
019
020/**
021 * |a:b:c| => |a|,|b|,|c|
022 * |:| => ||,||
023 * |a:| => |a|,||
024 * @author FangYidong<fangyidong@yahoo.com.cn>
025 * @deprecated since 2.0.0 all of the functionality provided by the class seems to be already provided in the JDK.
026 */
027@Deprecated
028public class ItemList {
029        private String sp=",";
030        List items=new ArrayList();
031        
032        
033        /**
034         * description omitted.
035         */
036        public ItemList(){}
037        
038        
039        /**
040         * @param s description omitted.
041         */
042        public ItemList(String s){
043                this.split(s,sp,items);
044        }
045        
046        /**
047         * @param s description omitted.
048         * @param sp description omitted.
049         */
050        public ItemList(String s,String sp){
051                this.sp=s;
052                this.split(s,sp,items);
053        }
054        
055        /**
056         * @param s description omitted.
057         * @param sp description omitted.
058         * @param isMultiToken description omitted.
059         */
060        public ItemList(String s,String sp,boolean isMultiToken){
061                split(s,sp,items,isMultiToken);
062        }
063        
064        /**
065         * description omitted.
066         *
067         * @return description omitted.
068         */
069        public List getItems(){
070                return this.items;
071        }
072        
073        /**
074         * description omitted.
075         *
076         * @return description omitted.
077         */
078        public String[] getArray(){
079                return (String[])this.items.toArray();
080        }
081        
082        /**
083         * description omitted.
084         *
085         * @param s description omitted.
086         * @param sp description omitted.
087         * @param append description omitted.
088         * @param isMultiToken description omitted.
089         */
090        public void split(String s,String sp,List append,boolean isMultiToken){
091                if(s==null || sp==null)
092                        return;
093                if(isMultiToken){
094                        StringTokenizer tokens=new StringTokenizer(s,sp);
095                        while(tokens.hasMoreTokens()){
096                                append.add(tokens.nextToken().trim());
097                        }
098                }
099                else{
100                        this.split(s,sp,append);
101                }
102        }
103        
104        /**
105         * description omitted.
106         *
107         * @param s description omitted.
108         * @param sp description omitted.
109         * @param append description omitted.
110         */
111        public void split(String s,String sp,List append){
112                if(s==null || sp==null)
113                        return;
114                int pos=0;
115                int prevPos=0;
116                do{
117                        prevPos=pos;
118                        pos=s.indexOf(sp,pos);
119                        if(pos==-1)
120                                break;
121                        append.add(s.substring(prevPos,pos).trim());
122                        pos+=sp.length();
123                }while(pos!=-1);
124                append.add(s.substring(prevPos).trim());
125        }
126        
127        /**
128         * description omitted.
129         *
130         * @param sp description omitted.
131         */
132        public void setSP(String sp){
133                this.sp=sp;
134        }
135        
136        /**
137         * description omitted.
138         *
139         * @param i description omitted.
140         * @param item description omitted.
141         */
142        public void add(int i,String item){
143                if(item==null)
144                        return;
145                items.add(i,item.trim());
146        }
147
148        /**
149         * description omitted.
150         *
151         * @param item description omitted.
152         */
153        public void add(String item){
154                if(item==null)
155                        return;
156                items.add(item.trim());
157        }
158        
159        /**
160         * description omitted.
161         *
162         * @param list description omitted.
163         */
164        public void addAll(ItemList list){
165                items.addAll(list.items);
166        }
167        
168        /**
169         * description omitted.
170         *
171         * @param s description omitted.
172         */
173        public void addAll(String s){
174                this.split(s,sp,items);
175        }
176        
177        /**
178         * description omitted.
179         *
180         * @param s description omitted.
181         * @param sp description omitted.
182         */
183        public void addAll(String s,String sp){
184                this.split(s,sp,items);
185        }
186        
187        /**
188         * description omitted.
189         *
190         * @param s description omitted.
191         * @param sp description omitted.
192         * @param isMultiToken description omitted.
193         */
194        public void addAll(String s,String sp,boolean isMultiToken){
195                this.split(s,sp,items,isMultiToken);
196        }
197        
198        /**
199         * @param i 0-based
200         * @return description omitted.
201         */
202        public String get(int i){
203                return (String)items.get(i);
204        }
205        
206        /**
207         * description omitted.
208         *
209         * @return description omitted.
210         */
211        public int size(){
212                return items.size();
213        }
214        @Override
215        public String toString(){
216                return toString(sp);
217        }
218        
219        /**
220         * description omitted.
221         *
222         * @param sp description omitted.
223         * @return description omitted.
224         */
225        public String toString(String sp){
226                StringBuffer sb=new StringBuffer();
227                
228                for(int i=0;i<items.size();i++){
229                        if(i==0)
230                                sb.append(items.get(i));
231                        else{
232                                sb.append(sp);
233                                sb.append(items.get(i));
234                        }
235                }
236                return sb.toString();
237
238        }
239        
240        /**
241         * description omitted.
242         *
243         */
244        public void clear(){
245                items.clear();
246        }
247        
248        /**
249         * description omitted.
250         *
251         */
252        public void reset(){
253                sp=",";
254                items.clear();
255        }
256}