View Javadoc

1   package org.apache.torque.oid;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.ArrayList;
23  import java.util.Collections;
24  import java.util.List;
25  
26  import org.apache.torque.adapter.Adapter;
27  import org.apache.torque.adapter.IDMethod;
28  
29  /**
30   * A factory which instantiates {@link
31   * org.apache.torque.oid.IdGenerator} implementations.
32   *
33   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
34   * @version $Id: IDGeneratorFactory.java 1375892 2012-08-22 04:11:33Z tfischer $
35   */
36  public final class IDGeneratorFactory
37  {
38      /**
39       * The list of ID generation method types which have associated
40       * {@link org.apache.torque.oid.IdGenerator} implementations.
41       */
42      public static final List<IDMethod> ID_GENERATOR_METHODS;
43  
44      static
45      {
46          List<IDMethod> idGeneratorMethods = new ArrayList<IDMethod>();
47          idGeneratorMethods.add(IDMethod.NATIVE);
48          idGeneratorMethods.add(IDMethod.AUTO_INCREMENT);
49          idGeneratorMethods.add(IDMethod.SEQUENCE);
50          ID_GENERATOR_METHODS = Collections.unmodifiableList(idGeneratorMethods);
51      }
52  
53      /**
54       * Private constructor to prevent initialisation.
55       *
56       * This class contains only static methods and thus should not be
57       * instantiated.
58       */
59      private IDGeneratorFactory()
60      {
61          // empty
62      }
63  
64      /**
65       * Factory method which instantiates {@link
66       * org.apache.torque.oid.IdGenerator} implementations based on the
67       * return value of the provided adapter's {@link
68       * org.apache.torque.adapter.Adapter#getIDMethodType()} method.
69       * Returns <code>null</code> for unknown types.
70       *
71       * @param adapter The type of adapter to create an ID generator for.
72       *
73       * @return The appropriate ID generator (possibly <code>null</code>).
74       */
75      public static IdGenerator create(Adapter adapter, String name)
76      {
77          IDMethod idMethod = adapter.getIDMethodType();
78          if (IDMethod.AUTO_INCREMENT == idMethod)
79          {
80              return new AutoIncrementIdGenerator(adapter, name);
81          }
82          else if (IDMethod.SEQUENCE == idMethod)
83          {
84              return new SequenceIdGenerator(adapter, name);
85          }
86          else
87          {
88              return null;
89          }
90      }
91  }