blob: 3082a28fc9c89d9fdb4a7203abd7501773b00d2e [file] [log] [blame]
Thomas Vachuska58de4162015-09-10 16:15:33 -07001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Thomas Vachuska58de4162015-09-10 16:15:33 -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 */
Ayaka Koshibe08e457a2015-06-25 17:11:54 -070016package org.onosproject.store.serializers;
17
18import org.onosproject.net.DefaultAnnotations;
19
20import com.esotericsoftware.kryo.Kryo;
21import com.esotericsoftware.kryo.Serializer;
22import com.esotericsoftware.kryo.io.Input;
23import com.esotericsoftware.kryo.io.Output;
Yuta HIGUCHId08e2e92016-07-10 00:15:10 -070024import com.esotericsoftware.kryo.serializers.DefaultSerializers;
25import com.esotericsoftware.kryo.serializers.DefaultSerializers.StringSerializer;
26import com.esotericsoftware.kryo.serializers.MapSerializer;
Ayaka Koshibe08e457a2015-06-25 17:11:54 -070027
28import java.util.HashMap;
Yuta HIGUCHId08e2e92016-07-10 00:15:10 -070029import java.util.Map;
Ayaka Koshibe08e457a2015-06-25 17:11:54 -070030
31public class AnnotationsSerializer extends Serializer<DefaultAnnotations> {
32
Yuta HIGUCHId08e2e92016-07-10 00:15:10 -070033 private static final StringSerializer STR_SERIALIZER
34 = new DefaultSerializers.StringSerializer();
35
36 private static final MapSerializer MAP_SERIALIZER = stringMapSerializer();
37
38 /**
39 * Returns a MapSerializer for {@code Map<String, String>} with
40 * no null key or value.
41 *
42 * @return serializer
43 */
44 private static MapSerializer stringMapSerializer() {
45 MapSerializer serializer = new MapSerializer();
46 serializer.setKeysCanBeNull(false);
47 serializer.setKeyClass(String.class, STR_SERIALIZER);
48 serializer.setValuesCanBeNull(false);
49 serializer.setValueClass(String.class, STR_SERIALIZER);
50 return serializer;
51 }
52
Ayaka Koshibe08e457a2015-06-25 17:11:54 -070053 public AnnotationsSerializer() {
54 super(false, true);
55 }
56
57 @Override
58 public void write(Kryo kryo, Output output, DefaultAnnotations object) {
Yuta HIGUCHId08e2e92016-07-10 00:15:10 -070059 kryo.writeObject(output, object.asMap(), MAP_SERIALIZER);
Ayaka Koshibe08e457a2015-06-25 17:11:54 -070060 }
61
62 @Override
63 public DefaultAnnotations read(Kryo kryo, Input input, Class<DefaultAnnotations> type) {
64 DefaultAnnotations.Builder b = DefaultAnnotations.builder();
Yuta HIGUCHId08e2e92016-07-10 00:15:10 -070065 Map<String, String> map = kryo.readObject(input, HashMap.class, MAP_SERIALIZER);
Ayaka Koshibe08e457a2015-06-25 17:11:54 -070066 map.forEach((k, v) -> b.set(k, v));
67
68 return b.build();
69 }
70
71}