Sho SHIMIZU
Committed by Gerrit Code Review

Change the return type of ResourceId#parent()

Change-Id: Ice8c46f4dca02cfc24f85a6758a8536872b4c262
......@@ -92,7 +92,7 @@ public final class ContinuousResource implements Resource {
@Override
public Optional<DiscreteResource> parent() {
return Optional.ofNullable(id.parent()).map(DiscreteResource::new);
return id.parent().map(DiscreteResource::new);
}
@Override
......
......@@ -19,6 +19,7 @@ import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableList;
import java.util.Objects;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkNotNull;
......@@ -72,14 +73,14 @@ public final class ContinuousResourceId extends ResourceId {
}
@Override
DiscreteResourceId parent() {
public Optional<DiscreteResourceId> parent() {
if (components.size() == 0) {
return null;
return Optional.empty();
}
if (components.size() == 1) {
return ROOT;
return Optional.of(ROOT);
} else {
return new DiscreteResourceId(components.subList(0, components.size() - 1));
return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1)));
}
}
......
......@@ -90,7 +90,7 @@ public final class DiscreteResource implements Resource {
@Override
public Optional<DiscreteResource> parent() {
return Optional.ofNullable(id.parent()).map(DiscreteResource::new);
return id.parent().map(DiscreteResource::new);
}
@Override
......
......@@ -19,6 +19,7 @@ import com.google.common.annotations.Beta;
import com.google.common.collect.ImmutableList;
import java.util.Objects;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
......@@ -63,14 +64,14 @@ public final class DiscreteResourceId extends ResourceId {
}
@Override
DiscreteResourceId parent() {
public Optional<DiscreteResourceId> parent() {
if (components.size() == 0) {
return null;
return Optional.empty();
}
if (components.size() == 1) {
return ROOT;
return Optional.of(ROOT);
} else {
return new DiscreteResourceId(components.subList(0, components.size() - 1));
return Optional.of(new DiscreteResourceId(components.subList(0, components.size() - 1)));
}
}
......
......@@ -21,6 +21,7 @@ import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import java.util.Arrays;
import java.util.Optional;
import static com.google.common.base.Preconditions.checkArgument;
......@@ -66,7 +67,13 @@ public abstract class ResourceId {
.add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last);
}
abstract DiscreteResourceId parent();
/**
* Returns the parent resource ID of this instance.
*
* @return the parent resource path of this instance.
* If there is no parent, empty instance will be returned.
*/
public abstract Optional<DiscreteResourceId> parent();
/**
* Returns a resource ID of a child of this resource based on the specified object.
......