diff --git a/chill-java/src/main/java/com/twitter/chill/java/LocaleSerializer.java b/chill-java/src/main/java/com/twitter/chill/java/LocaleSerializer.java new file mode 100644 index 00000000..aa769aa7 --- /dev/null +++ b/chill-java/src/main/java/com/twitter/chill/java/LocaleSerializer.java @@ -0,0 +1,37 @@ +/* +Copyright 2013 Twitter, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package com.twitter.chill.java; + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.serializers.JavaSerializer; +import com.esotericsoftware.kryo.Serializer; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +import com.twitter.chill.IKryoRegistrar; +import com.twitter.chill.SingleRegistrar; + +import java.util.Locale; + +/** The java serializer uses an cache of allocated instances so + * it is probably a bit hard to beat, so why bother + */ +public class LocaleSerializer extends JavaSerializer { + static public IKryoRegistrar registrar() { + return new SingleRegistrar(Locale.class, new LocaleSerializer()); + } +} diff --git a/chill-java/src/main/java/com/twitter/chill/java/PackageRegistrar.java b/chill-java/src/main/java/com/twitter/chill/java/PackageRegistrar.java index 0f429ef0..63a423da 100644 --- a/chill-java/src/main/java/com/twitter/chill/java/PackageRegistrar.java +++ b/chill-java/src/main/java/com/twitter/chill/java/PackageRegistrar.java @@ -38,6 +38,8 @@ static public IKryoRegistrar all() { TimestampSerializer.registrar(), URISerializer.registrar(), InetSocketAddressSerializer.registrar(), - UUIDSerializer.registrar()); + UUIDSerializer.registrar(), + LocaleSerializer.registrar(), + SimpleDateFormatSerializer.registrar()); } } diff --git a/chill-java/src/main/java/com/twitter/chill/java/SimpleDateFormatSerializer.java b/chill-java/src/main/java/com/twitter/chill/java/SimpleDateFormatSerializer.java new file mode 100644 index 00000000..f3711a30 --- /dev/null +++ b/chill-java/src/main/java/com/twitter/chill/java/SimpleDateFormatSerializer.java @@ -0,0 +1,33 @@ +/* +Copyright 2013 Twitter, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package com.twitter.chill.java; + +import com.esotericsoftware.kryo.serializers.JavaSerializer; + +import com.twitter.chill.IKryoRegistrar; +import com.twitter.chill.SingleRegistrar; + +import java.text.SimpleDateFormat; + +/** This class fails with the Fields serializer. + * If it is a perf bottleneck, we could write a Kryo serializer + */ +public class SimpleDateFormatSerializer extends JavaSerializer { + static public IKryoRegistrar registrar() { + return new SingleRegistrar(SimpleDateFormat.class, new SimpleDateFormatSerializer()); + } +} diff --git a/chill-java/src/test/scala/com/twitter/chill/java/LocaleTest.scala b/chill-java/src/test/scala/com/twitter/chill/java/LocaleTest.scala new file mode 100644 index 00000000..0b8f53cd --- /dev/null +++ b/chill-java/src/test/scala/com/twitter/chill/java/LocaleTest.scala @@ -0,0 +1,52 @@ +/* +Copyright 2012 Twitter, Inc. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package com.twitter.chill.java + +import org.specs._ + +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + +import org.objenesis.strategy.StdInstantiatorStrategy + +import _root_.java.util.Locale + +class LocaleSpec extends Specification { + noDetailedDiffs() //Fixes issue for scala 2.9 + + def rt[A](k: Kryo, a: A): A = { + val out = new Output(1000, -1) + k.writeClassAndObject(out, a.asInstanceOf[AnyRef]) + val in = new Input(out.toBytes) + k.readClassAndObject(in).asInstanceOf[A] + } + + "A Locale Serializer" should { + "serialize all the things" in { + import scala.collection.JavaConverters._ + + val kryo = new Kryo() + kryo.setInstantiatorStrategy(new StdInstantiatorStrategy) + LocaleSerializer.registrar()(kryo) + + Locale.getAvailableLocales.foreach { l => + rt(kryo, l) must be_==(l) + } + } + } +}