Refactor Resoruce/ResourceId sub-types
isTypeOf() and isSubTypeOf() of Resource is delegated to ResourceId Change-Id: Ief39f4967b8fb43ec0cb35dbd44bd2a7dde93680
Showing
5 changed files
with
71 additions
and
32 deletions
... | @@ -57,8 +57,7 @@ public final class ContinuousResource implements Resource { | ... | @@ -57,8 +57,7 @@ public final class ContinuousResource implements Resource { |
57 | public boolean isTypeOf(Class<?> type) { | 57 | public boolean isTypeOf(Class<?> type) { |
58 | checkNotNull(type); | 58 | checkNotNull(type); |
59 | 59 | ||
60 | - String typeName = (String) id.components().get(id.components().size() - 1); | 60 | + return id.isTypeOf(type); |
61 | - return typeName.equals(type.getCanonicalName()); | ||
62 | } | 61 | } |
63 | 62 | ||
64 | /** | 63 | /** |
... | @@ -74,13 +73,7 @@ public final class ContinuousResource implements Resource { | ... | @@ -74,13 +73,7 @@ public final class ContinuousResource implements Resource { |
74 | public boolean isSubTypeOf(Class<?> ancestor) { | 73 | public boolean isSubTypeOf(Class<?> ancestor) { |
75 | checkNotNull(ancestor); | 74 | checkNotNull(ancestor); |
76 | 75 | ||
77 | - String typeName = (String) id.components().get(id.components().size() - 1); | 76 | + return id.isSubTypeOf(ancestor); |
78 | - boolean foundInLeaf = typeName.equals(ancestor.getCanonicalName()); | ||
79 | - boolean foundInAncestor = id.components().subList(0, id.components().size()).stream() | ||
80 | - .filter(x -> ancestor.isAssignableFrom(x.getClass())) | ||
81 | - .findAny() | ||
82 | - .isPresent(); | ||
83 | - return foundInAncestor || foundInLeaf; | ||
84 | } | 77 | } |
85 | 78 | ||
86 | /** | 79 | /** | ... | ... |
... | @@ -52,6 +52,23 @@ public final class ContinuousResourceId extends ResourceId { | ... | @@ -52,6 +52,23 @@ public final class ContinuousResourceId extends ResourceId { |
52 | return name; | 52 | return name; |
53 | } | 53 | } |
54 | 54 | ||
55 | + @Override | ||
56 | + boolean isTypeOf(Class<?> type) { | ||
57 | + String typeName = (String) lastComponent(); | ||
58 | + return typeName.equals(type.getCanonicalName()); | ||
59 | + } | ||
60 | + | ||
61 | + @Override | ||
62 | + boolean isSubTypeOf(Class<?> ancestor) { | ||
63 | + String typeName = (String) lastComponent(); | ||
64 | + boolean foundInLeaf = typeName.equals(ancestor.getCanonicalName()); | ||
65 | + boolean foundInAncestor = components.subList(0, components.size()).stream() | ||
66 | + .filter(x -> ancestor.isAssignableFrom(x.getClass())) | ||
67 | + .findAny() | ||
68 | + .isPresent(); | ||
69 | + return foundInAncestor || foundInLeaf; | ||
70 | + } | ||
71 | + | ||
55 | /** | 72 | /** |
56 | * {@inheritDoc} | 73 | * {@inheritDoc} |
57 | * | 74 | * |
... | @@ -86,6 +103,10 @@ public final class ContinuousResourceId extends ResourceId { | ... | @@ -86,6 +103,10 @@ public final class ContinuousResourceId extends ResourceId { |
86 | } | 103 | } |
87 | } | 104 | } |
88 | 105 | ||
106 | + private Object lastComponent() { | ||
107 | + return components.get(components.size() - 1); | ||
108 | + } | ||
109 | + | ||
89 | @Override | 110 | @Override |
90 | public int hashCode() { | 111 | public int hashCode() { |
91 | return components.hashCode(); | 112 | return components.hashCode(); | ... | ... |
... | @@ -54,38 +54,21 @@ public final class DiscreteResource implements Resource { | ... | @@ -54,38 +54,21 @@ public final class DiscreteResource implements Resource { |
54 | public boolean isTypeOf(Class<?> type) { | 54 | public boolean isTypeOf(Class<?> type) { |
55 | checkNotNull(type); | 55 | checkNotNull(type); |
56 | 56 | ||
57 | - if (isRoot()) { | 57 | + return id.isTypeOf(type); |
58 | - return false; | ||
59 | - } | ||
60 | - | ||
61 | - return type.isAssignableFrom(id.components().get(id.components().size() - 1).getClass()); | ||
62 | } | 58 | } |
63 | 59 | ||
64 | @Override | 60 | @Override |
65 | public boolean isSubTypeOf(Class<?> ancestor) { | 61 | public boolean isSubTypeOf(Class<?> ancestor) { |
66 | checkNotNull(ancestor); | 62 | checkNotNull(ancestor); |
67 | 63 | ||
68 | - return id.components().stream() | 64 | + return id.isSubTypeOf(ancestor); |
69 | - .filter(x -> ancestor.isAssignableFrom(x.getClass())) | ||
70 | - .findAny() | ||
71 | - .isPresent(); | ||
72 | } | 65 | } |
73 | 66 | ||
74 | @Override | 67 | @Override |
75 | public <T> Optional<T> valueAs(Class<T> type) { | 68 | public <T> Optional<T> valueAs(Class<T> type) { |
76 | checkNotNull(type); | 69 | checkNotNull(type); |
77 | 70 | ||
78 | - if (!isTypeOf(type)) { | 71 | + return id.lastComponentAs(type); |
79 | - return Optional.empty(); | ||
80 | - } | ||
81 | - | ||
82 | - @SuppressWarnings("unchecked") | ||
83 | - T value = (T) id.components().get(id.components().size() - 1); | ||
84 | - return Optional.of(value); | ||
85 | - } | ||
86 | - | ||
87 | - private boolean isRoot() { | ||
88 | - return id.equals(ResourceId.ROOT); | ||
89 | } | 72 | } |
90 | 73 | ||
91 | @Override | 74 | @Override | ... | ... |
... | @@ -46,11 +46,28 @@ public final class DiscreteResourceId extends ResourceId { | ... | @@ -46,11 +46,28 @@ public final class DiscreteResourceId extends ResourceId { |
46 | 46 | ||
47 | @Override | 47 | @Override |
48 | String simpleTypeName() { | 48 | String simpleTypeName() { |
49 | - if (components.isEmpty()) { | 49 | + if (isRoot()) { |
50 | return "Root"; | 50 | return "Root"; |
51 | } | 51 | } |
52 | 52 | ||
53 | - return components.get(components.size() - 1).getClass().getSimpleName(); | 53 | + return lastComponent().getClass().getSimpleName(); |
54 | + } | ||
55 | + | ||
56 | + @Override | ||
57 | + boolean isTypeOf(Class<?> type) { | ||
58 | + if (isRoot()) { | ||
59 | + return false; | ||
60 | + } | ||
61 | + | ||
62 | + return type.isAssignableFrom(lastComponent().getClass()); | ||
63 | + } | ||
64 | + | ||
65 | + @Override | ||
66 | + boolean isSubTypeOf(Class<?> ancestor) { | ||
67 | + return components.stream() | ||
68 | + .filter(x -> ancestor.isAssignableFrom(x.getClass())) | ||
69 | + .findAny() | ||
70 | + .isPresent(); | ||
54 | } | 71 | } |
55 | 72 | ||
56 | @Override | 73 | @Override |
... | @@ -69,7 +86,7 @@ public final class DiscreteResourceId extends ResourceId { | ... | @@ -69,7 +86,7 @@ public final class DiscreteResourceId extends ResourceId { |
69 | 86 | ||
70 | @Override | 87 | @Override |
71 | public Optional<DiscreteResourceId> parent() { | 88 | public Optional<DiscreteResourceId> parent() { |
72 | - if (components.size() == 0) { | 89 | + if (isRoot()) { |
73 | return Optional.empty(); | 90 | return Optional.empty(); |
74 | } | 91 | } |
75 | if (components.size() == 1) { | 92 | if (components.size() == 1) { |
... | @@ -79,6 +96,25 @@ public final class DiscreteResourceId extends ResourceId { | ... | @@ -79,6 +96,25 @@ public final class DiscreteResourceId extends ResourceId { |
79 | } | 96 | } |
80 | } | 97 | } |
81 | 98 | ||
99 | + <T> Optional<T> lastComponentAs(Class<T> type) { | ||
100 | + if (!isTypeOf(type)) { | ||
101 | + return Optional.empty(); | ||
102 | + } | ||
103 | + | ||
104 | + @SuppressWarnings("unchecked") | ||
105 | + T value = (T) lastComponent(); | ||
106 | + return Optional.of(value); | ||
107 | + } | ||
108 | + | ||
109 | + | ||
110 | + private boolean isRoot() { | ||
111 | + return components.isEmpty(); | ||
112 | + } | ||
113 | + | ||
114 | + private Object lastComponent() { | ||
115 | + return components.get(components.size() - 1); | ||
116 | + } | ||
117 | + | ||
82 | @Override | 118 | @Override |
83 | public int hashCode() { | 119 | public int hashCode() { |
84 | return components.hashCode(); | 120 | return components.hashCode(); | ... | ... |
... | @@ -31,6 +31,12 @@ public abstract class ResourceId { | ... | @@ -31,6 +31,12 @@ public abstract class ResourceId { |
31 | 31 | ||
32 | abstract String simpleTypeName(); | 32 | abstract String simpleTypeName(); |
33 | 33 | ||
34 | + // caller must pass a non-null value | ||
35 | + abstract boolean isTypeOf(Class<?> type); | ||
36 | + | ||
37 | + // caller must pass a non-null value | ||
38 | + abstract boolean isSubTypeOf(Class<?> ancestor); | ||
39 | + | ||
34 | /** | 40 | /** |
35 | * Returns the parent resource ID of this instance. | 41 | * Returns the parent resource ID of this instance. |
36 | * | 42 | * | ... | ... |
-
Please register or login to post a comment