Skip to content

Commit 967725f

Browse files
committed
source: code cleanup
- refactor code - move builtin converter to enum
1 parent b0d7b2c commit 967725f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+316
-529
lines changed

jooby/src/main/java/io/jooby/BeanConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public interface BeanConverter extends ValueConverter<ValueNode> {
2121
* @param type Conversion type.
2222
* @return True if the converter applies for the given type.
2323
*/
24-
boolean supports(@NonNull Class type);
24+
boolean supports(@NonNull Class<?> type);
2525

2626
/**
2727
* Convert a node value into more specific type.
@@ -30,5 +30,5 @@ public interface BeanConverter extends ValueConverter<ValueNode> {
3030
* @param type Requested type.
3131
* @return Converted value.
3232
*/
33-
Object convert(@NonNull ValueNode node, @NonNull Class type);
33+
Object convert(@NonNull ValueNode node, @NonNull Class<?> type);
3434
}

jooby/src/main/java/io/jooby/Cookie.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private Cookie(@NonNull Cookie cookie) {
174174
* @return Cookie's domain..
175175
*/
176176
public @NonNull String getDomain(@NonNull String domain) {
177-
return this.domain == null ? domain : domain;
177+
return this.domain == null ? domain : this.domain;
178178
}
179179

180180
/**

jooby/src/main/java/io/jooby/Environment.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,10 @@ public Environment(
135135
public @NonNull Map<String, String> getProperties(@NonNull String key, @Nullable String prefix) {
136136
if (hasPath(config, key)) {
137137
Map<String, String> settings = new HashMap<>();
138-
String p = prefix == null || prefix.length() == 0 ? "" : prefix + ".";
139-
config.getConfig(key).entrySet().stream()
138+
String p = prefix == null || prefix.isEmpty() ? "" : prefix + ".";
139+
config
140+
.getConfig(key)
141+
.entrySet()
140142
.forEach(
141143
e -> {
142144
Object value = e.getValue().unwrapped();

jooby/src/main/java/io/jooby/Usage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import java.lang.reflect.Executable;
99
import java.lang.reflect.Parameter;
10-
import java.util.stream.Collectors;
1110
import java.util.stream.Stream;
1211

1312
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -58,7 +57,7 @@ public Usage(@NonNull String message, @NonNull String id) {
5857
*/
5958
public static @NonNull Usage parameterNameNotPresent(@NonNull Parameter parameter) {
6059
Executable executable = parameter.getDeclaringExecutable();
61-
int p = Stream.of(executable.getParameters()).collect(Collectors.toList()).indexOf(parameter);
60+
int p = Stream.of(executable.getParameters()).toList().indexOf(parameter);
6261
String message =
6362
"Unable to provision parameter at position: '"
6463
+ p

jooby/src/main/java/io/jooby/ValueConverter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface ValueConverter<V extends Value> {
2323
* @param type Conversion type.
2424
* @return True if the converter applies for the given type.
2525
*/
26-
boolean supports(@NonNull Class type);
26+
boolean supports(@NonNull Class<?> type);
2727

2828
/**
2929
* Convert simple to specific type.
@@ -32,14 +32,14 @@ public interface ValueConverter<V extends Value> {
3232
* @param type Requested type.
3333
* @return Converted value.
3434
*/
35-
Object convert(@NonNull V value, @NonNull Class type);
35+
Object convert(@NonNull V value, @NonNull Class<?> type);
3636

3737
/**
3838
* Immutable list of defaults/built-in {@link ValueConverter}.
3939
*
4040
* @return Immutable list of defaults/built-in {@link ValueConverter}.
4141
*/
42-
static List<ValueConverter> defaults() {
42+
static List<ValueConverter<?>> defaults() {
4343
return ValueConverters.defaultConverters();
4444
}
4545
}

jooby/src/main/java/io/jooby/handler/AssetHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,8 +257,8 @@ public AssetHandler notFound(@NonNull SneakyThrows.Consumer<Context> handler) {
257257
public void setRoute(Route route) {
258258
List<String> keys = route.getPathKeys();
259259
this.filekey = keys.isEmpty() ? route.getPattern().substring(1) : keys.get(0);
260-
// NOTE: It send an inputstream we don't need a renderer
261-
route.setReturnType(Context.class);
260+
// // NOTE: It send an inputstream we don't need a renderer
261+
// route.setReturnType(Context.class);
262262
}
263263

264264
private static AssetSource[] checkSource(AssetSource[] sources) {

jooby/src/main/java/io/jooby/handler/Cors.java

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.util.concurrent.TimeUnit;
1515
import java.util.function.Predicate;
1616
import java.util.regex.Pattern;
17-
import java.util.stream.Collectors;
1817

1918
import com.typesafe.config.Config;
2019
import edu.umd.cs.findbugs.annotations.NonNull;
@@ -43,28 +42,19 @@
4342
*/
4443
public class Cors {
4544

46-
private static class Matcher<T> implements Predicate<T> {
47-
48-
private List<String> values;
49-
50-
private Predicate<T> predicate;
51-
52-
private boolean wild;
53-
54-
Matcher(final List<String> values, final Predicate<T> predicate) {
55-
this.values = values;
56-
this.predicate = predicate;
57-
this.wild = values.contains("*");
45+
private record Matcher<T>(List<String> values, Predicate<T> predicate) implements Predicate<T> {
46+
boolean wild() {
47+
return values.contains("*");
5848
}
5949

6050
@Override
61-
public boolean test(final T value) {
62-
return predicate.test(value);
51+
public String toString() {
52+
return values.toString();
6353
}
6454

6555
@Override
66-
public String toString() {
67-
return values.toString();
56+
public boolean test(T value) {
57+
return predicate.test(value);
6858
}
6959
}
7060

@@ -127,7 +117,7 @@ public Cors setUseCredentials(boolean credentials) {
127117
* @return True if any origin is accepted.
128118
*/
129119
public boolean anyOrigin() {
130-
return origin.wild;
120+
return origin.wild();
131121
}
132122

133123
/**
@@ -215,7 +205,7 @@ public Cors setMethods(final List<String> methods) {
215205
* @return True if any header is allowed: <code>*</code>.
216206
*/
217207
public boolean anyHeader() {
218-
return headers.wild;
208+
return headers.wild();
219209
}
220210

221211
/**
@@ -368,13 +358,9 @@ private static Matcher<List<String>> allMatch(final List<String> values) {
368358
}
369359

370360
private static Matcher<String> firstMatch(final List<String> values) {
371-
List<Pattern> patterns = values.stream().map(Cors::rewrite).collect(Collectors.toList());
361+
var patterns = values.stream().map(Cors::rewrite).toList();
372362
Predicate<String> predicate =
373-
it ->
374-
patterns.stream()
375-
.filter(pattern -> pattern.matcher(it).matches())
376-
.findFirst()
377-
.isPresent();
363+
it -> patterns.stream().anyMatch(pattern -> pattern.matcher(it).matches());
378364

379365
return new Matcher<>(values, predicate);
380366
}

jooby/src/main/java/io/jooby/handler/CsrfHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public class CsrfHandler implements Route.Before {
7575
public static final Function<Context, String> DEFAULT_GENERATOR =
7676
ctx -> UUID.randomUUID().toString();
7777

78-
private String name;
78+
private final String name;
7979

8080
private Function<Context, String> generator = DEFAULT_GENERATOR;
8181

jooby/src/main/java/io/jooby/internal/Chi.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ public void clear() {
549549
}
550550

551551
private static class MultipleMethodMatcher implements MethodMatcher {
552-
private Map<String, StaticRouterMatch> methods = new ConcurrentHashMap<>();
552+
private final Map<String, StaticRouterMatch> methods = new ConcurrentHashMap<>();
553553

554554
public MultipleMethodMatcher(SingleMethodMatcher matcher) {
555555
methods.put(matcher.method, matcher.route);
@@ -696,7 +696,7 @@ Node insertRoute(String method, String pattern, Route route) {
696696

697697
while (true) {
698698
// Handle key exhaustion
699-
if (search.length() == 0) {
699+
if (search.isEmpty()) {
700700
// Insert or update the node's leaf handler
701701
n.setEndpoint(method, route);
702702
return n;
@@ -766,7 +766,7 @@ Node insertRoute(String method, String pattern, Route route) {
766766

767767
// If the new key is a subset, set the method/handler on this node and finish.
768768
search = search.substring(commonPrefix);
769-
if (search.length() == 0) {
769+
if (search.isEmpty()) {
770770
child.setEndpoint(method, route);
771771
return child;
772772
}
@@ -940,7 +940,7 @@ Route findRoute(RouterMatch rctx, String method, Slice path) {
940940
case ntParam:
941941
case ntRegexp:
942942
// short-circuit and return no matching route for empty param values
943-
if (xsearch.length() == 0) {
943+
if (xsearch.isEmpty()) {
944944
continue;
945945
}
946946
// serially loop through each node grouped by the tail delimiter
@@ -973,7 +973,7 @@ Route findRoute(RouterMatch rctx, String method, Slice path) {
973973
rctx.value(xsearch.substring(0, p).toString());
974974
xsearch = xsearch.substring(p);
975975

976-
if (xsearch.length() == 0) {
976+
if (xsearch.isEmpty()) {
977977
if (xn.isLeaf()) {
978978
Route h = xn.endpoints.get(method);
979979
if (h != null) {
@@ -998,7 +998,7 @@ Route findRoute(RouterMatch rctx, String method, Slice path) {
998998
default:
999999
// catch-all nodes
10001000
// rctx.routeParams.Values = append(rctx.routeParams.Values, search)
1001-
if (xsearch.length() > 0) {
1001+
if (!xsearch.isEmpty()) {
10021002
rctx.value(xsearch.toString());
10031003
}
10041004
xn = nds[0];
@@ -1164,7 +1164,7 @@ Segment patNextSegment(String pattern) {
11641164
// key = key.substring(0, idx);
11651165
}
11661166

1167-
if (rexpat.length() > 0) {
1167+
if (!rexpat.isEmpty()) {
11681168
if (rexpat.charAt(0) != '^') {
11691169
rexpat = "^" + rexpat;
11701170
}

jooby/src/main/java/io/jooby/internal/ClassPathAssetSource.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public class ClassPathAssetSource implements AssetSource {
3030
private final String prefix;
3131

3232
public ClassPathAssetSource(ClassLoader loader, String source) {
33-
if (source == null || source.trim().length() == 0 || source.trim().equals("/")) {
33+
if (source == null || source.trim().isEmpty() || source.trim().equals("/")) {
3434
throw new IllegalArgumentException(
3535
"For security reasons root classpath access is not allowed: " + source);
3636
}

0 commit comments

Comments
 (0)