-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from twitter/locale_and_more
Add a Locale serializer to chill-java
- Loading branch information
Showing
4 changed files
with
125 additions
and
1 deletion.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
chill-java/src/main/java/com/twitter/chill/java/LocaleSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
chill-java/src/main/java/com/twitter/chill/java/SimpleDateFormatSerializer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
52
chill-java/src/test/scala/com/twitter/chill/java/LocaleTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |