blob: d51f37bd5383766be024313ddada6964307e9bbc [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * 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
7 *
8 * 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.
15 */
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070016package org.onlab.onos.store.serializers;
17
Yuta HIGUCHI8d143d22014-10-19 23:15:09 -070018import org.onlab.util.KryoNamespace.FamilySerializer;
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070019
20import com.esotericsoftware.kryo.Kryo;
21import com.esotericsoftware.kryo.io.Input;
22import com.esotericsoftware.kryo.io.Output;
23import com.google.common.collect.ImmutableList;
24import com.google.common.collect.ImmutableList.Builder;
25
26/**
27 * Creates {@link ImmutableList} serializer instance.
28 */
29public class ImmutableListSerializer extends FamilySerializer<ImmutableList<?>> {
30
31 /**
32 * Creates {@link ImmutableList} serializer instance.
33 */
34 public ImmutableListSerializer() {
35 // non-null, immutable
36 super(false, true);
37 }
38 @Override
39 public void write(Kryo kryo, Output output, ImmutableList<?> object) {
40 output.writeInt(object.size());
41 for (Object e : object) {
42 kryo.writeClassAndObject(output, e);
43 }
44 }
45
46 @Override
47 public ImmutableList<?> read(Kryo kryo, Input input,
48 Class<ImmutableList<?>> type) {
49 final int size = input.readInt();
50 Builder<Object> builder = ImmutableList.builder();
51 for (int i = 0; i < size; ++i) {
52 builder.add(kryo.readClassAndObject(input));
53 }
54 return builder.build();
55 }
56
57 @Override
58 public void registerFamilies(Kryo kryo) {
59 kryo.register(ImmutableList.of(1).getClass(), this);
60 kryo.register(ImmutableList.of(1, 2).getClass(), this);
61 // TODO register required ImmutableList variants
62 }
63
64}