From 9370b51c6500e8bf7d4c36a34a86db80eecea274 Mon Sep 17 00:00:00 2001 From: Glavo Date: Tue, 5 Nov 2024 22:09:15 +0800 Subject: [PATCH] Update StdOut --- kala-base/src/main/java/kala/io/StdOut.java | 77 ++++++++++----------- 1 file changed, 37 insertions(+), 40 deletions(-) diff --git a/kala-base/src/main/java/kala/io/StdOut.java b/kala-base/src/main/java/kala/io/StdOut.java index 63e0fb70..2d60f2d7 100644 --- a/kala-base/src/main/java/kala/io/StdOut.java +++ b/kala-base/src/main/java/kala/io/StdOut.java @@ -1,3 +1,18 @@ +/* + * Copyright 2024 Glavo + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package kala.io; import kala.annotations.StaticClass; @@ -15,26 +30,17 @@ public static void print(Object x) { if (x == null) { System.out.print("null"); } else if (x.getClass().isArray()) { - if (x instanceof Object[]) { - print((Object[]) x); - } else if (x instanceof char[]) { - print((char[]) x); - } else if (x instanceof boolean[]) { - print((boolean[]) x); - } else if (x instanceof byte[]) { - print((byte[]) x); - } else if (x instanceof short[]) { - print((short[]) x); - } else if (x instanceof int[]) { - print((int[]) x); - } else if (x instanceof long[]) { - print((long[]) x); - } else if (x instanceof float[]) { - print((float[]) x); - } else if (x instanceof double[]) { - print((double[]) x); - } else { - throw new AssertionError("Unknown array type: " + x.getClass()); + switch (x) { + case Object[] objects -> print(objects); + case char[] chars -> print(chars); + case boolean[] booleans -> print(booleans); + case byte[] bytes -> print(bytes); + case short[] shorts -> print(shorts); + case int[] ints -> print(ints); + case long[] longs -> print(longs); + case float[] floats -> print(floats); + case double[] doubles -> print(doubles); + default -> throw new AssertionError("Unknown array type: " + x.getClass()); } } else { System.out.print(x); @@ -283,26 +289,17 @@ public static void println(Object x) { if (x == null) { System.out.println("null"); } else if (x.getClass().isArray()) { - if (x instanceof Object[]) { - println((Object[]) x); - } else if (x instanceof char[]) { - println((char[]) x); - } else if (x instanceof boolean[]) { - println((boolean[]) x); - } else if (x instanceof byte[]) { - println((byte[]) x); - } else if (x instanceof short[]) { - println((short[]) x); - } else if (x instanceof int[]) { - println((int[]) x); - } else if (x instanceof long[]) { - println((long[]) x); - } else if (x instanceof float[]) { - println((float[]) x); - } else if (x instanceof double[]) { - println((double[]) x); - } else { - throw new AssertionError("Unknown array type: " + x.getClass()); + switch (x) { + case Object[] objects -> println(objects); + case char[] chars -> println(chars); + case boolean[] booleans -> println(booleans); + case byte[] bytes -> println(bytes); + case short[] shorts -> println(shorts); + case int[] ints -> println(ints); + case long[] longs -> println(longs); + case float[] floats -> println(floats); + case double[] doubles -> println(doubles); + default -> throw new AssertionError("Unknown array type: " + x.getClass()); } } else { System.out.println(x);