Yuta HIGUCHI | f148aac | 2014-05-05 14:59:06 -0700 | [diff] [blame] | 1 | package net.onrc.onos.core.datastore.utils; |
| 2 | |
| 3 | import com.esotericsoftware.kryo.Kryo; |
| 4 | |
| 5 | /** |
| 6 | * Helper class to create thread local instance of Kryo. |
| 7 | */ |
| 8 | public final class ThreadLocalKryo extends ThreadLocal<Kryo> { |
| 9 | private final Class<?>[] expectedTypes; |
| 10 | |
| 11 | /** |
| 12 | * Constructor specifying expected classes to be serialized using this |
| 13 | * Kryo instance. |
| 14 | * <p/> |
| 15 | * @param expectedTypes list of .class to register to Kryo |
| 16 | */ |
| 17 | public ThreadLocalKryo(Class<?>... expectedTypes) { |
| 18 | this.expectedTypes = expectedTypes; |
| 19 | } |
| 20 | |
| 21 | @Override |
| 22 | protected Kryo initialValue() { |
| 23 | Kryo kryo = new Kryo(); |
| 24 | // kryo.setRegistrationRequired(true); |
| 25 | // kryo.setReferences(false); |
| 26 | for (Class<?> type : expectedTypes) { |
| 27 | kryo.register(type); |
| 28 | } |
| 29 | return kryo; |
| 30 | } |
| 31 | } |