From 446a1c8326462df5805b25ba2818b78bc3dd1e9d Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Wed, 7 Aug 2019 11:38:50 -0400 Subject: [PATCH] Clone OptionalInts too --- .../reflect/cloning/JavaOptionalCloner.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/comphenix/protocol/reflect/cloning/JavaOptionalCloner.java b/src/main/java/com/comphenix/protocol/reflect/cloning/JavaOptionalCloner.java index 040df3a5..65847cb2 100644 --- a/src/main/java/com/comphenix/protocol/reflect/cloning/JavaOptionalCloner.java +++ b/src/main/java/com/comphenix/protocol/reflect/cloning/JavaOptionalCloner.java @@ -4,6 +4,7 @@ package com.comphenix.protocol.reflect.cloning; import java.util.Optional; +import java.util.OptionalInt; /** * A cloner that can clone Java Optional objects @@ -18,13 +19,22 @@ public class JavaOptionalCloner implements Cloner { @Override public boolean canClone(Object source) { - return source instanceof Optional; + return source instanceof Optional || source instanceof OptionalInt; } @Override public Object clone(Object source) { - Optional optional = (Optional) source; - return optional.map(o -> wrapped.clone(o)); + if (source instanceof Optional) { + Optional optional = (Optional) source; + return optional.map(o -> wrapped.clone(o)); + } else if (source instanceof OptionalInt) { + // why Java felt the need to make each optional class distinct is beyond me + // like why couldn't they have given us at least a common interface or something + OptionalInt optional = (OptionalInt) source; + return optional.isPresent() ? OptionalInt.of(optional.getAsInt()) : OptionalInt.empty(); + } + + return null; } public Cloner getWrapped() {