Package: As
As
name | instruction | branch | complexity | line | method | ||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
as(As.Type) |
|
|
|
|
|
||||||||||||||||||||
as(Class) |
|
|
|
|
|
||||||||||||||||||||
asMany(As.Type) |
|
|
|
|
|
||||||||||||||||||||
forObject(Object) |
|
|
|
|
|
||||||||||||||||||||
forObject(Object, Collection) |
|
|
|
|
|
||||||||||||||||||||
forObject(Object, Object) |
|
|
|
|
|
||||||||||||||||||||
lambda$as$0(Class) |
|
|
|
|
|
||||||||||||||||||||
maybeAs(As.Type) |
|
|
|
|
|
||||||||||||||||||||
type(Class) |
|
|
|
|
|
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2024 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Collection;
31: import java.util.Optional;
32: import it.tidalwave.role.impl.AsDelegate;
33: import lombok.EqualsAndHashCode;
34: import lombok.RequiredArgsConstructor;
35: import lombok.ToString;
36:
37: /***********************************************************************************************************************
38: *
39: * Objects implementing this interface can provide am adapter of the required type. The adapter can be found with a
40: * variety of approaches that depend on the implementation. This capability can be used to implement a design based
41: * on the Data, Context and Interaction pattern (DCI). For further details, please look at the
42: * <a href="http://tidalwave.it/projects/thesefoolishthings">project website</a>, where a tutorial is available.
43: *
44: * @author Fabrizio Giudici
45: * @it.tidalwave.javadoc.stable
46: *
47: **********************************************************************************************************************/
48: public interface As
49: {
50: /*******************************************************************************************************************
51: *
52: * A type reference for roles that can be used in place of a class literal, especially when roles with generics are
53: * used. Example of usage:
54: * <pre>
55: *
56: * interface DataRetriever<T>
57: * {
58: * public List<T> retrieve();
59: * }
60: *
61: * class CodeSample
62: * {
63: * private static final As.Type<DataRetriever<String>> _StringRetriever_ = As.type(DataRetriever.class);
64: *
65: * public void method (As object)
66: * {
67: * List<String> f3 = object.as(_StringRetriever_).retrieve();
68: * }
69: * }
70: * </pre>
71: *
72: * @since 3.2-ALPHA-12
73: *
74: ******************************************************************************************************************/
75: @RequiredArgsConstructor @EqualsAndHashCode @ToString
76: public static final class Type<T>
77: {
78: @Nonnull
79: private final Class<?> type;
80:
81:
82: @Nonnull @SuppressWarnings("unchecked")
83: /* package */ Class<T> getType()
84: {
85: return (Class<T>)type;
86: }
87: }
88:
89: /*******************************************************************************************************************
90: *
91: * Creates an {@code As} implementation delegate for the given object (or returns the object itself if it is the
92: * default implementation of {@code As}).
93: *
94: * @param object the object
95: * @return the implementation
96: * @since 3.2-ALPHA-12
97: *
98: ******************************************************************************************************************/
99: @Nonnull
100: public static As forObject (@Nonnull final Object object)
101: {
102:• return (object instanceof AsDelegate) ? (As)object : new AsDelegate(object);
103: }
104:
105: /*******************************************************************************************************************
106: *
107: * Creates an {@code As} implementation delegate for the given object. It accepts a single pre-instantiated role,
108: * or a {@link RoleFactory} that will be invoked to create additional roles.
109: *
110: * @param object the object
111: * @param role the role or {@link it.tidalwave.util.RoleFactory}
112: * @return the implementation
113: * @since 3.2-ALPHA-13
114: *
115: ******************************************************************************************************************/
116: @Nonnull
117: public static As forObject (@Nonnull final Object object, @Nonnull final Object role)
118: {
119: return new AsDelegate(object, role);
120: }
121:
122: /*******************************************************************************************************************
123: *
124: * Creates an {@code As} implementation delegate for the given object. It accepts a collection of pre-instantiated
125: * roles, or instances of {@link RoleFactory} that will be invoked to create additional roles.
126: *
127: * @param object the object
128: * @param roles roles or {@link it.tidalwave.util.RoleFactory} instances
129: * @return the implementation
130: * @since 3.2-ALPHA-13
131: *
132: ******************************************************************************************************************/
133: @Nonnull
134: public static As forObject (@Nonnull final Object object, @Nonnull final Collection<Object> roles)
135: {
136: return new AsDelegate(object, roles);
137: }
138:
139: /*******************************************************************************************************************
140: *
141: * Returns an adapter to this object of the specified type. If the implementation can find multiple compliant
142: * adapters, only one will be returned.
143: *
144: * @param <T> the static type
145: * @param type the dynamic type
146: * @return the adapter
147: * @throws AsException if no adapter is found
148: *
149: ******************************************************************************************************************/
150: @Nonnull
151: public default <T> T as (@Nonnull final Class<? extends T> type)
152: {
153: return maybeAs(type).orElseThrow(() -> new AsException(type));
154: }
155:
156: /*******************************************************************************************************************
157: *
158: * Returns the requested role or an empty {@link Optional}.
159: *
160: * @param <T> the static type
161: * @param type the dynamic type
162: * @return the optional role
163: * @since 3.2-ALPHA-3
164: *
165: ******************************************************************************************************************/
166: @Nonnull
167: public <T> Optional<T> maybeAs (@Nonnull Class<? extends T> type);
168:
169: /*******************************************************************************************************************
170: *
171: * Searches for multiple adapters of the given type and returns them.
172: *
173: * @param <T> the static type
174: * @param type the dynamic type
175: * @return a collection of adapters, possibly empty
176: *
177: ******************************************************************************************************************/
178: @Nonnull
179: public <T> Collection<T> asMany (@Nonnull Class<? extends T> type);
180:
181: /*******************************************************************************************************************
182: *
183: * Creates a role type reference.
184: *
185: * @param <T> the static type
186: * @param type the dynamic type
187: * @return the type reference
188: * @since 3.2-ALPHA-12
189: *
190: ******************************************************************************************************************/
191: @Nonnull
192: public static <T> Type<T> type (@Nonnull final Class<?> type) // FIXME: there's no static check of the argument
193: {
194: return new Type<>(type);
195: }
196:
197: /*******************************************************************************************************************
198: *
199: * Returns a role for this object of the specified type. If the implementation can find multiple compliant
200: * roles, only one will be returned.
201: *
202: * @param <T> the static type
203: * @param type the type reference
204: * @return the role
205: * @since 3.2-ALPHA-12
206: *
207: ******************************************************************************************************************/
208: @Nonnull
209: public default <T> T as (@Nonnull final Type<? extends T> type)
210: {
211: return as(type.getType());
212: }
213:
214: /*******************************************************************************************************************
215: *
216: * Returns the requested role or an empty {@link Optional}.
217: *
218: * @param <T> the static type
219: * @param type the type reference
220: * @return the optional role
221: * @since 3.2-ALPHA-12
222: *
223: ******************************************************************************************************************/
224: @Nonnull
225: public default <T> Optional<T> maybeAs (@Nonnull final Type<? extends T> type)
226: {
227: return maybeAs(type.getType());
228: }
229:
230: /*******************************************************************************************************************
231: *
232: * Returns the requested role or an empty {@link Optional}.
233: *
234: * @param <T> the static type
235: * @param type the type reference
236: * @return the roles
237: * @since 3.2-ALPHA-12
238: *
239: ******************************************************************************************************************/
240: @Nonnull
241: public default <T> Collection<T> asMany (@Nonnull final Type<? extends T> type)
242: {
243: return asMany(type.getType());
244: }
245: }