/
ShapeVisitor.java
228 lines (176 loc) · 5.66 KB
/
ShapeVisitor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package software.amazon.smithy.model.shapes;
/**
* Shape visitor pattern.
*
* @param <R> Return type of the visitor.
* @see Default
*/
public interface ShapeVisitor<R> {
R blobShape(BlobShape shape);
R booleanShape(BooleanShape shape);
R listShape(ListShape shape);
@Deprecated
default R setShape(SetShape shape) {
return listShape(shape);
}
R mapShape(MapShape shape);
R byteShape(ByteShape shape);
R shortShape(ShortShape shape);
R integerShape(IntegerShape shape);
default R intEnumShape(IntEnumShape shape) {
return integerShape(shape);
}
R longShape(LongShape shape);
R floatShape(FloatShape shape);
R documentShape(DocumentShape shape);
R doubleShape(DoubleShape shape);
R bigIntegerShape(BigIntegerShape shape);
R bigDecimalShape(BigDecimalShape shape);
R operationShape(OperationShape shape);
R resourceShape(ResourceShape shape);
R serviceShape(ServiceShape shape);
R stringShape(StringShape shape);
default R enumShape(EnumShape shape) {
return stringShape(shape);
}
R structureShape(StructureShape shape);
R unionShape(UnionShape shape);
R memberShape(MemberShape shape);
R timestampShape(TimestampShape shape);
/**
* Creates {@link ShapeVisitor} that return a value when necessary
* when visiting shapes.
*
* @param <R> Return type.
*/
abstract class Default<R> implements ShapeVisitor<R> {
/**
* Returns a value for any unhandled shape.
*
* @param shape Shape that is being visited.
* @return Return value.
*/
protected abstract R getDefault(Shape shape);
@Override
public R blobShape(BlobShape shape) {
return getDefault(shape);
}
@Override
public R booleanShape(BooleanShape shape) {
return getDefault(shape);
}
@Override
public R listShape(ListShape shape) {
return getDefault(shape);
}
@Override
public R byteShape(ByteShape shape) {
return getDefault(shape);
}
@Override
public R shortShape(ShortShape shape) {
return getDefault(shape);
}
@Override
public R integerShape(IntegerShape shape) {
return getDefault(shape);
}
@Override
public R longShape(LongShape shape) {
return getDefault(shape);
}
@Override
public R floatShape(FloatShape shape) {
return getDefault(shape);
}
@Override
public R documentShape(DocumentShape shape) {
return getDefault(shape);
}
@Override
public R doubleShape(DoubleShape shape) {
return getDefault(shape);
}
@Override
public R bigIntegerShape(BigIntegerShape shape) {
return getDefault(shape);
}
@Override
public R bigDecimalShape(BigDecimalShape shape) {
return getDefault(shape);
}
@Override
public R mapShape(MapShape shape) {
return getDefault(shape);
}
@Override
public R operationShape(OperationShape shape) {
return getDefault(shape);
}
@Override
public R resourceShape(ResourceShape shape) {
return getDefault(shape);
}
@Override
public R serviceShape(ServiceShape shape) {
return getDefault(shape);
}
@Override
public R stringShape(StringShape shape) {
return getDefault(shape);
}
@Override
public R structureShape(StructureShape shape) {
return getDefault(shape);
}
@Override
public R unionShape(UnionShape shape) {
return getDefault(shape);
}
@Override
public R memberShape(MemberShape shape) {
return getDefault(shape);
}
@Override
public R timestampShape(TimestampShape shape) {
return getDefault(shape);
}
}
/**
* Creates {@link ShapeVisitor} that only requires implementation of
* all data shape branches, but does not support service shapes.
*
* @param <R> Return type.
*/
abstract class DataShapeVisitor<R> implements ShapeVisitor<R> {
@Override
public R operationShape(OperationShape shape) {
throw new IllegalArgumentException("DataShapeVisitor cannot be use to visit "
+ "Operation Shapes. Attempted to visit: " + shape);
}
@Override
public R resourceShape(ResourceShape shape) {
throw new IllegalArgumentException("DataShapeVisitor cannot be use to visit "
+ "Resource Shapes. Attempted to visit: " + shape);
}
@Override
public R serviceShape(ServiceShape shape) {
throw new IllegalArgumentException("DataShapeVisitor cannot be use to visit "
+ "Service Shapes. Attempted to visit: " + shape);
}
}
}