Archiviert
13
0

Clone Java optionals

Fixes #498
Dieser Commit ist enthalten in:
Dan Mulloy 2018-08-01 16:08:53 -04:00
Ursprung 3ef537242f
Commit 8d9bf7d91f
3 geänderte Dateien mit 38 neuen und 4 gelöschten Zeilen

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

@ -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;
}
}