KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > commons > map > AbstractMap


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.commons.map;
7
8 import java.util.*;
9
10 /**
11  * @author Ove Ranheim (oranheim@users.sourceforge.net)
12  * @since Nov 6, 2003 5:58:08 PM
13  *
14  */

15 public abstract class AbstractMap implements Map
16 {
17   // Generic Attributes
18

19   protected abstract Object JavaDoc getAttribute( String JavaDoc name );
20
21   protected abstract void setAttribute( String JavaDoc name, Object JavaDoc value );
22
23   protected abstract void removeAttribute( String JavaDoc name );
24
25   protected abstract Enumeration getAttributeNames();
26
27
28   // Bridge methods
29

30   public int size() {
31     int n = 0;
32     Enumeration keys = getAttributeNames();
33     while (keys.hasMoreElements()) {
34       String JavaDoc key = (String JavaDoc) 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 JavaDoc key ) {
45     return (getAttribute( (String JavaDoc) key ) != null);
46   }
47
48   public boolean containsValue( Object JavaDoc value ) {
49     boolean match = false;
50     Enumeration keys = getAttributeNames();
51     while (keys.hasMoreElements()) {
52       String JavaDoc key = (String JavaDoc) keys.nextElement();
53       Object JavaDoc val = getAttribute( key );
54       if (value.equals( val )) {
55         match = true;
56         break;
57       }
58     }
59     return match;
60   }
61
62   public Object JavaDoc get( Object JavaDoc key ) {
63     return getAttribute( (String JavaDoc) key );
64   }
65
66   public Object JavaDoc put( Object JavaDoc key, Object JavaDoc value ) {
67     Object JavaDoc result = null;
68     if (containsKey( key )) result = getAttribute( (String JavaDoc) key );
69     setAttribute( (String JavaDoc) key, value );
70     return result;
71   }
72
73   public Object JavaDoc remove( Object JavaDoc key ) {
74     Object JavaDoc result = getAttribute( (String JavaDoc) key );
75     removeAttribute( (String JavaDoc) key );
76     return result;
77   }
78
79   public void putAll( Map t ) {
80     for (Iterator i = t.keySet().iterator(); i.hasNext();) {
81       String JavaDoc key = (String JavaDoc) i.next();
82       Object JavaDoc value = t.get( key );
83       put( key, value );
84     }
85   }
86
87   public void clear() {
88     throw new UnsupportedOperationException JavaDoc();
89   }
90
91   public Set keySet() {
92     throw new UnsupportedOperationException JavaDoc();
93   }
94
95   public Collection values() {
96     throw new UnsupportedOperationException JavaDoc();
97   }
98
99   public Set entrySet() {
100     throw new UnsupportedOperationException JavaDoc();
101   }
102
103   public String JavaDoc toString() {
104     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
105     Enumeration keys = getAttributeNames();
106     while (keys.hasMoreElements()) {
107       String JavaDoc key = (String JavaDoc) keys.nextElement();
108       b.append(key).append("\n") ;
109     }
110     return b.toString() ;
111   }
112 }
113
Popular Tags