View Javadoc
1   /*
2    * Copyright 2005 Ralf Joachim
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5    * in compliance with the License. You may obtain a copy of the License at
6    *
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software distributed under the License
10   * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11   * or implied. See the License for the specific language governing permissions and limitations under
12   * the License.
13   */
14  package org.castor.mapping;
15  
16  import java.io.Serializable;
17  import java.util.HashMap;
18  import java.util.Iterator;
19  import java.util.Map;
20  
21  /**
22   * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
23   * @version $Revision: 5951 $ $Date: 2006-04-25 16:09:10 -0600 (Tue, 25 Apr 2006) $
24   */
25  public final class BindingType implements Cloneable, Comparable<BindingType>, Serializable {
26    // ------------------------------------------------------------------------
27  
28    /** SerialVersionUID */
29    private static final long serialVersionUID = -2116844968191798202L;
30  
31    private static final Map<String, BindingType> TYPES = new HashMap<>();
32  
33    public static final BindingType JDO = new BindingType("jdo");
34    public static final BindingType XML = new BindingType("xml");
35  
36    private final String _type;
37  
38    // ------------------------------------------------------------------------
39  
40    private BindingType(final String kind) {
41      _type = kind;
42      TYPES.put(kind, this);
43    }
44  
45    public static BindingType valueOf(final String kind) {
46      return (BindingType) TYPES.get(kind);
47    }
48  
49    public static Iterator<BindingType> iterator() {
50      return TYPES.values().iterator();
51    }
52  
53    // ------------------------------------------------------------------------
54  
55    /**
56     * Returns the String representation of this kind.
57     *
58     * @return String representation of this kind.
59     */
60    public String toString() {
61      return _type;
62    }
63  
64    /**
65     * Clone only returns the one and only instance of this kind.
66     *
67     * @return The original instance.
68     */
69    public Object clone() {
70      return this;
71    }
72  
73    /**
74     * Returns if the specified object and this are one and the same instance.
75     *
76     * @param other Object to be compared with this instance.
77     * @return <code>true</code> if other equals this else <code>false</code>.
78     */
79    public boolean equals(final Object other) {
80      return (this == other);
81    }
82  
83    /**
84     * Returns the hash code of this object.
85     *
86     * @return Hash code of this object.
87     */
88    public int hashCode() {
89      return _type.hashCode();
90    }
91  
92    /**
93     * Compares {@link #_type} against {@link #_type} of the specified object. So this method is
94     * inconsistent with {@link #equals}.
95     *
96     * @param other Object to be compared with this instance.
97     * @return A negative integer, zero, or a positive integer as this object is less than, equal to,
98     *         or greater than the specified object.
99     */
100   public int compareTo(final BindingType other) {
101     return _type.compareTo(other._type);
102   }
103 
104   /**
105    * Called during deserialization.
106    *
107    * @return The existing instance of the enum. <br>
108    *         So you can use '==' like 'equals' even if you use a deserialized Enum.
109    */
110   protected Object readResolve() {
111     return TYPES.get(_type);
112   }
113 
114   // ------------------------------------------------------------------------
115 }