001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.util;
012:
013: import java.util.Map;
014: import java.util.Iterator;
015:
016: /**
017: * A list of ints, doubles and Strings separated by delimeters. This class
018: * builds a list that can be parsed with StringListParser.
019: */
020: public final class StringList {
021:
022: private char delim;
023: private StringBuffer buf;
024: private boolean first = true;
025:
026: public StringList() {
027: this (StringListParser.DEFAULT_DELIM, 40);
028: }
029:
030: public StringList(int initialSize) {
031: this (StringListParser.DEFAULT_DELIM, initialSize);
032: }
033:
034: public StringList(char delim, int initialSize) {
035: this .delim = delim;
036: buf = new StringBuffer(initialSize);
037: }
038:
039: /**
040: * Append an int.
041: */
042: public void append(int x) {
043: if (first)
044: first = false;
045: else
046: buf.append(delim);
047: buf.append(x);
048: }
049:
050: /**
051: * Append a boolean.
052: */
053: public void append(boolean x) {
054: if (first)
055: first = false;
056: else
057: buf.append(delim);
058: buf.append(x ? 'Y' : 'N');
059: }
060:
061: /**
062: * Append a double.
063: */
064: public void append(double x) {
065: if (first)
066: first = false;
067: else
068: buf.append(delim);
069: buf.append(x);
070: }
071:
072: /**
073: * Append a String without escaping any delims in the String. The
074: * String may not contain any delim characters.
075: */
076: public void append(String s) {
077: if (first)
078: first = false;
079: else
080: buf.append(delim);
081: buf.append(s);
082: }
083:
084: /**
085: * Append a Class (may be null).
086: */
087: public void append(Class c) {
088: if (first)
089: first = false;
090: else
091: buf.append(delim);
092: if (c == null)
093: buf.append('-');
094: else
095: buf.append(c.getName());
096: }
097:
098: /**
099: * Append a double quoted String escaping any embedded double quotes.
100: * If the string is null then a single hyphen is appended.
101: */
102: public void appendQuoted(String s) {
103: if (first)
104: first = false;
105: else
106: buf.append(delim);
107: if (s == null) {
108: buf.append('-');
109: } else {
110: buf.append('"');
111: int n = s.length();
112: for (int i = 0; i < n; i++) {
113: char c = s.charAt(i);
114: if (c == '"')
115: buf.append('"');
116: buf.append(c);
117: }
118: buf.append('"');
119: }
120: }
121:
122: /**
123: * Append a Map of properties.
124: */
125: public void appendProperties(Map props) {
126: for (Iterator i = props.entrySet().iterator(); i.hasNext();) {
127: Map.Entry e = (Map.Entry) i.next();
128: append((String) e.getKey());
129: appendQuoted((String) e.getValue());
130: }
131: }
132:
133: /**
134: * Get the completed string.
135: */
136: public String toString() {
137: return buf.toString();
138: }
139:
140: /**
141: * Reset the list. This empties it.
142: */
143: public void reset() {
144: buf.setLength(0);
145: first = true;
146: }
147:
148: /**
149: * Get the number of characters in the buffer.
150: */
151: public int length() {
152: return buf.length();
153: }
154:
155: }
|