blob: 8c6ee38602e7db69ce6944e18cc5ae7af6ad60ab [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 HIGUCHI47c40882014-10-10 18:44:37 -070018import com.esotericsoftware.kryo.Kryo;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080019import com.esotericsoftware.kryo.Serializer;
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070020import com.esotericsoftware.kryo.io.Input;
21import com.esotericsoftware.kryo.io.Output;
22import com.google.common.collect.ImmutableList;
23import com.google.common.collect.ImmutableList.Builder;
24
25/**
26 * Creates {@link ImmutableList} serializer instance.
27 */
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080028public class ImmutableListSerializer extends Serializer<ImmutableList<?>> {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070029
30 /**
31 * Creates {@link ImmutableList} serializer instance.
32 */
33 public ImmutableListSerializer() {
34 // non-null, immutable
35 super(false, true);
36 }
37 @Override
38 public void write(Kryo kryo, Output output, ImmutableList<?> object) {
39 output.writeInt(object.size());
40 for (Object e : object) {
41 kryo.writeClassAndObject(output, e);
42 }
43 }
44
45 @Override
46 public ImmutableList<?> read(Kryo kryo, Input input,
47 Class<ImmutableList<?>> type) {
48 final int size = input.readInt();
49 Builder<Object> builder = ImmutableList.builder();
50 for (int i = 0; i < size; ++i) {
51 builder.add(kryo.readClassAndObject(input));
52 }
53 return builder.build();
54 }
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070055}