Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Locale serializer to chill-java #128

Merged
merged 2 commits into from
Sep 7, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}
}
}