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