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.Comparator;
17 * @author Tuukka Lehtonen
21 public class EnumeratedValue<T> implements Comparable<EnumeratedValue<T>>{
23 private static class InvalidEnumeratedValue<T> extends EnumeratedValue<T> {
24 public InvalidEnumeratedValue(String name) {
29 public static final <T> EnumeratedValue<T> invalid(String name) {
30 return new InvalidEnumeratedValue<T>(name);
33 public static final boolean isInvalid(EnumeratedValue<?> value) {
34 return value instanceof InvalidEnumeratedValue<?>;
37 public static final Comparator<EnumeratedValue<? extends Object>> CASE_INSENSITIVE_NAME_ORDER = new Comparator<EnumeratedValue<? extends Object>>() {
39 public int compare(EnumeratedValue<? extends Object> o1, EnumeratedValue<? extends Object> o2) {
40 return o1.getName().compareToIgnoreCase(o2.getName());
44 private final String name;
46 private final T object;
48 public static EnumeratedValue<String> make(String name) {
49 return new EnumeratedValue<String>(name, name);
52 public static <T> EnumeratedValue<T> make(String name, T object) {
53 return new EnumeratedValue<T>(name, object);
56 public EnumeratedValue(String name, T object) {
58 throw new NullPointerException("null enumerated value name");
64 * @return the name of the enumerated value
66 public String getName() {
71 * @return the object bound to this enumerated value, may be
74 public T getObject() {
79 public String toString() {
80 return name + "[" + object + "]";
84 public int compareTo(EnumeratedValue<T> o) {
85 return getName().compareTo(o.getName());
89 public int hashCode() {
92 result = prime * result + name.hashCode();
93 result = prime * result + ((object == null) ? 0 : object.hashCode());
98 public boolean equals(Object obj) {
103 if (getClass() != obj.getClass())
105 EnumeratedValue<?> other = (EnumeratedValue<?>) obj;
106 if (!name.equals(other.name))
108 if (object == null) {
109 if (other.object != null)
111 } else if (!object.equals(other.object))