KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > neu > ccs > jmk > StringList


1 // $Id: StringList.java,v 1.1.1.1 2000/09/26 11:20:35 ramsdell Exp $
2

3 // Implements a string list as a value.
4

5 /*
6  * Copyright 1999 by John D. Ramsdell
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 package edu.neu.ccs.jmk;
24
25 /**
26  * Implements a string list as a value.
27  * @version May 1999
28  * @author John D. Ramsdell
29  */

30 public final class StringList
31 implements Value
32 {
33   private /* final */ String JavaDoc string;
34   private StringList rest;
35
36   /**
37    * Construct a string list.
38    * @param string a non-null string
39    * @param rest tail of the list
40    * @exception NullPointerException when string is null
41    */

42   public StringList(String JavaDoc string, StringList rest) {
43     if (string == null)
44       throw new NullPointerException JavaDoc();
45     this.string = string;
46     this.rest = rest;
47   }
48
49   /**
50    * Construct a one element string list.
51    */

52   public StringList(String JavaDoc string) {
53     this(string, null);
54   }
55
56   /**
57    * Get string associated with this cell.
58    */

59   public String JavaDoc getString() {
60     return string;
61   }
62
63   /**
64    * Get string list following this cell.
65    */

66   public StringList getRest() {
67     return rest;
68   }
69
70   /**
71    * Set string list following this cell.
72    * Used only in this package because unrestricted use
73    * could cause problems with the evaluator.
74    */

75   void setRest(StringList rest) { // NOT public
76
this.rest = rest;
77   }
78
79   /**
80    * Determines which values are string lists.
81    */

82   public static boolean isStringList(Value v) {
83     return v == null || v instanceof StringList;
84   }
85
86   /**
87    * Number of strings in a string list.
88    */

89   public static int length(StringList sl) {
90     int len = 0;
91     for (; sl != null; sl = sl.rest)
92       len++;
93     return len;
94   }
95
96   /**
97    * Constructs a string array from a string list.
98    */

99   public static String JavaDoc[] list2array(StringList sl) {
100     int len = length(sl);
101     String JavaDoc[] result = new String JavaDoc[len];
102     for (int i = 0; sl != null; sl = sl.rest)
103       result[i++] = sl.string;
104     return result;
105   }
106
107   /**
108    * Constructs a string list from a string array.
109    */

110   public static StringList array2list(String JavaDoc[] sa) {
111     StringList result = null;
112     if (sa != null)
113       for (int i = sa.length - 1; i >= 0; i--)
114     result = new StringList(sa[i], result);
115     return result;
116   }
117
118   /**
119    * Appends two string lists.
120    */

121   public static StringList append(StringList sl1, StringList sl2) {
122     if (sl2 == null)
123       return sl1;
124     else if (sl1 == null)
125       return sl2;
126     else {
127       StringList sl0 = new StringList(sl1.getString());
128       StringList last = sl0;
129       for (sl1 = sl1.getRest(); sl1 != null; sl1 = sl1.getRest()) {
130     StringList temp = new StringList(sl1.getString());
131     last.rest = temp;
132     last = temp;
133       }
134       last.setRest(sl2);
135       return sl0;
136     }
137   }
138 }
139
Popular Tags