View Javadoc

1   /*
2    * Copyright 2006 Markku Saarela 
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at 
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, 
11   * software distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13   * either express or implied. 
14   * See the License for the specific language governing permissions and limitations under the License. 
15   */
16  package net.sourceforge.jivalo.editor;
17  
18  import java.io.ObjectStreamException;
19  import java.io.Serializable;
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.List;
23  
24  /**
25   * @author <a href="mailto:ivalo@iki.fi">Markku Saarela</a>
26   *
27   */
28  public final class TabStyle  implements Serializable
29  {
30  
31      private static final long serialVersionUID = 1L;
32  
33      /**
34       * Spaces Tab style.
35       */
36      public static final TabStyle SPACES = new TabStyle( "spaces" );
37  
38      /**
39       * Tabs Tab style.
40       */
41      public static final TabStyle TABS = new TabStyle( "tabs" );
42  
43      private static final TabStyle[] PRIVATE_VALUES = { SPACES, TABS };
44  
45      private static final List VALUES = Collections.unmodifiableList( Arrays
46              .asList( PRIVATE_VALUES ) );
47  
48      // next index to assign to an enumerator
49      private static int nextIndex = 0;
50  
51      // index for this enumerator
52      private final int index = nextIndex++;
53  
54      private final String style;
55  
56      private TabStyle( String style )
57      {
58          this.style = style;
59      }
60  
61      public String toString()
62      {
63          return style;
64      }
65  
66      public boolean equals( Object o )
67      {
68  
69          if ( !( o instanceof TabStyle ) )
70              return false;
71  
72          TabStyle s = ( TabStyle ) o;
73  
74          return this.style.equals( s.style );
75      }
76  
77      // Lazily initialize, cached hashCode
78      private volatile int hashCode = 0;
79  
80      public int hashCode()
81      {
82  
83          if ( hashCode == 0 )
84          {
85              int result = 17;
86              result = 37 * result + this.style.hashCode();
87              hashCode = result;
88          }
89          return hashCode;
90      }
91  
92      /**
93       * @throws CloneNotSupportedException.
94       *             This guarantees that enums are never cloned, which is
95       *             necessary to preserve their "singleton" status.
96       */
97      protected Object clone() throws CloneNotSupportedException
98      {
99  
100         throw new CloneNotSupportedException();
101     }
102 
103     // return alternative object
104     // as result of deserialization
105     private Object readResolve() throws ObjectStreamException
106     {
107         return PRIVATE_VALUES[index];
108     }
109 
110     /**
111      * 
112      * @param value
113      * @return
114      * @throws NullPointerException
115      *             if value is null
116      * @throws IllegalArgumentException
117      *             if TabStyle has no constant with the specified value.
118      */
119     public static TabStyle valueOf( String value )
120     {
121 
122         if ( value == null )
123         {
124             throw new NullPointerException( "value" );
125         }
126         TabStyle o = null;
127 
128         for ( int i = 0; i < PRIVATE_VALUES.length; i++ )
129         {
130             if ( PRIVATE_VALUES[i].style.equalsIgnoreCase( value.trim() ) )
131             {
132                 o = PRIVATE_VALUES[i];
133                 break;
134             }
135         }
136 
137         if ( o == null )
138         {
139             throw new IllegalArgumentException( value
140                     + " is not valid value of TabStyle." );
141         }
142         return o;
143     }
144 
145     public static List values()
146     {
147 
148         return VALUES;
149     }
150 
151 }