1 /*******************************************************************************
\r
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
\r
3 * in Industry THTH ry.
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.browsing.ui.common.modifiers;
\r
14 import java.util.Arrays;
\r
15 import java.util.List;
\r
18 * @author Tuukka Lehtonen
\r
22 public class Enumeration<T> {
\r
24 private final List<EnumeratedValue<T>> values;
\r
25 private final transient int hash;
\r
27 public static <T> Enumeration<T> make(List<EnumeratedValue<T>> values) {
\r
28 return new Enumeration<T>(values);
\r
31 public static <T> Enumeration<T> make(EnumeratedValue<T>... values) {
\r
32 return new Enumeration<T>(Arrays.asList(values));
\r
35 public Enumeration(List<EnumeratedValue<T>> values) {
\r
36 assert values != null;
\r
37 this.values = values;
\r
38 hash = values.hashCode();
\r
41 public List<EnumeratedValue<T>> values() {
\r
45 public boolean contains(EnumeratedValue<T> value) {
\r
46 return values.contains(value);
\r
49 public int indexOf(EnumeratedValue<T> value) {
\r
50 return values.indexOf(value);
\r
54 return values.size();
\r
59 * @return the enumerated value matching the specified object
\r
60 * @throws IllegalArgumentException if matching EnumeratedValue not found
\r
61 * @throws NullPointerException if null object is specified
\r
63 public EnumeratedValue<T> get(T object) {
\r
65 throw new NullPointerException("null object");
\r
66 for (EnumeratedValue<T> value : values)
\r
67 if (object.equals(value.getObject()))
\r
69 throw new IllegalArgumentException("object '" + object + "' not found among enumeration " + values);
\r
73 * Find an EnumeratedValue<T> matching the specified name.
\r
76 * @return <code>null</code> if corresponding EnumeratedValue not found
\r
78 public EnumeratedValue<T> findByName(String name) {
\r
81 for (EnumeratedValue<T> value : values)
\r
82 if (name.equals(value.getName()))
\r
88 * Find an EnumeratedValue<T> matching the specified name.
\r
91 * @return <code>null</code> if corresponding EnumeratedValue not found
\r
93 public EnumeratedValue<T> findByNameStartsWith(String name) {
\r
96 for (EnumeratedValue<T> value : values)
\r
97 if (value.getName().startsWith(name))
\r
103 * Find an EnumeratedValue<T> matching the specified object.
\r
106 * @return <code>null</code> if corresponding EnumeratedValue not found
\r
108 public EnumeratedValue<T> find(T object) {
\r
109 if (object == null)
\r
111 for (EnumeratedValue<T> value : values)
\r
112 if (object.equals(value.getObject()))
\r
118 public String toString() {
\r
119 return values.toString();
\r
123 public int hashCode() {
\r
128 public boolean equals(Object obj) {
\r
133 if (getClass() != obj.getClass())
\r
135 Enumeration<?> other = (Enumeration<?>) obj;
\r
136 return values.equals(other.values);
\r