blob: e5bf85a92d970d3f2086fb5a721aeaa96039a4c2 [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
Sho SHIMIZU0e77c502016-06-13 16:08:37 -070079 Class<?> encodedClass() {
80 Range<Integer> firstRange = rangeSet.asRanges().iterator().next();
81 return codec.decode(firstRange.lowerEndpoint()).getClass();
82 }
83
Sho SHIMIZU2795d632016-05-25 14:10:13 -070084 @SuppressWarnings("unchecked")
85 boolean contains(DiscreteResource resource) {
Sho SHIMIZU13bf46e2016-06-01 12:23:15 -070086 return resource.valueAs(Object.class)
87 .map(x -> codec.encode(x))
88 .map(rangeSet::contains)
89 .orElse(false);
Sho SHIMIZU2795d632016-05-25 14:10:13 -070090 }
91
Sho SHIMIZUb42d3832016-06-09 14:21:17 -070092 EncodedDiscreteResources difference(EncodedDiscreteResources other) {
93 checkArgument(this.codec.getClass() == other.codec.getClass());
94
95 RangeSet<Integer> newRangeSet = TreeRangeSet.create(this.rangeSet);
96 newRangeSet.removeAll(other.rangeSet);
97
98 return new EncodedDiscreteResources(newRangeSet, this.codec);
99 }
100
Sho SHIMIZUb15b2ea2016-06-09 15:35:59 -0700101 EncodedDiscreteResources add(EncodedDiscreteResources other) {
102 checkArgument(this.codec.getClass() == other.codec.getClass());
103
104 RangeSet<Integer> newRangeSet = TreeRangeSet.create(this.rangeSet);
105 newRangeSet.addAll(other.rangeSet);
106
107 return new EncodedDiscreteResources(newRangeSet, this.codec);
108 }
109
Sho SHIMIZU2795d632016-05-25 14:10:13 -0700110 boolean isEmpty() {
Sho SHIMIZU758cf632016-05-25 15:25:59 -0700111 return rangeSet.isEmpty();
Sho SHIMIZU2795d632016-05-25 14:10:13 -0700112 }
Sho SHIMIZU68e8bfa2016-06-08 12:01:31 -0700113
114 @Override
115 public int hashCode() {
116 return Objects.hash(rangeSet, codec);
117 }
118
119 @Override
120 public boolean equals(Object obj) {
121 if (this == obj) {
122 return true;
123 }
124 if (obj == null || getClass() != obj.getClass()) {
125 return false;
126 }
127 final EncodedDiscreteResources other = (EncodedDiscreteResources) obj;
128 return Objects.equals(this.rangeSet, other.rangeSet)
129 && Objects.equals(this.codec, other.codec);
130 }
131
132 @Override
133 public String toString() {
134 return MoreObjects.toStringHelper(this)
135 .add("rangeSet", rangeSet)
136 .add("codec", codec)
137 .toString();
138 }
Sho SHIMIZU2795d632016-05-25 14:10:13 -0700139}