001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.util.lang; 018 019import java.util.ArrayList; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.wicket.util.string.StringValue; 024 025 026/** 027 * A base class for defining enumerated types. Since this class extends StringValue, every 028 * enumerated type subclass is a StringValue that can be manipulated, converted and displayed in 029 * useful ways. In addition to constructing a type with the given name, lists are kept of all 030 * enumeration values by subclass. The list of available values in the enumeration represented by a 031 * given subclass can be retrieved by calling getValues(Class). 032 * 033 * @author Jonathan Locke 034 */ 035public abstract class EnumeratedType extends StringValue 036{ 037 /** 038 * 039 */ 040 private static final long serialVersionUID = 1L; 041 /** Map of type values by class */ 042 private static final Map<String, List<EnumeratedType>> valueListByClass = Generics.newConcurrentHashMap(); 043 044 /** 045 * Constructor. 046 * 047 * @param name 048 * Name of this enumerated type value 049 */ 050 public EnumeratedType(final String name) 051 { 052 super(name); 053 054 // Add this object to the list of values for our subclass 055 getValues(getClass()).add(this); 056 } 057 058 /** 059 * Gets the enumerated type values for a given subclass of EnumeratedType. 060 * 061 * @param c 062 * The enumerated type subclass to get values for 063 * @return List of all values of the given subclass 064 */ 065 public static List<EnumeratedType> getValues(final Class<? extends EnumeratedType> c) 066 { 067 // Get values for class 068 List<EnumeratedType> valueList = valueListByClass.get(c.getName()); 069 070 // If no list exists 071 if (valueList == null) 072 { 073 // create lazily 074 valueList = new ArrayList<>(); 075 valueListByClass.put(c.getName(), valueList); 076 } 077 078 return valueList; 079 } 080 081 /** 082 * Method to ensure that == works after deserialization 083 * 084 * @return object instance 085 * @throws java.io.ObjectStreamException 086 */ 087 public Object readResolve() throws java.io.ObjectStreamException 088 { 089 EnumeratedType result = this; 090 List<EnumeratedType> values = getValues(getClass()); 091 for (EnumeratedType value : values) 092 { 093 if ((value.toString() != null) && value.toString().equals(this.toString())) 094 { 095 result = value; 096 break; 097 } 098 } 099 return result; 100 } 101}