Clone Java optionals

Fixes #498
This commit is contained in:
Dan Mulloy 2018-08-01 16:08:53 -04:00
parent 3ef537242f
commit 8d9bf7d91f
3 changed files with 38 additions and 4 deletions

View File

@ -86,7 +86,8 @@ public class PacketContainer implements Serializable {
.instanceProvider(DefaultInstances.DEFAULT)
.andThen(BukkitCloner.class)
.andThen(ImmutableDetector.class)
.andThen(OptionalCloner.class)
.andThen(JavaOptionalCloner.class)
.andThen(GuavaOptionalCloner.class)
.andThen(CollectionCloner.class)
.andThen(getSpecializedDeepClonerFactory())
.build();

View File

@ -6,14 +6,14 @@ package com.comphenix.protocol.reflect.cloning;
import com.google.common.base.Optional;
/**
* A cloner that can clone Optional objects
* A cloner that can clone Guava Optional objects
* @author dmulloy2
*/
public class OptionalCloner implements Cloner {
public class GuavaOptionalCloner implements Cloner {
protected Cloner wrapped;
public OptionalCloner(Cloner wrapped) {
public GuavaOptionalCloner(Cloner wrapped) {
this.wrapped = wrapped;
}

View File

@ -0,0 +1,33 @@
/**
* (c) 2018 dmulloy2
*/
package com.comphenix.protocol.reflect.cloning;
import java.util.Optional;
/**
* A cloner that can clone Java Optional objects
* @author dmulloy2
*/
public class JavaOptionalCloner implements Cloner {
protected Cloner wrapped;
public JavaOptionalCloner(Cloner wrapped) {
this.wrapped = wrapped;
}
@Override
public boolean canClone(Object source) {
return source instanceof Optional;
}
@Override
public Object clone(Object source) {
Optional<?> optional = (Optional<?>) source;
return optional.map(o -> wrapped.clone(o));
}
public Cloner getWrapped() {
return wrapped;
}
}