blob: 5ccb6d2a7ec843fc8d321889bdcc97effda6efc6 [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 SHIMIZU68e8bfa2016-06-08 12:01:31 -070018import com.google.common.base.MoreObjects;
Sho SHIMIZU758cf632016-05-25 15:25:59 -070019import com.google.common.collect.DiscreteDomain;
20import com.google.common.collect.Range;
21import com.google.common.collect.RangeSet;
22import com.google.common.collect.TreeRangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070023import org.onlab.util.Tools;
24import org.onosproject.net.resource.DiscreteResource;
25import org.onosproject.net.resource.DiscreteResourceCodec;
26import org.onosproject.net.resource.DiscreteResourceId;
27import org.onosproject.net.resource.Resources;
28
29import java.util.LinkedHashSet;
Sho SHIMIZU68e8bfa2016-06-08 12:01:31 -070030import java.util.Objects;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070031import java.util.Set;
32import java.util.stream.Collectors;
Sho SHIMIZU758cf632016-05-25 15:25:59 -070033import java.util.stream.IntStream;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070034
Sho SHIMIZUb42d3832016-06-09 14:21:17 -070035import static com.google.common.base.Preconditions.checkArgument;
36
Sho SHIMIZU2795d632016-05-25 14:10:13 -070037/**
38 * Represents discrete resources encoded by a codec.
39 */
40final class EncodedDiscreteResources {
Sho SHIMIZU758cf632016-05-25 15:25:59 -070041 private final RangeSet<Integer> rangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070042 private final DiscreteResourceCodec codec;
43
Sho SHIMIZU758cf632016-05-25 15:25:59 -070044 EncodedDiscreteResources(RangeSet<Integer> rangeSet, DiscreteResourceCodec codec) {
45 this.rangeSet = rangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070046 this.codec = codec;
47 }
48
49 static EncodedDiscreteResources of(Set<DiscreteResource> resources, DiscreteResourceCodec codec) {
Sho SHIMIZU758cf632016-05-25 15:25:59 -070050 RangeSet<Integer> rangeSet = TreeRangeSet.create();
51 resources.stream()
Sho SHIMIZU2795d632016-05-25 14:10:13 -070052 .map(x -> x.valueAs(Object.class))
53 .flatMap(Tools::stream)
54 .map(x -> codec.encode(x))
Sho SHIMIZU758cf632016-05-25 15:25:59 -070055 .map(Range::singleton)
56 .map(x -> x.canonical(DiscreteDomain.integers()))
57 .forEach(rangeSet::add);
Sho SHIMIZU2795d632016-05-25 14:10:13 -070058
Sho SHIMIZU758cf632016-05-25 15:25:59 -070059 return new EncodedDiscreteResources(rangeSet, codec);
Sho SHIMIZU2795d632016-05-25 14:10:13 -070060 }
61
Sho SHIMIZU758cf632016-05-25 15:25:59 -070062 RangeSet<Integer> rangeSet() {
63 return rangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070064 }
65
66 DiscreteResourceCodec codec() {
67 return codec;
68 }
69
Sho SHIMIZU758cf632016-05-25 15:25:59 -070070 Set<DiscreteResource> values(DiscreteResourceId parent) {
71 return rangeSet.asRanges().stream()
72 .flatMapToInt(x1 -> IntStream.range(x1.lowerEndpoint(), x1.upperEndpoint()))
73 .boxed()
Sho SHIMIZU2795d632016-05-25 14:10:13 -070074 .map(x -> codec.decode(x))
75 .map(x -> Resources.discrete(parent, x).resource())
76 .collect(Collectors.toCollection(LinkedHashSet::new));
77 }
78
79 @SuppressWarnings("unchecked")
80 boolean contains(DiscreteResource resource) {
Sho SHIMIZU13bf46e2016-06-01 12:23:15 -070081 return resource.valueAs(Object.class)
82 .map(x -> codec.encode(x))
83 .map(rangeSet::contains)
84 .orElse(false);
Sho SHIMIZU2795d632016-05-25 14:10:13 -070085 }
86
Sho SHIMIZUb42d3832016-06-09 14:21:17 -070087 EncodedDiscreteResources difference(EncodedDiscreteResources other) {
88 checkArgument(this.codec.getClass() == other.codec.getClass());
89
90 RangeSet<Integer> newRangeSet = TreeRangeSet.create(this.rangeSet);
91 newRangeSet.removeAll(other.rangeSet);
92
93 return new EncodedDiscreteResources(newRangeSet, this.codec);
94 }
95
Sho SHIMIZU2795d632016-05-25 14:10:13 -070096 boolean isEmpty() {
Sho SHIMIZU758cf632016-05-25 15:25:59 -070097 return rangeSet.isEmpty();
Sho SHIMIZU2795d632016-05-25 14:10:13 -070098 }
Sho SHIMIZU68e8bfa2016-06-08 12:01:31 -070099
100 @Override
101 public int hashCode() {
102 return Objects.hash(rangeSet, codec);
103 }
104
105 @Override
106 public boolean equals(Object obj) {
107 if (this == obj) {
108 return true;
109 }
110 if (obj == null || getClass() != obj.getClass()) {
111 return false;
112 }
113 final EncodedDiscreteResources other = (EncodedDiscreteResources) obj;
114 return Objects.equals(this.rangeSet, other.rangeSet)
115 && Objects.equals(this.codec, other.codec);
116 }
117
118 @Override
119 public String toString() {
120 return MoreObjects.toStringHelper(this)
121 .add("rangeSet", rangeSet)
122 .add("codec", codec)
123 .toString();
124 }
Sho SHIMIZU2795d632016-05-25 14:10:13 -0700125}