blob: 95aa4ad6694d0a0c605208a01a669cf92ab2a700 [file] [log] [blame]
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2014-present Open Networking Laboratory
Thomas Vachuska4f1a60c2014-10-28 13:39:07 -07003 *
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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.serializers;
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070017
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;
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070023
24/**
25 * Creates {@link ImmutableList} serializer instance.
26 */
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080027public class ImmutableListSerializer extends Serializer<ImmutableList<?>> {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070028
29 /**
30 * Creates {@link ImmutableList} serializer instance.
31 */
32 public ImmutableListSerializer() {
33 // non-null, immutable
34 super(false, true);
35 }
36 @Override
37 public void write(Kryo kryo, Output output, ImmutableList<?> object) {
38 output.writeInt(object.size());
39 for (Object e : object) {
40 kryo.writeClassAndObject(output, e);
41 }
42 }
43
44 @Override
45 public ImmutableList<?> read(Kryo kryo, Input input,
Yuta HIGUCHId08e2e92016-07-10 00:15:10 -070046 Class<ImmutableList<?>> type) {
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070047 final int size = input.readInt();
Yuta HIGUCHId08e2e92016-07-10 00:15:10 -070048 switch (size) {
49 case 0:
50 return ImmutableList.of();
51 case 1:
52 return ImmutableList.of(kryo.readClassAndObject(input));
53 default:
54 Object[] elms = new Object[size];
55 for (int i = 0; i < size; ++i) {
56 elms[i] = kryo.readClassAndObject(input);
57 }
58 return ImmutableList.copyOf(elms);
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070059 }
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070060 }
Yuta HIGUCHI47c40882014-10-10 18:44:37 -070061}