001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.sqlParser;
034:
035: import java.util.ArrayList;
036:
037: /**
038: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
039: */
040: public class Value {
041:
042: private Object value;
043: private ArrayList<String> arFunctions = null;
044:
045: /**
046: * Compares to value objects.
047: *
048: * @param obj the object
049: * @return true if the objects are equal
050: */
051: public boolean equals(Object obj) {
052: if (!this .getClass().equals(obj.getClass())) {
053: return false;
054: }
055: Value comp = (Value) obj;
056: return this .getComperatorString().equals(
057: comp.getComperatorString());
058: }
059:
060: /**
061: * Helper fct for equals.
062: *
063: * @return -
064: */
065: private String getComperatorString() {
066: return value + "." + getFunctionsStart();
067: }
068:
069: /**
070: * Constructor.
071: *
072: * @param sValue the value
073: */
074: protected Value(Object sValue) {
075: this .value = sValue;
076: }
077:
078: /**
079: * Returns true if this is a NULL value.
080: *
081: * @return true if this is a NULL value
082: */
083: public boolean isNull() {
084: return this .value == null;
085: }
086:
087: /**
088: * Adds a function to the value info.
089: *
090: * @param functionName the name of the function
091: * @return the value info itself
092: */
093: public Value addFunction(String functionName) {
094: // Remove ending "(" if needed
095: if (functionName.endsWith("(")) {
096: functionName = functionName.substring(0, functionName
097: .length() - 1);
098: }
099:
100: // Add at beginning
101: if (arFunctions == null) {
102: arFunctions = new ArrayList<String>(5);
103: }
104: arFunctions.add(0, functionName);
105: return this ;
106: }
107:
108: /**
109: * Returns the value.
110: *
111: * @return the value
112: */
113: public Object getValue() {
114: return value;
115: }
116:
117: /**
118: * Returns all functions that wrapp the value in the correct order.
119: *
120: * @return all functions that wrapp the value.
121: */
122: public String[] getFunctions() {
123: if (arFunctions == null) {
124: return new String[0];
125: }
126: return arFunctions.toArray(new String[arFunctions.size()]);
127: }
128:
129: /**
130: * Returns the functions start, eg "upper(max(".
131: *
132: * @return the functions start
133: */
134: public String getFunctionsStart() {
135: if (arFunctions == null)
136: return "";
137: String result = "";
138: for (String fct : arFunctions) {
139: result += fct + "(";
140: }
141: return result;
142: }
143:
144: /**
145: * Returns the functions end, eg "))".
146: *
147: * @return the functions start
148: */
149: public String getFunctionsEnd() {
150: if (arFunctions == null)
151: return "";
152: String result = "";
153: //noinspection ForLoopReplaceableByForEach
154: for (int i = 0; i < arFunctions.size(); i++) {
155: result += ")";
156: }
157: return result;
158: }
159:
160: /**
161: * Returns true if the vlaue is wrapped by at least one function.
162: *
163: * @return true if the vlaue is wrapped by at least one function.
164: */
165: public boolean hasFunction() {
166: return (arFunctions != null && arFunctions.size() > 0);
167: }
168:
169: /**
170: * Returns a string representation of the object.
171: *
172: * @return a string representation of the object
173: */
174: public String toString() {
175: return this.getFunctionsStart() + this.value
176: + this.getFunctionsEnd();
177: }
178:
179: }
|