Skip to content

Commit

Permalink
[COMMON] Rewrite common.main by java17 toList and instanceof (#1777)
Browse files Browse the repository at this point in the history
  • Loading branch information
Haser0305 authored May 26, 2023
1 parent cf70087 commit e3372db
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 16 deletions.
24 changes: 12 additions & 12 deletions common/src/main/java/org/astraea/common/ByteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,18 +380,18 @@ public static ClusterInfo readClusterInfo(byte[] bytes) {
* and "char" in Protocol Buffers. Use "int" and "String" instead.
*/
private static PrimitiveOuterClass.Primitive primitive(Object v) throws SerializationException {
if (v instanceof Integer)
return PrimitiveOuterClass.Primitive.newBuilder().setInt((int) v).build();
else if (v instanceof Long)
return PrimitiveOuterClass.Primitive.newBuilder().setLong((long) v).build();
else if (v instanceof Float)
return PrimitiveOuterClass.Primitive.newBuilder().setFloat((float) v).build();
else if (v instanceof Double)
return PrimitiveOuterClass.Primitive.newBuilder().setDouble((double) v).build();
else if (v instanceof Boolean)
return PrimitiveOuterClass.Primitive.newBuilder().setBoolean((boolean) v).build();
else if (v instanceof String)
return PrimitiveOuterClass.Primitive.newBuilder().setStr(v.toString()).build();
if (v instanceof Integer value)
return PrimitiveOuterClass.Primitive.newBuilder().setInt(value).build();
else if (v instanceof Long value)
return PrimitiveOuterClass.Primitive.newBuilder().setLong(value).build();
else if (v instanceof Float value)
return PrimitiveOuterClass.Primitive.newBuilder().setFloat(value).build();
else if (v instanceof Double value)
return PrimitiveOuterClass.Primitive.newBuilder().setDouble(value).build();
else if (v instanceof Boolean value)
return PrimitiveOuterClass.Primitive.newBuilder().setBoolean(value).build();
else if (v instanceof String value)
return PrimitiveOuterClass.Primitive.newBuilder().setStr(value).build();
else
throw new SerializationException(
"Type "
Expand Down
3 changes: 1 addition & 2 deletions common/src/main/java/org/astraea/common/Header.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import java.util.List;
import java.util.Spliterators;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.kafka.common.header.Headers;

Expand All @@ -29,7 +28,7 @@ public static List<Header> of(Headers headers) {
if (!iter.hasNext()) return List.of();
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iter, 0), false)
.map(h -> new Header(h.key(), h.value()))
.collect(Collectors.toUnmodifiableList());
.toList();
}

public static Header of(String key, byte[] value) {
Expand Down
4 changes: 2 additions & 2 deletions common/src/main/java/org/astraea/common/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,8 @@ public static <R> R packException(Getter<R> getter) {
}

public static void close(Object obj) {
if (obj instanceof AutoCloseable) {
packException(() -> ((AutoCloseable) obj).close());
if (obj instanceof AutoCloseable autoCloseableObj) {
packException(autoCloseableObj::close);
}
}

Expand Down

0 comments on commit e3372db

Please sign in to comment.