001: /*
002: * $Id: ColumnIdentifier.java,v 1.25 2006/01/10 21:02:37 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: * An identifier for a column.
047: * <p>
048: * Column names and aliases always stored (and returned) in upper case.
049: *
050: * @version $Revision: 1.25 $ $Date: 2006/01/10 21:02:37 $
051: * @author Rodney Waldhoff
052: * @author Chuck Burdick
053: * @author Ahimanikya Satapathy
054: */
055: public class ColumnIdentifier extends BaseSelectable implements
056: Selectable {
057:
058: /**
059: * @param column the local name of my column
060: */
061: public ColumnIdentifier(String column) {
062: this (null, column);
063: }
064:
065: /**
066: * @param table my table, which may be <code>null</code>
067: * @param column my column
068: */
069: public ColumnIdentifier(TableIdentifier table, String columnName) {
070: this (table, columnName, null);
071: }
072:
073: /**
074: * @param table my table, which may be <code>null</code>
075: * @param column my column
076: * @param columnAlias the alias for my column, which may be <code>null</code>
077: */
078: public ColumnIdentifier(TableIdentifier table, String columnName,
079: String columnAlias) {
080: this (table, columnName, columnAlias, null);
081: }
082:
083: /**
084: * @param table my table, which may be <code>null</code>
085: * @param column my column
086: * @param columnAlias the alias for my column, which may be <code>null</code>
087: * @param type the {@link DataType}of my column, which may be <code>null</code>
088: */
089: public ColumnIdentifier(TableIdentifier table, String columnName,
090: String columnAlias, DataType type) {
091: setName(columnName);
092:
093: if (null == _table) {
094: _table = table;
095: }
096:
097: setAlias(columnAlias);
098: _type = (null == type ? AnyType.INSTANCE : type);
099: }
100:
101: /**
102: * Returns <code>true</code> iff <i>otherobject </i> is a {@link ColumnIdentifier}
103: * whose name, table identifier, and alias are equal to mine.
104: */
105: public boolean equals(Object otherobject) {
106: if (this == otherobject
107: || (_canonicalForm != null && _canonicalForm == otherobject)) {
108: return true;
109: }
110:
111: if (otherobject instanceof ColumnIdentifier) {
112: ColumnIdentifier that = (ColumnIdentifier) otherobject;
113: String this Name = getName();
114: String thatName = that.getName();
115: String this Alias = getAlias();
116: String thatAlias = that.getAlias();
117: return ((null == this Name ? null == thatName : this Name
118: .equals(thatName))
119: && (null == _table ? null == that._table : _table
120: .equals(that._table)) && (null == this Alias ? null == thatAlias
121: : this Alias.equals(thatAlias)));
122: }
123: return false;
124: }
125:
126: /**
127: * Returns the value of the column I identify within the given <i>row </i>.
128: */
129: public Object evaluate(RowDecorator row) throws AxionException {
130: if (null == row) {
131: throw new AxionException(
132: "Expected non-null RowDecorator here.");
133: }
134: return row.get(this );
135: }
136:
137: public ColumnIdentifier getCanonicalIdentifier() {
138: ColumnIdentifier cid = _canonicalForm;
139: if (null == cid) {
140: cid = new ColumnIdentifier(_table, getName(), null, _type);
141: _canonicalForm = cid;
142: }
143: return cid;
144: }
145:
146: /**
147: * Returns my {@link DataType}, if any.
148: */
149: public final DataType getDataType() {
150: return _type;
151: }
152:
153: /**
154: * Returns the alias name of my table or null. Unlike
155: * <code>{@link #getTableIdentifier getTableIdentifier()}.{@link TableIdentifier#getTableAlias getTableAlias()}</code>
156: * this method will return <code>null</code> when I don't have a table identifier.
157: */
158: public final String getTableAlias() {
159: return (null == _table ? null : _table.getTableAlias());
160: }
161:
162: /**
163: * Returns my table identifier, if any.
164: */
165: public final TableIdentifier getTableIdentifier() {
166: return _table;
167: }
168:
169: /**
170: * Returns the name of my table or null. Unlike
171: * <code>{@link #getTableIdentifier getTableIdentifier()}.{@link TableIdentifier#getTableName getTableName()}</code>
172: * this method will return <code>null</code> when I don't have a table identifier.
173: */
174: public final String getTableName() {
175: return (null == _table ? null : _table.getTableName());
176: }
177:
178: /**
179: * Returns a hash code in keeping with the standard {@link Object#equals equals}/
180: * {@link Object#hashCode hashCode}contract.
181: */
182: public int hashCode() {
183: int hashCode = _hash;
184: if (hashCode == 0) {
185: String name = getName();
186: String alias = getAlias();
187: if (null != name) {
188: hashCode = name.hashCode();
189: }
190: if (null != alias) {
191: hashCode ^= alias.hashCode();
192: }
193: if (null != _table) {
194: hashCode ^= _table.hashCode() << 4;
195: }
196: _hash = hashCode;
197:
198: }
199: return hashCode;
200: }
201:
202: /**
203: * Sets my {@link DataType}, if any.
204: */
205: public void setDataType(DataType type) {
206: _type = (null == type ? AnyType.INSTANCE : type);
207: clearCanonicalForm();
208: }
209:
210: /**
211: * Sets the name of this column, and the name of my table if the given name includes "
212: * <code>.</code>".
213: */
214: public void setName(String column) {
215: if (column != null) {
216: int pivot = column.indexOf(".");
217: if (pivot != -1) {
218: setTableIdentifier(new TableIdentifier(column
219: .substring(0, pivot)));
220: column = column.substring(pivot + 1);
221: }
222: super .setName(column);
223: }
224: clearCanonicalForm();
225: }
226:
227: /**
228: * Sets my table identifier, if any.
229: */
230: public void setTableIdentifier(TableIdentifier table) {
231: _table = table;
232: _hash = 0;
233: clearCanonicalForm();
234: }
235:
236: /**
237: * Returns a <code>String</code> representation of me, suitable for debugging
238: * output.
239: */
240: public String toString() {
241: StringBuffer result = new StringBuffer();
242: if (getTableIdentifier() != null) {
243: result.append("(");
244: result.append(getTableIdentifier().toString());
245: result.append(").");
246: }
247: result.append(getName());
248: if (null != getAlias()) {
249: result.append(" AS ");
250: result.append(getAlias());
251: }
252: return result.toString();
253: }
254:
255: private final void clearCanonicalForm() {
256: _canonicalForm = null;
257: }
258:
259: private ColumnIdentifier _canonicalForm;
260:
261: /** My {@link TableIdentifier}, if any. */
262: private TableIdentifier _table;
263: /** My {@link DataType}, if any. */
264: private DataType _type;
265:
266: private static final long serialVersionUID = -5021851410960110853L;
267: }
|