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 021package org.apache.directory.server.protocol.shared; 022 023 024import java.util.Collection; 025import java.util.Collections; 026import java.util.Dictionary; 027import java.util.Enumeration; 028import java.util.HashMap; 029import java.util.HashSet; 030import java.util.Hashtable; 031import java.util.Iterator; 032import java.util.Map; 033import 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 */ 042public class MapAdapter implements Map<Object, Object> 043{ 044 private Dictionary<Object, Object> dictionary; 045 046 047 /** 048 * Creates a new instance of MapAdapter. 049 * 050 * @param dictionary The dictionary to store 051 */ 052 public MapAdapter( Dictionary<Object, Object> dictionary ) 053 { 054 this.dictionary = dictionary; 055 } 056 057 058 /** 059 * @see java.util.Map#clear() 060 */ 061 public void clear() 062 { 063 dictionary = new Hashtable<>(); 064 } 065 066 067 /** 068 * @see java.util.Map#containsKey(java.lang.Object) 069 */ 070 public boolean containsKey( Object key ) 071 { 072 return Collections.list( dictionary.keys() ).contains( key ); 073 } 074 075 076 /** 077 * @see java.util.Map#containsValue(java.lang.Object) 078 */ 079 public boolean containsValue( Object value ) 080 { 081 return Collections.list( dictionary.elements() ).contains( value ); 082 } 083 084 085 /** 086 * @see java.util.Map#entrySet() 087 */ 088 public Set<Map.Entry<Object, Object>> entrySet() 089 { 090 Map<Object, Object> map = new HashMap<>(); 091 092 Enumeration<Object> e = dictionary.keys(); 093 094 while ( e.hasMoreElements() ) 095 { 096 Object key = e.nextElement(); 097 Object value = dictionary.get( key ); 098 map.put( key, value ); 099 } 100 101 return map.entrySet(); 102 } 103 104 105 /** 106 * @see java.util.Map#get(java.lang.Object) 107 */ 108 public Object get( Object key ) 109 { 110 return dictionary.get( key ); 111 } 112 113 114 /** 115 * @see java.util.Map#isEmpty() 116 */ 117 public boolean isEmpty() 118 { 119 return dictionary.isEmpty(); 120 } 121 122 123 /** 124 * @see java.util.Map#keySet() 125 */ 126 public Set<Object> keySet() 127 { 128 return new HashSet<>( Collections.list( dictionary.keys() ) ); 129 } 130 131 132 /** 133 * @see java.util.Map#put(java.lang.Object, java.lang.Object) 134 */ 135 public Object put( Object arg0, Object arg1 ) 136 { 137 return dictionary.put( arg0, arg1 ); 138 } 139 140 141 /** 142 * @see java.util.Map#putAll(java.util.Map) 143 */ 144 public void putAll( Map arg0 ) 145 { 146 Iterator it = arg0.entrySet().iterator(); 147 148 while ( it.hasNext() ) 149 { 150 Map.Entry entry = ( Map.Entry ) it.next(); 151 dictionary.put( entry.getKey(), entry.getValue() ); 152 } 153 } 154 155 156 /** 157 * @see java.util.Map#remove(java.lang.Object) 158 */ 159 public Object remove( Object key ) 160 { 161 return dictionary.remove( key ); 162 } 163 164 165 /** 166 * @see java.util.Map#size() 167 */ 168 public int size() 169 { 170 return dictionary.size(); 171 } 172 173 174 /** 175 * @see java.util.Map#values() 176 */ 177 public Collection<Object> values() 178 { 179 return Collections.list( dictionary.elements() ); 180 } 181}