KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > message > Attributes


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.message;
37
38 import java.io.IOException JavaDoc;
39 import java.io.ObjectInputStream JavaDoc;
40 import java.io.ObjectOutputStream JavaDoc;
41 import java.util.Hashtable JavaDoc;
42 import java.util.Iterator JavaDoc;
43 import java.util.Set JavaDoc;
44
45 /**
46  * Attributes are used to store additional attributes of a message.
47  * The information is stored in a Hashtable using key-value pairs.
48  * <p>
49  * Keys are Strings, values can be any Object.
50  *
51  * @author Timo Stich <tstich@users.sourceforge.net>
52  */

53 public class Attributes implements Saveable, Cloneable JavaDoc {
54
55     private Hashtable JavaDoc attributes;
56
57     /**
58      * Creates an empty Attributes object.
59      *
60      */

61     public Attributes() {
62         attributes = new Hashtable JavaDoc();
63     }
64
65     /**
66      * Creates an Attributes object and loads attributes from the given InputStream.
67      *
68      * @param in the InputSream to load from.
69      * @throws IOException thrown if there was a problem reading from the InputStream.
70      */

71     public Attributes(ObjectInputStream JavaDoc in) throws IOException JavaDoc {
72         load(in);
73     }
74
75     /**
76      * Sets the attribute key-value pair.
77      * @param key the key
78      * @param value the value
79      */

80     public void put(String JavaDoc key, Object JavaDoc value) {
81         attributes.put(key, value);
82     }
83
84     /**
85      * Returns the attribute value for the specified key.
86      * s
87      * @param key the attribute key.
88      * @return the value for the key.
89      */

90     public Object JavaDoc get(String JavaDoc key) {
91         return attributes.get(key);
92     }
93
94
95     /** {@inheritDoc} */
96     public final void load(ObjectInputStream JavaDoc in) throws IOException JavaDoc {
97         int size = in.readInt();
98         attributes = new Hashtable JavaDoc(size);
99         for (int i = 0; i < size; i++) {
100             try {
101                 attributes.put(in.readUTF(), in.readObject());
102             } catch (ClassNotFoundException JavaDoc e) {
103                 e.printStackTrace();
104             }
105         }
106     }
107
108     /** {@inheritDoc} */
109     public final void save(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
110         out.writeInt(attributes.size());
111         Iterator JavaDoc keys = attributes.keySet().iterator();
112
113         while (keys.hasNext()) {
114             String JavaDoc key = (String JavaDoc) keys.next();
115             Object JavaDoc value = attributes.get(key);
116             out.writeUTF(key);
117             out.writeObject(value);
118         }
119     }
120
121     /**
122      * Returns the number of attribute pairs.
123      * @return the number of attribute pairs.
124      */

125     public int count() {
126         return attributes.size();
127     }
128
129     /** {@inheritDoc} */
130     public Object JavaDoc clone() {
131         Attributes clone;
132         try {
133             clone = (Attributes) super.clone();
134             clone.attributes = (Hashtable JavaDoc) attributes.clone();
135             return clone;
136         } catch (CloneNotSupportedException JavaDoc e) {
137             throw new RuntimeException JavaDoc(e);
138         }
139     }
140
141     /** {@inheritDoc} */
142     public boolean equals(Object JavaDoc obj) {
143         boolean isEqual = false;
144         if ((obj != null) && (obj instanceof Attributes)) {
145             Attributes other = (Attributes) obj;
146             isEqual = attributes.equals(other.attributes);
147         }
148         return isEqual;
149     }
150
151     /** {@inheritDoc} */
152     public int hashCode() {
153         return attributes.hashCode();
154     }
155
156     /** {@inheritDoc} */
157     public String JavaDoc toString() {
158         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
159         buffer.append("Attributes[");
160         Set JavaDoc set = attributes.keySet();
161         for (Iterator JavaDoc iterator = set.iterator(); iterator.hasNext();) {
162             String JavaDoc key = (String JavaDoc) iterator.next();
163             buffer.append(key);
164             buffer.append("=");
165             buffer.append(attributes.get(key));
166             if (iterator.hasNext()) {
167                 buffer.append(", ");
168             }
169         }
170         buffer.append("]");
171         return buffer.toString();
172     }
173 }
174
Popular Tags