View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.id;
18  
19  /**
20   * Abstract superclass for StringIdentifierGenerator implementations.
21   *
22   * @author Commons-Id team
23   * @version $Id: AbstractStringIdentifierGenerator.java 480488 2006-11-29 08:57:26Z bayard $
24   */
25  public abstract class AbstractStringIdentifierGenerator implements StringIdentifierGenerator {
26  
27      /**
28       * Maximum length for a numeric string representing a long value.
29       */
30      protected static final int MAX_LONG_NUMERIC_VALUE_LENGTH =
31      Long.toString(Long.MIN_VALUE).length();
32  
33      /**
34       * Number of alphanumeric characters.
35       */
36      protected static final int ALPHA_NUMERIC_CHARSET_SIZE = 36;
37  
38      /**
39       * Maximum length for an alphanumeric string representing a long value.
40       */
41      protected static final int MAX_LONG_ALPHANUMERIC_VALUE_LENGTH =
42      Long.toString(Long.MAX_VALUE, ALPHA_NUMERIC_CHARSET_SIZE).length();
43  
44      /**
45       * Maximum length for a numeric string representing an integer value.
46       */
47      protected static final int MAX_INT_NUMERIC_VALUE_LENGTH =
48      Integer.toString(Integer.MIN_VALUE).length();
49  
50      /**
51       * Maximum length for an alphanumeric string representing an integer value.
52       */
53      protected static final int MAX_INT_ALPHANUMERIC_VALUE_LENGTH =
54      Integer.toString(Integer.MAX_VALUE, ALPHA_NUMERIC_CHARSET_SIZE).length();
55  
56      /**
57       * Default size of an alphanumeric identifier.
58       */
59      protected static final int DEFAULT_ALPHANUMERIC_IDENTIFIER_SIZE = 15;
60  
61      /**
62       * Constructor.
63       */
64      protected AbstractStringIdentifierGenerator() {
65          super();
66      }
67  
68      public abstract String nextStringIdentifier();
69  
70      /**
71       * Returns the maximum length (number or characters) for an identifier
72       * from this sequence.
73       * 
74       * <p>The default implementation returns {@link #INFINITE_MAX_LENGTH}. Implementations
75       * with bounded length identifiers should override this method to
76       * return the maximum length of a generated identifier.</p>
77       *
78       * @return {@inheritDoc}
79       */
80      public long maxLength() {
81          return INFINITE_MAX_LENGTH;
82      }
83  
84      /**
85       * Returns the minimum length (number of characters) for an identifier
86       * from this sequence.
87       *
88       * <p>The default implementation returns 0. Implementations
89       * with identifiers having a postive minimum length should override this
90       * method to return the maximum length of a generated identifier.</p>
91       *
92       * @return {@inheritDoc}
93       */
94      public long minLength() {
95          return 0;
96      }
97  
98      public Object nextIdentifier() {
99          return nextStringIdentifier();
100     }
101 }