Util { @Nullable public static <T> T checkSerializable(@Nullable T object, @Nonnull String objectName) { if (object == null) { return null; } if (object instanceof DataSerializable) { return object; } if (!(object instanceof Serializable)) { throw new IllegalArgumentException('"' + objectName + "\" must implement Serializable"); } try (ObjectOutputStream os = new ObjectOutputStream(new NullOutputStream())) { os.writeObject(object); } catch (NotSerializableException | InvalidClassException e) { throw new IllegalArgumentException("\"" + objectName + "\" must be serializable", e); } catch (IOException e) { throw new JetException(e); } return object; } private  Util(); static Supplier<T> memoize(Supplier<T> onceSupplier); static Supplier<T> memoizeConcurrent(Supplier<T> onceSupplier); static T uncheckCall(@Nonnull Callable<T> callable); static void uncheckRun(@Nonnull RunnableEx r); @CheckReturnValue static boolean tryIncrement(AtomicInteger value, int increment, int limit); static JetInstance getJetInstance(NodeEngine nodeEngine); static long addClamped(long a, long b); static long subtractClamped(long a, long b); static boolean sumHadOverflow(long a, long b, long sum); @Nullable static T checkSerializable(@Nullable T object, @Nonnull String objectName); @Nonnull static T checkNonNullAndSerializable(@Nonnull T object, @Nonnull String objectName); static Map<Integer, List<T>> distributeObjects(int count, List<T> objects); static int[] roundRobinPart(int objectCount, int count, int index); static String jobNameAndExecutionId(String jobName, long executionId); static String jobIdAndExecutionId(long jobId, long executionId); static LocalDateTime toLocalDateTime(long timestamp); static String toLocalTime(long timestamp); static int arrayIndexOf(int needle, int[] haystack); static CompletableFuture<T> exceptionallyCompletedFuture(@Nonnull Throwable exception); static void logLateEvent(ILogger logger, long currentWm, @Nonnull Object item); static long gcd(long... values); static long gcd(long a, long b); static void lazyIncrement(AtomicLongArray counters, int index); static void lazyAdd(AtomicLongArray counters, int index, long addend); static int sum(Collection<E> collection, ToIntFunction<E> toIntF); static String escapeGraphviz(String value); @SuppressWarnings("WeakerAccess") // used in jet-enterprise static CompletableFuture<Void> copyMapUsingJob(JetInstance instance, int queueSize,
                                                          String sourceMap, String targetMap); static String addOrIncrementIndexInName(String name); static String sanitizeLoggerNamePart(String name); static void doWithClassLoader(ClassLoader cl, RunnableEx action); static ProcessingGuarantee min(ProcessingGuarantee g1, ProcessingGuarantee g2); @Nonnull static List<R> toList(@Nonnull Collection<T> coll, Function<? super T, ? extends R> mapFn);  }
@Test public void whenNullToCheckSerializable_thenReturnNull() { Object returned = Util.checkSerializable(null, "object"); assertThat(returned).isNull(); }
@Test public void whenSerializableObjectToCheckSerializable_thenReturnObject() { Object o = "o"; Object returned = Util.checkSerializable(o, "object"); assertThat(returned).isSameAs(o); }
@Test public void whenNonSerializableObjectToCheckSerializable_thenThrowException() { assertThatThrownBy(() -> Util.checkSerializable(new HashMap<>().entrySet(), "object")) .isInstanceOf(IllegalArgumentException.class) .hasMessage("\"object\" must implement Serializable"); }