Archiviert
13
0

Clone OptionalInts too

Dieser Commit ist enthalten in:
Dan Mulloy 2019-08-07 11:38:50 -04:00
Ursprung 8c20d19339
Commit 446a1c8326

Datei anzeigen

@ -4,6 +4,7 @@
package com.comphenix.protocol.reflect.cloning; package com.comphenix.protocol.reflect.cloning;
import java.util.Optional; import java.util.Optional;
import java.util.OptionalInt;
/** /**
* A cloner that can clone Java Optional objects * A cloner that can clone Java Optional objects
@ -18,13 +19,22 @@ public class JavaOptionalCloner implements Cloner {
@Override @Override
public boolean canClone(Object source) { public boolean canClone(Object source) {
return source instanceof Optional; return source instanceof Optional || source instanceof OptionalInt;
} }
@Override @Override
public Object clone(Object source) { public Object clone(Object source) {
Optional<?> optional = (Optional<?>) source; if (source instanceof Optional) {
return optional.map(o -> wrapped.clone(o)); 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() { public Cloner getWrapped() {