1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
49 private static int nextIndex = 0;
50
51
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
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
104
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 }