KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > schema2beans > BeanComparator


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.schema2beans;
21
22 import java.util.*;
23
24 /**
25  * The class BeanComparator is the default comparator implementation
26  * for comparing and merging schema2beans graphs. This class has mainly two
27  * methods. One for deciding if two BaseBean objects are identicals and
28  * a second one to decide if two properties are identicals.
29  *
30  * These methods return either the original value to signal that they are
31  * the same or the second value, to signal that the two elements compared
32  * are not the same.
33  *
34  * This default implementation compares both the propertie and attribute
35  * values to decide for equality. This implementation also uses the key
36  * information that might be defined in the mdd file (see user doc for this
37  * file usage). If the key is specified (default is all properties are keys),
38  * then only the properties defined as keys are used during the comparison.
39  */

40 public class BeanComparator {
41     //
42
// This is set to true if the processing of compareBean or
43
// compareProperty uses at least one key element.
44
// The caller can use this information to know if the result of a
45
// comparison is due to a real equality or simply a lack of key.
46
//
47
private boolean hasKey = false;
48     
49     //
50
// Specify if the bean comparator should use the keys specify
51
// in the mdd file. The default value is true.
52
// If this is set to false, any property is considered a key in
53
// in the comparison, regardless of what has been specified in the mdd.
54
//
55
private boolean useMddKeys = true;
56     
57     // Same as above, but take precedence over it.
58
static boolean useComparatorsMddKeys = true;
59     
60     /**
61      * Default comparison implementation for comparing two beans.
62      * The two beans are considered identical if all its non-bean properties
63      * are identicals.
64      */

65     public BaseBean compareBean(String JavaDoc beanName,
66                 BaseBean curBean,
67                 BaseBean newBean) {
68                     
69     BaseBean ret = curBean;
70     Iterator it = curBean.beanPropsIterator();
71     boolean useKeys = useMddKeys;
72     
73     this.hasKey = false;
74     
75     if (!useComparatorsMddKeys)
76         useKeys = false;
77
78     if (curBean.getProperty() != null
79         && curBean.getProperty().isKey()) {
80
81         // Check the attributes first
82
BaseAttribute[] ba = curBean.listAttributes();
83
84         if (ba != null) {
85         for(int j=0; j<ba.length; j++) {
86             if (!ba[j].isFixed()) {
87             String JavaDoc attrName = ba[j].getName();
88             String JavaDoc curValue = curBean.getAttributeValue(attrName);
89             String JavaDoc otherValue = newBean.getAttributeValue(attrName);
90
91             if (curValue != otherValue) {
92                 if (curValue == null || otherValue == null ||
93                 !curValue.equals(otherValue)) {
94             
95                 // Diffenrence found - not the same bean
96
return newBean;
97                 }
98             }
99             }
100         }
101         }
102     }
103     
104     while (it.hasNext()) {
105         // Get our next property (as a BeanProp)
106
BeanProp prop = (BeanProp)it.next();
107         
108         if (prop == null)
109         continue;
110         
111         String JavaDoc name = prop.getBeanName();
112         boolean isArray = Common.isArray(prop.type);
113         boolean isBean = Common.isBean(prop.type);
114         boolean isKey = Common.isKey(prop.type) || !useKeys;
115         Object JavaDoc o1, o2, o3;
116         
117         if (!this.hasKey && isKey)
118         this.hasKey = true;
119         
120         if (isArray && !isBean && isKey) {
121         //
122
// An array of non-bean properties. Do a trivial comparison
123
// of the array.
124
//
125
int size1 = prop.size();
126         int size2 = newBean.size(name);
127         
128         if (size1 != size2) {
129             // Diffenrence found - not the same bean
130
ret = newBean;
131             break;
132         }
133         
134         for (int i=0; i<size1; i++) {
135             o1 = prop.getValue(i);
136             o2 = newBean.getValue(name, i);
137             o3 = this.compareProperty(name, curBean, o1, i,
138                           newBean, o2, i);
139             if (o3 != o1) {
140             // Diffenrence found - not the same bean
141
ret = newBean;
142             break;
143             }
144         }
145         }
146         else
147         if (!isBean && isKey) {
148             o1 = prop.getValue(0);
149             o2 = newBean.getValue(name);
150             o3 = this.compareProperty(name, curBean, o1, -1,
151                           newBean, o2, -1);
152             if (o3 != o1) {
153             // Diffenrence found - not the same bean
154
ret = newBean;
155             }
156         }
157     }
158     
159     if (DDLogFlags.debug) {
160         TraceLogger.put(TraceLogger.DEBUG,
161         TraceLogger.SVC_DD,
162         DDLogFlags.DBG_BLD, 5,
163         DDLogFlags.BEANCOMP,
164         beanName + ": " +
165         ((ret == curBean)? "same":"different"));
166     }
167     
168     return ret;
169     }
170     
171     /**
172      * Default comparison implementation for comparing two property values.
173      * @return curValue if the same, newValue if different.
174      */

175     public Object JavaDoc compareProperty(String JavaDoc propertyName,
176                                   BaseBean curBean,
177                                   Object JavaDoc curValue,
178                                   int curIndex,
179                                   BaseBean newBean,
180                                   Object JavaDoc newValue,
181                                   int newIndex) {
182         Object JavaDoc ret = curValue;
183         BeanProp prop = curBean.beanProp(propertyName);
184         boolean isKey = this.hasKeyDefined(prop);
185     
186         // Values are the same - check their attributes
187
if (isKey) {
188             if (curValue == null || !curValue.equals(newValue))
189                 ret = newValue;
190
191             String JavaDoc[] attrs = curBean.getAttributeNames(propertyName);
192             int i1 = 0;
193             int i2 = 0;
194
195             if (curIndex != -1) {
196                 i1 = curIndex;
197                 i2 = newIndex;
198             }
199
200             for(int j=0; j<attrs.length; j++) {
201                 String JavaDoc a = attrs[j];
202
203                 String JavaDoc v1 = curBean.getAttributeValue(propertyName, i1, a);
204                 String JavaDoc v2 = newBean.getAttributeValue(propertyName, i2, a);
205
206                 if (v1 != null) {
207                     if (!v1.equals(v2)) {
208                         ret = newValue;
209                         break;
210                     }
211                 } else if (v2 != v1) {
212                     ret = newValue;
213                     break;
214                 }
215         
216             }
217         } else {
218         }
219     
220         if (DDLogFlags.debug) {
221             TraceLogger.put(TraceLogger.DEBUG,
222                             TraceLogger.SVC_DD,
223                             DDLogFlags.DBG_BLD, 5,
224                             DDLogFlags.PROPCOMP,
225                             propertyName + " - " +
226                             ((curValue==null)?"<null>":curValue) +
227                             ((curIndex==-1)?"":("."+curIndex)) + " / " +
228                             ((newValue==null)?"<null>":newValue) +
229                             ((newIndex==-1)?"":("."+newIndex)) + " " +
230                             ((ret == curValue)? "same":"different") +
231                             " (" +((isKey)?"Key":"!Key") + ")");
232         }
233     
234         return ret;
235     }
236     
237     //
238
// Returns true if one of the element compared during the last call
239
// of compareBean and/or compareProperty used a key.
240
//
241
protected boolean hasKey() {
242     return this.hasKey;
243     }
244     
245     //
246
// Return if the key should be used comparing this element.
247
//
248
boolean hasKeyDefined(BeanProp prop) {
249     boolean useKeys = useMddKeys;
250     
251     if (!useComparatorsMddKeys)
252         useKeys = false;
253     
254     this.hasKey = Common.isKey(prop.type) || !useKeys;
255     return this.hasKey;
256     }
257     
258     public void enableKey(boolean b) {
259     this.useMddKeys = b;
260     }
261     
262     public static void enableComparatorsKey(boolean b) {
263     useComparatorsMddKeys = b;
264     }
265     
266 }
267
268
269
270
271
272
273
274
275
276
277
278
279
Popular Tags