View Javadoc

1   /* Copyright (c) 2008 Sascha Kohlmann
2    *
3    * This program is free software: you can redistribute it and/or modify
4    * it under the terms of the GNU Affero General Public License as published by
5    * the Free Software Foundation, either version 3 of the License, or
6    * (at your option) any later version.
7    *
8    * This program is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11   * GNU Affero General Public License for more details.
12   *
13   * You should have received a copy of the GNU Affero General Public License
14   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15   */
16  package net.sf.eos.util;
17  
18  import net.sf.eos.Supplier;
19  import net.sf.eos.config.ConfigurableSupplier;
20  import net.sf.eos.config.Configuration;
21  
22  import static net.sf.eos.util.Conditions.checkArgumentNotNull;
23  
24  public final class SupplierFactory<T> {
25  
26      /**
27       * Creates a new instance of the {@link Supplier} class. The
28       * {@code supplier} must support a default Constructor.
29       * @param <T> the type of the {@code supplier}
30       * @param clazz the class instance of the {@code supplier}
31       * @return the {@code supplier}
32       * @throw IllegalArgumentException if it is not possible to create
33       *                                 a new {@code supplier}
34       */
35      public static final <T> Supplier<T>
36                  newSupplier(final Class<? extends Supplier<T>> clazz) {
37          checkArgumentNotNull(clazz, "clazz is null");
38          try {
39              final Supplier<T> p = clazz.newInstance();
40              return p;
41          } catch (final InstantiationException e) {
42              throw new IllegalArgumentException(e);
43          } catch (final IllegalAccessException e) {
44              throw new IllegalArgumentException(e);
45          }
46      }
47  
48      /**
49       * Creates a new instance of the {@link ConfigurableSupplier} class.
50       * The {@code supplier} must support a default Constructor.
51       * @param <T> the type of the {@code supplier}
52       * @param clazz the class instance of the {@code supplier}
53       * @return the {@code supplier}
54       * @throw IllegalArgumentException if it is not possible to create
55       *                                 a new {@code supplier}
56       */
57      public static final <T> ConfigurableSupplier<T>
58              newSupplier(final Class<? extends ConfigurableSupplier<T>> clazz,
59                          final Configuration config) {
60          checkArgumentNotNull(config, "configuration is null");
61          final ConfigurableSupplier<T> cp =
62               (ConfigurableSupplier<T>) newSupplier(clazz);
63          cp.configure(config);
64          return cp;
65      }
66  
67      /**
68       * Creates a new instance of the {@link Supplier} class. The
69       * {@code supplier} must support a default Constructor.
70       * @param <T> the type of the {@code supplier}
71       * @param configName the name of the key that value in {@code config}
72       *                   is the name of a supplier class
73       * @param config the configuration containing the class name
74       * @return the {@code supplier}
75       * @throw IllegalArgumentException if it is impossible to create
76       *                                 a new {@code supplier}
77       */
78      public static final <T> Supplier<T> newSupplier(
79                   final String configName,
80                   final Configuration config) {
81          checkArgumentNotNull(configName, "config name is null");
82          checkArgumentNotNull(config, "configuration is null");
83          final String clazzName = config.get(configName);
84          return newSupplier(configName, config, clazzName);
85      }
86  
87      /**
88       * Creates a new instance of the {@link Supplier} class. The
89       * {@code supplier} must support a default Constructor.
90       * @param <T> the type of the {@code supplier}
91       * @param configName the name of the key that value in {@code config}
92       *                   is the name of a supplier class
93       * @param config the configuration containing the class name
94       * @param defaultClazzName the default supplier class name
95       * @return the {@code supplier}
96       * @throw IllegalArgumentException if it is impossible to create
97       *                                 a new {@code supplier}
98       */
99      public static final <T> Supplier<T> newSupplier(
100                 final String configName,
101                 final Configuration config,
102                 final String defaultClazzName) {
103         checkArgumentNotNull(configName, "config name is null");
104         checkArgumentNotNull(config, "configuration is null");
105         final String clazzName = config.get(configName, defaultClazzName);
106         try {
107             final Class<Supplier<T>> clazz =
108                 (Class<Supplier<T>>) Class.forName(clazzName);
109 
110             final Supplier<T> p = (Supplier<T>) newSupplier(clazz);
111             if (p instanceof ConfigurableSupplier) {
112                 final ConfigurableSupplier<T> cp =
113                     (ConfigurableSupplier<T>) p;
114                 cp.configure(config);
115             }
116             return p;
117         } catch (final ClassNotFoundException e) {
118             throw new IllegalArgumentException(e);
119         }
120     }
121 }