KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > expr > Keyword


1 package gnu.expr;
2 import gnu.mapping.*;
3 import java.io.*;
4 import gnu.text.Printable;
5 import gnu.lists.Consumer;
6
7 public class Keyword extends Symbol
8   implements Printable, Externalizable
9 {
10   public static final Namespace keywordNamespace = new Namespace();
11   static { keywordNamespace.setName("(keywords)"); }
12
13   public Keyword()
14   {
15   }
16
17   private Keyword (String JavaDoc name)
18   {
19     super(keywordNamespace, name);
20   }
21
22   /** Used for constructing literals (int gnu.expr.LitTable). */
23   public Keyword (Namespace namespace, String JavaDoc name)
24   {
25     super(namespace, name);
26   }
27
28   /** Get the corresponding non-keyword symbol.
29    * Informally, the symbol corresponding to dropping the ':'.
30    */

31   public Symbol asSymbol ()
32   {
33     return Namespace.EmptyNamespace.getSymbol(getName());
34   }
35
36   /**
37    * Create or find a Keyword with a given name (without final ':').
38    * @param name the print-name of the desired Keyword
39    * @return a Keyword with the given name, newly created iff none such exist
40    */

41   static public Keyword make (String JavaDoc name)
42   {
43     int hash = name.hashCode();
44     Keyword keyword = (Keyword) keywordNamespace.lookup(name, hash, false);
45     if (keyword == null)
46       {
47     keyword = new Keyword(name);
48     keywordNamespace.add(keyword, hash);
49     }
50     return keyword;
51   }
52
53   /*
54   public FString toSchemeString()
55   {
56     return new FString(name);
57   }
58   */

59
60   public static boolean isKeyword (Object JavaDoc obj)
61   {
62     return obj instanceof Keyword;
63   }
64
65   public final String JavaDoc toString()
66   {
67     return getName()+':';
68   }
69
70   public void print (Consumer out)
71   {
72     Symbols.print(getName(), out);
73     out.write(':');
74   }
75
76   /**
77    * Search vals[0:offset-1] for a keyword.
78    * Each key at vals[i] is followed by a value at keys[i+1].
79    * (This is used to search for a keyword parameter in an argument list.)
80    * @param vals the list to search in
81    * @param offset the index in vals to start the search at
82    * @param keyword the keyword to search for
83    * @return vals[i+1] such that vals[i]==keyword (and (i-offset) is even
84    * and non-negative); if there is no such i, return Special.dfault.
85    */

86   public static Object JavaDoc searchForKeyword (Object JavaDoc[] vals,
87                      int offset, Object JavaDoc keyword)
88   {
89     for (int i = offset; i < vals.length; i += 2)
90       {
91     if (vals[i] == keyword)
92       return vals[i+1];
93       }
94     return Special.dfault;
95   }
96
97   /**
98    * Search vals[0:offset-1] for a keyword.
99    * Each key at vals[i] is followed by a value at keys[i+1].
100    * (This is used to search for a keyword parameter in an argument list.)
101    * @param vals the list to search in
102    * @param offset the index in vals to start the search at
103    * @param keyword the keyword to search for
104    * @param dfault the value to return if there is no match
105    * @return vals[i+1] such that vals[i]==keyword (and (i-offset) is even
106    * and non-negative); if there is no such i, return dfault.
107    */

108   public static Object JavaDoc searchForKeyword (Object JavaDoc[] vals,
109                      int offset, Object JavaDoc keyword,
110                      Object JavaDoc dfault)
111   {
112     for (int i = offset; i < vals.length; i += 2)
113       {
114     if (vals[i] == keyword)
115       return vals[i+1];
116       }
117     return dfault;
118   }
119 }
120
Popular Tags