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 org.apache.commons.collections.iterators.ObjectArrayIterator;
036:
037: import java.util.ArrayList;
038: import java.util.Iterator;
039:
040: /**
041: * Array of constants
042: *
043: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
044: */
045: public class ConstantArray extends Constant {
046:
047: private Constant[] array;
048:
049: /**
050: * Converts a constant array to a string representation.
051: *
052: * @param arr the array
053: * @return the string value
054: */
055: private static String arrayToString(Constant arr[]) {
056: if (arr == null || arr.length == 0) {
057: return "(null)";
058: }
059: StringBuffer sb = new StringBuffer(1024);
060: sb.append("(");
061: int pos = 0;
062: for (Constant c : arr) {
063: if ((pos++) > 0) {
064: sb.append(",");
065: }
066: sb.append(c.getValue());
067: }
068: sb.append(")");
069: return sb.toString();
070: }
071:
072: /**
073: * Constructor
074: *
075: * @param arr the array
076: */
077: public ConstantArray(Constant[] arr) {
078: super (arrayToString(arr));
079: this .array = arr;
080: }
081:
082: /**
083: * Adds constant value to the end of the list.
084: *
085: * @param con the constant to add
086: * @return returns the ConstantArray itself
087: */
088: public ConstantArray add(Constant con) {
089: if (array == null)
090: array = new Constant[0];
091: ArrayList<Constant> result = new ArrayList<Constant>(
092: array.length + 1);
093: result.add(con);
094: for (Constant tmp : array) {
095: result.add(tmp);
096: }
097: array = result.toArray(new Constant[result.size()]);
098: return this ;
099: }
100:
101: /**
102: * Returns a string representation of the array list that can be used in sql.
103: * <p />
104: * Example result: '(1,2,3,4,5)'
105: * @return a string representation of the array list that can be used in sql.
106: */
107: @Override
108: public String getValue() {
109: return arrayToString(this .array);
110: }
111:
112: /**
113: * Returns a string representation of the array list.
114: *
115: * @return a string representation of the array list
116: */
117: @Override
118: public String toString() {
119: return arrayToString(this .array);
120: }
121:
122: /** {@inheritDoc} */
123: @Override
124: @SuppressWarnings({"unchecked"})
125: public Iterator<Constant> iterator() {
126: return new ObjectArrayIterator(array);
127: }
128:
129: /**
130: * Returns the number of elements in the array
131: * @return the number of elements in the array
132: */
133: public int size() {
134: return array == null ? 0 : array.length;
135: }
136:
137: }
|