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 * <code>StringIdentifierGenerator</code> that always returns the same
21 * string. Use with {@link CompositeIdentifierGenerator} to embed constant
22 * string identifiers, or to add prefixes or suffixes.
23 * <p>
24 * Null constant values are not allowed. The default
25 * (assumed by the argumentless constructor) is an empty string.</p>
26 *
27 * @author Commons-Id team
28 * @version $Id$
29 */
30 public class ConstantIdentifierGenerator extends
31 AbstractStringIdentifierGenerator {
32
33 /**
34 * Constant string value always returned by this generator.
35 */
36 private final String identifier;
37
38 /**
39 * Factory method to create a new <code>ConstantIdentifierGenerator</code>
40 * with the given constant value. Does not allow null values.
41 *
42 * @param identifier constant value returned by the newly created generator.
43 * Must not be null.
44 * @return a new ConstantIdentifierGenerator
45 * @throws IllegalArgumentException if identifier is null
46 */
47 public static StringIdentifierGenerator getInstance(String identifier) {
48 return new ConstantIdentifierGenerator(identifier);
49 }
50
51 /**
52 * Creates a new ConstantIdentifierGenerator using the
53 * default (empty string) constant value.
54 *
55 */
56 public ConstantIdentifierGenerator() {
57 super();
58 this.identifier = "";
59 }
60
61 /**
62 * Creates a new ConstantIdentifierGenerator using the
63 * given constant value.
64 * <p>
65 * Null constant values are not allowed.</p>
66 *
67 * @param identifier constant value returned by the generator. Must not
68 * be null.
69 * @throws IllegalArgumentExeption if identifier is null
70 */
71 public ConstantIdentifierGenerator(String identifier) {
72 super();
73 if (identifier == null) {
74 throw new IllegalArgumentException
75 ("Constant identifier value must not be null");
76 }
77 this.identifier = identifier;
78 }
79
80 public String nextStringIdentifier() {
81 return identifier;
82 }
83
84 /**
85 * Returns the length of the constant string returned by this generator.
86 * If the constant is null or an empty string, 0 is returned.
87 *
88 * @return the length of the constant string returned by this generator
89 */
90 public long maxLength() {
91 if (identifier == null) {
92 return 0;
93 } else {
94 return identifier.length();
95 }
96 }
97
98 /**
99 * Returns the length of the constant string returned by this generator.
100 * If the constant is null or an empty string, 0 is returned.
101 *
102 * @return the length of the constant string returned by this generator
103 */
104 public long minLength() {
105 return maxLength();
106 }
107 }