Skip to contentPackage: AsDelegateProvider$Locator
AsDelegateProvider$Locator
Coverage
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 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.spi;
28:
29: import javax.annotation.Nonnull;
30: import java.util.ArrayList;
31: import java.util.Collection;
32: import java.util.Iterator;
33: import java.util.ServiceLoader;
34: import it.tidalwave.util.LazySupplier;
35: import lombok.AccessLevel;
36: import lombok.NoArgsConstructor;
37:
38: /***********************************************************************************************************************
39: *
40: * @author Fabrizio Giudici
41: *
42: **********************************************************************************************************************/
43: public interface AsDelegateProvider
44: {
45: public static final LazySupplier<EmptyAsDelegateProvider> EMPTY_REF =
46: LazySupplier.of(EmptyAsDelegateProvider::new);
47:
48: @NoArgsConstructor(access = AccessLevel.PRIVATE)
49: public static final class Locator
50: {
51: private static final LazySupplier<AsDelegateProvider> AS_DELEGATE_PROVIDER_REF =
52: LazySupplier.of(Locator::findAsSpiProvider);
53:
54: @Nonnull
55: public static AsDelegateProvider find()
56: {
57: return AS_DELEGATE_PROVIDER_REF.get();
58: }
59:
60: @Nonnull
61: private static AsDelegateProvider findAsSpiProvider()
62: {
63: final ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
64: final Iterator<AsDelegateProvider> i =
65: ServiceLoader.load(AsDelegateProvider.class, classLoader).iterator();
66:
67:• if (!i.hasNext())
68: {
69: final String message = "No ServiceProvider for AsDelegateProvider found in a ServiceLoader - if " +
70: "you are running tests perhaps you should first call " +
71: "AsDelegateProvider.Locator.set(AsDelegateProvider.empty()) or " +
72: "AsDelegateProvider.Locator.set(new MockSimpleAsDelegateProvider()) or " +
73: "another appropriate AsDelegateProvider";
74: throw new RuntimeException(message);
75: }
76:
77: return i.next();
78: }
79:
80: /***************************************************************************************************************
81: *
82: * <b>This method is for testing only.</b> Sets the global {@link AsDelegateProvider}.
83: *
84: * @param provider the provider
85: * @see #reset()
86: *
87: **************************************************************************************************************/
88: public static void set (@Nonnull final AsDelegateProvider provider)
89: {
90: AS_DELEGATE_PROVIDER_REF.set(provider);
91: }
92:
93: /***************************************************************************************************************
94: *
95: * <b>This method is for testing only.</b> Resets the global {@link AsDelegateProvider}; it must be called at
96: * the test completion whenever {@link #set(AsDelegateProvider)} has been called, to avoid polluting the
97: * context of further tests.
98: *
99: * @see #set(AsDelegateProvider)
100: *
101: **************************************************************************************************************/
102: public static void reset()
103: {
104: AS_DELEGATE_PROVIDER_REF.clear();
105: }
106: }
107:
108: /*******************************************************************************************************************
109: *
110: * Creates an {@link AsDelegate} for the given object
111: *
112: * @param datum the object
113: * @return {@code AsDelegate}
114: *
115: ******************************************************************************************************************/
116: @Nonnull
117: public AsDelegate createAsDelegate (@Nonnull Object datum);
118:
119: /*******************************************************************************************************************
120: *
121: * Returns an empty implementation. Useful for setting up test environment.
122: *
123: * <pre>
124: * AsDelegateProvider.Locator.set(AsDelegateProvider.empty());
125: * </pre>
126: *
127: * @return the empty implementation
128: * @since 3.2-ALPHA-1
129: *
130: ******************************************************************************************************************/
131: @Nonnull
132: public static AsDelegateProvider empty()
133: {
134: return EMPTY_REF.get();
135: }
136:
137: /*******************************************************************************************************************
138: *
139: ******************************************************************************************************************/
140: static class EmptyAsDelegateProvider implements AsDelegateProvider
141: {
142: @Override @Nonnull
143: public AsDelegate createAsDelegate (@Nonnull final Object owner)
144: {
145: return new AsDelegate()
146: {
147: @Override @Nonnull
148: public <T> Collection<T> as (@Nonnull final Class<T> type)
149: {
150: return new ArrayList<>(); // must be mutable
151: }
152: };
153: }
154: }
155: }