blob: 4681f5ae03249e8ec1b3dbefd79af80ead218553 [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;
23import org.onosproject.net.resource.DiscreteResourceSet;
24
25import java.util.LinkedHashSet;
26import java.util.Set;
27import java.util.stream.Collectors;
28
29/**
30 * Kryo serializer for {@link EncodableDiscreteResources}.
31 */
32class EncodableDiscreteResourcesSerializer extends Serializer<EncodableDiscreteResources> {
33 @Override
34 public void write(Kryo kryo, Output output, EncodableDiscreteResources object) {
35 kryo.writeObject(output, object.parent());
36 kryo.writeObject(output, new LinkedHashSet<>(object.rawValues().values()));
37 }
38
39 @Override
40 public EncodableDiscreteResources read(Kryo kryo, Input input, Class<EncodableDiscreteResources> cls) {
41 DiscreteResource parent = kryo.readObject(input, DiscreteResource.class);
42 @SuppressWarnings("unchecked")
43 Set<DiscreteResourceSet> resources = kryo.readObject(input, LinkedHashSet.class);
44
45 return EncodableDiscreteResources.of(parent,
46 resources.stream()
47 .flatMap(x -> x.values().stream())
48 .collect(Collectors.toCollection(LinkedHashSet::new)));
49 }
50}