/******************************************************************************* * Copyright (c) 2007, 2010 Association for Decentralized Information Management * in Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.browsing.ui.common.modifiers; import java.util.Arrays; import java.util.List; /** * @author Tuukka Lehtonen * * @param */ public class Enumeration { private final List> values; private final transient int hash; public static Enumeration make(List> values) { return new Enumeration(values); } @SafeVarargs public static Enumeration make(EnumeratedValue... values) { return new Enumeration(Arrays.asList(values)); } public Enumeration(List> values) { assert values != null; this.values = values; hash = values.hashCode(); } public List> values() { return values; } public boolean contains(EnumeratedValue value) { return values.contains(value); } public int indexOf(EnumeratedValue value) { return values.indexOf(value); } public int size() { return values.size(); } /** * @param object * @return the enumerated value matching the specified object * @throws IllegalArgumentException if matching EnumeratedValue not found * @throws NullPointerException if null object is specified */ public EnumeratedValue get(T object) { if (object == null) throw new NullPointerException("null object"); for (EnumeratedValue value : values) if (object.equals(value.getObject())) return value; throw new IllegalArgumentException("object '" + object + "' not found among enumeration " + values); } /** * Find an EnumeratedValue matching the specified name. * * @param name * @return null if corresponding EnumeratedValue not found */ public EnumeratedValue findByName(String name) { if (name == null) return null; for (EnumeratedValue value : values) if (name.equals(value.getName())) return value; return null; } /** * Find an EnumeratedValue matching the specified name. * * @param name * @return null if corresponding EnumeratedValue not found */ public EnumeratedValue findByNameStartsWith(String name) { if (name == null) return null; for (EnumeratedValue value : values) if (value.getName().startsWith(name)) return value; return null; } /** * Find an EnumeratedValue matching the specified object. * * @param object * @return null if corresponding EnumeratedValue not found */ public EnumeratedValue find(T object) { if (object == null) return null; for (EnumeratedValue value : values) if (object.equals(value.getObject())) return value; return null; } @Override public String toString() { return values.toString(); } @Override public int hashCode() { return hash; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Enumeration other = (Enumeration) obj; return values.equals(other.values); } }