Skip to content

Commit 6225f8f

Browse files
Use java streams and try resources
1 parent fb0b537 commit 6225f8f

File tree

3 files changed

+17
-36
lines changed

3 files changed

+17
-36
lines changed

src/main/java/com/authenteq/util/DriverUtils.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
import com.google.gson.JsonObject;
2525
import com.google.gson.JsonParser;
2626

27-
import java.util.List;
28-
import java.util.stream.Collectors;
29-
3027
/**
3128
* The Class DriverUtils.
3229
*/
@@ -85,7 +82,7 @@ public static JsonObject makeSelfSortingGson(JsonObject input ) {
8582

8683
JsonObject json = new JsonObject();
8784

88-
for( String key: input.keySet().stream().sorted().collect( Collectors.toList() ) ) {
85+
input.keySet().stream().sorted().forEach(key -> {
8986
JsonElement j = input.get(key);
9087
if (j instanceof JsonObject) {
9188
json.add(key, makeSelfSortingGson((JsonObject) j));
@@ -105,7 +102,7 @@ public static JsonObject makeSelfSortingGson(JsonObject input ) {
105102
} else {
106103
json.add(key, j);
107104
}
108-
}
105+
});
109106

110107
return json;
111108
}

src/main/java/com/authenteq/util/JsonUtils.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.util.Map;
99
import java.util.concurrent.ConcurrentHashMap;
10+
import java.util.stream.Stream;
1011

1112
/**
1213
* Utility class for handling JSON serialization and deserialization.
@@ -35,7 +36,7 @@ public class JsonUtils {
3536
*/
3637
private JsonUtils() {
3738
}
38-
39+
3940
private static synchronized GsonBuilder base()
4041
{
4142
GsonBuilder builder = new GsonBuilder();
@@ -58,11 +59,8 @@ private static synchronized GsonBuilder base()
5859
public static Gson getGson() {
5960
GsonBuilder builder = base();
6061

61-
for( TypeAdapter value : typeAdaptersDeserialize.values() )
62-
builder.registerTypeAdapter( value.getType(), value.getSerializer() );
63-
64-
for( TypeAdapter value : typeAdaptersSerialize.values() )
65-
builder.registerTypeAdapter( value.getType(), value.getSerializer() );
62+
Stream.concat(typeAdaptersDeserialize.values().stream(), typeAdaptersSerialize.values().stream())
63+
.forEach(value -> builder.registerTypeAdapter(value.getType(), value.getSerializer()));
6664

6765
return builder.create();
6866
}
@@ -83,18 +81,9 @@ public static Gson getGson( Class ignoreClass, ExclusionStrategy... exclusionStr
8381
{
8482
GsonBuilder builder = base();
8583

86-
for( TypeAdapter value : typeAdaptersDeserialize.values() ) {
87-
if( ignoreClass != null && value.getType().equals( ignoreClass ) )
88-
continue;
89-
builder.registerTypeAdapter( value.getType(), value.getSerializer() );
90-
}
91-
92-
for( TypeAdapter value : typeAdaptersSerialize.values() ) {
93-
if( ignoreClass != null && value.getType().equals( ignoreClass ) )
94-
continue;
95-
96-
builder.registerTypeAdapter( value.getType(), value.getSerializer() );
97-
}
84+
Stream.concat(typeAdaptersDeserialize.values().stream(), typeAdaptersSerialize.values().stream())
85+
.filter(value -> !value.getType().equals(ignoreClass))
86+
.forEach(value -> builder.registerTypeAdapter(value.getType(), value.getSerializer()));
9887

9988
return builder.setExclusionStrategies( exclusionStrategies ).create();
10089
}
@@ -171,7 +160,7 @@ public static String toJson(Object src, ExclusionStrategy ... exclusionStrategie
171160

172161
/*
173162
* (non-Javadoc)
174-
*
163+
*
175164
* @see java.lang.Object#toString()
176165
*/
177166
@Override

src/main/java/com/authenteq/util/ScannerUtil.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@ public class ScannerUtil {
1212
* exit when enter string "exit".
1313
*/
1414
public static void monitorExit() {
15-
Scanner scanner = new Scanner(System.in);
16-
try{
17-
while (true) {
18-
String line = scanner.nextLine();
19-
if("exit".equals(line)){
20-
break;
21-
}
22-
}
23-
} catch (Exception ex) {
24-
25-
} finally {
26-
scanner.close();
15+
try (Scanner scanner = new Scanner(System.in)) {
16+
while (true) {
17+
String line = scanner.nextLine();
18+
if ("exit".equals(line)) {
19+
break;
20+
}
21+
}
2722
}
2823
}
2924
}

0 commit comments

Comments
 (0)