View Javadoc
1   package com.atlassian.activeobjects.internal;
2   
3   import com.atlassian.activeobjects.config.ActiveObjectsConfiguration;
4   import com.atlassian.activeobjects.external.ActiveObjects;
5   import com.atlassian.tenancy.api.Tenant;
6   import com.google.common.collect.ImmutableSet;
7   
8   import java.util.Collection;
9   
10  /**
11   * A delegating factory that will check multiple factories to achieve its goal.
12   */
13  public final class DelegatingActiveObjectsFactory implements ActiveObjectsFactory {
14      private final ImmutableSet<ActiveObjectsFactory> factories;
15  
16      public DelegatingActiveObjectsFactory(Collection<ActiveObjectsFactory> factories) {
17          this.factories = ImmutableSet.<ActiveObjectsFactory>builder().addAll(factories).build();
18      }
19  
20      public boolean accept(ActiveObjectsConfiguration configuration) {
21          for (ActiveObjectsFactory factory : factories) {
22              if (factory.accept(configuration)) {
23                  return true;
24              }
25          }
26          return false;
27      }
28  
29      public ActiveObjects create(ActiveObjectsConfiguration configuration, Tenant tenant) {
30          for (ActiveObjectsFactory factory : factories) {
31              if (factory.accept(configuration)) {
32                  return factory.create(configuration, tenant);
33              }
34          }
35          throw new IllegalStateException("Could not find a factory for this configuration, " + configuration + ", " +
36                  "did you call #accept(ActiveObjectsConfiguration) before calling me?");
37      }
38  }