Skip to content

Commit c5e9d61

Browse files
mtdowlingadwsingh
authored andcommitted
Use switch pattern matching
1 parent 9ef76c3 commit c5e9d61

File tree

10 files changed

+168
-192
lines changed

10 files changed

+168
-192
lines changed

client/client-core/src/main/java/software/amazon/smithy/java/client/core/ClientTransport.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -68,21 +68,15 @@ default void configureClient(ClientConfig.Builder config) {
6868
* @return the remapped exception. A given {@code CallException} or {@code TransportException} is returned as-is.
6969
*/
7070
static CallException remapExceptions(Throwable e) {
71-
if (e instanceof CallException ce) {
72-
return ce; // rethrow CallException and TransportException as-is.
73-
} else if (e instanceof ConnectException) {
74-
return new ConnectTimeoutException(e);
75-
} else if (e instanceof SocketTimeoutException) {
76-
return new TransportSocketTimeout(e);
77-
} else if (e instanceof SocketException) {
78-
return new TransportSocketException(e);
79-
} else if (e instanceof SSLException) {
80-
return new TlsException(e);
81-
} else if (e instanceof ProtocolException) {
82-
return new TransportProtocolException(e);
83-
} else {
71+
return switch (e) {
72+
case CallException ce -> ce; // rethrow CallException and TransportException as-is.
73+
case ConnectException connectException -> new ConnectTimeoutException(e);
74+
case SocketTimeoutException socketTimeoutException -> new TransportSocketTimeout(e);
75+
case SocketException socketException -> new TransportSocketException(e);
76+
case SSLException sslException -> new TlsException(e);
77+
case ProtocolException protocolException -> new TransportProtocolException(e);
8478
// Wrap all other exceptions as a TransportException.
85-
return new TransportException(e);
86-
}
79+
case null, default -> new TransportException(e);
80+
};
8781
}
8882
}

