Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -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 | |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 18 | import com.google.common.collect.DiscreteDomain; |
| 19 | import com.google.common.collect.Range; |
| 20 | import com.google.common.collect.RangeSet; |
| 21 | import com.google.common.collect.TreeRangeSet; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 22 | import org.onlab.util.Tools; |
| 23 | import org.onosproject.net.resource.DiscreteResource; |
| 24 | import org.onosproject.net.resource.DiscreteResourceCodec; |
| 25 | import org.onosproject.net.resource.DiscreteResourceId; |
| 26 | import org.onosproject.net.resource.Resources; |
| 27 | |
| 28 | import java.util.LinkedHashSet; |
| 29 | import java.util.Set; |
| 30 | import java.util.stream.Collectors; |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 31 | import java.util.stream.IntStream; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Represents discrete resources encoded by a codec. |
| 35 | */ |
| 36 | final class EncodedDiscreteResources { |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 37 | private final RangeSet<Integer> rangeSet; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 38 | private final DiscreteResourceCodec codec; |
| 39 | |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 40 | EncodedDiscreteResources(RangeSet<Integer> rangeSet, DiscreteResourceCodec codec) { |
| 41 | this.rangeSet = rangeSet; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 42 | this.codec = codec; |
| 43 | } |
| 44 | |
| 45 | static EncodedDiscreteResources of(Set<DiscreteResource> resources, DiscreteResourceCodec codec) { |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 46 | RangeSet<Integer> rangeSet = TreeRangeSet.create(); |
| 47 | resources.stream() |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 48 | .map(x -> x.valueAs(Object.class)) |
| 49 | .flatMap(Tools::stream) |
| 50 | .map(x -> codec.encode(x)) |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 51 | .map(Range::singleton) |
| 52 | .map(x -> x.canonical(DiscreteDomain.integers())) |
| 53 | .forEach(rangeSet::add); |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 54 | |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 55 | return new EncodedDiscreteResources(rangeSet, codec); |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 56 | } |
| 57 | |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 58 | RangeSet<Integer> rangeSet() { |
| 59 | return rangeSet; |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | DiscreteResourceCodec codec() { |
| 63 | return codec; |
| 64 | } |
| 65 | |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 66 | Set<DiscreteResource> values(DiscreteResourceId parent) { |
| 67 | return rangeSet.asRanges().stream() |
| 68 | .flatMapToInt(x1 -> IntStream.range(x1.lowerEndpoint(), x1.upperEndpoint())) |
| 69 | .boxed() |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 70 | .map(x -> codec.decode(x)) |
| 71 | .map(x -> Resources.discrete(parent, x).resource()) |
| 72 | .collect(Collectors.toCollection(LinkedHashSet::new)); |
| 73 | } |
| 74 | |
| 75 | @SuppressWarnings("unchecked") |
| 76 | boolean contains(DiscreteResource resource) { |
Sho SHIMIZU | 13bf46e | 2016-06-01 12:23:15 -0700 | [diff] [blame^] | 77 | return resource.valueAs(Object.class) |
| 78 | .map(x -> codec.encode(x)) |
| 79 | .map(rangeSet::contains) |
| 80 | .orElse(false); |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | boolean isEmpty() { |
Sho SHIMIZU | 758cf63 | 2016-05-25 15:25:59 -0700 | [diff] [blame] | 84 | return rangeSet.isEmpty(); |
Sho SHIMIZU | 2795d63 | 2016-05-25 14:10:13 -0700 | [diff] [blame] | 85 | } |
| 86 | } |