Skip to content

Commit ee285e5

Browse files
committed
Trivial refactor: simplify code with switch expression
1 parent 1fb9c1e commit ee285e5

File tree

32 files changed

+354
-787
lines changed

32 files changed

+354
-787
lines changed

src/main/java/groovy/lang/ExpandoMetaClass.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -720,16 +720,10 @@ public Object getProperty(String property) {
720720
}
721721

722722
public static boolean isValidExpandoProperty(String property) {
723-
switch (property) {
724-
case META_CLASS_PROPERTY:
725-
case CLASS_PROPERTY:
726-
case META_METHODS:
727-
case METHODS:
728-
case PROPERTIES:
729-
return false;
730-
default:
731-
return true;
732-
}
723+
return switch (property) {
724+
case META_CLASS_PROPERTY, CLASS_PROPERTY, META_METHODS, METHODS, PROPERTIES -> false;
725+
default -> true;
726+
};
733727
}
734728

735729
/* (non-Javadoc)

src/main/java/groovy/lang/GroovyClassLoader.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,14 +1240,11 @@ public void call(final SourceUnit source, final GeneratorContext context, final
12401240
*/
12411241
public String genEncodingString(CharSequence chars) {
12421242
try {
1243-
switch(HASH_ALGORITHM) {
1244-
case "md5":
1245-
return EncodingGroovyMethods.md5(chars);
1246-
case "sha256":
1247-
return EncodingGroovyMethods.sha256(chars);
1248-
default:
1249-
throw new IllegalStateException("Unknown hash algorithm");
1250-
}
1243+
return switch (HASH_ALGORITHM) {
1244+
case "md5" -> EncodingGroovyMethods.md5(chars);
1245+
case "sha256" -> EncodingGroovyMethods.sha256(chars);
1246+
default -> throw new IllegalStateException("Unknown hash algorithm");
1247+
};
12511248
} catch (NoSuchAlgorithmException e) {
12521249
throw new GroovyRuntimeException(e);
12531250
}

src/main/java/groovy/util/ObservableList.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -488,23 +488,15 @@ public enum ChangeType {
488488
public static final Object newValue = new Object[0];
489489

490490
public static ChangeType resolve(int ordinal) {
491-
switch (ordinal) {
492-
case 0:
493-
return ADDED;
494-
case 2:
495-
return REMOVED;
496-
case 3:
497-
return CLEARED;
498-
case 4:
499-
return MULTI_ADD;
500-
case 5:
501-
return MULTI_REMOVE;
502-
case 6:
503-
return NONE;
504-
case 1:
505-
default:
506-
return UPDATED;
507-
}
491+
return switch (ordinal) {
492+
case 0 -> ADDED;
493+
case 2 -> REMOVED;
494+
case 3 -> CLEARED;
495+
case 4 -> MULTI_ADD;
496+
case 5 -> MULTI_REMOVE;
497+
case 6 -> NONE;
498+
default -> UPDATED;
499+
};
508500
}
509501
}
510502

src/main/java/groovy/util/ObservableMap.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -325,21 +325,14 @@ public enum ChangeType {
325325
public static final Object newValue = new Object[0];
326326

327327
public static ChangeType resolve(int ordinal) {
328-
switch (ordinal) {
329-
case 0:
330-
return ADDED;
331-
case 2:
332-
return REMOVED;
333-
case 3:
334-
return CLEARED;
335-
case 4:
336-
return MULTI;
337-
case 5:
338-
return NONE;
339-
case 1:
340-
default:
341-
return UPDATED;
342-
}
328+
return switch (ordinal) {
329+
case 0 -> ADDED;
330+
case 2 -> REMOVED;
331+
case 3 -> CLEARED;
332+
case 4 -> MULTI;
333+
case 5 -> NONE;
334+
default -> UPDATED;
335+
};
343336
}
344337
}
345338

src/main/java/org/apache/groovy/ast/tools/ExpressionUtils.java

Lines changed: 14 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -138,42 +138,20 @@ public static ConstantExpression transformBinaryConstantExpression(final BinaryE
138138
Number left = safeNumber((ConstantExpression) leftX);
139139
Number right = safeNumber((ConstantExpression) rightX);
140140
if (left == null || right == null) return null;
141-
Number result = null;
142-
switch (opType) {
143-
case PLUS:
144-
result = NumberMath.add(left, right);
145-
break;
146-
case MINUS:
147-
result = NumberMath.subtract(left, right);
148-
break;
149-
case MULTIPLY:
150-
result = NumberMath.multiply(left, right);
151-
break;
152-
case DIVIDE:
153-
result = NumberMath.divide(left, right);
154-
break;
155-
case LEFT_SHIFT:
156-
result = NumberMath.leftShift(left, right);
157-
break;
158-
case RIGHT_SHIFT:
159-
result = NumberMath.rightShift(left, right);
160-
break;
161-
case RIGHT_SHIFT_UNSIGNED:
162-
result = NumberMath.rightShiftUnsigned(left, right);
163-
break;
164-
case BITWISE_AND:
165-
result = NumberMath.and(left, right);
166-
break;
167-
case BITWISE_OR:
168-
result = NumberMath.or(left, right);
169-
break;
170-
case BITWISE_XOR:
171-
result = NumberMath.xor(left, right);
172-
break;
173-
case POWER:
174-
result = DefaultGroovyMethods.power(left, right);
175-
break;
176-
}
141+
Number result = switch (opType) {
142+
case PLUS -> NumberMath.add(left, right);
143+
case MINUS -> NumberMath.subtract(left, right);
144+
case MULTIPLY -> NumberMath.multiply(left, right);
145+
case DIVIDE -> NumberMath.divide(left, right);
146+
case LEFT_SHIFT -> NumberMath.leftShift(left, right);
147+
case RIGHT_SHIFT -> NumberMath.rightShift(left, right);
148+
case RIGHT_SHIFT_UNSIGNED -> NumberMath.rightShiftUnsigned(left, right);
149+
case BITWISE_AND -> NumberMath.and(left, right);
150+
case BITWISE_OR -> NumberMath.or(left, right);
151+
case BITWISE_XOR -> NumberMath.xor(left, right);
152+
case POWER -> DefaultGroovyMethods.power(left, right);
153+
default -> null;
154+
};
177155
if (result != null) {
178156
ConstantExpression constantExpression = transformNumberConstantExpression(be, wrapperType, result);
179157
if (constantExpression != null) return constantExpression;

src/main/java/org/codehaus/groovy/ast/AnnotationNode.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -173,32 +173,20 @@ public boolean isTargetAllowed(int target) {
173173
}
174174

175175
public static String targetToName(int target) {
176-
switch(target) {
177-
case TYPE_TARGET:
178-
return "TYPE";
179-
case CONSTRUCTOR_TARGET:
180-
return "CONSTRUCTOR";
181-
case METHOD_TARGET:
182-
return "METHOD";
183-
case FIELD_TARGET:
184-
return "FIELD";
185-
case PARAMETER_TARGET:
186-
return "PARAMETER";
187-
case LOCAL_VARIABLE_TARGET:
188-
return "LOCAL_VARIABLE";
189-
case ANNOTATION_TARGET:
190-
return "ANNOTATION";
191-
case PACKAGE_TARGET:
192-
return "PACKAGE";
193-
case TYPE_PARAMETER_TARGET:
194-
return "TYPE_PARAMETER";
195-
case TYPE_USE_TARGET:
196-
return "TYPE_USE";
197-
case RECORD_COMPONENT_TARGET:
198-
return "RECORD_COMPONENT";
199-
default:
200-
return "unknown target";
201-
}
176+
return switch (target) {
177+
case TYPE_TARGET -> "TYPE";
178+
case CONSTRUCTOR_TARGET -> "CONSTRUCTOR";
179+
case METHOD_TARGET -> "METHOD";
180+
case FIELD_TARGET -> "FIELD";
181+
case PARAMETER_TARGET -> "PARAMETER";
182+
case LOCAL_VARIABLE_TARGET -> "LOCAL_VARIABLE";
183+
case ANNOTATION_TARGET -> "ANNOTATION";
184+
case PACKAGE_TARGET -> "PACKAGE";
185+
case TYPE_PARAMETER_TARGET -> "TYPE_PARAMETER";
186+
case TYPE_USE_TARGET -> "TYPE_USE";
187+
case RECORD_COMPONENT_TARGET -> "RECORD_COMPONENT";
188+
default -> "unknown target";
189+
};
202190
}
203191

204192
@Override

src/main/java/org/codehaus/groovy/ast/tools/ClosureUtils.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,12 @@ public static Parameter[] getParametersSafe(final ClosureExpression ce) {
8989
* @since 3.0.5
9090
*/
9191
public static String getResolveStrategyName(final int resolveStrategy) {
92-
switch (resolveStrategy) {
93-
case Closure.DELEGATE_FIRST:
94-
return "DELEGATE_FIRST";
95-
case Closure.DELEGATE_ONLY:
96-
return "DELEGATE_ONLY";
97-
case Closure.OWNER_ONLY:
98-
return "OWNER_ONLY";
99-
case Closure.TO_SELF:
100-
return "TO_SELF";
101-
}
102-
return "OWNER_FIRST";
92+
return switch (resolveStrategy) {
93+
case Closure.DELEGATE_FIRST -> "DELEGATE_FIRST";
94+
case Closure.DELEGATE_ONLY -> "DELEGATE_ONLY";
95+
case Closure.OWNER_ONLY -> "OWNER_ONLY";
96+
case Closure.TO_SELF -> "TO_SELF";
97+
default -> "OWNER_FIRST";
98+
};
10399
}
104100
}

src/main/java/org/codehaus/groovy/classgen/FinalVariableAnalyzer.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,10 @@ private enum VariableState {
8080
}
8181

8282
public VariableState getNext() {
83-
switch (this) {
84-
case is_uninitialized:
85-
return is_final;
86-
default:
87-
return is_var;
88-
}
83+
return switch (this) {
84+
case is_uninitialized -> is_final;
85+
default -> is_var;
86+
};
8987
}
9088

9189
public boolean isFinal() {

src/main/java/org/codehaus/groovy/runtime/ArrayGroovyMethods.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,6 @@
6868
import java.util.Collection;
6969
import java.util.Collections;
7070
import java.util.Comparator;
71-
import java.util.HashMap;
7271
import java.util.HashSet;
7372
import java.util.Iterator;
7473
import java.util.LinkedHashMap;
@@ -82,16 +81,16 @@
8281
import java.util.TreeSet;
8382
import java.util.function.Consumer;
8483
import java.util.function.DoubleConsumer;
84+
import java.util.function.DoublePredicate;
8585
import java.util.function.DoubleUnaryOperator;
8686
import java.util.function.Function;
8787
import java.util.function.IntConsumer;
88+
import java.util.function.IntPredicate;
8889
import java.util.function.IntUnaryOperator;
8990
import java.util.function.LongConsumer;
91+
import java.util.function.LongPredicate;
9092
import java.util.function.LongUnaryOperator;
9193
import java.util.function.Predicate;
92-
import java.util.function.IntPredicate;
93-
import java.util.function.LongPredicate;
94-
import java.util.function.DoublePredicate;
9594

9695
/**
9796
* Defines new groovy methods which appear on arrays inside the Groovy environment.
@@ -6612,15 +6611,14 @@ public static <T> T[] minus(T[] self, Iterable<?> removeMe) {
66126611
* @since 1.5.5
66136612
*/
66146613
public static <T> T[] minus(T[] self, Object[] removeMe) {
6615-
switch (removeMe.length) {
6616-
case 0:
6617-
return self.clone();
6618-
case 1:
6619-
return ArrayGroovyMethods.minus(self, removeMe[0]);
6620-
default:
6621-
Collection<T> temp = DefaultGroovyMethods.minus((Collection<T>) toList(self), Arrays.asList(removeMe));
6622-
return temp.toArray(createSimilarArray(self, temp.size()));
6623-
}
6614+
return switch (removeMe.length) {
6615+
case 0 -> self.clone();
6616+
case 1 -> ArrayGroovyMethods.minus(self, removeMe[0]);
6617+
default -> {
6618+
Collection<T> temp = DefaultGroovyMethods.minus((Collection<T>) toList(self), Arrays.asList(removeMe));
6619+
yield temp.toArray(createSimilarArray(self, temp.size()));
6620+
}
6621+
};
66246622
}
66256623

66266624
/**

src/main/java/org/codehaus/groovy/runtime/MethodClosure.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,11 @@ public Class<?> getOwnerClass() {
133133

134134
@Override
135135
public Object getProperty(final String property) {
136-
switch (property) {
137-
case "method":
138-
return getMethod();
139-
case ANY_INSTANCE_METHOD_EXISTS:
140-
return anyInstanceMethodExists;
141-
default:
142-
return super.getProperty(property);
143-
}
136+
return switch (property) {
137+
case "method" -> getMethod();
138+
case ANY_INSTANCE_METHOD_EXISTS -> anyInstanceMethodExists;
139+
default -> super.getProperty(property);
140+
};
144141
}
145142

146143
// TODO: This method seems to be never called..., because MetaClassImpl.invokeMethod will intercept calls and return the result.

0 commit comments

Comments
 (0)