Skip to contentPackage: As$Defaults
As$Defaults
Coverage
1: /*
2: * #%L
3: * *********************************************************************************************************************
4: *
5: * These Foolish Things - Miscellaneous utilities
6: * http://thesefoolishthings.tidalwave.it - git clone git@bitbucket.org:tidalwave/thesefoolishthings-src.git
7: * %%
8: * Copyright (C) 2009 - 2018 Tidalwave s.a.s. (http://tidalwave.it)
9: * %%
10: * *********************************************************************************************************************
11: *
12: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
13: * the License. You may obtain a copy of the License at
14: *
15: * http://www.apache.org/licenses/LICENSE-2.0
16: *
17: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
18: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
19: * specific language governing permissions and limitations under the License.
20: *
21: * *********************************************************************************************************************
22: *
23: * $Id$
24: *
25: * *********************************************************************************************************************
26: * #L%
27: */
28: package it.tidalwave.util;
29:
30: import javax.annotation.Nonnull;
31: import javax.annotation.Nullable;
32: import java.util.Collection;
33:
34: /***********************************************************************************************************************
35: *
36: * Objects implementing this interface can provide am adapter of the required type. The adapter can be found with a
37: * variety of approaches that depend on the implementation. This capability can be used to implement a design based
38: * on the Data, Context and Interaction pattern (DCI).
39: *
40: * @author Fabrizio Giudici
41: * @version $Id$
42: * @it.tidalwave.javadoc.stable
43: *
44: **********************************************************************************************************************/
45: public interface As
46: {
47: /*******************************************************************************************************************
48: *
49: * @it.tidalwave.javadoc.stable
50: *
51: ******************************************************************************************************************/
52: public static interface NotFoundBehaviour<T>
53: {
54: @Nonnull
55: public T run (@Nullable final Throwable t);
56: }
57:
58: /*******************************************************************************************************************
59: *
60: *
61: ******************************************************************************************************************/
62: public final static class Defaults
63: {
64: private Defaults()
65: {
66: }
67:
68: public static <X> NotFoundBehaviour<X> throwAsException (final @Nonnull Class<X> clazz)
69: {
70: return new NotFoundBehaviour<X>()
71: {
72: // @Override
73: @Nonnull
74: public X run (final @Nonnull Throwable t)
75: {
76: throw new AsException(clazz, t);
77: }
78: };
79: }
80: }
81:
82: /*******************************************************************************************************************
83: *
84: * Returns an adapter to this object of the specified type. If the implementation can find multiple compliant
85: * adapters, only one will be returned.
86: *
87: * @paramm type the type
88: * @return the adapter
89: * @throws AsException if no adapter is found
90: *
91: ******************************************************************************************************************/
92: @Nonnull
93: public <T> T as (@Nonnull Class<T> type);
94:
95: /*******************************************************************************************************************
96: *
97: * Returns an adapter to this object of the specified type. If the implementation can find multiple compliant
98: * adapters, only one will be returned. If no adapter is found, the result provided by the given default
99: * behaviour will be returned.
100: *
101: * @paramm type the type
102: * @param notFoundBehaviour the behaviour to apply when an adapter is not found
103: *
104: ******************************************************************************************************************/
105: @Nonnull
106: public <T> T as (@Nonnull Class<T> type, @Nonnull NotFoundBehaviour<T> notFoundBehaviour);
107:
108: /*******************************************************************************************************************
109: *
110: * Searches for multiple adapters of the given type and returns them.
111: *
112: * @param type the adapter type
113: * @return a collection of adapters, possibly empty
114: *
115: ******************************************************************************************************************/
116: @Nonnull
117: public <T> Collection<T> asMany (@Nonnull Class<T> type);
118: }