blob: 6e3f6134e7d85807207fb93d6cfa1bdc2e73300b [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
35/**
36 * Represents discrete resources encoded by a codec.
37 */
38final class EncodedDiscreteResources {
Sho SHIMIZU758cf632016-05-25 15:25:59 -070039 private final RangeSet<Integer> rangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070040 private final DiscreteResourceCodec codec;
41
Sho SHIMIZU758cf632016-05-25 15:25:59 -070042 EncodedDiscreteResources(RangeSet<Integer> rangeSet, DiscreteResourceCodec codec) {
43 this.rangeSet = rangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070044 this.codec = codec;
45 }
46
47 static EncodedDiscreteResources of(Set<DiscreteResource> resources, DiscreteResourceCodec codec) {
Sho SHIMIZU758cf632016-05-25 15:25:59 -070048 RangeSet<Integer> rangeSet = TreeRangeSet.create();
49 resources.stream()
Sho SHIMIZU2795d632016-05-25 14:10:13 -070050 .map(x -> x.valueAs(Object.class))
51 .flatMap(Tools::stream)
52 .map(x -> codec.encode(x))
Sho SHIMIZU758cf632016-05-25 15:25:59 -070053 .map(Range::singleton)
54 .map(x -> x.canonical(DiscreteDomain.integers()))
55 .forEach(rangeSet::add);
Sho SHIMIZU2795d632016-05-25 14:10:13 -070056
Sho SHIMIZU758cf632016-05-25 15:25:59 -070057 return new EncodedDiscreteResources(rangeSet, codec);
Sho SHIMIZU2795d632016-05-25 14:10:13 -070058 }
59
Sho SHIMIZU758cf632016-05-25 15:25:59 -070060 RangeSet<Integer> rangeSet() {
61 return rangeSet;
Sho SHIMIZU2795d632016-05-25 14:10:13 -070062 }
63
64 DiscreteResourceCodec codec() {
65 return codec;
66 }
67
Sho SHIMIZU758cf632016-05-25 15:25:59 -070068 Set<DiscreteResource> values(DiscreteResourceId parent) {
69 return rangeSet.asRanges().stream()
70 .flatMapToInt(x1 -> IntStream.range(x1.lowerEndpoint(), x1.upperEndpoint()))
71 .boxed()
Sho SHIMIZU2795d632016-05-25 14:10:13 -070072 .map(x -> codec.decode(x))
73 .map(x -> Resources.discrete(parent, x).resource())
74 .collect(Collectors.toCollection(LinkedHashSet::new));
75 }
76
77 @SuppressWarnings("unchecked")
78 boolean contains(DiscreteResource resource) {
Sho SHIMIZU13bf46e2016-06-01 12:23:15 -070079 return resource.valueAs(Object.class)
80 .map(x -> codec.encode(x))
81 .map(rangeSet::contains)
82 .orElse(false);
Sho SHIMIZU2795d632016-05-25 14:10:13 -070083 }
84
85 boolean isEmpty() {
Sho SHIMIZU758cf632016-05-25 15:25:59 -070086 return rangeSet.isEmpty();
Sho SHIMIZU2795d632016-05-25 14:10:13 -070087 }
Sho SHIMIZU68e8bfa2016-06-08 12:01:31 -070088
89 @Override
90 public int hashCode() {
91 return Objects.hash(rangeSet, codec);
92 }
93
94 @Override
95 public boolean equals(Object obj) {
96 if (this == obj) {
97 return true;
98 }
99 if (obj == null || getClass() != obj.getClass()) {
100 return false;
101 }
102 final EncodedDiscreteResources other = (EncodedDiscreteResources) obj;
103 return Objects.equals(this.rangeSet, other.rangeSet)
104 && Objects.equals(this.codec, other.codec);
105 }
106
107 @Override
108 public String toString() {
109 return MoreObjects.toStringHelper(this)
110 .add("rangeSet", rangeSet)
111 .add("codec", codec)
112 .toString();
113 }
Sho SHIMIZU2795d632016-05-25 14:10:13 -0700114}