Skip to content

Commit

Permalink
Merge pull request #128 from twitter/locale_and_more
Browse files Browse the repository at this point in the history
Add a Locale serializer to chill-java
  • Loading branch information
sritchie committed Sep 7, 2013
2 parents 94429e6 + 44f3127 commit 4e294c4
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ static public IKryoRegistrar all() {
TimestampSerializer.registrar(),
URISerializer.registrar(),
InetSocketAddressSerializer.registrar(),
UUIDSerializer.registrar());
UUIDSerializer.registrar(),
LocaleSerializer.registrar(),
SimpleDateFormatSerializer.registrar());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
52 changes: 52 additions & 0 deletions chill-java/src/test/scala/com/twitter/chill/java/LocaleTest.scala
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
}

0 comments on commit 4e294c4

Please sign in to comment.