Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[COMMON] Rewrite common.main by java17 toList and instanceof #1777

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -398,18 +398,18 @@ public Set<TopicPartition> topicPartitions() {
* 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