Mercurial > hg > Members > sugi > MessagePack-java
comparison src/main/java/org/msgpack/type/ArrayValueImpl.java @ 0:cb825acd883a
first commit
author | sugi |
---|---|
date | Sat, 18 Oct 2014 15:06:15 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:cb825acd883a |
---|---|
1 // | |
2 // MessagePack for Java | |
3 // | |
4 // Copyright (C) 2009 - 2013 FURUHASHI Sadayuki | |
5 // | |
6 // Licensed under the Apache License, Version 2.0 (the "License"); | |
7 // you may not use this file except in compliance with the License. | |
8 // You may obtain a copy of the License at | |
9 // | |
10 // http://www.apache.org/licenses/LICENSE-2.0 | |
11 // | |
12 // Unless required by applicable law or agreed to in writing, software | |
13 // distributed under the License is distributed on an "AS IS" BASIS, | |
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
15 // See the License for the specific language governing permissions and | |
16 // limitations under the License. | |
17 // | |
18 package org.msgpack.type; | |
19 | |
20 import java.util.ListIterator; | |
21 import java.io.IOException; | |
22 import org.msgpack.packer.Packer; | |
23 | |
24 class ArrayValueImpl extends AbstractArrayValue { | |
25 private static ArrayValueImpl emptyInstance = new ArrayValueImpl(new Value[0], true); | |
26 | |
27 public static ArrayValue getEmptyInstance() { | |
28 return emptyInstance; | |
29 } | |
30 | |
31 private Value[] array; | |
32 | |
33 @Override | |
34 public Value[] getElementArray() { | |
35 return array; | |
36 } | |
37 | |
38 ArrayValueImpl(Value[] array, boolean gift) { | |
39 if (gift) { | |
40 this.array = array; | |
41 } else { | |
42 this.array = new Value[array.length]; | |
43 System.arraycopy(array, 0, this.array, 0, array.length); | |
44 } | |
45 } | |
46 | |
47 @Override | |
48 public int size() { | |
49 return array.length; | |
50 } | |
51 | |
52 @Override | |
53 public boolean isEmpty() { | |
54 return array.length == 0; | |
55 } | |
56 | |
57 @Override | |
58 public Value get(int index) { | |
59 if (index < 0 || array.length <= index) { | |
60 throw new IndexOutOfBoundsException(); | |
61 } | |
62 return array[index]; | |
63 } | |
64 | |
65 @Override | |
66 public int indexOf(Object o) { | |
67 if (o == null) { | |
68 return -1; // FIXME NullPointerException? | |
69 } | |
70 for (int i = 0; i < array.length; i++) { | |
71 if (array[i].equals(o)) { | |
72 return i; | |
73 } | |
74 } | |
75 return -1; | |
76 } | |
77 | |
78 @Override | |
79 public int lastIndexOf(Object o) { | |
80 if (o == null) { | |
81 return -1; // FIXME NullPointerException? | |
82 } | |
83 for (int i = array.length - 1; i >= 0; i--) { | |
84 if (array[i].equals(o)) { | |
85 return i; | |
86 } | |
87 } | |
88 return -1; | |
89 } | |
90 | |
91 @Override | |
92 public void writeTo(Packer pk) throws IOException { | |
93 pk.writeArrayBegin(array.length); | |
94 for (int i = 0; i < array.length; i++) { | |
95 array[i].writeTo(pk); | |
96 } | |
97 pk.writeArrayEnd(); | |
98 } | |
99 | |
100 @Override | |
101 public boolean equals(Object o) { | |
102 if (o == this) { | |
103 return true; | |
104 } | |
105 if (!(o instanceof Value)) { | |
106 return false; | |
107 } | |
108 Value v = (Value) o; | |
109 if (!v.isArrayValue()) { | |
110 return false; | |
111 } | |
112 | |
113 if (v.getClass() == ArrayValueImpl.class) { | |
114 return equals((ArrayValueImpl) v); | |
115 } | |
116 | |
117 ListIterator<Value> oi = v.asArrayValue().listIterator(); | |
118 int i = 0; | |
119 while (i < array.length) { | |
120 if (!oi.hasNext() || !array[i].equals(oi.next())) { | |
121 return false; | |
122 } | |
123 i++; | |
124 } | |
125 return !oi.hasNext(); | |
126 } | |
127 | |
128 private boolean equals(ArrayValueImpl o) { | |
129 if (array.length != o.array.length) { | |
130 return false; | |
131 } | |
132 for (int i = 0; i < array.length; i++) { | |
133 if (!array[i].equals(o.array[i])) { | |
134 return false; | |
135 } | |
136 } | |
137 return true; | |
138 } | |
139 | |
140 // TODO compareTo? | |
141 | |
142 @Override | |
143 public int hashCode() { | |
144 int h = 1; | |
145 for (int i = 0; i < array.length; i++) { | |
146 Value obj = array[i]; | |
147 h = 31 * h + obj.hashCode(); | |
148 } | |
149 return h; | |
150 } | |
151 | |
152 @Override | |
153 public String toString() { | |
154 return toString(new StringBuilder()).toString(); | |
155 } | |
156 | |
157 @Override | |
158 public StringBuilder toString(StringBuilder sb) { | |
159 if (array.length == 0) { | |
160 return sb.append("[]"); | |
161 } | |
162 sb.append("["); | |
163 sb.append(array[0]); | |
164 for (int i = 1; i < array.length; i++) { | |
165 sb.append(","); | |
166 array[i].toString(sb); | |
167 } | |
168 sb.append("]"); | |
169 return sb; | |
170 } | |
171 } |