001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xpath.expr;
030:
031: import com.caucho.util.CharBuffer;
032:
033: /**
034: * A variable containing a double
035: */
036: public class NumberVar extends Var {
037: private static NumberVar[] intVar = new NumberVar[256];
038:
039: private double value;
040: private Double objValue;
041: private String strValue;
042:
043: /**
044: * Creates a new object variable with the object.
045: */
046: private NumberVar(double value) {
047: this .value = value;
048: }
049:
050: /**
051: * Creates a new number var with the object.
052: */
053: public static NumberVar create(double value) {
054: NumberVar var;
055:
056: int index = (int) value;
057:
058: if (index == value && index > -128 && index < 128) {
059: var = intVar[index + 128];
060: if (var == null) {
061: var = new NumberVar(value);
062: intVar[index + 128] = var;
063: }
064:
065: return var;
066: } else
067: return new NumberVar(value);
068: }
069:
070: /**
071: * Returns the value as a double.
072: */
073: double getDouble() {
074: return value;
075: }
076:
077: /**
078: * Returns the value as an object.
079: */
080: Object getObject() {
081: if (objValue == null)
082: objValue = new Double(value);
083:
084: return objValue;
085: }
086:
087: /**
088: * Returns the value as a string.
089: */
090: String getString() {
091: if (strValue == null) {
092: if ((int) value == value) {
093: CharBuffer cb = CharBuffer.allocate();
094: cb.append((int) value);
095: strValue = cb.close();
096: } else
097: strValue = String.valueOf(value);
098: }
099:
100: return strValue;
101: }
102:
103: /**
104: * Appends the buffer with the string value.
105: */
106: void getString(CharBuffer cb) {
107: if ((int) value == value)
108: cb.append((int) value);
109: else
110: cb.append(value);
111: }
112:
113: /**
114: * Clones the variable.
115: */
116: public Object clone() {
117: NumberVar var = (NumberVar) NumberVar.create(value);
118: var.objValue = objValue;
119: var.strValue = strValue;
120:
121: return var;
122: }
123:
124: public String toString() {
125: return "[NumberVar " + value + "]";
126: }
127: }
|