PageRenderTime 26ms CodeModel.GetById 17ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/interpreter/tags/at2dist170907/src/edu/vub/util/IdentityHashMap.java

http://ambienttalk.googlecode.com/
Java | 155 lines | 94 code | 25 blank | 36 comment | 1 complexity | 941088b575496751fd6d0f7dfc274d28 MD5 | raw file
  1/**
  2 * Copyright (c) 2003-2004, Joe Walnes
  3 * All rights reserved.
  4 *
  5 * Redistribution and use in source and binary forms, with or without
  6 * modification, are permitted provided that the following conditions are met:
  7 *
  8 * Redistributions of source code must retain the above copyright notice, this list of
  9 * conditions and the following disclaimer. Redistributions in binary form must reproduce
 10 * the above copyright notice, this list of conditions and the following disclaimer in
 11 * the documentation and/or other materials provided with the distribution.
 12 *
 13 * Neither the name of XStream nor the names of its contributors may be used to endorse
 14 * or promote products derived from this software without specific prior written
 15 * permission.
 16 *
 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
 18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 20 * SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 22 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
 23 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
 25 * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
 26 * DAMAGE.
 27 */
 28
 29package edu.vub.util;
 30
 31import java.util.Collection;
 32import java.util.HashMap;
 33import java.util.Iterator;
 34import java.util.Map;
 35import java.util.Set;
 36
 37/**
 38 * Behaves the same way as JDK1.4 java.util.IdentityHashMap, but in JDK1.3 as well.
 39 * <p/>
 40 * Modified version of ObjectIdDictionary from XStream.
 41 */
 42public class IdentityHashMap implements Map
 43{
 44    private transient Map map;
 45
 46    private static class KeyWrapper
 47    {
 48        private final Object obj;
 49
 50        KeyWrapper(Object obj)
 51        {
 52            this.obj = obj;
 53        }
 54
 55        public int hashCode()
 56        {
 57            return System.identityHashCode(obj);
 58        }
 59
 60        public boolean equals(Object other)
 61        {
 62            return obj == ((KeyWrapper)other).obj;
 63        }
 64
 65        public String toString()
 66        {
 67            return obj.toString();
 68        }
 69    }
 70
 71    /** Default Constructor */
 72    public IdentityHashMap()
 73    {
 74        map = new HashMap();
 75    }
 76
 77    public int size()
 78    {
 79        return map.size();
 80    }
 81
 82    public boolean isEmpty()
 83    {
 84        return map.isEmpty();
 85    }
 86
 87    public boolean containsKey(Object key)
 88    {
 89        return map.containsKey(new KeyWrapper(key));
 90    }
 91
 92    public boolean containsValue(Object value)
 93    {
 94        return map.containsValue(value);
 95    }
 96
 97    public Object get(Object key)
 98    {
 99        return map.get(new KeyWrapper(key));
100    }
101
102    public Object put(Object key, Object value)
103    {
104        return map.put(new KeyWrapper(key), value);
105    }
106
107    public Object remove(Object key)
108    {
109        return map.remove(new KeyWrapper(key));
110    }
111
112    public void putAll(Map t)
113    {
114        throw new UnsupportedOperationException("unimplemented");
115    }
116
117    public void clear()
118    {
119        map.clear();
120    }
121
122    public int hashCode()
123    {
124        return map.hashCode();
125    }
126
127    public Set keySet()
128    {
129        return map.keySet();
130    }
131
132    public Collection values()
133    {
134        return map.values();
135    }
136
137    public Set entrySet()
138    {
139        return map.entrySet();
140    }
141
142    /**
143     * @return an iterator over the set of keys.
144     */
145    public Iterator iterator()
146    {
147        return map.keySet().iterator();
148    }
149
150    public boolean equals(Object o)
151    {
152        throw new RuntimeException("unimplemented");
153    }
154
155}