blob: 83bc342010c1ec990f0b7fc8f0fc4f51b3fe3ee8 [file] [log] [blame]
Sho SHIMIZUe4f76ed2016-05-19 11:40:31 -07001/*
2 * Copyright 2016-present 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 */
16package org.onosproject.store.resource.impl;
17
18import com.esotericsoftware.kryo.Kryo;
19import com.esotericsoftware.kryo.Serializer;
20import com.esotericsoftware.kryo.io.Input;
21import com.esotericsoftware.kryo.io.Output;
22import org.onosproject.net.resource.DiscreteResource;
Sho SHIMIZUe4f76ed2016-05-19 11:40:31 -070023
Sho SHIMIZU0e77c502016-06-13 16:08:37 -070024import java.util.LinkedHashMap;
Sho SHIMIZUe4f76ed2016-05-19 11:40:31 -070025import java.util.LinkedHashSet;
26import java.util.Set;
Sho SHIMIZU0e77c502016-06-13 16:08:37 -070027import java.util.function.Function;
Sho SHIMIZUe4f76ed2016-05-19 11:40:31 -070028import java.util.stream.Collectors;
29
30/**
31 * Kryo serializer for {@link EncodableDiscreteResources}.
32 */
33class EncodableDiscreteResourcesSerializer extends Serializer<EncodableDiscreteResources> {
34 @Override
35 public void write(Kryo kryo, Output output, EncodableDiscreteResources object) {
36 kryo.writeObject(output, object.parent());
37 kryo.writeObject(output, new LinkedHashSet<>(object.rawValues().values()));
38 }
39
40 @Override
41 public EncodableDiscreteResources read(Kryo kryo, Input input, Class<EncodableDiscreteResources> cls) {
42 DiscreteResource parent = kryo.readObject(input, DiscreteResource.class);
43 @SuppressWarnings("unchecked")
Sho SHIMIZU2795d632016-05-25 14:10:13 -070044 Set<EncodedDiscreteResources> resources = kryo.readObject(input, LinkedHashSet.class);
Sho SHIMIZUe4f76ed2016-05-19 11:40:31 -070045
Sho SHIMIZU0e77c502016-06-13 16:08:37 -070046 return new EncodableDiscreteResources(parent, resources.stream()
47 .collect(Collectors.toMap(
48 EncodedDiscreteResources::encodedClass,
49 Function.identity(),
50 (v1, v2) -> v1,
51 LinkedHashMap::new)));
Sho SHIMIZUe4f76ed2016-05-19 11:40:31 -070052 }
53}