]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/modifiers/Enumeration.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / modifiers / Enumeration.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.browsing.ui.common.modifiers;
13
14 import java.util.Arrays;
15 import java.util.List;
16
17 /**
18  * @author Tuukka Lehtonen
19  * 
20  * @param <T>
21  */
22 public class Enumeration<T> {
23
24     private final List<EnumeratedValue<T>> values;
25     private final transient int hash;
26
27     public static <T> Enumeration<T> make(List<EnumeratedValue<T>> values) {
28         return new Enumeration<T>(values);
29     }
30
31     @SafeVarargs
32     public static <T> Enumeration<T> make(EnumeratedValue<T>... values) {
33         return new Enumeration<T>(Arrays.asList(values));
34     }
35
36     public Enumeration(List<EnumeratedValue<T>> values) {
37         assert values != null;
38         this.values = values;
39         hash = values.hashCode();
40     }
41
42     public List<EnumeratedValue<T>> values() {
43         return values;
44     }
45
46     public boolean contains(EnumeratedValue<T> value) {
47         return values.contains(value);
48     }
49
50     public int indexOf(EnumeratedValue<T> value) {
51         return values.indexOf(value);
52     }
53
54     public int size() {
55         return values.size();
56     }
57
58     /**
59      * @param object
60      * @return the enumerated value matching the specified object
61      * @throws IllegalArgumentException if matching EnumeratedValue not found
62      * @throws NullPointerException if null object is specified
63      */
64     public EnumeratedValue<T> get(T object) {
65         if (object == null)
66             throw new NullPointerException("null object");
67         for (EnumeratedValue<T> value : values)
68             if (object.equals(value.getObject()))
69                 return value;
70         throw new IllegalArgumentException("object '" + object + "' not found among enumeration " + values);
71     }
72
73     /**
74      * Find an EnumeratedValue<T> matching the specified name.
75      * 
76      * @param name
77      * @return <code>null</code> if corresponding EnumeratedValue not found
78      */
79     public EnumeratedValue<T> findByName(String name) {
80         if (name == null)
81             return null;
82         for (EnumeratedValue<T> value : values)
83             if (name.equals(value.getName()))
84                 return value;
85         return null;
86     }
87
88     /**
89      * Find an EnumeratedValue<T> matching the specified name.
90      * 
91      * @param name
92      * @return <code>null</code> if corresponding EnumeratedValue not found
93      */
94     public EnumeratedValue<T> findByNameStartsWith(String name) {
95         if (name == null)
96             return null;
97         for (EnumeratedValue<T> value : values)
98             if (value.getName().startsWith(name))
99                 return value;
100         return null;
101     }
102
103     /**
104      * Find an EnumeratedValue<T> matching the specified object.
105      * 
106      * @param object
107      * @return <code>null</code> if corresponding EnumeratedValue not found
108      */
109     public EnumeratedValue<T> find(T object) {
110         if (object == null)
111             return null;
112         for (EnumeratedValue<T> value : values)
113             if (object.equals(value.getObject()))
114                 return value;
115         return null;
116     }
117
118     @Override
119     public String toString() {
120         return values.toString();
121     }
122
123     @Override
124     public int hashCode() {
125         return hash;
126     }
127
128     @Override
129     public boolean equals(Object obj) {
130         if (this == obj)
131             return true;
132         if (obj == null)
133             return false;
134         if (getClass() != obj.getClass())
135             return false;
136         Enumeration<?> other = (Enumeration<?>) obj;
137         return values.equals(other.values);
138     }
139
140 }