blob: cb9b05437dc869ced52cb102ab2902672f345587 [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 */
Brian O'Connorabafb502014-12-02 22:26:20 -080016package org.onosproject.store.serializers;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070017
18import java.util.ArrayList;
19import java.util.List;
20
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070021import com.esotericsoftware.kryo.Kryo;
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080022import com.esotericsoftware.kryo.Serializer;
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070023import com.esotericsoftware.kryo.io.Input;
24import com.esotericsoftware.kryo.io.Output;
25import com.esotericsoftware.kryo.serializers.CollectionSerializer;
26import com.google.common.collect.ImmutableSet;
27
28/**
29* Kryo Serializer for {@link ImmutableSet}.
30*/
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080031public class ImmutableSetSerializer extends Serializer<ImmutableSet<?>> {
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070032
33 private final CollectionSerializer serializer = new CollectionSerializer();
34
Yuta HIGUCHI03fec1f2014-10-03 09:13:50 -070035 /**
36 * Creates {@link ImmutableSet} serializer instance.
37 */
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070038 public ImmutableSetSerializer() {
39 // non-null, immutable
40 super(false, true);
Yuta HIGUCHI91768e32014-11-22 05:06:35 -080041 serializer.setElementsCanBeNull(false);
Yuta HIGUCHI533ec322014-09-30 13:29:52 -070042 }
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 HIGUCHI533ec322014-09-30 13:29:52 -070055}