Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2014-present Open Networking Laboratory |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -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 | */ |
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.Collections; |
| 19 | import java.util.HashMap; |
| 20 | import java.util.Map; |
| 21 | |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 22 | import com.esotericsoftware.kryo.Kryo; |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 23 | import com.esotericsoftware.kryo.Serializer; |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 24 | import com.esotericsoftware.kryo.io.Input; |
| 25 | import com.esotericsoftware.kryo.io.Output; |
| 26 | import com.esotericsoftware.kryo.serializers.MapSerializer; |
| 27 | import com.google.common.collect.ImmutableMap; |
| 28 | |
| 29 | /** |
| 30 | * Kryo Serializer for {@link ImmutableMap}. |
| 31 | */ |
Yuta HIGUCHI | 91768e3 | 2014-11-22 05:06:35 -0800 | [diff] [blame] | 32 | public class ImmutableMapSerializer extends Serializer<ImmutableMap<?, ?>> { |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 33 | |
| 34 | private final MapSerializer mapSerializer = new MapSerializer(); |
| 35 | |
Yuta HIGUCHI | 03fec1f | 2014-10-03 09:13:50 -0700 | [diff] [blame] | 36 | /** |
| 37 | * Creates {@link ImmutableMap} serializer instance. |
| 38 | */ |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 39 | public ImmutableMapSerializer() { |
| 40 | // non-null, immutable |
| 41 | super(false, true); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | public void write(Kryo kryo, Output output, ImmutableMap<?, ?> object) { |
| 46 | // wrapping with unmodifiableMap proxy |
| 47 | // to avoid Kryo from writing only the reference marker of this instance, |
| 48 | // which will be embedded right before this method call. |
| 49 | kryo.writeObject(output, Collections.unmodifiableMap(object), mapSerializer); |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public ImmutableMap<?, ?> read(Kryo kryo, Input input, |
| 54 | Class<ImmutableMap<?, ?>> type) { |
| 55 | Map<?, ?> map = kryo.readObject(input, HashMap.class, mapSerializer); |
| 56 | return ImmutableMap.copyOf(map); |
| 57 | } |
Yuta HIGUCHI | 533ec32 | 2014-09-30 13:29:52 -0700 | [diff] [blame] | 58 | } |