blob: 59c39d85957bd07b2a31465e5e8a6ee5bb6643d4 [file] [log] [blame]
Sho SHIMIZU2795d632016-05-25 14:10:13 -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
Sho SHIMIZU758cf632016-05-25 15:25:59 -070018import com.google.common.collect.DiscreteDomain;
19import com.google.common.collect.Range;
20import com.google.common.collect.RangeSet;
21import com.google.common.collect.TreeRangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070022import org.onlab.util.Tools;
23import org.onosproject.net.resource.DiscreteResource;
24import org.onosproject.net.resource.DiscreteResourceCodec;
25import org.onosproject.net.resource.DiscreteResourceId;
26import org.onosproject.net.resource.Resources;
27
28import java.util.LinkedHashSet;
29import java.util.Set;
30import java.util.stream.Collectors;
Sho SHIMIZU758cf632016-05-25 15:25:59 -070031import java.util.stream.IntStream;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070032
33/**
34 * Represents discrete resources encoded by a codec.
35 */
36final class EncodedDiscreteResources {
Sho SHIMIZU758cf632016-05-25 15:25:59 -070037 private final RangeSet<Integer> rangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070038 private final DiscreteResourceCodec codec;
39
Sho SHIMIZU758cf632016-05-25 15:25:59 -070040 EncodedDiscreteResources(RangeSet<Integer> rangeSet, DiscreteResourceCodec codec) {
41 this.rangeSet = rangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070042 this.codec = codec;
43 }
44
45 static EncodedDiscreteResources of(Set<DiscreteResource> resources, DiscreteResourceCodec codec) {
Sho SHIMIZU758cf632016-05-25 15:25:59 -070046 RangeSet<Integer> rangeSet = TreeRangeSet.create();
47 resources.stream()
Sho SHIMIZU2795d632016-05-25 14:10:13 -070048 .map(x -> x.valueAs(Object.class))
49 .flatMap(Tools::stream)
50 .map(x -> codec.encode(x))
Sho SHIMIZU758cf632016-05-25 15:25:59 -070051 .map(Range::singleton)
52 .map(x -> x.canonical(DiscreteDomain.integers()))
53 .forEach(rangeSet::add);
Sho SHIMIZU2795d632016-05-25 14:10:13 -070054
Sho SHIMIZU758cf632016-05-25 15:25:59 -070055 return new EncodedDiscreteResources(rangeSet, codec);
Sho SHIMIZU2795d632016-05-25 14:10:13 -070056 }
57
Sho SHIMIZU758cf632016-05-25 15:25:59 -070058 RangeSet<Integer> rangeSet() {
59 return rangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070060 }
61
62 DiscreteResourceCodec codec() {
63 return codec;
64 }
65
Sho SHIMIZU758cf632016-05-25 15:25:59 -070066 Set<DiscreteResource> values(DiscreteResourceId parent) {
67 return rangeSet.asRanges().stream()
68 .flatMapToInt(x1 -> IntStream.range(x1.lowerEndpoint(), x1.upperEndpoint()))
69 .boxed()
Sho SHIMIZU2795d632016-05-25 14:10:13 -070070 .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 SHIMIZU13bf46e2016-06-01 12:23:15 -070077 return resource.valueAs(Object.class)
78 .map(x -> codec.encode(x))
79 .map(rangeSet::contains)
80 .orElse(false);
Sho SHIMIZU2795d632016-05-25 14:10:13 -070081 }
82
83 boolean isEmpty() {
Sho SHIMIZU758cf632016-05-25 15:25:59 -070084 return rangeSet.isEmpty();
Sho SHIMIZU2795d632016-05-25 14:10:13 -070085 }
86}