blob: d1f95c05a86513a7d38c2b8b55ca2b66022d8d78 [file] [log] [blame]
Thomas Vachuska24c849c2014-10-27 09:53:05 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska24c849c2014-10-27 09:53:05 -07003 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
Thomas Vachuska24c849c2014-10-27 09:53:05 -07007 *
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07008 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
Thomas Vachuska24c849c2014-10-27 09:53:05 -070015 */
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070016package org.onlab.util;
17
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070018import com.esotericsoftware.kryo.Kryo;
HIGUCHI Yuta0a1f29e2016-05-05 15:34:41 -070019import com.esotericsoftware.kryo.Registration;
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070020import com.esotericsoftware.kryo.Serializer;
Yuta HIGUCHIf4b107e2014-09-29 17:27:26 -070021import com.esotericsoftware.kryo.io.ByteBufferInput;
22import com.esotericsoftware.kryo.io.ByteBufferOutput;
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070023import com.esotericsoftware.kryo.io.Input;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080024import com.esotericsoftware.kryo.pool.KryoCallback;
Yuta HIGUCHI633cf882014-10-20 09:10:28 -070025import com.esotericsoftware.kryo.pool.KryoFactory;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080026import com.esotericsoftware.kryo.pool.KryoPool;
27import com.google.common.base.MoreObjects;
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070028import com.google.common.collect.ImmutableList;
Jonathan Hartbe093f72016-03-25 11:14:29 -070029import org.apache.commons.lang3.tuple.Pair;
30import org.objenesis.strategy.StdInstantiatorStrategy;
31import org.slf4j.Logger;
32
33import java.io.InputStream;
34import java.io.OutputStream;
35import java.nio.ByteBuffer;
36import java.util.ArrayList;
37import java.util.List;
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -070038import java.util.Objects;
Jonathan Hartbe093f72016-03-25 11:14:29 -070039
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -070040import static com.google.common.base.Preconditions.checkNotNull;
Jonathan Hartbe093f72016-03-25 11:14:29 -070041import static org.slf4j.LoggerFactory.getLogger;
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070042
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070043/**
44 * Pool of Kryo instances, with classes pre-registered.
45 */
46//@ThreadSafe
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080047public final class KryoNamespace implements KryoFactory, KryoPool {
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070048
49 /**
50 * Default buffer size used for serialization.
51 *
52 * @see #serialize(Object)
53 */
Yuta HIGUCHI38782052014-11-09 23:51:58 -080054 public static final int DEFAULT_BUFFER_SIZE = 4096;
Yuta HIGUCHId2a38822014-11-06 19:05:04 -080055 public static final int MAX_BUFFER_SIZE = 100 * 1000 * 1000;
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070056
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080057 /**
58 * ID to use if this KryoNamespace does not define registration id.
59 */
60 public static final int FLOATING_ID = -1;
61
62 /**
63 * Smallest ID free to use for user defined registrations.
64 */
HIGUCHI Yuta0a1f29e2016-05-05 15:34:41 -070065 public static final int INITIAL_ID = 16;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080066
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -070067 private static final String NO_NAME = "(no name)";
68
HIGUCHI Yutab49b0072016-02-22 22:50:45 -080069 private static final Logger log = getLogger(KryoNamespace.class);
70
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080071
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -070072
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080073 private final KryoPool pool = new KryoPool.Builder(this)
74 .softReferences()
75 .build();
76
77 private final ImmutableList<RegistrationBlock> registeredBlocks;
78
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070079 private final boolean registrationRequired;
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -070080 private final String friendlyName;
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070081
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080082
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070083 /**
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070084 * KryoNamespace builder.
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070085 */
86 //@NotThreadSafe
87 public static final class Builder {
88
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080089 private int blockHeadId = INITIAL_ID;
90 private List<Pair<Class<?>, Serializer<?>>> types = new ArrayList<>();
91 private List<RegistrationBlock> blocks = new ArrayList<>();
Yuta HIGUCHI2befc662014-10-30 15:57:49 -070092 private boolean registrationRequired = true;
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070093
94 /**
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070095 * Builds a {@link KryoNamespace} instance.
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070096 *
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070097 * @return KryoNamespace
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -070098 */
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070099 public KryoNamespace build() {
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700100 return build(NO_NAME);
101 }
102
103 /**
104 * Builds a {@link KryoNamespace} instance.
105 *
106 * @param friendlyName friendly name for the namespace
107 * @return KryoNamespace
108 */
109 public KryoNamespace build(String friendlyName) {
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800110 if (!types.isEmpty()) {
111 blocks.add(new RegistrationBlock(this.blockHeadId, types));
112 }
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700113 return new KryoNamespace(blocks, registrationRequired, friendlyName).populate(1);
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800114 }
115
116 /**
117 * Sets the next Kryo registration Id for following register entries.
118 *
119 * @param id Kryo registration Id
120 * @return this
121 *
122 * @see Kryo#register(Class, Serializer, int)
123 */
124 public Builder nextId(final int id) {
125 if (!types.isEmpty()) {
HIGUCHI Yutab49b0072016-02-22 22:50:45 -0800126 if (id != FLOATING_ID && id < blockHeadId + types.size()) {
127
HIGUCHI Yuta163efb52016-05-18 19:24:19 -0700128 if (log.isWarnEnabled()) {
129 log.warn("requested nextId {} could potentially overlap " +
130 "with existing registrations {}+{} ",
131 id, blockHeadId, types.size(), new RuntimeException());
132 }
HIGUCHI Yutab49b0072016-02-22 22:50:45 -0800133 }
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800134 blocks.add(new RegistrationBlock(this.blockHeadId, types));
135 types = new ArrayList<>();
136 }
137 this.blockHeadId = id;
138 return this;
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700139 }
140
141 /**
142 * Registers classes to be serialized using Kryo default serializer.
143 *
144 * @param expectedTypes list of classes
145 * @return this
146 */
147 public Builder register(final Class<?>... expectedTypes) {
148 for (Class<?> clazz : expectedTypes) {
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800149 types.add(Pair.of(clazz, null));
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700150 }
151 return this;
152 }
153
154 /**
155 * Registers a class and it's serializer.
156 *
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800157 * @param classes list of classes to register
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700158 * @param serializer serializer to use for the class
159 * @return this
160 */
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800161 public Builder register(Serializer<?> serializer, final Class<?>... classes) {
162 for (Class<?> clazz : classes) {
163 types.add(Pair.of(clazz, serializer));
164 }
165 return this;
166 }
167
168 private Builder register(RegistrationBlock block) {
169 if (block.begin() != FLOATING_ID) {
170 // flush pending types
171 nextId(block.begin());
172 blocks.add(block);
173 nextId(block.begin() + block.types().size());
174 } else {
175 // flush pending types
176 final int addedBlockBegin = blockHeadId + types.size();
177 nextId(addedBlockBegin);
178 blocks.add(new RegistrationBlock(addedBlockBegin, block.types()));
179 nextId(addedBlockBegin + block.types().size());
180 }
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700181 return this;
182 }
183
184 /**
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700185 * Registers all the class registered to given KryoNamespace.
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700186 *
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800187 * @param ns KryoNamespace
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700188 * @return this
189 */
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800190 public Builder register(final KryoNamespace ns) {
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700191
192 if (blocks.containsAll(ns.registeredBlocks)) {
193 // Everything was already registered.
194 log.debug("Ignoring {}, already registered.", ns);
195 return this;
196 }
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800197 for (RegistrationBlock block : ns.registeredBlocks) {
198 this.register(block);
199 }
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700200 return this;
201 }
Yuta HIGUCHI2befc662014-10-30 15:57:49 -0700202
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800203 /**
204 * Sets the registrationRequired flag.
205 *
206 * @param registrationRequired Kryo's registrationRequired flag
207 * @return this
208 *
209 * @see Kryo#setRegistrationRequired(boolean)
210 */
Yuta HIGUCHI2befc662014-10-30 15:57:49 -0700211 public Builder setRegistrationRequired(boolean registrationRequired) {
212 this.registrationRequired = registrationRequired;
213 return this;
214 }
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700215 }
216
217 /**
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700218 * Creates a new {@link KryoNamespace} builder.
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700219 *
220 * @return builder
221 */
222 public static Builder newBuilder() {
223 return new Builder();
224 }
225
226 /**
227 * Creates a Kryo instance pool.
228 *
Jonathan Hart4f60f982014-10-27 08:11:17 -0700229 * @param registeredTypes types to register
Yuta HIGUCHI2befc662014-10-30 15:57:49 -0700230 * @param registrationRequired
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700231 * @param friendlyName friendly name for the namespace
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700232 */
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700233 private KryoNamespace(final List<RegistrationBlock> registeredTypes,
234 boolean registrationRequired,
235 String friendlyName) {
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800236 this.registeredBlocks = ImmutableList.copyOf(registeredTypes);
Yuta HIGUCHI2befc662014-10-30 15:57:49 -0700237 this.registrationRequired = registrationRequired;
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700238 this.friendlyName = checkNotNull(friendlyName);
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700239 }
240
241 /**
242 * Populates the Kryo pool.
243 *
244 * @param instances to add to the pool
245 * @return this
246 */
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -0700247 public KryoNamespace populate(int instances) {
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800248
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700249 for (int i = 0; i < instances; ++i) {
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800250 release(create());
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700251 }
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700252 return this;
253 }
254
255 /**
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700256 * Serializes given object to byte array using Kryo instance in pool.
257 * <p>
Yuta HIGUCHI38782052014-11-09 23:51:58 -0800258 * Note: Serialized bytes must be smaller than {@link #MAX_BUFFER_SIZE}.
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700259 *
260 * @param obj Object to serialize
261 * @return serialized bytes
262 */
263 public byte[] serialize(final Object obj) {
264 return serialize(obj, DEFAULT_BUFFER_SIZE);
265 }
266
267 /**
268 * Serializes given object to byte array using Kryo instance in pool.
269 *
270 * @param obj Object to serialize
271 * @param bufferSize maximum size of serialized bytes
272 * @return serialized bytes
273 */
274 public byte[] serialize(final Object obj, final int bufferSize) {
Yuta HIGUCHId2a38822014-11-06 19:05:04 -0800275 ByteBufferOutput out = new ByteBufferOutput(bufferSize, MAX_BUFFER_SIZE);
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700276 try {
Madan Jampani22fa5cb2015-04-13 15:53:44 -0700277 Kryo kryo = borrow();
278 try {
279 kryo.writeClassAndObject(out, obj);
280 out.flush();
281 return out.toBytes();
282 } finally {
283 release(kryo);
284 }
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700285 } finally {
Madan Jampani22fa5cb2015-04-13 15:53:44 -0700286 out.release();
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700287 }
288 }
289
290 /**
Yuta HIGUCHIf4b107e2014-09-29 17:27:26 -0700291 * Serializes given object to byte buffer using Kryo instance in pool.
292 *
293 * @param obj Object to serialize
294 * @param buffer to write to
295 */
296 public void serialize(final Object obj, final ByteBuffer buffer) {
297 ByteBufferOutput out = new ByteBufferOutput(buffer);
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800298 Kryo kryo = borrow();
Yuta HIGUCHIf4b107e2014-09-29 17:27:26 -0700299 try {
300 kryo.writeClassAndObject(out, obj);
Yuta HIGUCHIcac919c2014-10-20 22:17:20 -0700301 out.flush();
Yuta HIGUCHIf4b107e2014-09-29 17:27:26 -0700302 } finally {
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800303 release(kryo);
304 }
305 }
306
307 /**
308 * Serializes given object to OutputStream using Kryo instance in pool.
309 *
310 * @param obj Object to serialize
311 * @param stream to write to
312 */
313 public void serialize(final Object obj, final OutputStream stream) {
314 serialize(obj, stream, DEFAULT_BUFFER_SIZE);
315 }
316
317 /**
318 * Serializes given object to OutputStream using Kryo instance in pool.
319 *
320 * @param obj Object to serialize
321 * @param stream to write to
322 * @param bufferSize size of the buffer in front of the stream
323 */
324 public void serialize(final Object obj, final OutputStream stream, final int bufferSize) {
325 ByteBufferOutput out = new ByteBufferOutput(stream, bufferSize);
326 Kryo kryo = borrow();
327 try {
328 kryo.writeClassAndObject(out, obj);
329 out.flush();
330 } finally {
331 release(kryo);
Yuta HIGUCHIf4b107e2014-09-29 17:27:26 -0700332 }
333 }
334
335 /**
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700336 * Deserializes given byte array to Object using Kryo instance in pool.
337 *
338 * @param bytes serialized bytes
339 * @param <T> deserialized Object type
340 * @return deserialized Object
341 */
342 public <T> T deserialize(final byte[] bytes) {
343 Input in = new Input(bytes);
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800344 Kryo kryo = borrow();
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700345 try {
346 @SuppressWarnings("unchecked")
347 T obj = (T) kryo.readClassAndObject(in);
348 return obj;
349 } finally {
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800350 release(kryo);
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700351 }
352 }
353
Yuta HIGUCHIf4b107e2014-09-29 17:27:26 -0700354 /**
355 * Deserializes given byte buffer to Object using Kryo instance in pool.
356 *
357 * @param buffer input with serialized bytes
358 * @param <T> deserialized Object type
359 * @return deserialized Object
360 */
361 public <T> T deserialize(final ByteBuffer buffer) {
362 ByteBufferInput in = new ByteBufferInput(buffer);
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800363 Kryo kryo = borrow();
Yuta HIGUCHIf4b107e2014-09-29 17:27:26 -0700364 try {
365 @SuppressWarnings("unchecked")
366 T obj = (T) kryo.readClassAndObject(in);
367 return obj;
368 } finally {
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800369 release(kryo);
Yuta HIGUCHIf4b107e2014-09-29 17:27:26 -0700370 }
371 }
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700372
373 /**
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800374 * Deserializes given InputStream to an Object using Kryo instance in pool.
375 *
376 * @param stream input stream
377 * @param <T> deserialized Object type
378 * @return deserialized Object
379 */
380 public <T> T deserialize(final InputStream stream) {
381 return deserialize(stream, DEFAULT_BUFFER_SIZE);
382 }
383
384 /**
385 * Deserializes given InputStream to an Object using Kryo instance in pool.
386 *
387 * @param stream input stream
388 * @param <T> deserialized Object type
389 * @return deserialized Object
390 * @param bufferSize size of the buffer in front of the stream
391 */
392 public <T> T deserialize(final InputStream stream, final int bufferSize) {
393 ByteBufferInput in = new ByteBufferInput(stream, bufferSize);
394 Kryo kryo = borrow();
395 try {
396 @SuppressWarnings("unchecked")
397 T obj = (T) kryo.readClassAndObject(in);
398 return obj;
399 } finally {
400 release(kryo);
401 }
402 }
403
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700404 private String friendlyName() {
405 return friendlyName;
406 }
407
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800408 /**
409 * Creates a Kryo instance.
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700410 *
411 * @return Kryo instance
412 */
Yuta HIGUCHI633cf882014-10-20 09:10:28 -0700413 @Override
414 public Kryo create() {
HIGUCHI Yuta0a1f29e2016-05-05 15:34:41 -0700415 log.trace("Creating Kryo instance for {}", this);
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700416 Kryo kryo = new Kryo();
417 kryo.setRegistrationRequired(registrationRequired);
Jonathan Hartbe093f72016-03-25 11:14:29 -0700418
419 // TODO rethink whether we want to use StdInstantiatorStrategy
420 kryo.setInstantiatorStrategy(
421 new Kryo.DefaultInstantiatorStrategy(new StdInstantiatorStrategy()));
422
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800423 for (RegistrationBlock block : registeredBlocks) {
424 int id = block.begin();
425 if (id == FLOATING_ID) {
426 id = kryo.getNextRegistrationId();
427 }
428 for (Pair<Class<?>, Serializer<?>> entry : block.types()) {
HIGUCHI Yuta0a1f29e2016-05-05 15:34:41 -0700429 register(kryo, entry.getLeft(), entry.getRight(), id++);
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700430 }
431 }
432 return kryo;
433 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700434
HIGUCHI Yuta0a1f29e2016-05-05 15:34:41 -0700435 /**
436 * Register {@code type} and {@code serializer} to {@code kryo} instance.
437 *
438 * @param kryo Kryo instance
439 * @param type type to register
440 * @param serializer Specific serializer to register or null to use default.
441 * @param id type registration id to use
442 */
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700443 private void register(Kryo kryo, Class<?> type, Serializer<?> serializer, int id) {
HIGUCHI Yuta0a1f29e2016-05-05 15:34:41 -0700444 Registration existing = kryo.getRegistration(id);
445 if (existing != null) {
446 if (existing.getType() != type) {
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700447 log.error("{}: Failed to register {} as {}, {} was already registered.",
448 friendlyName(), type, id, existing.getType());
HIGUCHI Yuta0a1f29e2016-05-05 15:34:41 -0700449
450 throw new IllegalStateException(String.format(
451 "Failed to register %s as %s, %s was already registered.",
452 type, id, existing.getType()));
453 }
454 // falling through to register call for now.
455 // Consider skipping, if there's reasonable
456 // way to compare serializer equivalence.
457 }
458 Registration r;
459 if (serializer == null) {
460 r = kryo.register(type, id);
461 } else {
462 r = kryo.register(type, serializer, id);
463 }
464 if (r.getId() != id) {
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700465 log.warn("{}: {} already registed as {}. Skipping {}.",
466 friendlyName(), r.getType(), r.getId(), id);
HIGUCHI Yuta0a1f29e2016-05-05 15:34:41 -0700467 }
468 log.trace("{} registered as {}", r.getType(), r.getId());
469 }
470
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800471 @Override
472 public Kryo borrow() {
473 return pool.borrow();
474 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700475
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800476 @Override
477 public void release(Kryo kryo) {
478 pool.release(kryo);
479 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700480
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800481 @Override
482 public <T> T run(KryoCallback<T> callback) {
483 return pool.run(callback);
484 }
485
486 @Override
487 public String toString() {
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700488 if (friendlyName != NO_NAME) {
489 return MoreObjects.toStringHelper(getClass())
490 .omitNullValues()
491 .add("friendlyName", friendlyName)
492 // omit lengthy detail, when there's a name
493 .toString();
494 }
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800495 return MoreObjects.toStringHelper(getClass())
496 .add("registeredBlocks", registeredBlocks)
497 .toString();
498 }
499
500 static final class RegistrationBlock {
501 private final int begin;
502 private final ImmutableList<Pair<Class<?>, Serializer<?>>> types;
503
504 public RegistrationBlock(int begin, List<Pair<Class<?>, Serializer<?>>> types) {
505 this.begin = begin;
506 this.types = ImmutableList.copyOf(types);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700507 }
508
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800509 public int begin() {
510 return begin;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700511 }
512
Yuta HIGUCHI91768e32014-11-22 05:06:35 -0800513 public ImmutableList<Pair<Class<?>, Serializer<?>>> types() {
514 return types;
515 }
516
517 @Override
518 public String toString() {
519 return MoreObjects.toStringHelper(getClass())
520 .add("begin", begin)
521 .add("types", types)
522 .toString();
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700523 }
HIGUCHI Yutab2d0fd82016-05-17 20:34:58 -0700524
525 @Override
526 public int hashCode() {
527 return types.hashCode();
528 }
529
530 // Only the registered types are used for equality.
531 @Override
532 public boolean equals(Object obj) {
533 if (this == obj) {
534 return true;
535 }
536
537 if (obj instanceof RegistrationBlock) {
538 RegistrationBlock that = (RegistrationBlock) obj;
539 return Objects.equals(this.types, that.types);
540 }
541 return false;
542 }
Yuta HIGUCHI533ec322014-09-30 13:29:52 -0700543 }
Yuta HIGUCHI24a086b2014-09-21 23:28:41 -0700544}