View Javadoc

1   package org.apache.torque.oid;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License")
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import org.apache.torque.adapter.DB;
20  import org.apache.torque.adapter.IDMethod;
21  
22  /***
23   * A factory which instantiates {@link
24   * org.apache.torque.oid.IdGenerator} implementations.
25   *
26   * @author <a href="mailto:dlr@collab.net">Daniel Rall</a>
27   * @version $Id: IDGeneratorFactory.java 239630 2005-08-24 12:25:32Z henning $
28   */
29  public class IDGeneratorFactory
30  {
31      /***
32       * The list of ID generation method types which have associated
33       * {@link org.apache.torque.oid.IdGenerator} implementations.
34       */
35      public static final String[] ID_GENERATOR_METHODS =
36      {
37          IDMethod.NATIVE, IDMethod.AUTO_INCREMENT, IDMethod.SEQUENCE
38      };
39  
40      /***
41       * Factory method which instantiates {@link
42       * org.apache.torque.oid.IdGenerator} implementations based on the
43       * return value of the provided adapter's {@link
44       * org.apache.torque.adapter.DB#getIDMethodType()} method.
45       * Returns <code>null</code> for unknown types.
46       *
47       * @param dbAdapter The type of adapter to create an ID generator for.
48       * @return The appropriate ID generator (possibly <code>null</code>).
49       */
50      public static IdGenerator create(DB dbAdapter, String name)
51      {
52          String idMethod = dbAdapter.getIDMethodType();
53          if (IDMethod.AUTO_INCREMENT.equals(idMethod))
54          {
55              return new AutoIncrementIdGenerator(dbAdapter, name);
56          }
57          else if (IDMethod.SEQUENCE.equals(idMethod))
58          {
59              return new SequenceIdGenerator(dbAdapter, name);
60          }
61          else
62          {
63              return null;
64          }
65      }
66  }