001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    
021    package org.apache.directory.server.protocol.shared;
022    
023    
024    import java.util.Collection;
025    import java.util.Collections;
026    import java.util.Dictionary;
027    import java.util.Enumeration;
028    import java.util.HashMap;
029    import java.util.HashSet;
030    import java.util.Hashtable;
031    import java.util.Iterator;
032    import java.util.Map;
033    import java.util.Set;
034    
035    
036    /**
037     * Adapter to add the Map interface to Dictionary's.  Many of the OSGi interfaces use
038     * Dictionary's for legacy reasons, but the Dictionary is obsolete.
039     *
040     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
041     * @version $Rev: 586763 $, $Date: 2007-10-20 19:26:29 +0200 (Sat, 20 Oct 2007) $
042     */
043    public class MapAdapter implements Map<Object, Object>
044    {
045        private Dictionary<Object, Object> dictionary;
046    
047    
048        /**
049         * Creates a new instance of MapAdapter.
050         *
051         * @param dictionary
052         */
053        public MapAdapter( Dictionary<Object, Object> dictionary )
054        {
055            this.dictionary = dictionary;
056        }
057    
058    
059        /**
060         * @see java.util.Map#clear()
061         */
062        public void clear()
063        {
064            dictionary = new Hashtable<Object, Object>();
065        }
066    
067    
068        /**
069         * @see java.util.Map#containsKey(java.lang.Object)
070         */
071        public boolean containsKey( Object key )
072        {
073            return Collections.list( dictionary.keys() ).contains( key );
074        }
075    
076    
077        /**
078         * @see java.util.Map#containsValue(java.lang.Object)
079         */
080        public boolean containsValue( Object value )
081        {
082            return Collections.list( dictionary.elements() ).contains( value );
083        }
084    
085    
086        /**
087         * @see java.util.Map#entrySet()
088         */
089        public Set<Map.Entry<Object, Object>> entrySet()
090        {
091            Map<Object, Object> map = new HashMap<Object, Object>();
092    
093            Enumeration<Object> e = dictionary.keys();
094    
095            while ( e.hasMoreElements() )
096            {
097                Object key = e.nextElement();
098                Object value = dictionary.get( key );
099                map.put( key, value );
100            }
101    
102            return map.entrySet();
103        }
104    
105    
106        /**
107         * @see java.util.Map#get(java.lang.Object)
108         */
109        public Object get( Object key )
110        {
111            return dictionary.get( key );
112        }
113    
114    
115        /**
116         * @see java.util.Map#isEmpty()
117         */
118        public boolean isEmpty()
119        {
120            return dictionary.isEmpty();
121        }
122    
123    
124        /**
125         * @see java.util.Map#keySet()
126         */
127        public Set<Object> keySet()
128        {
129            return new HashSet<Object>( Collections.list( dictionary.keys() ) );
130        }
131    
132    
133        /**
134         * @see java.util.Map#put(java.lang.Object, java.lang.Object)
135         */
136        public Object put( Object arg0, Object arg1 )
137        {
138            return dictionary.put( arg0, arg1 );
139        }
140    
141    
142        /**
143         * @see java.util.Map#putAll(java.util.Map)
144         */
145        public void putAll( Map arg0 )
146        {
147            Iterator it = arg0.entrySet().iterator();
148    
149            while ( it.hasNext() )
150            {
151                Map.Entry entry = ( Map.Entry ) it.next();
152                dictionary.put( entry.getKey(), entry.getValue() );
153            }
154        }
155    
156    
157        /**
158         * @see java.util.Map#remove(java.lang.Object)
159         */
160        public Object remove( Object key )
161        {
162            return dictionary.remove( key );
163        }
164    
165    
166        /**
167         * @see java.util.Map#size()
168         */
169        public int size()
170        {
171            return dictionary.size();
172        }
173    
174    
175        /**
176         * @see java.util.Map#values()
177         */
178        public Collection<Object> values()
179        {
180            return Collections.list( dictionary.elements() );
181        }
182    }