1 5 6 package org.exoplatform.commons.map; 7 8 import java.util.*; 9 10 15 public abstract class AbstractMap implements Map 16 { 17 19 protected abstract Object getAttribute( String name ); 20 21 protected abstract void setAttribute( String name, Object value ); 22 23 protected abstract void removeAttribute( String name ); 24 25 protected abstract Enumeration getAttributeNames(); 26 27 28 30 public int size() { 31 int n = 0; 32 Enumeration keys = getAttributeNames(); 33 while (keys.hasMoreElements()) { 34 String key = (String ) keys.nextElement(); 35 n++; 36 } 37 return n; 38 } 39 40 public boolean isEmpty() { 41 return !getAttributeNames().hasMoreElements(); 42 } 43 44 public boolean containsKey( Object key ) { 45 return (getAttribute( (String ) key ) != null); 46 } 47 48 public boolean containsValue( Object value ) { 49 boolean match = false; 50 Enumeration keys = getAttributeNames(); 51 while (keys.hasMoreElements()) { 52 String key = (String ) keys.nextElement(); 53 Object val = getAttribute( key ); 54 if (value.equals( val )) { 55 match = true; 56 break; 57 } 58 } 59 return match; 60 } 61 62 public Object get( Object key ) { 63 return getAttribute( (String ) key ); 64 } 65 66 public Object put( Object key, Object value ) { 67 Object result = null; 68 if (containsKey( key )) result = getAttribute( (String ) key ); 69 setAttribute( (String ) key, value ); 70 return result; 71 } 72 73 public Object remove( Object key ) { 74 Object result = getAttribute( (String ) key ); 75 removeAttribute( (String ) key ); 76 return result; 77 } 78 79 public void putAll( Map t ) { 80 for (Iterator i = t.keySet().iterator(); i.hasNext();) { 81 String key = (String ) i.next(); 82 Object value = t.get( key ); 83 put( key, value ); 84 } 85 } 86 87 public void clear() { 88 throw new UnsupportedOperationException (); 89 } 90 91 public Set keySet() { 92 throw new UnsupportedOperationException (); 93 } 94 95 public Collection values() { 96 throw new UnsupportedOperationException (); 97 } 98 99 public Set entrySet() { 100 throw new UnsupportedOperationException (); 101 } 102 103 public String toString() { 104 StringBuffer b = new StringBuffer () ; 105 Enumeration keys = getAttributeNames(); 106 while (keys.hasMoreElements()) { 107 String key = (String ) keys.nextElement(); 108 b.append(key).append("\n") ; 109 } 110 return b.toString() ; 111 } 112 } 113 | Popular Tags |