KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > util > StringList


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.util;
13
14 import java.util.Map JavaDoc;
15 import java.util.Iterator JavaDoc;
16
17 /**
18  * A list of ints, doubles and Strings separated by delimeters. This class
19  * builds a list that can be parsed with StringListParser.
20  */

21 public final class StringList {
22
23     private char delim;
24     private StringBuffer JavaDoc buf;
25     private boolean first = true;
26
27     public StringList() {
28         this(StringListParser.DEFAULT_DELIM, 40);
29     }
30
31     public StringList(int initialSize) {
32         this(StringListParser.DEFAULT_DELIM, initialSize);
33     }
34
35     public StringList(char delim, int initialSize) {
36         this.delim = delim;
37         buf = new StringBuffer JavaDoc(initialSize);
38     }
39
40     /**
41      * Append an int.
42      */

43     public void append(int x) {
44         if (first) first = false;
45         else buf.append(delim);
46         buf.append(x);
47     }
48
49     /**
50      * Append a boolean.
51      */

52     public void append(boolean x) {
53         if (first) first = false;
54         else buf.append(delim);
55         buf.append(x ? 'Y' : 'N');
56     }
57
58     /**
59      * Append a double.
60      */

61     public void append(double x) {
62         if (first) first = false;
63         else buf.append(delim);
64         buf.append(x);
65     }
66
67     /**
68      * Append a String without escaping any delims in the String. The
69      * String may not contain any delim characters.
70      */

71     public void append(String JavaDoc s) {
72         if (first) first = false;
73         else buf.append(delim);
74         buf.append(s);
75     }
76
77     /**
78      * Append a Class (may be null).
79      */

80     public void append(Class JavaDoc c) {
81         if (first) first = false;
82         else buf.append(delim);
83         if (c == null) buf.append('-');
84         else buf.append(c.getName());
85     }
86
87     /**
88      * Append a double quoted String escaping any embedded double quotes.
89      * If the string is null then a single hyphen is appended.
90      */

91     public void appendQuoted(String JavaDoc s) {
92         if (first) first = false;
93         else buf.append(delim);
94         if (s == null) {
95             buf.append('-');
96         } else {
97             buf.append('"');
98             int n = s.length();
99             for (int i = 0; i < n; i++) {
100                 char c = s.charAt(i);
101                 if (c == '"') buf.append('"');
102                 buf.append(c);
103             }
104             buf.append('"');
105         }
106     }
107
108     /**
109      * Append a Map of properties.
110      */

111     public void appendProperties(Map JavaDoc props) {
112         for (Iterator JavaDoc i = props.entrySet().iterator(); i.hasNext(); ) {
113             Map.Entry JavaDoc e = (Map.Entry JavaDoc)i.next();
114             append((String JavaDoc)e.getKey());
115             appendQuoted((String JavaDoc)e.getValue());
116         }
117     }
118
119     /**
120      * Get the completed string.
121      */

122     public String JavaDoc toString() { return buf.toString(); }
123
124     /**
125      * Reset the list. This empties it.
126      */

127     public void reset() {
128         buf.setLength(0);
129         first = true;
130     }
131
132     /**
133      * Get the number of characters in the buffer.
134      */

135     public int length() { return buf.length(); }
136
137 }
138
Popular Tags