Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame^] | 1 | /* |
| 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 HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.serializers; |
| 17 | |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 18 | import org.onlab.util.KryoNamespace.FamilySerializer; |
Yuta HIGUCHI | 47c4088 | 2014-10-10 18:44:37 -0700 | [diff] [blame] | 19 | |
| 20 | import com.esotericsoftware.kryo.Kryo; |
| 21 | import com.esotericsoftware.kryo.io.Input; |
| 22 | import com.esotericsoftware.kryo.io.Output; |
| 23 | import com.google.common.collect.ImmutableList; |
| 24 | import com.google.common.collect.ImmutableList.Builder; |
| 25 | |
| 26 | /** |
| 27 | * Creates {@link ImmutableList} serializer instance. |
| 28 | */ |
| 29 | public 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 | } |