Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Thomas Vachuska | 58de416 | 2015-09-10 16:15:33 -0700 | [diff] [blame] | 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 | */ |
Ayaka Koshibe | 08e457a | 2015-06-25 17:11:54 -0700 | [diff] [blame] | 16 | package org.onosproject.store.serializers; |
| 17 | |
| 18 | import org.onosproject.net.DefaultAnnotations; |
| 19 | |
| 20 | import com.esotericsoftware.kryo.Kryo; |
| 21 | import com.esotericsoftware.kryo.Serializer; |
| 22 | import com.esotericsoftware.kryo.io.Input; |
| 23 | import com.esotericsoftware.kryo.io.Output; |
Yuta HIGUCHI | d08e2e9 | 2016-07-10 00:15:10 -0700 | [diff] [blame] | 24 | import com.esotericsoftware.kryo.serializers.DefaultSerializers; |
| 25 | import com.esotericsoftware.kryo.serializers.DefaultSerializers.StringSerializer; |
| 26 | import com.esotericsoftware.kryo.serializers.MapSerializer; |
Ayaka Koshibe | 08e457a | 2015-06-25 17:11:54 -0700 | [diff] [blame] | 27 | |
| 28 | import java.util.HashMap; |
Yuta HIGUCHI | d08e2e9 | 2016-07-10 00:15:10 -0700 | [diff] [blame] | 29 | import java.util.Map; |
Ayaka Koshibe | 08e457a | 2015-06-25 17:11:54 -0700 | [diff] [blame] | 30 | |
| 31 | public class AnnotationsSerializer extends Serializer<DefaultAnnotations> { |
| 32 | |
Yuta HIGUCHI | d08e2e9 | 2016-07-10 00:15:10 -0700 | [diff] [blame] | 33 | 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 Koshibe | 08e457a | 2015-06-25 17:11:54 -0700 | [diff] [blame] | 53 | public AnnotationsSerializer() { |
| 54 | super(false, true); |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public void write(Kryo kryo, Output output, DefaultAnnotations object) { |
Yuta HIGUCHI | d08e2e9 | 2016-07-10 00:15:10 -0700 | [diff] [blame] | 59 | kryo.writeObject(output, object.asMap(), MAP_SERIALIZER); |
Ayaka Koshibe | 08e457a | 2015-06-25 17:11:54 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public DefaultAnnotations read(Kryo kryo, Input input, Class<DefaultAnnotations> type) { |
| 64 | DefaultAnnotations.Builder b = DefaultAnnotations.builder(); |
Yuta HIGUCHI | d08e2e9 | 2016-07-10 00:15:10 -0700 | [diff] [blame] | 65 | Map<String, String> map = kryo.readObject(input, HashMap.class, MAP_SERIALIZER); |
Ayaka Koshibe | 08e457a | 2015-06-25 17:11:54 -0700 | [diff] [blame] | 66 | map.forEach((k, v) -> b.set(k, v)); |
| 67 | |
| 68 | return b.build(); |
| 69 | } |
| 70 | |
| 71 | } |