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 | */ |
Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame] | 16 | package org.onosproject.store.serializers; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 17 | |
| 18 | import java.util.ArrayList; |
| 19 | import java.util.List; |
| 20 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 21 | import com.esotericsoftware.kryo.Kryo; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 22 | import com.esotericsoftware.kryo.Serializer; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 23 | import com.esotericsoftware.kryo.io.Input; |
| 24 | import com.esotericsoftware.kryo.io.Output; |
| 25 | import com.esotericsoftware.kryo.serializers.CollectionSerializer; |
| 26 | import com.google.common.collect.ImmutableSet; |
| 27 | |
| 28 | /** |
| 29 | * Kryo Serializer for {@link ImmutableSet}. |
| 30 | */ |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 31 | public class ImmutableSetSerializer extends Serializer<ImmutableSet<?>> { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 32 | |
| 33 | private final CollectionSerializer serializer = new CollectionSerializer(); |
| 34 | |
Yuta HIGUCHI | 03fec1f | 2014-10-03 09:13:50 -0700 | [diff] [blame] | 35 | /** |
| 36 | * Creates {@link ImmutableSet} serializer instance. |
| 37 | */ |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 38 | public ImmutableSetSerializer() { |
| 39 | // non-null, immutable |
| 40 | super(false, true); |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 41 | serializer.setElementsCanBeNull(false); |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 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 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 55 | } |