Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package org.onosproject.store.resource.impl; |
| 17 | |
| 18 | import com.esotericsoftware.kryo.Kryo; |
| 19 | import com.esotericsoftware.kryo.Serializer; |
| 20 | import com.esotericsoftware.kryo.io.Input; |
| 21 | import com.esotericsoftware.kryo.io.Output; |
| 22 | import org.onosproject.net.resource.DiscreteResource; |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 23 | |
| 24 | import java.util.LinkedHashSet; |
| 25 | import java.util.Set; |
| 26 | import java.util.stream.Collectors; |
| 27 | |
| 28 | /** |
| 29 | * Kryo serializer for {@link EncodableDiscreteResources}. |
| 30 | */ |
| 31 | class EncodableDiscreteResourcesSerializer extends Serializer<EncodableDiscreteResources> { |
| 32 | @Override |
| 33 | public void write(Kryo kryo, Output output, EncodableDiscreteResources object) { |
| 34 | kryo.writeObject(output, object.parent()); |
| 35 | kryo.writeObject(output, new LinkedHashSet<>(object.rawValues().values())); |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public EncodableDiscreteResources read(Kryo kryo, Input input, Class<EncodableDiscreteResources> cls) { |
| 40 | DiscreteResource parent = kryo.readObject(input, DiscreteResource.class); |
| 41 | @SuppressWarnings("unchecked") |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 42 | Set<EncodedDiscreteResources> resources = kryo.readObject(input, LinkedHashSet.class); |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 43 | |
| 44 | return EncodableDiscreteResources.of(parent, |
| 45 | resources.stream() |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 46 | .flatMap(x -> x.values(parent.id()).stream()) |
Sho SHIMIZU | e4f76ed | 2016-05-19 11:40:31 -0700 | [diff] [blame] | 47 | .collect(Collectors.toCollection(LinkedHashSet::new))); |
| 48 | } |
| 49 | } |