client/client-core/src/main/java/software/amazon/smithy/java/client/core/pagination/PaginationTokenExtractor.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ <O extends SerializableStruct> Result extract(O outputShape) {
4646
var items = getValueForPath(itemsPathSchemas, outputShape);
4747
int totalItems = 0;
4848
if (items != null) {
49-
if (items instanceof Collection<?> ic) {
50-
totalItems = ic.size();
51-
} else if (items instanceof Map<?, ?> im) {
52-
totalItems = im.size();
53-
} else if (items instanceof Document doc) {
54-
totalItems = doc.size();
49+
switch (items) {
50+
case Collection<?> ic -> totalItems = ic.size();
51+
case Map<?, ?> im -> totalItems = im.size();
52+
case Document doc -> totalItems = doc.size();
53+
default -> {
54+
}
5555
}
5656
}
5757
return new Result(token, totalItems);

client/client-rulesengine/src/main/java/software/amazon/smithy/java/client/rulesengine/BytecodeCompiler.java

Lines changed: 49 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -90,17 +90,17 @@ Bytecode compile() {
9090
// Compile all results
9191
for (Rule result : bdd.getResults()) {
9292
writer.markResultStart();
93-
if (result == null || result instanceof NoMatchRule) {
94-
// No match: push null and return
95-
writer.writeByte(Opcodes.LOAD_CONST);
96-
writer.writeByte(writer.getConstantIndex(null));
97-
writer.writeByte(Opcodes.RETURN_VALUE);
98-
} else if (result instanceof EndpointRule e) {
99-
compileEndpointRule(e);
100-
} else if (result instanceof ErrorRule e) {
101-
compileErrorRule(e);
102-
} else {
103-
throw new UnsupportedOperationException("Unexpected result type: " + result.getClass());
93+
result = result == null ? NoMatchRule.INSTANCE : result;
94+
switch (result) {
95+
case NoMatchRule nr -> {
96+
// No match: push null and return
97+
writer.writeByte(Opcodes.LOAD_CONST);
98+
writer.writeByte(writer.getConstantIndex(null));
99+
writer.writeByte(Opcodes.RETURN_VALUE);
100+
}
101+
case EndpointRule e -> compileEndpointRule(e);
102+
case ErrorRule e -> compileErrorRule(e);
103+
default -> throw new UnsupportedOperationException("Unexpected result type: " + result);
104104
}
105105
}
106106

@@ -375,47 +375,49 @@ private void compileGetAttr(GetAttr getAttr) {
375375
}
376376

377377
private void compileLiteral(Literal literal) {
378-
if (literal instanceof StringLiteral s) {
379-
var template = s.value();
380-
var parts = template.getParts();
381-
382-
if (parts.size() == 1 && parts.get(0) instanceof Template.Literal) {
383-
// Simple string with no interpolation
384-
addLoadConst(parts.get(0).toString());
385-
} else if (parts.size() == 1 && parts.get(0) instanceof Template.Dynamic dynamic) {
386-
// Single dynamic expression, so just evaluate it
387-
compileExpression(dynamic.toExpression());
388-
} else {
389-
// Multiple parts - need to concatenate
390-
int expressionCount = 0;
391-
for (var part : parts) {
392-
if (part instanceof Template.Dynamic d) {
393-
compileExpression(d.toExpression());
394-
} else {
395-
addLoadConst(part.toString());
378+
switch (literal) {
379+
case StringLiteral s -> {
380+
var template = s.value();
381+
var parts = template.getParts();
382+
383+
if (parts.size() == 1 && parts.get(0) instanceof Template.Literal) {
384+
// Simple string with no interpolation
385+
addLoadConst(parts.get(0).toString());
386+
} else if (parts.size() == 1 && parts.get(0) instanceof Template.Dynamic dynamic) {
387+
// Single dynamic expression, so just evaluate it
388+
compileExpression(dynamic.toExpression());
389+
} else {
390+
// Multiple parts - need to concatenate
391+
int expressionCount = 0;
392+
for (var part : parts) {
393+
if (part instanceof Template.Dynamic d) {
394+
compileExpression(d.toExpression());
395+
} else {
396+
addLoadConst(part.toString());
397+
}
398+
expressionCount++;
396399
}
397-
expressionCount++;
400+
writer.writeByte(Opcodes.RESOLVE_TEMPLATE);
401+
writer.writeByte(expressionCount);
398402
}
399-
writer.writeByte(Opcodes.RESOLVE_TEMPLATE);
400-
writer.writeByte(expressionCount);
401403
}
402-
} else if (literal instanceof TupleLiteral t) {
403-
for (var e : t.members()) {
404-
compileLiteral(e);
404+
case TupleLiteral t -> {
405+
for (var e : t.members()) {
406+
compileLiteral(e);
407+
}
408+
compileListCreation(t.members().size());
405409
}
406-
compileListCreation(t.members().size());
407-
} else if (literal instanceof RecordLiteral r) {
408-
for (var e : r.members().entrySet()) {
409-
compileLiteral(e.getValue()); // value then key to make popping ordered
410-
addLoadConst(e.getKey().toString());
410+
case RecordLiteral r -> {
411+
for (var e : r.members().entrySet()) {
412+
compileLiteral(e.getValue()); // value then key to make popping ordered
413+
addLoadConst(e.getKey().toString());
414+
}
415+
compileMapCreation(r.members().size());
411416
}
412-
compileMapCreation(r.members().size());
413-
} else if (literal instanceof BooleanLiteral b) {
414-
addLoadConst(b.value().getValue());
415-
} else if (literal instanceof IntegerLiteral i) {
416-
addLoadConst(i.toNode().expectNumberNode().getValue());
417-
} else {
418-
throw new UnsupportedOperationException("Unexpected rules engine Literal type: " + literal);
417+
case BooleanLiteral b -> addLoadConst(b.value().getValue());
418+
case IntegerLiteral i -> addLoadConst(i.toNode().expectNumberNode().getValue());
419+
case null, default ->
420+
throw new UnsupportedOperationException("Unexpected rules engine Literal type: " + literal);
419421
}
420422
}
421423

client/client-rulesengine/src/main/java/software/amazon/smithy/java/client/rulesengine/BytecodeDisassembler.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -338,17 +338,13 @@ private void appendSymbolicInfo(StringBuilder s, BytecodeWalker walker, Show sho
338338
}
339339

340340
private String formatConstant(Object value) {
341-
if (value == null) {
342-
return "null";
343-
} else if (value instanceof String) {
344-
return "\"" + escapeString((String) value) + "\"";
345-
} else if (value instanceof List<?> list) {
346-
return "List[" + list.size() + " items]";
347-
} else if (value instanceof Map<?, ?> map) {
348-
return "Map[" + map.size() + " entries]";
349-
} else {
350-
return value.getClass().getSimpleName() + "[" + value + "]";
351-
}
341+
return switch (value) {
342+
case null -> "null";
343+
case String s -> "\"" + escapeString(s) + "\"";
344+
case List<?> list -> "List[" + list.size() + " items]";
345+
case Map<?, ?> map -> "Map[" + map.size() + " entries]";
346+
default -> value.getClass().getSimpleName() + "[" + value + "]";
347+
};
352348
}
353349

354350
private String formatValue(Object value) {

client/client-rulesengine/src/main/java/software/amazon/smithy/java/client/rulesengine/BytecodeEvaluator.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -400,19 +400,18 @@ private Object run(int start) {
400400

401401
// Get a property from a map or URI, or return null.
402402
private Object getProperty(Object target, String propertyName) {
403-
if (target instanceof Map<?, ?> m) {
404-
return m.get(propertyName);
405-
} else if (target instanceof URI u) {
406-
return switch (propertyName) {
403+
return switch (target) {
404+
case Map<?, ?> m -> m.get(propertyName);
405+
case URI u -> switch (propertyName) {
407406
case "scheme" -> u.getScheme();
408407
case "path" -> u.getRawPath();
409408
case "normalizedPath" -> ParseUrl.normalizePath(u.getRawPath());
410409
case "authority" -> u.getAuthority();
411410
case "isIp" -> ParseUrl.isIpAddr(u.getHost());
412411
default -> null;
413412
};
414-
}
415-
return null;
413+
case null, default -> null;
414+
};
416415
}
417416

418417
// Get a value by index from an object. If not an array, returns null.

client/client-rulesengine/src/main/java/software/amazon/smithy/java/client/rulesengine/BytecodeWriter.java

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -297,35 +297,39 @@ private void writeConstantPool(DataOutputStream dos) throws IOException {
297297
}
298298

299299
private void writeConstantValue(DataOutputStream dos, Object value) throws IOException {
300-
if (value == null) {
301-
dos.writeByte(Bytecode.CONST_NULL);
302-
} else if (value instanceof String s) {
303-
dos.writeByte(Bytecode.CONST_STRING);
304-
writeUTF(dos, s);
305-
} else if (value instanceof Integer i) {
306-
dos.writeByte(Bytecode.CONST_INTEGER);
307-
dos.writeInt(i);
308-
} else if (value instanceof Boolean b) {
309-
dos.writeByte(Bytecode.CONST_BOOLEAN);
310-
dos.writeByte(b ? 1 : 0);
311-
} else if (value instanceof List<?> list) {
312-
dos.writeByte(Bytecode.CONST_LIST);
313-
dos.writeShort(list.size());
314-
for (Object element : list) {
315-
writeConstantValue(dos, element);
300+
switch (value) {
301+
case null -> dos.writeByte(Bytecode.CONST_NULL);
302+
case String s -> {
303+
dos.writeByte(Bytecode.CONST_STRING);
304+
writeUTF(dos, s);
316305
}
317-
} else if (value instanceof Map<?, ?> map) {
318-
dos.writeByte(Bytecode.CONST_MAP);
319-
dos.writeShort(map.size());
320-
for (Map.Entry<?, ?> entry : map.entrySet()) {
321-
if (!(entry.getKey() instanceof String)) {
322-
throw new IOException("Map keys must be strings, found: " + entry.getKey().getClass());
306+
case Integer i -> {
307+
dos.writeByte(Bytecode.CONST_INTEGER);
308+
dos.writeInt(i);
309+
}
310+
case Boolean b -> {
311+
dos.writeByte(Bytecode.CONST_BOOLEAN);
312+
dos.writeByte(b ? 1 : 0);
313+
}
314+
case List<?> list -> {
315+
dos.writeByte(Bytecode.CONST_LIST);
316+
dos.writeShort(list.size());
317+
for (Object element : list) {
318+
writeConstantValue(dos, element);
323319
}
324-
writeUTF(dos, (String) entry.getKey());
325-
writeConstantValue(dos, entry.getValue());
326320
}
327-
} else {
328-
throw new IOException("Unsupported constant type: " + value.getClass());
321+
case Map<?, ?> map -> {
322+
dos.writeByte(Bytecode.CONST_MAP);
323+
dos.writeShort(map.size());
324+
for (Map.Entry<?, ?> entry : map.entrySet()) {
325+
if (!(entry.getKey() instanceof String)) {
326+
throw new IOException("Map keys must be strings, found: " + entry.getKey().getClass());
327+
}
328+
writeUTF(dos, (String) entry.getKey());
329+
writeConstantValue(dos, entry.getValue());
330+
}
331+
}
332+
default -> throw new IOException("Unsupported constant type: " + value.getClass());
329333
}
330334
}
331335

client/client-rulesengine/src/main/java/software/amazon/smithy/java/client/rulesengine/EndpointUtils.java

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,29 +56,27 @@ public static Object convertNode(Node value) {
5656
}
5757

5858
static Value convertToValue(Object o) {
59-
if (o == null) {
60-
return Value.emptyValue();
61-
} else if (o instanceof String s) {
62-
return Value.stringValue(s);
63-
} else if (o instanceof Number n) {
64-
return Value.integerValue(n.intValue());
65-
} else if (o instanceof Boolean b) {
66-
return Value.booleanValue(b);
67-
} else if (o instanceof List<?> l) {
68-
List<Value> valueList = new ArrayList<>(l.size());
69-
for (var entry : l) {
70-
valueList.add(convertToValue(entry));
59+
return switch (o) {
60+
case null -> Value.emptyValue();
61+
case String s -> Value.stringValue(s);
62+
case Number n -> Value.integerValue(n.intValue());
63+
case Boolean b -> Value.booleanValue(b);
64+
case List<?> l -> {
65+
List<Value> valueList = new ArrayList<>(l.size());
66+
for (var entry : l) {
67+
valueList.add(convertToValue(entry));
68+
}
69+
yield Value.arrayValue(valueList);
7170
}
72-
return Value.arrayValue(valueList);
73-
} else if (o instanceof Map<?, ?> m) {
74-
Map<Identifier, Value> valueMap = new HashMap<>(m.size());
75-
for (var e : m.entrySet()) {
76-
valueMap.put(Identifier.of(e.getKey().toString()), convertToValue(e.getValue()));
71+
case Map<?, ?> m -> {
72+
Map<Identifier, Value> valueMap = new HashMap<>(m.size());
73+
for (var e : m.entrySet()) {
74+
valueMap.put(Identifier.of(e.getKey().toString()), convertToValue(e.getValue()));
75+
}
76+
yield Value.recordValue(valueMap);
7777
}
78-
return Value.recordValue(valueMap);
79-
} else {
80-
throw new RulesEvaluationError("Unsupported value type: " + o);
81-
}
78+
default -> throw new RulesEvaluationError("Unsupported value type: " + o);
79+
};
8280
}
8381

8482
// Read big-endian unsigned short (2 bytes)

codegen/codegen-core/src/main/java/software/amazon/smithy/java/codegen/writer/JavaWriter.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -181,17 +181,14 @@ public String apply(Object type, String indent) {
181181
}
182182

183183
private static Symbol getTypeSymbol(Object type, char formatChar) {
184-
if (type instanceof Symbol s) {
185-
return s;
186-
} else if (type instanceof Class<?> c) {
187-
return CodegenUtils.fromClass(c);
188-
} else if (type instanceof SymbolReference r) {
189-
return r.getSymbol();
190-
} else {
191-
throw new IllegalArgumentException(
184+
return switch (type) {
185+
case Symbol s -> s;
186+
case Class<?> c -> CodegenUtils.fromClass(c);
187+
case SymbolReference r -> r.getSymbol();
188+
case null, default -> throw new IllegalArgumentException(
192189
"Invalid type provided for " + formatChar + ". Expected a Symbol or Class"
193190
+ " but found: `" + type + "`.");
194-
}
191+
};
195192
}
196193

197194
/**

core/src/main/java/software/amazon/smithy/java/core/serde/TimestampFormatter.java

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -159,25 +159,17 @@ public Instant readFromString(String value, boolean strict) {
159159
@Override
160160
public Instant readFromNumber(Number value) {
161161
// The most common types for serialized epoch-seconds, double/integer/long, are checked first.
162-
if (value instanceof Double f) {
163-
return Instant.ofEpochMilli((long) (f * 1000f));
164-
} else if (value instanceof Integer i) {
165-
return Instant.ofEpochMilli(i * 1000L);
166-
} else if (value instanceof Long l) {
167-
return Instant.ofEpochMilli(l * 1000L);
168-
} else if (value instanceof Byte b) {
169-
return Instant.ofEpochMilli(b * 1000L);
170-
} else if (value instanceof Short s) {
171-
return Instant.ofEpochMilli(s * 1000L);
172-
} else if (value instanceof Float f) {
173-
return Instant.ofEpochMilli((long) (f * 1000f));
174-
} else if (value instanceof BigInteger bi) {
175-
return Instant.ofEpochMilli(bi.longValue() * 1000);
176-
} else if (value instanceof BigDecimal bd) {
177-
return Instant.ofEpochMilli(bd.longValue() * 1000);
178-
} else {
179-
throw new TimestampSyntaxError(format(), ExpectedType.NUMBER, value);
180-
}
162+
return switch (value) {
163+
case Double f -> Instant.ofEpochMilli((long) (f * 1000f));
164+
case Integer i -> Instant.ofEpochMilli(i * 1000L);
165+
case Long l -> Instant.ofEpochMilli(l * 1000L);
166+
case Byte b -> Instant.ofEpochMilli(b * 1000L);
167+
case Short s -> Instant.ofEpochMilli(s * 1000L);
168+
case Float f -> Instant.ofEpochMilli((long) (f * 1000f));
169+
case BigInteger bi -> Instant.ofEpochMilli(bi.longValue() * 1000);
170+
case BigDecimal bd -> Instant.ofEpochMilli(bd.longValue() * 1000);
171+
case null, default -> throw new TimestampSyntaxError(format(), ExpectedType.NUMBER, value);
172+
};
181173
}
182174

183175
@Override

0 commit comments

Comments
 (0)