Formatting java codes with sbt-java-formatter.

This commit is contained in:
hepin1989 2019-01-12 04:00:53 +08:00
parent 27500001ea
commit 998c5a9285
401 changed files with 19750 additions and 17450 deletions

View file

@ -11,104 +11,104 @@ import org.junit.Test;
import static org.junit.Assert.*;
import java.nio.charset.StandardCharsets;
//#imports
// #imports
import akka.actor.*;
import akka.serialization.*;
//#imports
// #imports
public class SerializationDocTest {
static
//#my-own-serializer
public class MyOwnSerializer extends JSerializer {
public
// #my-own-serializer
static class MyOwnSerializer extends JSerializer {
// If you need logging here, introduce a constructor that takes an ExtendedActorSystem.
// public MyOwnSerializer(ExtendedActorSystem actorSystem)
// public MyOwnSerializer(ExtendedActorSystem actorSystem)
// Get a logger using:
// private final LoggingAdapter logger = Logging.getLogger(actorSystem, this);
// private final LoggingAdapter logger = Logging.getLogger(actorSystem, this);
// This is whether "fromBinary" requires a "clazz" or not
@Override public boolean includeManifest() {
@Override
public boolean includeManifest() {
return false;
}
// Pick a unique identifier for your Serializer,
// you've got a couple of billions to choose from,
// 0 - 40 is reserved by Akka itself
@Override public int identifier() {
@Override
public int identifier() {
return 1234567;
}
// "toBinary" serializes the given object to an Array of Bytes
@Override public byte[] toBinary(Object obj) {
@Override
public byte[] toBinary(Object obj) {
// Put the code that serializes the object here
//#...
// #...
return new byte[0];
//#...
// #...
}
// "fromBinary" deserializes the given array,
// using the type hint (if any, see "includeManifest" above)
@Override public Object fromBinaryJava(byte[] bytes,
Class<?> clazz) {
@Override
public Object fromBinaryJava(byte[] bytes, Class<?> clazz) {
// Put your code that deserializes here
//#...
// #...
return null;
//#...
// #...
}
}
//#my-own-serializer
// #my-own-serializer
static class Customer {
public final String name;
Customer(String name) {
this.name = name;
}
}
static class User {
public final String name;
User(String name) {
this.name = name;
}
}
static
//#my-own-serializer2
public class MyOwnSerializer2 extends SerializerWithStringManifest {
public
// #my-own-serializer2
static class MyOwnSerializer2 extends SerializerWithStringManifest {
private static final String CUSTOMER_MANIFEST = "customer";
private static final String USER_MANIFEST = "user";
private static final String UTF_8 = StandardCharsets.UTF_8.name();
// Pick a unique identifier for your Serializer,
// you've got a couple of billions to choose from,
// 0 - 40 is reserved by Akka itself
@Override public int identifier() {
@Override
public int identifier() {
return 1234567;
}
@Override public String manifest(Object obj) {
if (obj instanceof Customer)
return CUSTOMER_MANIFEST;
else if (obj instanceof User)
return USER_MANIFEST;
else
throw new IllegalArgumentException("Unknown type: " + obj);
@Override
public String manifest(Object obj) {
if (obj instanceof Customer) return CUSTOMER_MANIFEST;
else if (obj instanceof User) return USER_MANIFEST;
else throw new IllegalArgumentException("Unknown type: " + obj);
}
// "toBinary" serializes the given object to an Array of Bytes
@Override public byte[] toBinary(Object obj) {
@Override
public byte[] toBinary(Object obj) {
// Put the real code that serializes the object here
try {
if (obj instanceof Customer)
return ((Customer) obj).name.getBytes(UTF_8);
else if (obj instanceof User)
return ((User) obj).name.getBytes(UTF_8);
else
throw new IllegalArgumentException("Unknown type: " + obj);
if (obj instanceof Customer) return ((Customer) obj).name.getBytes(UTF_8);
else if (obj instanceof User) return ((User) obj).name.getBytes(UTF_8);
else throw new IllegalArgumentException("Unknown type: " + obj);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
@ -116,29 +116,26 @@ public class SerializationDocTest {
// "fromBinary" deserializes the given array,
// using the type hint
@Override public Object fromBinary(byte[] bytes, String manifest) {
@Override
public Object fromBinary(byte[] bytes, String manifest) {
// Put the real code that deserializes here
try {
if (manifest.equals(CUSTOMER_MANIFEST))
return new Customer(new String(bytes, UTF_8));
else if (manifest.equals(USER_MANIFEST))
return new User(new String(bytes, UTF_8));
else
throw new IllegalArgumentException("Unknown manifest: " + manifest);
if (manifest.equals(CUSTOMER_MANIFEST)) return new Customer(new String(bytes, UTF_8));
else if (manifest.equals(USER_MANIFEST)) return new User(new String(bytes, UTF_8));
else throw new IllegalArgumentException("Unknown manifest: " + manifest);
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}
//#my-own-serializer2
// #my-own-serializer2
@Test public void serializeActorRefs() {
final ExtendedActorSystem extendedSystem = (ExtendedActorSystem)
ActorSystem.create("whatever");
final ActorRef theActorRef =
extendedSystem.deadLetters(); // Of course this should be you
@Test
public void serializeActorRefs() {
final ExtendedActorSystem extendedSystem = (ExtendedActorSystem) ActorSystem.create("whatever");
final ActorRef theActorRef = extendedSystem.deadLetters(); // Of course this should be you
//#actorref-serializer
// #actorref-serializer
// Serialize
// (beneath toBinary)
String identifier = Serialization.serializedActorPath(theActorRef);
@ -147,16 +144,15 @@ public class SerializationDocTest {
// Deserialize
// (beneath fromBinary)
final ActorRef deserializedActorRef = extendedSystem.provider().resolveActorRef(
identifier);
final ActorRef deserializedActorRef = extendedSystem.provider().resolveActorRef(identifier);
// Then just use the ActorRef
//#actorref-serializer
// #actorref-serializer
TestKit.shutdownActorSystem(extendedSystem);
}
static
//#external-address
public class ExternalAddressExt implements Extension {
public
// #external-address
static class ExternalAddressExt implements Extension {
private final ExtendedActorSystem system;
public ExternalAddressExt(ExtendedActorSystem system) {
@ -164,22 +160,20 @@ public class SerializationDocTest {
}
public Address getAddressFor(Address remoteAddress) {
final scala.Option<Address> optAddr = system.provider()
.getExternalAddressFor(remoteAddress);
final scala.Option<Address> optAddr = system.provider().getExternalAddressFor(remoteAddress);
if (optAddr.isDefined()) {
return optAddr.get();
} else {
throw new UnsupportedOperationException(
"cannot send to remote address " + remoteAddress);
throw new UnsupportedOperationException("cannot send to remote address " + remoteAddress);
}
}
}
//#external-address
static
//#external-address
public class ExternalAddress extends
AbstractExtensionId<ExternalAddressExt> implements ExtensionIdProvider {
// #external-address
public
// #external-address
static class ExternalAddress extends AbstractExtensionId<ExternalAddressExt>
implements ExtensionIdProvider {
public static final ExternalAddress ID = new ExternalAddress();
public ExternalAddress lookup() {
@ -191,24 +185,24 @@ public class SerializationDocTest {
}
}
//#external-address
static
//#external-address
public class ExternalAddressExample {
//#external-address
// #external-address
public
// #external-address
static class ExternalAddressExample {
// #external-address
final ActorSystem system = ActorSystem.create();
//#external-address
// #external-address
public String serializeTo(ActorRef ref, Address remote) {
return ref.path().toSerializationFormatWithAddress(
ExternalAddress.ID.get(system).getAddressFor(remote));
return ref.path()
.toSerializationFormatWithAddress(ExternalAddress.ID.get(system).getAddressFor(remote));
}
}
//#external-address
// #external-address
static
//#external-address-default
public class DefaultAddressExt implements Extension {
public
// #external-address-default
static class DefaultAddressExt implements Extension {
private final ExtendedActorSystem system;
public DefaultAddressExt(ExtendedActorSystem system) {
@ -220,11 +214,11 @@ public class SerializationDocTest {
}
}
//#external-address-default
static
//#external-address-default
public class DefaultAddress extends
AbstractExtensionId<DefaultAddressExt> implements ExtensionIdProvider {
// #external-address-default
public
// #external-address-default
static class DefaultAddress extends AbstractExtensionId<DefaultAddressExt>
implements ExtensionIdProvider {
public static final DefaultAddress ID = new DefaultAddress();
public DefaultAddress lookup() {
@ -236,7 +230,7 @@ public class SerializationDocTest {
}
}
//#external-address-default
// #external-address-default
public void demonstrateDefaultAddress() {
// this is not meant to be run, only to be compiled
@ -249,7 +243,7 @@ public class SerializationDocTest {
@Test
public void demonstrateTheProgrammaticAPI() {
//#programmatic
// #programmatic
ActorSystem system = ActorSystem.create("example");
// Get the Serialization Extension
@ -271,7 +265,7 @@ public class SerializationDocTest {
// Voilá!
assertEquals(original, back);
//#programmatic
// #programmatic
TestKit.shutdownActorSystem(system);
}
}