1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.browsing.ui.common.modifiers;
14 import java.util.Arrays;
15 import java.util.List;
18 * @author Tuukka Lehtonen
22 public class Enumeration<T> {
24 private final List<EnumeratedValue<T>> values;
25 private final transient int hash;
27 public static <T> Enumeration<T> make(List<EnumeratedValue<T>> values) {
28 return new Enumeration<T>(values);
32 public static <T> Enumeration<T> make(EnumeratedValue<T>... values) {
33 return new Enumeration<T>(Arrays.asList(values));
36 public Enumeration(List<EnumeratedValue<T>> values) {
37 assert values != null;
39 hash = values.hashCode();
42 public List<EnumeratedValue<T>> values() {
46 public boolean contains(EnumeratedValue<T> value) {
47 return values.contains(value);
50 public int indexOf(EnumeratedValue<T> value) {
51 return values.indexOf(value);
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
64 public EnumeratedValue<T> get(T object) {
66 throw new NullPointerException("null object");
67 for (EnumeratedValue<T> value : values)
68 if (object.equals(value.getObject()))
70 throw new IllegalArgumentException("object '" + object + "' not found among enumeration " + values);
74 * Find an EnumeratedValue<T> matching the specified name.
77 * @return <code>null</code> if corresponding EnumeratedValue not found
79 public EnumeratedValue<T> findByName(String name) {
82 for (EnumeratedValue<T> value : values)
83 if (name.equals(value.getName()))
89 * Find an EnumeratedValue<T> matching the specified name.
92 * @return <code>null</code> if corresponding EnumeratedValue not found
94 public EnumeratedValue<T> findByNameStartsWith(String name) {
97 for (EnumeratedValue<T> value : values)
98 if (value.getName().startsWith(name))
104 * Find an EnumeratedValue<T> matching the specified object.
107 * @return <code>null</code> if corresponding EnumeratedValue not found
109 public EnumeratedValue<T> find(T object) {
112 for (EnumeratedValue<T> value : values)
113 if (object.equals(value.getObject()))
119 public String toString() {
120 return values.toString();
124 public int hashCode() {
129 public boolean equals(Object obj) {
134 if (getClass() != obj.getClass())
136 Enumeration<?> other = (Enumeration<?>) obj;
137 return values.equals(other.values);