KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > amber > expr > EnumExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free Software Foundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Rodrigo Westrupp
27  */

28
29 package com.caucho.amber.expr;
30
31 import com.caucho.amber.query.QueryParser;
32 import com.caucho.amber.type.PrimitiveIntType;
33 import com.caucho.amber.type.Type;
34 import com.caucho.util.CharBuffer;
35
36
37 /**
38  * Enum expression for Amber.
39  */

40 public class EnumExpr extends AbstractAmberExpr {
41   private Class JavaDoc _javaType;
42
43   // enum string value
44
private String JavaDoc _name;
45
46   // enum integer value
47
private int _ordinal;
48
49   private boolean _isOrdinal = true;
50
51   /**
52    * Creates a new enum expression.
53    *
54    * @param javaType the java type of the enum
55    * @param name the string name of the enum value
56    * @param ordinal the integer value of the enum
57    */

58   public EnumExpr(Class JavaDoc javaType,
59                   String JavaDoc name,
60                   int ordinal)
61   {
62     _javaType = javaType;
63     _name = name;
64     _ordinal = ordinal;
65   }
66
67   /**
68    * Returns the expr type.
69    */

70   public Type getType()
71   {
72     return PrimitiveIntType.create();
73   }
74
75   /**
76    * Returns the java type
77    */

78   public Class JavaDoc getJavaType()
79   {
80     return _javaType;
81   }
82
83   /**
84    * Returns the enum value
85    */

86   public int getOrdinal()
87   {
88     return _ordinal;
89   }
90
91   /**
92    * Returns true for ordinal
93    */

94   public boolean isOrdinal()
95   {
96     return _isOrdinal;
97   }
98
99   /**
100    * Sets true for ordinal
101    */

102   public void setOrdinal(boolean isOrdinal)
103   {
104     _isOrdinal = isOrdinal;
105   }
106
107   /**
108    * Binds the expression as a select item.
109    */

110   public AmberExpr bindSelect(QueryParser parser)
111   {
112     return this;
113   }
114
115   /**
116    * Generates the enum.
117    */

118   public void generateWhere(CharBuffer cb)
119   {
120     if (_isOrdinal)
121       cb.append(_ordinal);
122     else {
123       cb.append('\'');
124       cb.append(_name);
125       cb.append('\'');
126     }
127   }
128
129   /**
130    * Generates the (update) enum.
131    */

132   public void generateUpdateWhere(CharBuffer cb)
133   {
134     generateWhere(cb);
135   }
136
137   /**
138    * Generates the having expression.
139    */

140   public void generateHaving(CharBuffer cb)
141   {
142     generateWhere(cb);
143   }
144
145   public String JavaDoc toString()
146   {
147     return "EnumExpr[" + _javaType.getName() + "." + _name + "(" + _ordinal + ")]";
148   }
149 }
150
Popular Tags