001: /***
002: * Retrotranslator: a Java bytecode transformer that translates Java classes
003: * compiled with JDK 5.0 into classes that can be run on JVM 1.4.
004: *
005: * Copyright (c) 2005 - 2008 Taras Puchko
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: * 3. Neither the name of the copyright holders nor the names of its
017: * contributors may be used to endorse or promote products derived from
018: * this software without specific prior written permission.
019: *
020: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
021: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
022: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
023: * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
024: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
025: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
026: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
027: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
028: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
029: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
030: * THE POSSIBILITY OF SUCH DAMAGE.
031: */package net.sf.retrotranslator.runtime.java.util;
032:
033: import java.io.Serializable;
034: import java.util.*;
035:
036: /**
037: * @author Taras Puchko
038: */
039: public class EnumMap_<K extends Enum<K>, V> extends TreeMap<K, V> {
040:
041: private static final long serialVersionUID = 3267103726949236459L;
042:
043: private Class<K> keyType;
044:
045: public EnumMap_(Class<K> keyType) {
046: super (new EnumComparator());
047: if (!Enum.class.isAssignableFrom(keyType)) {
048: throw new NullPointerException();
049: }
050: this .keyType = keyType;
051: }
052:
053: public EnumMap_(EnumMap_<K, ? extends V> map) {
054: super (map);
055: this .keyType = map.keyType;
056: }
057:
058: public EnumMap_(Map<K, ? extends V> map) {
059: this (getKeyType(map));
060: putAll(map);
061: }
062:
063: public V put(K key, V value) {
064: Class<? extends Enum> aClass = key.getClass();
065: if (aClass != keyType && aClass.getSuperclass() != keyType) {
066: throw new ClassCastException(aClass.getName());
067: }
068: return super .put(key, value);
069: }
070:
071: public void putAll(Map<? extends K, ? extends V> map) {
072: for (Map.Entry<? extends K, ? extends V> entry : map.entrySet()) {
073: put(entry.getKey(), entry.getValue());
074: }
075: }
076:
077: public EnumMap_<K, V> clone() {
078: return (EnumMap_<K, V>) super .clone();
079: }
080:
081: private static <K extends Enum<K>, V> Class<K> getKeyType(
082: Map<K, ? extends V> map) {
083: if (map instanceof EnumMap_) {
084: return ((EnumMap_<K, ? extends V>) map).keyType;
085: }
086: if (map.isEmpty()) {
087: throw new IllegalArgumentException();
088: }
089: return map.keySet().iterator().next().getDeclaringClass();
090: }
091:
092: private static class EnumComparator implements Comparator,
093: Serializable {
094: public int compare(Object o1, Object o2) {
095: if (o1 instanceof Enum) {
096: return o2 instanceof Enum ? ((Enum) o1).ordinal()
097: - ((Enum) o2).ordinal() : 1;
098: } else {
099: return o2 instanceof Enum ? -1 : 0;
100: }
101: }
102: }
103:
104: }
|