001: /*
002: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
003: * (http://h2database.com/html/license.html).
004: * Initial Developer: H2 Group
005: */
006: package org.h2.value;
007:
008: import java.sql.PreparedStatement;
009: import java.sql.SQLException;
010:
011: import org.h2.message.Message;
012: import org.h2.util.MathUtils;
013:
014: /**
015: * Implementation of the ARRAY data type.
016: */
017: public class ValueArray extends Value {
018: private final Value[] values;
019: private int hash;
020:
021: public static ValueArray get(Value[] list) {
022: return new ValueArray(list);
023: }
024:
025: private ValueArray(Value[] list) {
026: this .values = list;
027: }
028:
029: public int hashCode() {
030: if (hash != 0) {
031: return hash;
032: }
033: int h = 1;
034: for (int i = 0; i < values.length;) {
035: h = h * 31 + values[i++].hashCode();
036: }
037: hash = h;
038: return h;
039: }
040:
041: public Value[] getList() {
042: return values;
043: }
044:
045: public int getType() {
046: return Value.ARRAY;
047: }
048:
049: public long getPrecision() {
050: return 0;
051: }
052:
053: public String getString() {
054: StringBuffer buff = new StringBuffer();
055: buff.append('(');
056: for (int i = 0; i < values.length; i++) {
057: if (i > 0) {
058: buff.append(", ");
059: }
060: buff.append(values[i].getString());
061: }
062: buff.append(')');
063: return buff.toString();
064: }
065:
066: protected int compareSecure(Value o, CompareMode mode)
067: throws SQLException {
068: ValueArray v = (ValueArray) o;
069: if (values == v.values) {
070: return 0;
071: }
072: int l = values.length;
073: int ol = v.values.length;
074: int len = Math.min(l, ol);
075: for (int i = 0; i < len; i++) {
076: Value v1 = values[i];
077: Value v2 = v.values[i];
078: int comp = v1.compareTo(v2, mode);
079: if (comp != 0) {
080: return comp;
081: }
082: }
083: return l > ol ? 1 : l == ol ? 0 : -1;
084: }
085:
086: public Object getObject() {
087: Object[] list = new Object[values.length];
088: for (int i = 0; i < values.length; i++) {
089: list[i] = values[i].getObject();
090: }
091: return list;
092: }
093:
094: public void set(PreparedStatement prep, int parameterIndex)
095: throws SQLException {
096: throw Message.getUnsupportedException();
097: }
098:
099: public String getSQL() {
100: StringBuffer buff = new StringBuffer();
101: buff.append('(');
102: for (int i = 0; i < values.length; i++) {
103: if (i > 0) {
104: buff.append(", ");
105: }
106: buff.append(values[i].getSQL());
107: }
108: buff.append(')');
109: return buff.toString();
110: }
111:
112: public int getDisplaySize() {
113: long size = 0;
114: for (int i = 0; i < values.length; i++) {
115: size += values[i].getDisplaySize();
116: }
117: return MathUtils.convertLongToInt(size);
118: }
119:
120: public boolean equals(Object other) {
121: if (!(other instanceof ValueArray)) {
122: return false;
123: }
124: ValueArray v = (ValueArray) other;
125: if (values == v.values) {
126: return true;
127: }
128: if (values.length != v.values.length) {
129: return false;
130: }
131: for (int i = 0; i < values.length; i++) {
132: if (!values[i].equals(v.values[i])) {
133: return false;
134: }
135: }
136: return true;
137: }
138:
139: public int getMemory() {
140: int memory = 0;
141: for (int i = 0; i < values.length; i++) {
142: memory += values[i].getMemory();
143: }
144: return memory;
145: }
146:
147: }
|