/*
* JBoss, Home of Professional Open Source
* Copyright 2009, Red Hat Middleware LLC, and individual contributors
* as indicated by the @author tags. See the copyright.txt file in the
* distribution for a full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.osgi.metadata;

import java.util.Collections;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Set;

import org.jboss.util.collection.Iterators;

/**
 * CaseInsensitiveDictionary.
 * 
 * @author <a href="adrian@jboss.com">Adrian Brock</a>
 * @version $Revision: 1.1 $
 */
@SuppressWarnings("rawtypes")
public class CaseInsensitiveDictionary extends Hashtable
{
   private static final long serialVersionUID = 5802491129524016545L;

   /** The delegate dictionary */
   private Dictionary<String, Object> delegate;

   /** The original keys */
   private Set<String> originalKeys;

   /**
    * Create a new CaseInsensitiveDictionary.
    * 
    * @param delegate the delegate
    */
   @SuppressWarnings("unchecked")
   public CaseInsensitiveDictionary(Dictionary delegate)
   {
      if (delegate == null)
         throw new IllegalArgumentException("Null delegaqte");

      this.delegate = new Hashtable<String, Object>(delegate.size());
      this.originalKeys = Collections.synchronizedSet(new HashSet<String>());
      Enumeration<String> e = delegate.keys();
      while (e.hasMoreElements())
      {
         String key = e.nextElement();
         if (get(key) != null)
            throw new IllegalArgumentException("Properties contain duplicates with varying case for key=" + key + " : " + delegate);

         this.delegate.put(key.toLowerCase(), delegate.get(key));
         originalKeys.add(key);
      }
   }

   public Enumeration<Object> elements()
   {
      return delegate.elements();
   }

   @SuppressWarnings("unchecked")
   public synchronized boolean equals(Object obj)
   {
      if (obj == this)
         return true;
      if (obj == null || obj instanceof Dictionary == false)
         return false;

      Dictionary<String, Object> other = (Dictionary)obj;

      if (size() != other.size())
         return false;
      if (isEmpty())
         return true;

      for (String key : originalKeys)
      {
         if (get(key).equals(other.get(key)))
            return false;
      }
      return true;
   }

   public Object get(Object key)
   {
      if (key instanceof String)
         key = ((String)key).toLowerCase();
      return delegate.get(key);
   }

   public int hashCode()
   {
      return delegate.hashCode();
   }

   public boolean isEmpty()
   {
      return delegate.isEmpty();
   }

   @SuppressWarnings("unchecked")
   public Enumeration<String> keys()
   {
      return Iterators.toEnumeration(originalKeys.iterator());
   }

   public Object put(Object key, Object value)
   {
      throw new UnsupportedOperationException("immutable");
   }

   public Object remove(Object key)
   {
      throw new UnsupportedOperationException("immutable");
   }

   public int size()
   {
      return delegate.size();
   }

   public String toString()
   {
      return delegate.toString();
   }
}

@@ -1181,16 +1181,38 @@
 htable;%0A
+import java.util.Map;%0A
 import j
@@ -3822,32 +3822,831 @@
 utable%22);%0A   %7D%0A%0A
+   @Override%0A   public Set keySet()%0A   %7B%0A      return originalKeys;%0A   %7D%0A%0A   @Override%0A   public Set entrySet()%0A   %7B%0A      Set%3CMap.Entry%3E entrySet = new HashSet%3CMap.Entry%3E();%0A      for (final String key : originalKeys)%0A      %7B%0A         final Object value = get(key);%0A         Map.Entry entry = new Map.Entry()%0A         %7B%0A%0A            @Override%0A            public Object getKey()%0A            %7B%0A               return key;%0A            %7D%0A%0A            @Override%0A            public Object getValue()%0A            %7B%0A               return value;%0A            %7D%0A%0A            @Override%0A            public Object setValue(Object value)%0A            %7B%0A               throw new UnsupportedOperationException(%22immutable%22);%0A            %7D%0A         %7D;%0A         entrySet.add(entry);%0A      %7D%0A      return entrySet;%0A   %7D%0A%0A
    public int si

Fix entrySet and keySet handling