Coverage Report - org.apache.myfaces.util.AbstractAttributeMap
 
Classes in this File Line Coverage Branch Coverage Complexity
AbstractAttributeMap
0%
0/37
0%
0/24
0
AbstractAttributeMap$1
N/A
N/A
0
AbstractAttributeMap$AbstractAttributeIterator
0%
0/8
0%
0/2
0
AbstractAttributeMap$AbstractAttributeSet
0%
0/5
N/A
0
AbstractAttributeMap$EntryIterator
0%
0/2
N/A
0
AbstractAttributeMap$EntrySet
0%
0/18
0%
0/16
0
AbstractAttributeMap$EntrySetEntry
0%
0/23
0%
0/14
0
AbstractAttributeMap$KeyIterator
0%
0/2
N/A
0
AbstractAttributeMap$KeySet
0%
0/4
0%
0/2
0
AbstractAttributeMap$Values
0%
0/15
0%
0/12
0
AbstractAttributeMap$ValuesIterator
0%
0/2
N/A
0
 
 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  
 package org.apache.myfaces.util;
 20  
 
 21  
 import java.util.AbstractMap;
 22  
 import java.util.AbstractSet;
 23  
 import java.util.ArrayList;
 24  
 import java.util.Collection;
 25  
 import java.util.Enumeration;
 26  
 import java.util.Iterator;
 27  
 import java.util.List;
 28  
 import java.util.Map;
 29  
 import java.util.NoSuchElementException;
 30  
 import java.util.Set;
 31  
 
 32  
 /**
 33  
  * Helper Map implementation for use with different Attribute Maps.
 34  
  * 
 35  
  * @author Anton Koinov (latest modification by $Author: bommel $)
 36  
  * @version $Revision: 693059 $ $Date: 2008-09-08 06:42:28 -0500 (Mon, 08 Sep 2008) $
 37  
  */
 38  0
 public abstract class AbstractAttributeMap<V> extends AbstractMap<String, V>
 39  
 {
 40  
     private Set<String> _keySet;
 41  
     private Collection<V> _values;
 42  
     private Set<Entry<String, V>> _entrySet;
 43  
 
 44  
     public void clear()
 45  
     {
 46  0
         final List<String> names = new ArrayList<String>();
 47  0
         for (final Enumeration<String> e = getAttributeNames(); e.hasMoreElements();)
 48  
         {
 49  0
             names.add(e.nextElement());
 50  
         }
 51  
 
 52  0
         for (String name : names) {
 53  0
           removeAttribute(name);
 54  
         }
 55  0
     }
 56  
 
 57  
     public final boolean containsKey(final Object key)
 58  
     {
 59  0
         return getAttribute(key.toString()) != null;
 60  
     }
 61  
 
 62  
     public boolean containsValue(final Object findValue)
 63  
     {
 64  0
         if (findValue == null)
 65  
         {
 66  0
             return false;
 67  
         }
 68  
 
 69  0
         for (final Enumeration<String> e = getAttributeNames(); e.hasMoreElements();)
 70  
         {
 71  0
             final Object value = getAttribute(e.nextElement());
 72  0
             if (findValue.equals(value))
 73  
             {
 74  0
                 return true;
 75  
             }
 76  0
         }
 77  
 
 78  0
         return false;
 79  
     }
 80  
 
 81  
     @Override
 82  
     public Set<Entry<String, V>> entrySet()
 83  
     {
 84  0
         return (_entrySet != null) ? _entrySet : (_entrySet = new EntrySet());
 85  
     }
 86  
 
 87  
     public V get(final Object key)
 88  
     {
 89  0
         return getAttribute(key.toString());
 90  
     }
 91  
 
 92  
     public boolean isEmpty()
 93  
     {
 94  0
         return !getAttributeNames().hasMoreElements();
 95  
     }
 96  
 
 97  
     public Set<String> keySet()
 98  
     {
 99  0
         return (_keySet != null) ? _keySet : (_keySet = new KeySet());
 100  
     }
 101  
 
 102  
     public final V put(final String key, final V value)
 103  
     {
 104  0
         final V retval = getAttribute(key);
 105  0
         setAttribute(key, value);
 106  0
         return retval;
 107  
     }
 108  
 
 109  
     public void putAll(final Map<? extends String, ? extends V> t)
 110  
     {
 111  0
       for (final Entry<? extends String, ? extends V> entry : t.entrySet()) {
 112  0
         setAttribute(entry.getKey(), entry.getValue());
 113  
       }
 114  0
     }
 115  
 
 116  
     @Override
 117  
     public final V remove(final Object key)
 118  
     {
 119  0
         final String key_ = key.toString();
 120  0
         final V retval = getAttribute(key_);
 121  0
         removeAttribute(key_);
 122  0
         return retval;
 123  
     }
 124  
 
 125  
     @Override
 126  
     public int size()
 127  
     {
 128  0
         int size = 0;
 129  0
         for (final Enumeration e = getAttributeNames(); e.hasMoreElements();)
 130  
         {
 131  0
             size++;
 132  0
             e.nextElement();
 133  
         }
 134  0
         return size;
 135  
     }
 136  
 
 137  
     @Override
 138  
     public Collection<V> values()
 139  
     {
 140  0
         return (_values != null) ? _values : (_values = new Values());
 141  
     }
 142  
 
 143  
     abstract protected V getAttribute(String key);
 144  
 
 145  
     abstract protected void setAttribute(String key, V value);
 146  
 
 147  
     abstract protected void removeAttribute(String key);
 148  
 
 149  
     abstract protected Enumeration<String> getAttributeNames();
 150  
 
 151  0
     private abstract class AbstractAttributeSet<E> extends AbstractSet<E>
 152  
     {
 153  
         @Override
 154  
         public boolean isEmpty()
 155  
         {
 156  0
             return AbstractAttributeMap.this.isEmpty();
 157  
         }
 158  
 
 159  
         @Override
 160  
         public int size()
 161  
         {
 162  0
             return AbstractAttributeMap.this.size();
 163  
         }
 164  
 
 165  
         @Override
 166  
         public void clear()
 167  
         {
 168  0
             AbstractAttributeMap.this.clear();
 169  0
         }
 170  
     }
 171  
 
 172  0
     private final class KeySet extends AbstractAttributeSet<String>
 173  
     {
 174  
         @Override
 175  
         public Iterator<String> iterator()
 176  
         {
 177  0
             return new KeyIterator();
 178  
         }
 179  
 
 180  
         @Override
 181  
         public boolean contains(final Object o)
 182  
         {
 183  0
             return AbstractAttributeMap.this.containsKey(o);
 184  
         }
 185  
 
 186  
         @Override
 187  
         public boolean remove(final Object o)
 188  
         {
 189  0
             return AbstractAttributeMap.this.remove(o) != null;
 190  
         }
 191  
 
 192  
     }
 193  
 
 194  0
     private abstract class AbstractAttributeIterator<E> implements Iterator<E>
 195  
     {
 196  0
         protected final Enumeration<String> _e = getAttributeNames();
 197  
         protected String _currentKey;
 198  
 
 199  
         public void remove()
 200  
         {
 201  
             // remove() may cause ConcurrentModificationException.
 202  
             // We could throw an exception here, but not throwing an exception
 203  
             // allows one call to remove() to succeed
 204  0
             if (_currentKey == null)
 205  
             {
 206  0
                 throw new NoSuchElementException("You must call next() at least once");
 207  
             }
 208  0
             AbstractAttributeMap.this.remove(_currentKey);
 209  0
         }
 210  
 
 211  
         public boolean hasNext()
 212  
         {
 213  0
             return _e.hasMoreElements();
 214  
         }
 215  
 
 216  
         public E next()
 217  
         {
 218  0
             return getValue(_currentKey = _e.nextElement());
 219  
         }
 220  
 
 221  
         protected abstract E getValue(String attributeName);
 222  
     }
 223  
 
 224  0
     private final class KeyIterator extends AbstractAttributeIterator<String>
 225  
     {
 226  
         @Override
 227  
         protected String getValue(final String attributeName)
 228  
         {
 229  0
             return attributeName;
 230  
         }
 231  
     }
 232  
 
 233  0
     private class Values extends AbstractAttributeSet<V>
 234  
     {
 235  
         @Override
 236  
         public Iterator<V> iterator()
 237  
         {
 238  0
             return new ValuesIterator();
 239  
         }
 240  
 
 241  
         @Override
 242  
         public boolean contains(final Object o)
 243  
         {
 244  0
             if (o == null)
 245  
             {
 246  0
                 return false;
 247  
             }
 248  
 
 249  0
             for (final Iterator it = iterator(); it.hasNext();)
 250  
             {
 251  0
                 if (o.equals(it.next()))
 252  
                 {
 253  0
                     return true;
 254  
                 }
 255  
             }
 256  
 
 257  0
             return false;
 258  
         }
 259  
 
 260  
         @Override
 261  
         public boolean remove(final Object o)
 262  
         {
 263  0
             if (o == null)
 264  
             {
 265  0
                 return false;
 266  
             }
 267  
 
 268  0
             for (final Iterator it = iterator(); it.hasNext();)
 269  
             {
 270  0
                 if (o.equals(it.next()))
 271  
                 {
 272  0
                     it.remove();
 273  0
                     return true;
 274  
                 }
 275  
             }
 276  
 
 277  0
             return false;
 278  
         }
 279  
     }
 280  
 
 281  0
     private class ValuesIterator extends AbstractAttributeIterator<V>
 282  
     {
 283  
         @Override
 284  
         protected V getValue(final String attributeName)
 285  
         {
 286  0
             return AbstractAttributeMap.this.get(attributeName);
 287  
         }
 288  
     }
 289  
 
 290  0
     private final class EntrySet extends AbstractAttributeSet<Entry<String, V>>
 291  
     {
 292  
         @Override
 293  
         public Iterator<Entry<String, V>> iterator()
 294  
         {
 295  0
             return new EntryIterator();
 296  
         }
 297  
 
 298  
         @Override
 299  
         public boolean contains(final Object o)
 300  
         {
 301  0
             if (!(o instanceof Entry))
 302  
             {
 303  0
                 return false;
 304  
             }
 305  
 
 306  0
             final Entry entry = (Entry) o;
 307  0
             final Object key = entry.getKey();
 308  0
             final Object value = entry.getValue();
 309  0
             if (key == null || value == null)
 310  
             {
 311  0
                 return false;
 312  
             }
 313  
 
 314  0
             return value.equals(AbstractAttributeMap.this.get(key));
 315  
         }
 316  
 
 317  
         @Override
 318  
         public boolean remove(final Object o)
 319  
         {
 320  0
             if (!(o instanceof Entry))
 321  
             {
 322  0
                 return false;
 323  
             }
 324  
 
 325  0
             final Entry entry = (Entry) o;
 326  0
             final Object key = entry.getKey();
 327  0
             final Object value = entry.getValue();
 328  0
             if (key == null || value == null || !value.equals(AbstractAttributeMap.this.get(key)))
 329  
             {
 330  0
                 return false;
 331  
             }
 332  
 
 333  0
             return AbstractAttributeMap.this.remove(((Entry) o).getKey()) != null;
 334  
         }
 335  
     }
 336  
 
 337  
     /**
 338  
      * Not very efficient since it generates a new instance of <code>Entry</code> for each element and still internaly
 339  
      * uses the <code>KeyIterator</code>. It is more efficient to use the <code>KeyIterator</code> directly.
 340  
      */
 341  0
     private final class EntryIterator extends AbstractAttributeIterator<Entry<String, V>>
 342  
     {
 343  
         @Override
 344  
         protected Entry<String, V> getValue(final String attributeName)
 345  
         {
 346  
             // Must create new Entry every time--value of the entry must stay
 347  
             // linked to the same attribute name
 348  0
             return new EntrySetEntry(attributeName);
 349  
         }
 350  
     }
 351  
 
 352  0
     private final class EntrySetEntry implements Entry<String, V>
 353  
     {
 354  
         private final String _currentKey;
 355  
 
 356  
         public EntrySetEntry(final String currentKey)
 357  0
         {
 358  0
             _currentKey = currentKey;
 359  0
         }
 360  
 
 361  
         public String getKey()
 362  
         {
 363  0
             return _currentKey;
 364  
         }
 365  
 
 366  
         public V getValue()
 367  
         {
 368  0
             return AbstractAttributeMap.this.get(_currentKey);
 369  
         }
 370  
 
 371  
         public V setValue(final V value)
 372  
         {
 373  0
             return AbstractAttributeMap.this.put(_currentKey, value);
 374  
         }
 375  
 
 376  
         @Override
 377  
         public int hashCode()
 378  
         {
 379  0
             int result = 1;
 380  0
             result = 31 * result + ((_currentKey == null) ? 0 : _currentKey.hashCode());
 381  0
             return result;
 382  
         }
 383  
 
 384  
         @Override
 385  
         public boolean equals(final Object obj)
 386  
         {
 387  0
             if (this == obj)
 388  0
                 return true;
 389  0
             if (obj == null)
 390  0
                 return false;
 391  0
             if (getClass() != obj.getClass())
 392  0
                 return false;
 393  0
             final EntrySetEntry other = (EntrySetEntry) obj;
 394  0
             if (_currentKey == null)
 395  
             {
 396  0
                 if (other._currentKey != null)
 397  0
                     return false;
 398  
             }
 399  0
             else if (!_currentKey.equals(other._currentKey))
 400  0
                 return false;
 401  0
             return true;
 402  
         }
 403  
 
 404  
     }
 405  
 }