1 /*
2 * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.opensaml.xml.util;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23 * An abstract Template design pattern implementation of {@link SingletonFactory}.
24 *
25 * @param <Input> the factory input class type
26 * @param <Output> the factory output class type
27 */
28 public abstract class AbstractSingletonFactory<Input, Output> implements SingletonFactory<Input, Output> {
29
30 /** Class logger. */
31 private final Logger log = LoggerFactory.getLogger(AbstractSingletonFactory.class);
32
33 /** {@inheritDoc} */
34 public synchronized Output getInstance(Input input) {
35 Output output = get(input);
36 if (output != null) {
37 log.trace("Input key mapped to a non-null value, returning output");
38 return output;
39 } else {
40 log.trace("Input key mapped to a null value");
41 }
42
43 log.trace("Creating new output instance and inserting to factory map");
44 output = createNewInstance(input);
45 if (output == null) {
46 log.error("New output instance was not created");
47 return null;
48 }
49
50 put(input, output);
51
52 return output;
53 }
54
55 /**
56 * Get the output instance currently associated with
57 * the input instance.
58 *
59 * @param input the input instance key
60 * @return the output instance which corresponds to the input instance,
61 * or null if not present
62 */
63 protected abstract Output get(Input input);
64
65 /**
66 * Store the input and output instance association.
67 *
68 * @param input the input instance key
69 * @param output the output instance value
70 */
71 protected abstract void put(Input input, Output output);
72
73 /**
74 * Create a new instance of the output class based on the input
75 * class instance.
76 *
77 * @param input the input class instance
78 * @return an output class instance
79 */
80 protected abstract Output createNewInstance(Input input);
81
82 }