View Javadoc

1   /*
2    *  Licensed to the Apache Software Foundation (ASF) under one
3    *  or more contributor license agreements.  See the NOTICE file
4    *  distributed with this work for additional information
5    *  regarding copyright ownership.  The ASF licenses this file
6    *  to you under the Apache License, Version 2.0 (the
7    *  "License"); you may not use this file except in compliance
8    *  with the License.  You may obtain a copy of the License at
9    *  
10   *    http://www.apache.org/licenses/LICENSE-2.0
11   *  
12   *  Unless required by applicable law or agreed to in writing,
13   *  software distributed under the License is distributed on an
14   *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *  KIND, either express or implied.  See the License for the
16   *  specific language governing permissions and limitations
17   *  under the License. 
18   *  
19   */
20  
21  package org.apache.directory.server.protocol.shared;
22  
23  
24  import java.util.Collection;
25  import java.util.Collections;
26  import java.util.Dictionary;
27  import java.util.Enumeration;
28  import java.util.HashMap;
29  import java.util.HashSet;
30  import java.util.Hashtable;
31  import java.util.Iterator;
32  import java.util.Map;
33  import java.util.Set;
34  
35  
36  /**
37   * Adapter to add the Map interface to Dictionary's.  Many of the OSGi interfaces use
38   * Dictionary's for legacy reasons, but the Dictionary is obsolete.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   * @version $Rev: 586763 $, $Date: 2007-10-20 19:26:29 +0200 (Sa, 20 Okt 2007) $
42   */
43  public class MapAdapter implements Map<Object, Object>
44  {
45      private Dictionary<Object, Object> dictionary;
46  
47  
48      /**
49       * Creates a new instance of MapAdapter.
50       *
51       * @param dictionary
52       */
53      public MapAdapter( Dictionary<Object, Object> dictionary )
54      {
55          this.dictionary = dictionary;
56      }
57  
58  
59      /**
60       * @see java.util.Map#clear()
61       */
62      public void clear()
63      {
64          dictionary = new Hashtable<Object, Object>();
65      }
66  
67  
68      /**
69       * @see java.util.Map#containsKey(java.lang.Object)
70       */
71      public boolean containsKey( Object key )
72      {
73          return Collections.list( dictionary.keys() ).contains( key );
74      }
75  
76  
77      /**
78       * @see java.util.Map#containsValue(java.lang.Object)
79       */
80      public boolean containsValue( Object value )
81      {
82          return Collections.list( dictionary.elements() ).contains( value );
83      }
84  
85  
86      /**
87       * @see java.util.Map#entrySet()
88       */
89      public Set<Map.Entry<Object, Object>> entrySet()
90      {
91          Map<Object, Object> map = new HashMap<Object, Object>();
92  
93          Enumeration<Object> e = dictionary.keys();
94  
95          while ( e.hasMoreElements() )
96          {
97              Object key = e.nextElement();
98              Object value = dictionary.get( key );
99              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 }