Sho SHIMIZU
Committed by Gerrit Code Review

Introduce abstract interface for set of discrete resources

Change-Id: Iddbed1a2dd0c6ce3d7288371a47a7b0aeba46803
......@@ -71,7 +71,7 @@ public class ConsistentResourceStore extends AbstractStore<ResourceEvent, Resour
static final Serializer SERIALIZER = Serializer.using(
Arrays.asList(KryoNamespaces.API),
DiscreteResources.class,
NonEncodableDiscreteResources.class,
ContinuousResourceAllocation.class);
// TODO: We should provide centralized values for this
......
......@@ -15,99 +15,31 @@
*/
package org.onosproject.store.resource.impl;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Sets;
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.Resources;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
final class DiscreteResources {
private final Set<DiscreteResource> values;
interface DiscreteResources {
static DiscreteResources empty() {
return new DiscreteResources();
}
private DiscreteResources() {
this.values = new LinkedHashSet<>();
}
DiscreteResources(List<DiscreteResource> values) {
this.values = new LinkedHashSet<>(values);
return NonEncodableDiscreteResources.empty();
}
private DiscreteResources(Set<DiscreteResource> values) {
this.values = values;
}
Optional<DiscreteResource> lookup(DiscreteResourceId id);
Optional<DiscreteResource> lookup(DiscreteResourceId id) {
DiscreteResource resource = Resources.discrete(id).resource();
if (values.contains(resource)) {
return Optional.of(resource);
} else {
return Optional.empty();
}
}
DiscreteResources difference(DiscreteResources other);
DiscreteResources difference(DiscreteResources other) {
return new DiscreteResources(Sets.difference(this.values, other.values));
}
boolean isEmpty() {
return values.isEmpty();
}
boolean isEmpty();
boolean containsAny(List<DiscreteResource> other) {
return other.stream().anyMatch(values::contains);
}
boolean containsAny(List<DiscreteResource> other);
// returns a new instance, not mutate the current instance
DiscreteResources add(DiscreteResources other) {
Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
newValues.addAll(other.values);
return new DiscreteResources(newValues);
}
DiscreteResources add(DiscreteResources other);
// returns a new instance, not mutate the current instance
DiscreteResources remove(List<DiscreteResource> removed) {
Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
newValues.removeAll(removed);
return new DiscreteResources(newValues);
}
Set<DiscreteResource> values() {
// breaks immutability, but intentionally returns the field
// because this class is transient
return values;
}
DiscreteResources remove(List<DiscreteResource> removed);
@Override
public int hashCode() {
return Objects.hash(values);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final DiscreteResources other = (DiscreteResources) obj;
return Objects.equals(this.values, other.values);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("values", values)
.toString();
}
Set<DiscreteResource> values();
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.store.resource.impl;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Sets;
import org.onosproject.net.resource.DiscreteResource;
import org.onosproject.net.resource.DiscreteResourceId;
import org.onosproject.net.resource.Resources;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
final class NonEncodableDiscreteResources implements DiscreteResources {
private final Set<DiscreteResource> values;
static NonEncodableDiscreteResources empty() {
return new NonEncodableDiscreteResources();
}
private NonEncodableDiscreteResources() {
this.values = new LinkedHashSet<>();
}
NonEncodableDiscreteResources(List<DiscreteResource> values) {
this.values = new LinkedHashSet<>(values);
}
private NonEncodableDiscreteResources(Set<DiscreteResource> values) {
this.values = values;
}
@Override
public Optional<DiscreteResource> lookup(DiscreteResourceId id) {
DiscreteResource resource = Resources.discrete(id).resource();
if (values.contains(resource)) {
return Optional.of(resource);
} else {
return Optional.empty();
}
}
@Override
public DiscreteResources difference(DiscreteResources other) {
return new NonEncodableDiscreteResources(Sets.difference(this.values, other.values()));
}
@Override
public boolean isEmpty() {
return values.isEmpty();
}
@Override
public boolean containsAny(List<DiscreteResource> other) {
return other.stream().anyMatch(values::contains);
}
// returns a new instance, not mutate the current instance
@Override
public DiscreteResources add(DiscreteResources other) {
Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
newValues.addAll(other.values());
return new NonEncodableDiscreteResources(newValues);
}
// returns a new instance, not mutate the current instance
@Override
public DiscreteResources remove(List<DiscreteResource> removed) {
Set<DiscreteResource> newValues = new LinkedHashSet<>(this.values);
newValues.removeAll(removed);
return new NonEncodableDiscreteResources(newValues);
}
@Override
public Set<DiscreteResource> values() {
// breaks immutability, but intentionally returns the field
// because this class is transient
return values;
}
@Override
public int hashCode() {
return Objects.hash(values);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final NonEncodableDiscreteResources other = (NonEncodableDiscreteResources) obj;
return Objects.equals(this.values, other.values);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("values", values)
.toString();
}
}
......@@ -59,7 +59,7 @@ class TransactionalDiscreteResourceSubStore {
return true;
}
DiscreteResources requested = new DiscreteResources(values);
DiscreteResources requested = new NonEncodableDiscreteResources(values);
DiscreteResources oldValues = childMap.putIfAbsent(key, requested);
if (oldValues == null) {
return true;
......