View Javadoc

1   package com.atlassian.pageobjects.inject;
2   
3   import com.atlassian.annotations.ExperimentalApi;
4   import com.google.common.collect.Lists;
5   
6   import javax.annotation.Nonnull;
7   import java.util.List;
8   
9   import static com.google.common.base.Preconditions.checkNotNull;
10  
11  /**
12   * Abstract implementation of {@link com.atlassian.pageobjects.inject.InjectionConfiguration}.
13   *
14   * @since 2.1
15   */
16  @ExperimentalApi
17  public abstract class AbstractInjectionConfiguration implements InjectionConfiguration
18  {
19      protected final List<InterfaceToImpl> interfacesToImpls = Lists.newLinkedList();
20      protected final List<InterfaceToInstance> interfacesToInstances = Lists.newLinkedList();
21  
22      protected static class InterfaceToImpl
23      {
24          public final Class<?> interfaceType;
25          public final Class<?> implementation;
26  
27          private InterfaceToImpl(Class<?> interfaceType, Class<?> implementation)
28          {
29              this.interfaceType = checkNotNull(interfaceType, "interfaceType");
30              this.implementation = checkNotNull(implementation, "implementation");
31              if (!interfaceType.isAssignableFrom(implementation))
32              {
33                  throw new IllegalArgumentException("Implementation type " + implementation.getName() + " does not "
34                          + "implement " + interfaceType.getName());
35              }
36          }
37      }
38  
39      protected static class InterfaceToInstance
40      {
41          public final Class<?> interfaceType;
42          public final Object instance;
43  
44          private InterfaceToInstance(Class<?> interfaceType, Object instance)
45          {
46              this.interfaceType = checkNotNull(interfaceType, "interfaceType");
47              this.instance = checkNotNull(instance, "instance");
48              if (!interfaceType.isInstance(instance))
49              {
50                  throw new IllegalArgumentException("Object " + instance + " does not "
51                          + "implement " + interfaceType.getName());
52              }
53          }
54      }
55  
56      @Override
57      @Nonnull
58      public final <I> InjectionConfiguration addImplementation(@Nonnull Class<I> interfaceType, @Nonnull Class<? extends I> implementationType)
59      {
60          checkNotNull(interfaceType, "interfaceType");
61          checkNotNull(implementationType, "implementationType");
62          interfacesToImpls.add(new InterfaceToImpl(interfaceType, implementationType));
63          return this;
64      }
65  
66      @Override
67      @Nonnull
68      public final <C, I extends C> InjectionConfiguration addSingleton(@Nonnull Class<C> type, @Nonnull I instance)
69      {
70          checkNotNull(type, "type");
71          checkNotNull(instance, "instance");
72          interfacesToInstances.add(new InterfaceToInstance(type, instance));
73          return this;
74      }
75  
76  
77  }