KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > repository > DjUid


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  * Redistribution and use in source and binary forms, with or without
4  * modification, is permitted provided that the following conditions are met: -
5  * Redistributions of source code must retain the above copyright notice, this
6  * list of conditions and the following disclaimer. - Redistributions in binary
7  * form must reproduce the above copyright notice, this list of conditions and
8  * the following disclaimer in the documentation and/or other materials
9  * provided with the distribution. - All advertising materials mentioning
10  * features or use of this software must display the following acknowledgment:
11  * "This product includes Djeneric." - Products derived from this software may
12  * not be called "Djeneric" nor may "Djeneric" appear in their names without
13  * prior written permission of Genimen BV. - Redistributions of any form
14  * whatsoever must retain the following acknowledgment: "This product includes
15  * Djeneric." THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND
16  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
17  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
18  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV,
19  * DJENERIC.ORG, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */

27 package com.genimen.djeneric.repository;
28
29 import java.util.ArrayList JavaDoc;
30 import java.util.HashMap JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 import com.genimen.djeneric.repository.exceptions.DjenericException;
34
35 public class DjUid
36 {
37   DjExtent _extent;
38   HashMap JavaDoc _properties;
39   long _associatedObjectid;
40   String JavaDoc _descriptor;
41
42   public DjUid(DjExtent extent)
43   {
44     _extent = extent;
45     _properties = new HashMap JavaDoc();
46   }
47
48   public void setProperty(String JavaDoc propertyName, String JavaDoc value) throws DjenericException
49   {
50     if (!_extent.getProperty(propertyName).isPartOfUID()) throw new DjenericException("Property " + propertyName
51                                                                                       + " is not part of the UID");
52     _properties.put(propertyName, value);
53   }
54
55   public void setProperty(String JavaDoc propertyName, DjUid value) throws DjenericException
56   {
57     DjProperty prop = _extent.getProperty(propertyName);
58     if (!prop.isPartOfUID()) throw new DjenericException("Property " + propertyName + " is not part of the UID");
59     if (!(prop.getType() instanceof DjExtent)) throw new DjenericException("Property " + propertyName
60                                                                            + " is a link to a master");
61     _properties.put(propertyName, value);
62   }
63
64   public int getPropertyCount()
65   {
66     return _properties.size();
67   }
68
69   public String JavaDoc[] getPropertyNames()
70   {
71     ArrayList JavaDoc lst = new ArrayList JavaDoc();
72     Iterator JavaDoc it = _properties.keySet().iterator();
73     while (it.hasNext())
74       lst.add(it.next());
75     return (String JavaDoc[]) lst.toArray(new String JavaDoc[0]);
76   }
77
78   public boolean isUidProperty(String JavaDoc propertyName)
79   {
80     return _properties.get(propertyName) instanceof DjUid;
81   }
82
83   public String JavaDoc getString(String JavaDoc propertyName)
84   {
85     return _properties.get(propertyName).toString();
86   }
87
88   public DjUid getUid(String JavaDoc propertyName)
89   {
90     return (DjUid) _properties.get(propertyName);
91   }
92
93   public long getAssociatedObjectid()
94   {
95     return _associatedObjectid;
96   }
97
98   public void setAssociatedObjectid(long objectid)
99   {
100     _associatedObjectid = objectid;
101   }
102
103   public String JavaDoc getDescriptor()
104   {
105     return _descriptor;
106   }
107
108   public void setDescriptor(String JavaDoc descriptor)
109   {
110     _descriptor = descriptor;
111   }
112
113   public String JavaDoc toString()
114   {
115     return toString(0);
116   }
117
118   public String JavaDoc toString(int indent)
119   {
120     StringBuffer JavaDoc result = new StringBuffer JavaDoc(500);
121     StringBuffer JavaDoc indentStr = new StringBuffer JavaDoc(20);
122
123     for (int i = 0; i < indent; i++)
124       indentStr.append(" ");
125
126     result.append(indentStr.toString());
127     result.append("<uid extent=\"" + _extent.getName() + "\" id=\"" + getAssociatedObjectid() + "\">\n");
128     if (getDescriptor() != null)
129     {
130       result.append(indentStr.toString());
131       result.append(" <descr><![CDATA[" + getDescriptor() + "]]></descr>\n");
132     }
133     Iterator JavaDoc it = _properties.keySet().iterator();
134     while (it.hasNext())
135     {
136       String JavaDoc key = it.next().toString();
137       Object JavaDoc value = _properties.get(key);
138       result.append(indentStr.toString());
139       result.append(" <property name=\"" + key + "\" type=\"");
140
141       if (value instanceof DjUid)
142       {
143         result.append("uid\">\n" + ((DjUid) value).toString(indent + 4) + "\n" + indentStr.toString()
144                       + " </property>\n");
145       }
146       else
147       {
148         if (value != null) result.append("text\"><![CDATA[" + value.toString() + "]]></property>\n");
149       }
150     }
151     result.append(indentStr.toString());
152     result.append("</uid>");
153     return result.toString();
154   }
155
156   public DjExtent getExtent()
157   {
158     return _extent;
159   }
160
161   public void setExtent(DjExtent extent)
162   {
163     _extent = extent;
164   }
165
166   public boolean equals(Object JavaDoc obj)
167   {
168     if (!(obj instanceof DjUid)) return false;
169     DjUid other = (DjUid) obj;
170
171     if (getPropertyCount() != other.getPropertyCount()) return false;
172
173     String JavaDoc[] names = getPropertyNames();
174     for (int i = 0; i < names.length; i++)
175     {
176       Object JavaDoc v1 = _properties.get(names[i]);
177       Object JavaDoc v2 = other._properties.get(names[i]);
178       if (v1 == null && v2 != null) return false;
179       if (v1 != null && v2 == null) return false;
180       if (!v1.equals(v2)) return false;
181     }
182     return true;
183   }
184
185   public int hashCode()
186   {
187     int result = 0;
188
189     String JavaDoc[] names = getPropertyNames();
190     for (int i = 0; i < names.length; i++)
191     {
192       Object JavaDoc o = _properties.get(names[i]);
193       if (o != null) result += o.hashCode();
194     }
195     return result;
196   }
197
198 }
199
Popular Tags