Use concrete types instead of abstract types
- ResourceId -> DiscreteResourceId/ContinuousResourceId - Resource -> DiscreteResource/ContinuousResource In addition, stop sharing the implementations in the super classes Change-Id: I44662f6b7c23a23c30844a5b693e1cabab2cc091
Showing
7 changed files
with
262 additions
and
169 deletions
... | @@ -16,8 +16,11 @@ | ... | @@ -16,8 +16,11 @@ |
16 | package org.onosproject.net.newresource; | 16 | package org.onosproject.net.newresource; |
17 | 17 | ||
18 | import com.google.common.annotations.Beta; | 18 | import com.google.common.annotations.Beta; |
19 | +import com.google.common.base.MoreObjects; | ||
19 | 20 | ||
21 | +import java.util.List; | ||
20 | import java.util.Objects; | 22 | import java.util.Objects; |
23 | +import java.util.Optional; | ||
21 | 24 | ||
22 | /** | 25 | /** |
23 | * Represents a resource path which specifies a resource which can be measured | 26 | * Represents a resource path which specifies a resource which can be measured |
... | @@ -27,15 +30,20 @@ import java.util.Objects; | ... | @@ -27,15 +30,20 @@ import java.util.Objects; |
27 | * implementation only. It is not for resource API user. | 30 | * implementation only. It is not for resource API user. |
28 | */ | 31 | */ |
29 | @Beta | 32 | @Beta |
30 | -// TODO: consider how to restrict the visibility | 33 | +public final class ContinuousResource implements Resource { |
31 | -public final class ContinuousResource extends Resource { | 34 | + private final ContinuousResourceId id; |
32 | private final double value; | 35 | private final double value; |
33 | 36 | ||
34 | - ContinuousResource(ResourceId id, double value) { | 37 | + ContinuousResource(ContinuousResourceId id, double value) { |
35 | - super(id); | 38 | + this.id = id; |
36 | this.value = value; | 39 | this.value = value; |
37 | } | 40 | } |
38 | 41 | ||
42 | + @Override | ||
43 | + public ContinuousResourceId id() { | ||
44 | + return id; | ||
45 | + } | ||
46 | + | ||
39 | /** | 47 | /** |
40 | * The user of this methods must receive the return value as Double or double. | 48 | * The user of this methods must receive the return value as Double or double. |
41 | * Otherwise, this methods throws an exception. | 49 | * Otherwise, this methods throws an exception. |
... | @@ -49,6 +57,44 @@ public final class ContinuousResource extends Resource { | ... | @@ -49,6 +57,44 @@ public final class ContinuousResource extends Resource { |
49 | return (T) Double.valueOf(value); | 57 | return (T) Double.valueOf(value); |
50 | } | 58 | } |
51 | 59 | ||
60 | + /** | ||
61 | + * Returns the value of the resource amount. | ||
62 | + * | ||
63 | + * @return the value of the resource amount | ||
64 | + */ | ||
65 | + // FIXME: overlapping a purpose with volume() | ||
66 | + public double value() { | ||
67 | + return value; | ||
68 | + } | ||
69 | + | ||
70 | + @Override | ||
71 | + public List<Object> components() { | ||
72 | + return id.components; | ||
73 | + } | ||
74 | + | ||
75 | + @Override | ||
76 | + public Object last() { | ||
77 | + if (id.components.isEmpty()) { | ||
78 | + return null; | ||
79 | + } | ||
80 | + return id.components.get(id.components.size() - 1); | ||
81 | + } | ||
82 | + | ||
83 | + @Override | ||
84 | + public DiscreteResource child(Object child) { | ||
85 | + throw new UnsupportedOperationException(); | ||
86 | + } | ||
87 | + | ||
88 | + @Override | ||
89 | + public ContinuousResource child(Class<?> child, double value) { | ||
90 | + throw new UnsupportedOperationException(); | ||
91 | + } | ||
92 | + | ||
93 | + @Override | ||
94 | + public Optional<DiscreteResource> parent() { | ||
95 | + return Optional.ofNullable(id.parent()).map(DiscreteResource::new); | ||
96 | + } | ||
97 | + | ||
52 | @Override | 98 | @Override |
53 | public int hashCode() { | 99 | public int hashCode() { |
54 | return Objects.hash(id(), value); | 100 | return Objects.hash(id(), value); |
... | @@ -67,13 +113,11 @@ public final class ContinuousResource extends Resource { | ... | @@ -67,13 +113,11 @@ public final class ContinuousResource extends Resource { |
67 | && Objects.equals(this.value, other.value); | 113 | && Objects.equals(this.value, other.value); |
68 | } | 114 | } |
69 | 115 | ||
70 | - /** | 116 | + @Override |
71 | - * Returns the value of the resource amount. | 117 | + public String toString() { |
72 | - * | 118 | + return MoreObjects.toStringHelper(this) |
73 | - * @return the value of the resource amount | 119 | + .add("id", id) |
74 | - */ | 120 | + .add("volume", value) |
75 | - // FIXME: overlapping a purpose with volume() | 121 | + .toString(); |
76 | - public double value() { | ||
77 | - return value; | ||
78 | } | 122 | } |
79 | } | 123 | } | ... | ... |
... | @@ -18,6 +18,8 @@ package org.onosproject.net.newresource; | ... | @@ -18,6 +18,8 @@ package org.onosproject.net.newresource; |
18 | import com.google.common.annotations.Beta; | 18 | import com.google.common.annotations.Beta; |
19 | import com.google.common.collect.ImmutableList; | 19 | import com.google.common.collect.ImmutableList; |
20 | 20 | ||
21 | +import java.util.Objects; | ||
22 | + | ||
21 | import static com.google.common.base.Preconditions.checkNotNull; | 23 | import static com.google.common.base.Preconditions.checkNotNull; |
22 | 24 | ||
23 | /** | 25 | /** |
... | @@ -27,16 +29,73 @@ import static com.google.common.base.Preconditions.checkNotNull; | ... | @@ -27,16 +29,73 @@ import static com.google.common.base.Preconditions.checkNotNull; |
27 | * implementation only. It is not for resource API user. | 29 | * implementation only. It is not for resource API user. |
28 | */ | 30 | */ |
29 | @Beta | 31 | @Beta |
30 | -// TODO: consider how to restrict the visibility | ||
31 | public final class ContinuousResourceId extends ResourceId { | 32 | public final class ContinuousResourceId extends ResourceId { |
33 | + final ImmutableList<Object> components; | ||
34 | + | ||
32 | // for printing purpose only (used in toString() implementation) | 35 | // for printing purpose only (used in toString() implementation) |
33 | private final String name; | 36 | private final String name; |
34 | 37 | ||
35 | ContinuousResourceId(ImmutableList<Object> components, String name) { | 38 | ContinuousResourceId(ImmutableList<Object> components, String name) { |
36 | - super(components); | 39 | + this.components = components; |
37 | this.name = checkNotNull(name); | 40 | this.name = checkNotNull(name); |
38 | } | 41 | } |
39 | 42 | ||
43 | + ContinuousResourceId(ImmutableList.Builder<Object> parentComponents, Class<?> last) { | ||
44 | + this.components = parentComponents.add(last.getCanonicalName()).build(); | ||
45 | + this.name = last.getSimpleName(); | ||
46 | + } | ||
47 | + | ||
48 | + /** | ||
49 | + * {@inheritDoc} | ||
50 | + * | ||
51 | + * A child of a continuous-type resource is prohibited. | ||
52 | + * {@link UnsupportedOperationException} is always thrown. | ||
53 | + */ | ||
54 | + @Override | ||
55 | + public DiscreteResourceId child(Object child) { | ||
56 | + throw new UnsupportedOperationException(); | ||
57 | + } | ||
58 | + | ||
59 | + /** | ||
60 | + * {@inheritDoc} | ||
61 | + * | ||
62 | + * A child of a continuous-type resource is prohibited. | ||
63 | + * {@link UnsupportedOperationException} is always thrown. | ||
64 | + */ | ||
65 | + @Override | ||
66 | + public ContinuousResourceId child(Class<?> child) { | ||
67 | + throw new UnsupportedOperationException(); | ||
68 | + } | ||
69 | + | ||
70 | + @Override | ||
71 | + DiscreteResourceId parent() { | ||
72 | + if (components.size() == 0) { | ||
73 | + return null; | ||
74 | + } | ||
75 | + if (components.size() == 1) { | ||
76 | + return ROOT; | ||
77 | + } else { | ||
78 | + return new DiscreteResourceId(components.subList(0, components.size() - 1)); | ||
79 | + } | ||
80 | + } | ||
81 | + | ||
82 | + @Override | ||
83 | + public int hashCode() { | ||
84 | + return components.hashCode(); | ||
85 | + } | ||
86 | + | ||
87 | + @Override | ||
88 | + public boolean equals(Object obj) { | ||
89 | + if (this == obj) { | ||
90 | + return true; | ||
91 | + } | ||
92 | + if (obj == null || getClass() != obj.getClass()) { | ||
93 | + return false; | ||
94 | + } | ||
95 | + final ContinuousResourceId other = (ContinuousResourceId) obj; | ||
96 | + return Objects.equals(this.components, other.components); | ||
97 | + } | ||
98 | + | ||
40 | @Override | 99 | @Override |
41 | public String toString() { | 100 | public String toString() { |
42 | // due to performance consideration, the value might need to be stored in a field | 101 | // due to performance consideration, the value might need to be stored in a field | ... | ... |
... | @@ -16,8 +16,13 @@ | ... | @@ -16,8 +16,13 @@ |
16 | package org.onosproject.net.newresource; | 16 | package org.onosproject.net.newresource; |
17 | 17 | ||
18 | import com.google.common.annotations.Beta; | 18 | import com.google.common.annotations.Beta; |
19 | +import com.google.common.base.MoreObjects; | ||
19 | 20 | ||
21 | +import java.util.List; | ||
20 | import java.util.Objects; | 22 | import java.util.Objects; |
23 | +import java.util.Optional; | ||
24 | + | ||
25 | +import static com.google.common.base.Preconditions.checkArgument; | ||
21 | 26 | ||
22 | /** | 27 | /** |
23 | * Represents a resource path which specifies a resource which can be measured | 28 | * Represents a resource path which specifies a resource which can be measured |
... | @@ -28,14 +33,20 @@ import java.util.Objects; | ... | @@ -28,14 +33,20 @@ import java.util.Objects; |
28 | * </p> | 33 | * </p> |
29 | */ | 34 | */ |
30 | @Beta | 35 | @Beta |
31 | -// TODO: consider how to restrict the visibility | 36 | +public final class DiscreteResource implements Resource { |
32 | -public final class DiscreteResource extends Resource { | 37 | + private final DiscreteResourceId id; |
38 | + | ||
39 | + DiscreteResource(DiscreteResourceId id) { | ||
40 | + this.id = id; | ||
41 | + } | ||
42 | + | ||
33 | protected DiscreteResource() { | 43 | protected DiscreteResource() { |
34 | - super(); | 44 | + this.id = ResourceId.ROOT; |
35 | } | 45 | } |
36 | 46 | ||
37 | - DiscreteResource(ResourceId id) { | 47 | + @Override |
38 | - super(id); | 48 | + public DiscreteResourceId id() { |
49 | + return id; | ||
39 | } | 50 | } |
40 | 51 | ||
41 | /** | 52 | /** |
... | @@ -53,6 +64,36 @@ public final class DiscreteResource extends Resource { | ... | @@ -53,6 +64,36 @@ public final class DiscreteResource extends Resource { |
53 | } | 64 | } |
54 | 65 | ||
55 | @Override | 66 | @Override |
67 | + public List<Object> components() { | ||
68 | + return id.components; | ||
69 | + } | ||
70 | + | ||
71 | + @Override | ||
72 | + public Object last() { | ||
73 | + if (id.components.isEmpty()) { | ||
74 | + return null; | ||
75 | + } | ||
76 | + return id.components.get(id.components.size() - 1); | ||
77 | + } | ||
78 | + | ||
79 | + @Override | ||
80 | + public DiscreteResource child(Object child) { | ||
81 | + checkArgument(!(child instanceof Class<?>)); | ||
82 | + | ||
83 | + return new DiscreteResource(id().child(child)); | ||
84 | + } | ||
85 | + | ||
86 | + @Override | ||
87 | + public ContinuousResource child(Class<?> child, double value) { | ||
88 | + return new ContinuousResource(id.child(child), value); | ||
89 | + } | ||
90 | + | ||
91 | + @Override | ||
92 | + public Optional<DiscreteResource> parent() { | ||
93 | + return Optional.ofNullable(id.parent()).map(DiscreteResource::new); | ||
94 | + } | ||
95 | + | ||
96 | + @Override | ||
56 | public int hashCode() { | 97 | public int hashCode() { |
57 | // the value returing from volume() is excluded due to optimization | 98 | // the value returing from volume() is excluded due to optimization |
58 | return id().hashCode(); | 99 | return id().hashCode(); |
... | @@ -70,4 +111,12 @@ public final class DiscreteResource extends Resource { | ... | @@ -70,4 +111,12 @@ public final class DiscreteResource extends Resource { |
70 | // the value returing from volume() is excluded due to optimization | 111 | // the value returing from volume() is excluded due to optimization |
71 | return Objects.equals(this.id(), other.id()); | 112 | return Objects.equals(this.id(), other.id()); |
72 | } | 113 | } |
114 | + | ||
115 | + @Override | ||
116 | + public String toString() { | ||
117 | + return MoreObjects.toStringHelper(this) | ||
118 | + .add("id", id) | ||
119 | + .add("volume", volume()) | ||
120 | + .toString(); | ||
121 | + } | ||
73 | } | 122 | } | ... | ... |
... | @@ -18,6 +18,11 @@ package org.onosproject.net.newresource; | ... | @@ -18,6 +18,11 @@ package org.onosproject.net.newresource; |
18 | import com.google.common.annotations.Beta; | 18 | import com.google.common.annotations.Beta; |
19 | import com.google.common.collect.ImmutableList; | 19 | import com.google.common.collect.ImmutableList; |
20 | 20 | ||
21 | +import java.util.Objects; | ||
22 | + | ||
23 | +import static com.google.common.base.Preconditions.checkArgument; | ||
24 | +import static com.google.common.base.Preconditions.checkNotNull; | ||
25 | + | ||
21 | /** | 26 | /** |
22 | * ResourceId for {@link DiscreteResource}. | 27 | * ResourceId for {@link DiscreteResource}. |
23 | * | 28 | * |
... | @@ -25,13 +30,65 @@ import com.google.common.collect.ImmutableList; | ... | @@ -25,13 +30,65 @@ import com.google.common.collect.ImmutableList; |
25 | * implementation only. It is not for resource API user. | 30 | * implementation only. It is not for resource API user. |
26 | */ | 31 | */ |
27 | @Beta | 32 | @Beta |
28 | -// TODO: consider how to restrict the visibility | ||
29 | public final class DiscreteResourceId extends ResourceId { | 33 | public final class DiscreteResourceId extends ResourceId { |
34 | + final ImmutableList<Object> components; | ||
35 | + | ||
30 | DiscreteResourceId(ImmutableList<Object> components) { | 36 | DiscreteResourceId(ImmutableList<Object> components) { |
31 | - super(components); | 37 | + this.components = components; |
32 | } | 38 | } |
33 | 39 | ||
34 | DiscreteResourceId() { | 40 | DiscreteResourceId() { |
35 | - super(); | 41 | + this.components = ImmutableList.of(); |
42 | + } | ||
43 | + | ||
44 | + @Override | ||
45 | + public DiscreteResourceId child(Object child) { | ||
46 | + checkArgument(!(child instanceof Class<?>)); | ||
47 | + | ||
48 | + return new DiscreteResourceId(ImmutableList.builder() | ||
49 | + .addAll(components) | ||
50 | + .add(child) | ||
51 | + .build()); | ||
52 | + } | ||
53 | + | ||
54 | + @Override | ||
55 | + public ContinuousResourceId child(Class<?> child) { | ||
56 | + checkNotNull(child); | ||
57 | + | ||
58 | + return new ContinuousResourceId(ImmutableList.builder().addAll(components), child); | ||
59 | + } | ||
60 | + | ||
61 | + @Override | ||
62 | + DiscreteResourceId parent() { | ||
63 | + if (components.size() == 0) { | ||
64 | + return null; | ||
65 | + } | ||
66 | + if (components.size() == 1) { | ||
67 | + return ROOT; | ||
68 | + } else { | ||
69 | + return new DiscreteResourceId(components.subList(0, components.size() - 1)); | ||
70 | + } | ||
71 | + } | ||
72 | + | ||
73 | + @Override | ||
74 | + public int hashCode() { | ||
75 | + return components.hashCode(); | ||
76 | + } | ||
77 | + | ||
78 | + @Override | ||
79 | + public boolean equals(Object obj) { | ||
80 | + if (this == obj) { | ||
81 | + return true; | ||
82 | + } | ||
83 | + if (obj == null || getClass() != obj.getClass()) { | ||
84 | + return false; | ||
85 | + } | ||
86 | + final DiscreteResourceId other = (DiscreteResourceId) obj; | ||
87 | + return Objects.equals(this.components, other.components); | ||
88 | + } | ||
89 | + | ||
90 | + @Override | ||
91 | + public String toString() { | ||
92 | + return components.toString(); | ||
36 | } | 93 | } |
37 | } | 94 | } | ... | ... |
... | @@ -16,7 +16,6 @@ | ... | @@ -16,7 +16,6 @@ |
16 | package org.onosproject.net.newresource; | 16 | package org.onosproject.net.newresource; |
17 | 17 | ||
18 | import com.google.common.annotations.Beta; | 18 | import com.google.common.annotations.Beta; |
19 | -import com.google.common.base.MoreObjects; | ||
20 | import org.onosproject.net.DeviceId; | 19 | import org.onosproject.net.DeviceId; |
21 | import org.onosproject.net.PortNumber; | 20 | import org.onosproject.net.PortNumber; |
22 | 21 | ||
... | @@ -24,8 +23,6 @@ import java.util.List; | ... | @@ -24,8 +23,6 @@ import java.util.List; |
24 | import java.util.Optional; | 23 | import java.util.Optional; |
25 | 24 | ||
26 | import static com.google.common.base.Preconditions.checkArgument; | 25 | import static com.google.common.base.Preconditions.checkArgument; |
27 | -import static com.google.common.base.Preconditions.checkNotNull; | ||
28 | -import static com.google.common.base.Preconditions.checkState; | ||
29 | 26 | ||
30 | /** | 27 | /** |
31 | * An object that represent a resource in a network. | 28 | * An object that represent a resource in a network. |
... | @@ -44,14 +41,11 @@ import static com.google.common.base.Preconditions.checkState; | ... | @@ -44,14 +41,11 @@ import static com.google.common.base.Preconditions.checkState; |
44 | * VLAN ID:100/Device:1/Port:1 is not valid because a link is not a sub-component of a VLAN ID. | 41 | * VLAN ID:100/Device:1/Port:1 is not valid because a link is not a sub-component of a VLAN ID. |
45 | */ | 42 | */ |
46 | @Beta | 43 | @Beta |
47 | -public abstract class Resource { | 44 | +public interface Resource { |
48 | 45 | ||
49 | - private final DiscreteResource parent; | 46 | + DiscreteResource ROOT = new DiscreteResource(); |
50 | - private final ResourceId id; | ||
51 | 47 | ||
52 | - public static final DiscreteResource ROOT = new DiscreteResource(); | 48 | + static DiscreteResource discrete(DeviceId device) { |
53 | - | ||
54 | - public static Resource discrete(DeviceId device) { | ||
55 | return new DiscreteResource(ResourceId.discrete(device)); | 49 | return new DiscreteResource(ResourceId.discrete(device)); |
56 | } | 50 | } |
57 | 51 | ||
... | @@ -62,7 +56,7 @@ public abstract class Resource { | ... | @@ -62,7 +56,7 @@ public abstract class Resource { |
62 | * @param components following components of the path. The order represents hierarchical structure of the resource. | 56 | * @param components following components of the path. The order represents hierarchical structure of the resource. |
63 | * @return resource path instance | 57 | * @return resource path instance |
64 | */ | 58 | */ |
65 | - public static Resource discrete(DeviceId device, Object... components) { | 59 | + static DiscreteResource discrete(DeviceId device, Object... components) { |
66 | return new DiscreteResource(ResourceId.discrete(device, components)); | 60 | return new DiscreteResource(ResourceId.discrete(device, components)); |
67 | } | 61 | } |
68 | 62 | ||
... | @@ -74,7 +68,7 @@ public abstract class Resource { | ... | @@ -74,7 +68,7 @@ public abstract class Resource { |
74 | * @param components following components of the path. The order represents hierarchical structure of the resource. | 68 | * @param components following components of the path. The order represents hierarchical structure of the resource. |
75 | * @return resource path instance | 69 | * @return resource path instance |
76 | */ | 70 | */ |
77 | - public static Resource discrete(DeviceId device, PortNumber port, Object... components) { | 71 | + static DiscreteResource discrete(DeviceId device, PortNumber port, Object... components) { |
78 | return new DiscreteResource(ResourceId.discrete(device, port, components)); | 72 | return new DiscreteResource(ResourceId.discrete(device, port, components)); |
79 | } | 73 | } |
80 | 74 | ||
... | @@ -88,7 +82,7 @@ public abstract class Resource { | ... | @@ -88,7 +82,7 @@ public abstract class Resource { |
88 | * an IllegalArgumentException. | 82 | * an IllegalArgumentException. |
89 | * @return resource path instance | 83 | * @return resource path instance |
90 | */ | 84 | */ |
91 | - public static Resource continuous(double value, DeviceId device, Object... components) { | 85 | + static ContinuousResource continuous(double value, DeviceId device, Object... components) { |
92 | checkArgument(components.length > 0, | 86 | checkArgument(components.length > 0, |
93 | "Length of components must be greater thant 0, but " + components.length); | 87 | "Length of components must be greater thant 0, but " + components.length); |
94 | 88 | ||
... | @@ -106,40 +100,16 @@ public abstract class Resource { | ... | @@ -106,40 +100,16 @@ public abstract class Resource { |
106 | * an IllegalArgumentException. | 100 | * an IllegalArgumentException. |
107 | * @return resource path instance | 101 | * @return resource path instance |
108 | */ | 102 | */ |
109 | - public static Resource continuous(double value, DeviceId device, PortNumber port, Object... components) { | 103 | + static ContinuousResource continuous(double value, DeviceId device, PortNumber port, Object... components) { |
110 | return new ContinuousResource(ResourceId.continuous(device, port, components), value); | 104 | return new ContinuousResource(ResourceId.continuous(device, port, components), value); |
111 | } | 105 | } |
112 | 106 | ||
113 | /** | 107 | /** |
114 | - * Creates an resource path from the specified id. | ||
115 | - * | ||
116 | - * @param id id of the path | ||
117 | - */ | ||
118 | - protected Resource(ResourceId id) { | ||
119 | - checkNotNull(id); | ||
120 | - | ||
121 | - this.id = id; | ||
122 | - if (id.components.size() == 1) { | ||
123 | - this.parent = ROOT; | ||
124 | - } else { | ||
125 | - this.parent = new DiscreteResource(id.parent()); | ||
126 | - } | ||
127 | - } | ||
128 | - | ||
129 | - // for serialization | ||
130 | - protected Resource() { | ||
131 | - this.parent = null; | ||
132 | - this.id = ResourceId.ROOT; | ||
133 | - } | ||
134 | - | ||
135 | - /** | ||
136 | * Returns the components of this resource path. | 108 | * Returns the components of this resource path. |
137 | * | 109 | * |
138 | * @return the components of this resource path | 110 | * @return the components of this resource path |
139 | */ | 111 | */ |
140 | - public List<Object> components() { | 112 | + List<Object> components(); |
141 | - return id.components; | ||
142 | - } | ||
143 | 113 | ||
144 | /** | 114 | /** |
145 | * Returns the volume of this resource. | 115 | * Returns the volume of this resource. |
... | @@ -147,7 +117,7 @@ public abstract class Resource { | ... | @@ -147,7 +117,7 @@ public abstract class Resource { |
147 | * @return the volume of this resource | 117 | * @return the volume of this resource |
148 | */ | 118 | */ |
149 | // TODO: think about other naming possibilities. amount? quantity? | 119 | // TODO: think about other naming possibilities. amount? quantity? |
150 | - public abstract <T> T volume(); | 120 | + <T> T volume(); |
151 | 121 | ||
152 | /** | 122 | /** |
153 | * Returns the parent resource path of this instance. | 123 | * Returns the parent resource path of this instance. |
... | @@ -156,9 +126,7 @@ public abstract class Resource { | ... | @@ -156,9 +126,7 @@ public abstract class Resource { |
156 | * @return the parent resource path of this instance. | 126 | * @return the parent resource path of this instance. |
157 | * If there is no parent, empty instance will be returned. | 127 | * If there is no parent, empty instance will be returned. |
158 | */ | 128 | */ |
159 | - public Optional<DiscreteResource> parent() { | 129 | + Optional<DiscreteResource> parent(); |
160 | - return Optional.ofNullable(parent); | ||
161 | - } | ||
162 | 130 | ||
163 | /** | 131 | /** |
164 | * Returns a child resource path of this instance with specifying the child object. | 132 | * Returns a child resource path of this instance with specifying the child object. |
... | @@ -167,11 +135,7 @@ public abstract class Resource { | ... | @@ -167,11 +135,7 @@ public abstract class Resource { |
167 | * @param child child object | 135 | * @param child child object |
168 | * @return a child resource path | 136 | * @return a child resource path |
169 | */ | 137 | */ |
170 | - public Resource child(Object child) { | 138 | + DiscreteResource child(Object child); |
171 | - checkState(this instanceof DiscreteResource); | ||
172 | - | ||
173 | - return new DiscreteResource(id().child(child)); | ||
174 | - } | ||
175 | 139 | ||
176 | /** | 140 | /** |
177 | * Returns a child resource path of this instance with specifying a child object and | 141 | * Returns a child resource path of this instance with specifying a child object and |
... | @@ -181,11 +145,7 @@ public abstract class Resource { | ... | @@ -181,11 +145,7 @@ public abstract class Resource { |
181 | * @param value value | 145 | * @param value value |
182 | * @return a child resource path | 146 | * @return a child resource path |
183 | */ | 147 | */ |
184 | - public Resource child(Object child, double value) { | 148 | + ContinuousResource child(Class<?> child, double value); |
185 | - checkState(this instanceof DiscreteResource); | ||
186 | - | ||
187 | - return new ContinuousResource(id.child(child), value); | ||
188 | - } | ||
189 | 149 | ||
190 | /** | 150 | /** |
191 | * Returns the last component of this instance. | 151 | * Returns the last component of this instance. |
... | @@ -193,28 +153,12 @@ public abstract class Resource { | ... | @@ -193,28 +153,12 @@ public abstract class Resource { |
193 | * @return the last component of this instance. | 153 | * @return the last component of this instance. |
194 | * The return value is equal to the last object of {@code components()}. | 154 | * The return value is equal to the last object of {@code components()}. |
195 | */ | 155 | */ |
196 | - public Object last() { | 156 | + Object last(); |
197 | - if (id.components.isEmpty()) { | ||
198 | - return null; | ||
199 | - } | ||
200 | - return id.components.get(id.components.size() - 1); | ||
201 | - } | ||
202 | 157 | ||
203 | /** | 158 | /** |
204 | * Returns the ID of this resource path. | 159 | * Returns the ID of this resource path. |
205 | * | 160 | * |
206 | * @return the ID of this resource path | 161 | * @return the ID of this resource path |
207 | */ | 162 | */ |
208 | - public ResourceId id() { | 163 | + ResourceId id(); |
209 | - return id; | ||
210 | - } | ||
211 | - | ||
212 | - @Override | ||
213 | - public String toString() { | ||
214 | - return MoreObjects.toStringHelper(this) | ||
215 | - .add("id", id()) | ||
216 | - .add("volume", volume()) | ||
217 | - .toString(); | ||
218 | - } | ||
219 | - | ||
220 | } | 164 | } | ... | ... |
... | @@ -21,11 +21,8 @@ import org.onosproject.net.DeviceId; | ... | @@ -21,11 +21,8 @@ import org.onosproject.net.DeviceId; |
21 | import org.onosproject.net.PortNumber; | 21 | import org.onosproject.net.PortNumber; |
22 | 22 | ||
23 | import java.util.Arrays; | 23 | import java.util.Arrays; |
24 | -import java.util.Objects; | ||
25 | 24 | ||
26 | import static com.google.common.base.Preconditions.checkArgument; | 25 | import static com.google.common.base.Preconditions.checkArgument; |
27 | -import static com.google.common.base.Preconditions.checkNotNull; | ||
28 | -import static com.google.common.base.Preconditions.checkState; | ||
29 | 26 | ||
30 | /** | 27 | /** |
31 | * Represents identifier of resource. | 28 | * Represents identifier of resource. |
... | @@ -33,18 +30,16 @@ import static com.google.common.base.Preconditions.checkState; | ... | @@ -33,18 +30,16 @@ import static com.google.common.base.Preconditions.checkState; |
33 | */ | 30 | */ |
34 | @Beta | 31 | @Beta |
35 | public abstract class ResourceId { | 32 | public abstract class ResourceId { |
36 | - static final ResourceId ROOT = new DiscreteResourceId(); | 33 | + static final DiscreteResourceId ROOT = new DiscreteResourceId(); |
37 | 34 | ||
38 | - final ImmutableList<Object> components; | 35 | + static DiscreteResourceId discrete(DeviceId device, Object... components) { |
39 | - | ||
40 | - static ResourceId discrete(DeviceId device, Object... components) { | ||
41 | return new DiscreteResourceId(ImmutableList.builder() | 36 | return new DiscreteResourceId(ImmutableList.builder() |
42 | .add(device) | 37 | .add(device) |
43 | .add(components) | 38 | .add(components) |
44 | .build()); | 39 | .build()); |
45 | } | 40 | } |
46 | 41 | ||
47 | - static ResourceId discrete(DeviceId device, PortNumber port, Object... components) { | 42 | + static DiscreteResourceId discrete(DeviceId device, PortNumber port, Object... components) { |
48 | return new DiscreteResourceId(ImmutableList.builder() | 43 | return new DiscreteResourceId(ImmutableList.builder() |
49 | .add(device) | 44 | .add(device) |
50 | .add(port) | 45 | .add(port) |
... | @@ -52,92 +47,41 @@ public abstract class ResourceId { | ... | @@ -52,92 +47,41 @@ public abstract class ResourceId { |
52 | .build()); | 47 | .build()); |
53 | } | 48 | } |
54 | 49 | ||
55 | - static ResourceId continuous(DeviceId device, Object... components) { | 50 | + static ContinuousResourceId continuous(DeviceId device, Object... components) { |
56 | Object last = components[components.length - 1]; | 51 | Object last = components[components.length - 1]; |
57 | checkArgument(last instanceof Class<?>); | 52 | checkArgument(last instanceof Class<?>); |
58 | 53 | ||
59 | - return continuous(ImmutableList.builder() | 54 | + return new ContinuousResourceId(ImmutableList.builder() |
60 | .add(device) | 55 | .add(device) |
61 | .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last); | 56 | .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last); |
62 | } | 57 | } |
63 | 58 | ||
64 | - static ResourceId continuous(DeviceId device, PortNumber port, Object... components) { | 59 | + static ContinuousResourceId continuous(DeviceId device, PortNumber port, Object... components) { |
65 | Object last = components[components.length - 1]; | 60 | Object last = components[components.length - 1]; |
66 | checkArgument(last instanceof Class<?>); | 61 | checkArgument(last instanceof Class<?>); |
67 | 62 | ||
68 | - return continuous(ImmutableList.builder() | 63 | + return new ContinuousResourceId(ImmutableList.builder() |
69 | .add(device) | 64 | .add(device) |
70 | .add(port) | 65 | .add(port) |
71 | .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last); | 66 | .add(Arrays.copyOfRange(components, 0, components.length - 1)), (Class<?>) last); |
72 | } | 67 | } |
73 | 68 | ||
74 | - private static ResourceId continuous(ImmutableList.Builder<Object> parentComponents, Class<?> last) { | 69 | + abstract DiscreteResourceId parent(); |
75 | - return new ContinuousResourceId(parentComponents | ||
76 | - .add(last.getCanonicalName()) | ||
77 | - .build(), last.getSimpleName()); | ||
78 | - } | ||
79 | - | ||
80 | - protected ResourceId(ImmutableList<Object> components) { | ||
81 | - this.components = checkNotNull(components); | ||
82 | - } | ||
83 | - | ||
84 | - // for serializer | ||
85 | - protected ResourceId() { | ||
86 | - this.components = ImmutableList.of(); | ||
87 | - } | ||
88 | - | ||
89 | - // IndexOutOfBoundsException is raised when the instance is equal to ROOT | ||
90 | - ResourceId parent() { | ||
91 | - if (components.size() == 1) { | ||
92 | - return ROOT; | ||
93 | - } else { | ||
94 | - return new DiscreteResourceId(components.subList(0, components.size() - 1)); | ||
95 | - } | ||
96 | - } | ||
97 | 70 | ||
98 | /** | 71 | /** |
99 | * Returns a resource ID of a child of this resource based on the specified object. | 72 | * Returns a resource ID of a child of this resource based on the specified object. |
100 | - * If the argument is an instance of {@link Class}, this method returns an instance of | 73 | + * If the given object is a {@link Class} instance, {@link IllegalArgumentException} is thrown. |
101 | - * {@link ContinuousResourceId}. Otherwise, it returns an instance of {@link DiscreteResourceId} | ||
102 | - * This method only work when the receiver is {@link DiscreteResourceId}. Otherwise, | ||
103 | - * this method throws an exception. | ||
104 | * | 74 | * |
105 | * @param child the last component of the child | 75 | * @param child the last component of the child |
106 | * @return a child resource ID | 76 | * @return a child resource ID |
107 | */ | 77 | */ |
108 | - public ResourceId child(Object child) { | 78 | + public abstract DiscreteResourceId child(Object child); |
109 | - checkState(this instanceof DiscreteResourceId); | ||
110 | - | ||
111 | - if (child instanceof Class<?>) { | ||
112 | - return continuous(ImmutableList.builder().addAll(components), (Class<?>) child); | ||
113 | - } else { | ||
114 | - return new DiscreteResourceId(ImmutableList.builder() | ||
115 | - .addAll(components) | ||
116 | - .add(child) | ||
117 | - .build()); | ||
118 | - } | ||
119 | - } | ||
120 | - | ||
121 | - @Override | ||
122 | - public int hashCode() { | ||
123 | - return components.hashCode(); | ||
124 | - } | ||
125 | - | ||
126 | - @Override | ||
127 | - public boolean equals(Object obj) { | ||
128 | - if (this == obj) { | ||
129 | - return true; | ||
130 | - } | ||
131 | - if (obj == null || getClass() != obj.getClass()) { | ||
132 | - return false; | ||
133 | - } | ||
134 | - final ResourceId other = (ResourceId) obj; | ||
135 | - return Objects.equals(this.components, other.components); | ||
136 | - } | ||
137 | - | ||
138 | - @Override | ||
139 | - public String toString() { | ||
140 | - return components.toString(); | ||
141 | - } | ||
142 | 79 | ||
80 | + /** | ||
81 | + * Returns a resource ID of a child of this resource based on the specified object. | ||
82 | + * | ||
83 | + * @param child the last component of the child | ||
84 | + * @return a child resource ID | ||
85 | + */ | ||
86 | + public abstract ContinuousResourceId child(Class<?> child); | ||
143 | } | 87 | } | ... | ... |
... | @@ -181,8 +181,6 @@ import org.onosproject.net.newresource.ContinuousResourceId; | ... | @@ -181,8 +181,6 @@ import org.onosproject.net.newresource.ContinuousResourceId; |
181 | import org.onosproject.net.newresource.DiscreteResource; | 181 | import org.onosproject.net.newresource.DiscreteResource; |
182 | import org.onosproject.net.newresource.DiscreteResourceId; | 182 | import org.onosproject.net.newresource.DiscreteResourceId; |
183 | import org.onosproject.net.newresource.ResourceAllocation; | 183 | import org.onosproject.net.newresource.ResourceAllocation; |
184 | -import org.onosproject.net.newresource.ResourceId; | ||
185 | -import org.onosproject.net.newresource.Resource; | ||
186 | import org.onosproject.net.packet.DefaultOutboundPacket; | 184 | import org.onosproject.net.packet.DefaultOutboundPacket; |
187 | import org.onosproject.net.packet.DefaultPacketRequest; | 185 | import org.onosproject.net.packet.DefaultPacketRequest; |
188 | import org.onosproject.net.packet.PacketPriority; | 186 | import org.onosproject.net.packet.PacketPriority; |
... | @@ -442,10 +440,8 @@ public final class KryoNamespaces { | ... | @@ -442,10 +440,8 @@ public final class KryoNamespaces { |
442 | DefaultLinkResourceAllocations.class, | 440 | DefaultLinkResourceAllocations.class, |
443 | BandwidthResourceAllocation.class, | 441 | BandwidthResourceAllocation.class, |
444 | LambdaResourceAllocation.class, | 442 | LambdaResourceAllocation.class, |
445 | - Resource.class, | ||
446 | DiscreteResource.class, | 443 | DiscreteResource.class, |
447 | ContinuousResource.class, | 444 | ContinuousResource.class, |
448 | - ResourceId.class, | ||
449 | DiscreteResourceId.class, | 445 | DiscreteResourceId.class, |
450 | ContinuousResourceId.class, | 446 | ContinuousResourceId.class, |
451 | ResourceAllocation.class, | 447 | ResourceAllocation.class, | ... | ... |
-
Please register or login to post a comment