From 688634ae5a5aaf663159032e67d2132ea61c5d5f Mon Sep 17 00:00:00 2001 From: Pacien TRAN-GIRARD Date: Sun, 4 May 2014 17:37:41 +0200 Subject: Implement "save" and "load" --- src/org/json/simple/ItemList.java | 158 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 src/org/json/simple/ItemList.java (limited to 'src/org/json/simple/ItemList.java') 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 @@ +/* + * $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 + */ +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(); + } +} -- cgit v1.2.3