KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > xpath > expr > NumberVar


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 SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.xpath.expr;
30
31 import com.caucho.util.CharBuffer;
32
33 /**
34  * A variable containing a double
35  */

36 public class NumberVar extends Var {
37   private static NumberVar []intVar = new NumberVar[256];
38   
39   private double value;
40   private Double JavaDoc objValue;
41   private String JavaDoc strValue;
42
43   /**
44    * Creates a new object variable with the object.
45    */

46   private NumberVar(double value)
47   {
48     this.value = value;
49   }
50
51   /**
52    * Creates a new number var with the object.
53    */

54   public static NumberVar create(double value)
55   {
56     NumberVar var;
57
58     int index = (int) value;
59     
60     if (index == value && index > -128 && index < 128) {
61       var = intVar[index + 128];
62       if (var == null) {
63         var = new NumberVar(value);
64         intVar[index + 128] = var;
65       }
66
67       return var;
68     }
69     else
70       return new NumberVar(value);
71   }
72   
73   /**
74    * Returns the value as a double.
75    */

76   double getDouble()
77   {
78     return value;
79   }
80   
81   /**
82    * Returns the value as an object.
83    */

84   Object JavaDoc getObject()
85   {
86     if (objValue == null)
87       objValue = new Double JavaDoc(value);
88     
89     return objValue;
90   }
91   
92   /**
93    * Returns the value as a string.
94    */

95   String JavaDoc getString()
96   {
97     if (strValue == null) {
98       if ((int) value == value) {
99         CharBuffer cb = CharBuffer.allocate();
100         cb.append((int) value);
101         strValue = cb.close();
102       }
103       else
104         strValue = String.valueOf(value);
105     }
106
107     return strValue;
108   }
109   
110   /**
111    * Appends the buffer with the string value.
112    */

113   void getString(CharBuffer cb)
114   {
115     if ((int) value == value)
116       cb.append((int) value);
117     else
118       cb.append(value);
119   }
120
121   /**
122    * Clones the variable.
123    */

124   public Object JavaDoc clone()
125   {
126     NumberVar var = (NumberVar) NumberVar.create(value);
127     var.objValue = objValue;
128     var.strValue = strValue;
129     
130     return var;
131   }
132   
133   public String JavaDoc toString()
134   {
135     return "[NumberVar " + value + "]";
136   }
137 }
138
Popular Tags