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 | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 16 | package org.onlab.onos.store.serializers; |
| 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
| 20 | |
Yuta HIGUCHI | 8d143d2 | 2014-10-19 23:15:09 -0700 | [diff] [blame] | 21 | import org.onlab.util.KryoNamespace.FamilySerializer; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 22 | |
| 23 | import com.esotericsoftware.kryo.Kryo; |
| 24 | import com.esotericsoftware.kryo.io.Input; |
| 25 | import com.esotericsoftware.kryo.io.Output; |
| 26 | import com.esotericsoftware.kryo.serializers.CollectionSerializer; |
| 27 | import com.google.common.collect.ImmutableSet; |
| 28 | |
| 29 | /** |
| 30 | * Kryo Serializer for {@link ImmutableSet}. |
| 31 | */ |
| 32 | public class ImmutableSetSerializer extends FamilySerializer<ImmutableSet<?>> { |
| 33 | |
| 34 | private final CollectionSerializer serializer = new CollectionSerializer(); |
| 35 | |
Yuta HIGUCHI | 03fec1f | 2014-10-03 09:13:50 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Creates {@link ImmutableSet} serializer instance. |
| 38 | */ |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 39 | public ImmutableSetSerializer() { |
| 40 | // non-null, immutable |
| 41 | super(false, true); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void write(Kryo kryo, Output output, ImmutableSet<?> object) { |
| 46 | kryo.writeObject(output, object.asList(), serializer); |
| 47 | } |
| 48 | |
| 49 | @Override |
| 50 | public ImmutableSet<?> read(Kryo kryo, Input input, |
| 51 | Class<ImmutableSet<?>> type) { |
| 52 | List<?> elms = kryo.readObject(input, ArrayList.class, serializer); |
| 53 | return ImmutableSet.copyOf(elms); |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | public void registerFamilies(Kryo kryo) { |
| 58 | kryo.register(ImmutableSet.of().getClass(), this); |
| 59 | kryo.register(ImmutableSet.of(1).getClass(), this); |
| 60 | kryo.register(ImmutableSet.of(1, 2).getClass(), this); |
| 61 | // TODO register required ImmutableSet variants |
| 62 | } |
| 63 | } |