001: /*
002: * $Id: Literal.java,v 1.16 2005/12/20 18:32:40 ahimanikya Exp $
003: * =======================================================================
004: * Copyright (c) 2002-2006 Axion Development Team. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above
011: * copyright notice, this list of conditions and the following
012: * disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The names "Tigris", "Axion", nor the names of its contributors may
020: * not be used to endorse or promote products derived from this
021: * software without specific prior written permission.
022: *
023: * 4. Products derived from this software may not be called "Axion", nor
024: * may "Tigris" or "Axion" appear in their names without specific prior
025: * written permission.
026: *
027: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
028: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
029: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
030: * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
031: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
032: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
033: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
034: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
035: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
036: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
037: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
038: * =======================================================================
039: */
040:
041: package org.axiondb;
042:
043: import org.axiondb.types.AnyType;
044:
045: /**
046: * A {@link DataType typed}literal value.
047: *
048: * @version $Revision: 1.16 $ $Date: 2005/12/20 18:32:40 $
049: * @author Rodney Waldhoff
050: * @author Chuck Burdick
051: * @author Ahimanikya Satapathy
052: */
053: public class Literal extends BaseSelectable implements Selectable {
054:
055: public Literal(Object value) {
056: this (value, AnyType.INSTANCE);
057: }
058:
059: public Literal(Object value, DataType type) {
060: _value = value;
061: _type = type;
062: }
063:
064: protected Literal(DataType type) {
065: this (null, type);
066: }
067:
068: /**
069: * Returns <code>true</code> iff <i>otherobject </i> is a {@link Literal}whose name
070: * are equal to mine.
071: */
072: public boolean equals(Object otherobject) {
073: if (this == otherobject) {
074: return true;
075: }
076:
077: if (otherobject instanceof Literal) {
078: Literal that = (Literal) otherobject;
079: return getName().equals(that.getName());
080: }
081: return false;
082: }
083:
084: public Object evaluate() throws AxionException {
085: if (null == _value && null == _type) {
086: return _value;
087: } else if (null == _type) {
088: return _value;
089: } else {
090: // store the converted value, so we may not have to convert again.
091: if (!_evaluated) {
092: _value = _type.convert(_value);
093: _evaluated = true;
094: }
095: return _value;
096: }
097: }
098:
099: /**
100: * @param row is ignored and may be null.
101: * @see #evaluate
102: */
103: public final Object evaluate(RowDecorator row)
104: throws AxionException {
105: return evaluate();
106: }
107:
108: public DataType getDataType() {
109: return _type;
110: }
111:
112: /**
113: * Returns a hash code in keeping with the standard {@link Object#equals equals}/
114: * {@link Object#hashCode hashCode}contract.
115: */
116: public int hashCode() {
117: int hashCode = _hash;
118: if (hashCode == 0) {
119: hashCode = getName().hashCode();
120: _hash = hashCode;
121: }
122: return hashCode;
123: }
124:
125: public void setDataType(DataType type) {
126: _type = type;
127: _hash = 0;
128: }
129:
130: /**
131: * Returns my Literal name.
132: */
133: public String getLabel() {
134: return getName();
135: }
136:
137: /**
138: * Returns the name of Literal, if any.
139: */
140: public String getName() {
141: if (getAlias() != null) {
142: return getAlias();
143: }
144: return String.valueOf(_value);
145: }
146:
147: /**
148: * Returns a <code>String</code> representation of me, suitable for debugging
149: * output.
150: */
151: public String toString() {
152: return getName();
153: }
154:
155: private boolean _evaluated = false;
156: private DataType _type;
157: protected Object _value;
158: }
|