Skip to content

Commit 4bcfab5

Browse files
committed
Add visitor interface
1 parent b123089 commit 4bcfab5

16 files changed

Lines changed: 221 additions & 90 deletions

msgpack-core/src/main/java/org/msgpack/core/MessageOverflowException.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package org.msgpack.core;
22

33
/**
4-
* Created on 5/28/14.
4+
* Thrown when converting a message into a type that causes truncation or rounding.
5+
* For example, {@link org.msgpack.value.NumberValue#asInt()} throws this error if
6+
* it is a long value more than {@link java.lang.Integer#MAX_VALUE}.
57
*/
68
public class MessageOverflowException extends MessageTypeException {
79

msgpack-core/src/main/java/org/msgpack/value/Value.java

Lines changed: 69 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -27,230 +27,226 @@
2727
public interface Value {
2828
/**
2929
* Returns type of this value.
30-
*
30+
* <p/>
3131
* Note that you can't use <code>instanceof</code> to check type of a value because type of a mutable value is variable.
3232
*/
3333
public ValueType getValueType();
3434

3535
/**
3636
* Returns immutable copy of this value.
37-
*
37+
* <p/>
3838
* This method simply returns <code>this</code> without copying the value if this value is already immutable.
3939
*/
40-
public ImmutableValue immutableValue();
40+
ImmutableValue toImmutable();
4141

4242
/**
4343
* Returns true if type of this value is Nil.
44-
*
44+
* <p/>
4545
* If this method returns true, {@code asNilValue} never throws exceptions.
4646
* Note that you can't use <code>instanceof</code> or cast <code>((NilValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
4747
*/
48-
public boolean isNilValue();
48+
boolean isNilValue();
4949

5050
/**
5151
* Returns true if type of this value is Boolean.
52-
*
52+
* <p/>
5353
* If this method returns true, {@code asBooleanValue} never throws exceptions.
5454
* Note that you can't use <code>instanceof</code> or cast <code>((BooleanValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
5555
*/
56-
public boolean isBooleanValue();
56+
boolean isBooleanValue();
5757

5858
/**
5959
* Returns true if type of this value is Integer or Float.
60-
*
60+
* <p/>
6161
* If this method returns true, {@code asNumberValue} never throws exceptions.
6262
* Note that you can't use <code>instanceof</code> or cast <code>((NumberValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
6363
*/
64-
public boolean isNumberValue();
64+
boolean isNumberValue();
6565

6666
/**
6767
* Returns true if type of this value is Integer.
68-
*
68+
* <p/>
6969
* If this method returns true, {@code asIntegerValue} never throws exceptions.
7070
* Note that you can't use <code>instanceof</code> or cast <code>((IntegerValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
7171
*/
72-
public boolean isIntegerValue();
72+
boolean isIntegerValue();
7373

7474
/**
7575
* Returns true if type of this value is Float.
76-
*
76+
* <p/>
7777
* If this method returns true, {@code asFloatValue} never throws exceptions.
7878
* Note that you can't use <code>instanceof</code> or cast <code>((FloatValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
7979
*/
80-
public boolean isFloatValue();
80+
boolean isFloatValue();
8181

8282
/**
8383
* Returns true if type of this value is String or Binary.
84-
*
84+
* <p/>
8585
* If this method returns true, {@code asRawValue} never throws exceptions.
8686
* Note that you can't use <code>instanceof</code> or cast <code>((RawValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
8787
*/
88-
public boolean isRawValue();
88+
boolean isRawValue();
8989

9090
/**
9191
* Returns true if type of this value is Binary.
92-
*
92+
* <p/>
9393
* If this method returns true, {@code asBinaryValue} never throws exceptions.
9494
* Note that you can't use <code>instanceof</code> or cast <code>((BinaryValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
9595
*/
96-
public boolean isBinaryValue();
96+
boolean isBinaryValue();
9797

9898
/**
9999
* Returns true if type of this value is String.
100-
*
100+
* <p/>
101101
* If this method returns true, {@code asStringValue} never throws exceptions.
102102
* Note that you can't use <code>instanceof</code> or cast <code>((StringValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
103103
*/
104-
public boolean isStringValue();
104+
boolean isStringValue();
105105

106106
/**
107107
* Returns true if type of this value is Array.
108-
*
108+
* <p/>
109109
* If this method returns true, {@code asArrayValue} never throws exceptions.
110110
* Note that you can't use <code>instanceof</code> or cast <code>((ArrayValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
111111
*/
112-
public boolean isArrayValue();
112+
boolean isArrayValue();
113113

114114
/**
115115
* Returns true if type of this value is Map.
116-
*
116+
* <p/>
117117
* If this method returns true, {@code asMapValue} never throws exceptions.
118118
* Note that you can't use <code>instanceof</code> or cast <code>((MapValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
119119
*/
120-
public boolean isMapValue();
120+
boolean isMapValue();
121121

122122
/**
123123
* Returns true if type of this an Extension.
124-
*
124+
* <p/>
125125
* If this method returns true, {@code asExtensionValue} never throws exceptions.
126126
* Note that you can't use <code>instanceof</code> or cast <code>((ExtensionValue) thisValue)</code> to check type of a value because
127127
* type of a mutable value is variable.
128128
*/
129-
public boolean isExtensionValue();
129+
boolean isExtensionValue();
130130

131131
/**
132132
* Returns the value as {@code NilValue}. Otherwise throws {@code MessageTypeCastException}.
133-
*
133+
* <p/>
134134
* Note that you can't use <code>instanceof</code> or cast <code>((NilValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
135135
*
136-
* @throws MessageTypeCastException
137-
* If type of this value is not Nil.
136+
* @throws MessageTypeCastException If type of this value is not Nil.
138137
*/
139-
public NilValue asNilValue();
138+
NilValue asNilValue();
140139

141140
/**
142141
* Returns the value as {@code BooleanValue}. Otherwise throws {@code MessageTypeCastException}.
143-
*
142+
* <p/>
144143
* Note that you can't use <code>instanceof</code> or cast <code>((BooleanValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
145144
*
146-
* @throws MessageTypeCastException
147-
* If type of this value is not Boolean.
145+
* @throws MessageTypeCastException If type of this value is not Boolean.
148146
*/
149-
public BooleanValue asBooleanValue();
147+
BooleanValue asBooleanValue();
150148

151149
/**
152150
* Returns the value as {@code NumberValue}. Otherwise throws {@code MessageTypeCastException}.
153-
*
151+
* <p/>
154152
* Note that you can't use <code>instanceof</code> or cast <code>((NumberValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
155153
*
156-
* @throws MessageTypeCastException
157-
* If type of this value is not Integer or Float.
154+
* @throws MessageTypeCastException If type of this value is not Integer or Float.
158155
*/
159-
public NumberValue asNumberValue();
156+
NumberValue asNumberValue();
160157

161158
/**
162159
* Returns the value as {@code IntegerValue}. Otherwise throws {@code MessageTypeCastException}.
163-
*
160+
* <p/>
164161
* Note that you can't use <code>instanceof</code> or cast <code>((IntegerValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
165162
*
166-
* @throws MessageTypeCastException
167-
* If type of this value is not Integer.
163+
* @throws MessageTypeCastException If type of this value is not Integer.
168164
*/
169-
public IntegerValue asIntegerValue();
165+
IntegerValue asIntegerValue();
170166

171167
/**
172168
* Returns the value as {@code FloatValue}. Otherwise throws {@code MessageTypeCastException}.
173-
*
169+
* <p/>
174170
* Note that you can't use <code>instanceof</code> or cast <code>((FloatValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
175171
*
176-
* @throws MessageTypeCastException
177-
* If type of this value is not Float.
172+
* @throws MessageTypeCastException If type of this value is not Float.
178173
*/
179-
public FloatValue asFloatValue();
174+
FloatValue asFloatValue();
180175

181176
/**
182177
* Returns the value as {@code RawValue}. Otherwise throws {@code MessageTypeCastException}.
183-
*
178+
* <p/>
184179
* Note that you can't use <code>instanceof</code> or cast <code>((RawValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
185180
*
186-
* @throws MessageTypeCastException
187-
* If type of this value is not Binary or String.
181+
* @throws MessageTypeCastException If type of this value is not Binary or String.
188182
*/
189-
public RawValue asRawValue();
183+
RawValue asRawValue();
190184

191185
/**
192186
* Returns the value as {@code BinaryValue}. Otherwise throws {@code MessageTypeCastException}.
193-
*
187+
* <p/>
194188
* Note that you can't use <code>instanceof</code> or cast <code>((BinaryValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
195189
*
196-
* @throws MessageTypeCastException
197-
* If type of this value is not Binary.
190+
* @throws MessageTypeCastException If type of this value is not Binary.
198191
*/
199-
public BinaryValue asBinaryValue();
192+
BinaryValue asBinaryValue();
200193

201194
/**
202195
* Returns the value as {@code StringValue}. Otherwise throws {@code MessageTypeCastException}.
203-
*
196+
* <p/>
204197
* Note that you can't use <code>instanceof</code> or cast <code>((StringValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
205198
*
206-
* @throws MessageTypeCastException
207-
* If type of this value is not String.
199+
* @throws MessageTypeCastException If type of this value is not String.
208200
*/
209-
public StringValue asStringValue();
201+
StringValue asStringValue();
210202

211203
/**
212204
* Returns the value as {@code ArrayValue}. Otherwise throws {@code MessageTypeCastException}.
213-
*
205+
* <p/>
214206
* Note that you can't use <code>instanceof</code> or cast <code>((ArrayValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
215207
*
216-
* @throws MessageTypeCastException
217-
* If type of this value is not Array.
208+
* @throws MessageTypeCastException If type of this value is not Array.
218209
*/
219-
public ArrayValue asArrayValue();
210+
ArrayValue asArrayValue();
220211

221212
/**
222213
* Returns the value as {@code MapValue}. Otherwise throws {@code MessageTypeCastException}.
223-
*
214+
* <p/>
224215
* Note that you can't use <code>instanceof</code> or cast <code>((MapValue) thisValue)</code> to check type of a value because type of a mutable value is variable.
225216
*
226-
* @throws MessageTypeCastException
227-
* If type of this value is not Map.
217+
* @throws MessageTypeCastException If type of this value is not Map.
228218
*/
229-
public MapValue asMapValue();
219+
MapValue asMapValue();
230220

231221
/**
232222
* Returns the value as {@code ExtensionValue}. Otherwise throws {@code MessageTypeCastException}.
233-
*
223+
* <p/>
234224
* Note that you can't use <code>instanceof</code> or cast <code>((ExtensionValue) thisValue)</code> to check type of a value
235225
* because type of a mutable value is variable.
236226
*
237-
* @throws MessageTypeCastException
238-
* If type of this value is not an Extension.
227+
* @throws MessageTypeCastException If type of this value is not an Extension.
239228
*/
240-
public ExtensionValue asExtensionValue();
229+
ExtensionValue asExtensionValue();
241230

242231
/**
243232
* Serializes the value using the specified {@code MessagePacker}
244233
*
245-
* @see MessagePacker
234+
* @see MessagePacker
246235
*/
247-
public void writeTo(MessagePacker pk) throws IOException;
236+
void writeTo(MessagePacker pk) throws IOException;
237+
238+
239+
/**
240+
* Accept a visitor to traverse this value
241+
* @param visitor
242+
*/
243+
void accept(ValueVisitor visitor);
248244

249245
/**
250246
* Compares this value to the specified object.
251-
*
247+
* <p/>
252248
* This method returns {@code true} if type and value are equivalent.
253249
* If this value is {@code MapValue} or {@code ArrayValue}, this method check equivalence of elements recursively.
254250
*/
255-
public boolean equals(Object obj);
251+
boolean equals(Object obj);
256252
}

msgpack-core/src/main/java/org/msgpack/value/ValueFactory.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public static ImmutableArrayValue newArray(Value[] array) {
103103
return new ImmutableArrayValueImpl(Arrays.copyOf(array, array.length));
104104
}
105105

106+
public static ImmutableArrayValue emptyArray() {
107+
return ImmutableArrayValueImpl.empty();
108+
}
109+
110+
106111
public static <K extends Value, V extends Value>
107112
ImmutableMapValue newMap(Map<K, V> map) {
108113
Value[] kvs = new Value[map.size() * 2];
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package org.msgpack.value;
17+
18+
/**
19+
* Interface for implementing the visitor pattern on message-packed values
20+
*/
21+
public interface ValueVisitor {
22+
23+
void visitNil();
24+
void visitBoolean(BooleanValue v);
25+
void visitInteger(IntegerValue v);
26+
void visitFloat(FloatValue v);
27+
void visitBinary(BinaryValue v);
28+
void visitString(StringValue v);
29+
void visitArray(ArrayValue v);
30+
void visitMap(MapValue v);
31+
void visitExtension(ExtensionValue v);
32+
33+
/**
34+
* Visitor can use this method to handle an exception occurred while visiting a value
35+
* @param e
36+
*/
37+
void onError(Exception e);
38+
}

0 commit comments

Comments
 (0)