KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > value > ValueArray


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.value;
6
7 import java.sql.PreparedStatement JavaDoc;
8 import java.sql.SQLException JavaDoc;
9
10 import org.h2.message.Message;
11
12 public class ValueArray extends Value {
13     private Value[] values;
14     private int hash;
15
16     public static ValueArray get(Value[] list) {
17         return new ValueArray(list);
18     }
19
20     private ValueArray(Value[] list) {
21         this.values = list;
22     }
23
24     public int hashCode() {
25         if (hash != 0) {
26             return hash;
27         }
28         int h = 1;
29         for (int i = 0; i < values.length;) {
30             h = h * 31 + values[i++].hashCode();
31         }
32         return hash = h;
33     }
34
35     public Value[] getList() {
36         return values;
37     }
38     
39     public int compareTo(ValueArray other, CompareMode mode) throws SQLException JavaDoc {
40         for (int i = 0; i < values.length; i++) {
41             Value v1 = values[i];
42             Value v2 = other.values[i];
43             int comp = v1.compareTo(v2, mode);
44             if (comp != 0) {
45                 return comp;
46             }
47         }
48         return 0;
49     }
50
51     public int getType() {
52         return Value.ARRAY;
53     }
54
55     public long getPrecision() {
56         return 0;
57     }
58
59     public String JavaDoc getString() throws SQLException JavaDoc {
60         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
61         buff.append('(');
62         for (int i = 0; i < values.length; i++) {
63             if (i > 0) {
64                 buff.append(", ");
65             }
66             buff.append(values[i].getString());
67         }
68         buff.append(')');
69         return buff.toString();
70     }
71
72 // public String getJavaString() {
73
// StringBuffer buff = new StringBuffer();
74
// buff.append('{');
75
// for (int i = 0; i < values.length; i++) {
76
// if (i > 0) {
77
// buff.append(", ");
78
// }
79
// buff.append(values[i].getJavaString());
80
// }
81
// buff.append('}');
82
// return buff.toString();
83
// }
84

85     protected int compareSecure(Value o, CompareMode mode) throws SQLException JavaDoc {
86         ValueArray v = (ValueArray) o;
87         if (values == v.values) {
88             return 0;
89         }
90         if (values.length != v.values.length) {
91             return values.length > v.values.length ? 1 : -1;
92         }
93         for (int i = 0; i < values.length; i++) {
94             Value v1 = values[i];
95             Value v2 = v.values[i];
96             int c;
97             if (v1 == ValueNull.INSTANCE) {
98                 c = v2 == ValueNull.INSTANCE ? 0 : -1;
99             } else if (v2 == ValueNull.INSTANCE) {
100                 c = 1;
101             } else {
102                 c = v1.compareSecure(v2, mode);
103             }
104             if (c != 0) {
105                 return c;
106             }
107         }
108         return 0;
109     }
110
111     public Object JavaDoc getObject() throws SQLException JavaDoc {
112         Object JavaDoc[] list = new Object JavaDoc[values.length];
113         for (int i = 0; i < values.length; i++) {
114             list[i] = values[i].getObject();
115         }
116         return list;
117     }
118
119     public void set(PreparedStatement JavaDoc prep, int parameterIndex) throws SQLException JavaDoc {
120         throw Message.getUnsupportedException();
121     }
122
123     public String JavaDoc getSQL() {
124         StringBuffer JavaDoc buff = new StringBuffer JavaDoc();
125         buff.append('(');
126         for (int i = 0; i < values.length; i++) {
127             if (i > 0) {
128                 buff.append(", ");
129             }
130             buff.append(values[i].getSQL());
131         }
132         buff.append(')');
133         return buff.toString();
134     }
135     
136     public int getDisplaySize() {
137         int size = 0;
138         for(int i=0; i<values.length; i++) {
139             size += values[i].getDisplaySize();
140         }
141         return size;
142     }
143
144     protected boolean isEqual(Value o) {
145         if(!(o instanceof ValueArray)) {
146             return false;
147         }
148         ValueArray v = (ValueArray) o;
149         if (values == v.values) {
150             return true;
151         }
152         if (values.length != v.values.length) {
153             return false;
154         }
155         for (int i = 0; i < values.length; i++) {
156             if(!values[i].isEqual(v.values[i])) {
157                 return false;
158             }
159         }
160         return true;
161     }
162
163 }
164
Popular Tags