1 /**
2 * Copyright 2005 Steve Molloy
3 *
4 * This file is part of OV4J.
5 *
6 * OV4J is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
8 *
9 * OV4J is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License along with OV4J; if not, write to the Free Software
13 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
14 *
15 */
16 package org.ov4j.comp;
17
18 import java.lang.reflect.Array;
19 import java.util.logging.Level;
20 import java.util.logging.Logger;
21
22 import org.ov4j.data.ClassComparable;
23
24 /**
25 * Class used to compare 2 ClassComparable objects.
26 *
27 * @author smolloy
28 *
29 */
30 public class ClassComparableComparisonResult extends ComparisonResult<ClassComparable> {
31 /**
32 * Logger for this class
33 */
34 private static final Logger logger = Logger.getLogger(ClassComparableComparisonResult.class.getName());
35
36 /**
37 * @see org.ov4j.comp.ComparisonResult#compute()
38 */
39 @Override
40 public void compute() {
41 if (ClassComparableComparisonResult.logger.isLoggable(Level.FINER)) {
42 ClassComparableComparisonResult.logger.entering("ClassComparableComparisonResult", "compute()", "start");
43 }
44
45 double prec = 0.0;
46 double recall = 0.0;
47 if (getOriginal() != null && getChanged() != null) {
48 if (getOriginal().getTheObject().getClass().isArray()) {
49 final int numOri = Array.getLength(getOriginal().getTheObject());
50 final int numCh = Array.getLength(getChanged().getTheObject());
51 for (int i = 0; i < Array.getLength(getOriginal().getTheObject()); i++) {
52 final Object a1 = Array.get(getOriginal().getTheObject(), i);
53 final Object a2 = Array.get(getChanged().getTheObject(), i);
54 ComparisonResult<Object> result = null;
55 try {
56 final Class<? extends ComparisonResult<Object>> compResultClass =
57 ComparisonResult.findComparisonResultClass(a1);
58 result = compResultClass.newInstance();
59 } catch (final Exception e) {
60 if (ClassComparableComparisonResult.logger.isLoggable(Level.FINE)) {
61 ClassComparableComparisonResult.logger.logp(Level.FINE, "ClassComparableComparisonResult",
62 "compute()", "Exception caught", e);
63 }
64
65 result = new DefaultComparisonResult();
66 }
67 result.setOriginal(a1);
68 result.setChanged(a2);
69 result.compute();
70 prec += result.getPrecision();
71 recall = result.getRecall();
72 }
73 if (numCh > 0) {
74 setPrecision(prec / numCh);
75 } else {
76 setPrecision(1.0);
77 }
78 if (numOri > 0) {
79 setRecall(recall / numCh);
80 } else {
81 setRecall(1.0);
82 }
83 } else {
84 ComparisonResult<Object> result = null;
85 try {
86 final Class<? extends ComparisonResult<Object>> compResultClass =
87 ComparisonResult.findComparisonResultClass(getOriginal().getTheObject());
88 result = compResultClass.newInstance();
89 } catch (final Exception e) {
90 if (ClassComparableComparisonResult.logger.isLoggable(Level.FINE)) {
91 ClassComparableComparisonResult.logger.logp(Level.FINE, "ClassComparableComparisonResult",
92 "compute()", "Exception caught", e);
93 }
94
95 result = new DefaultComparisonResult();
96 }
97 result.setOriginal(getOriginal().getTheObject());
98 result.setChanged(getChanged().getTheObject());
99 result.compute();
100 setPrecision(result.getPrecision());
101 setRecall(result.getRecall());
102 }
103 }
104
105 if (ClassComparableComparisonResult.logger.isLoggable(Level.FINER)) {
106 ClassComparableComparisonResult.logger.exiting("ClassComparableComparisonResult", "compute()", "end");
107 }
108 }
109
110 /**
111 * @see org.ov4j.comp.ComparisonResult#fastCompute()
112 */
113 @Override
114 public void fastCompute() {
115 if (ClassComparableComparisonResult.logger.isLoggable(Level.FINER)) {
116 ClassComparableComparisonResult.logger
117 .entering("ClassComparableComparisonResult", "fastCompute()", "start");
118 }
119
120 if (getOriginal() != null && getChanged() != null) {
121 if (getOriginal().getTheObject().getClass().isArray()) {
122 final int numOri = Array.getLength(getOriginal().getTheObject());
123 final int numCh = Array.getLength(getChanged().getTheObject());
124 if (numOri == numCh) {
125 int i = 0;
126 for (; i < Array.getLength(getOriginal().getTheObject()); i++) {
127 final Object a1 = Array.get(getOriginal().getTheObject(), i);
128 final Object a2 = Array.get(getChanged().getTheObject(), i);
129 ComparisonResult<Object> result = null;
130 try {
131 final Class<? extends ComparisonResult<Object>> compResultClass =
132 ComparisonResult.findComparisonResultClass(a1);
133 result = compResultClass.newInstance();
134 } catch (final Exception e) {
135 if (ClassComparableComparisonResult.logger.isLoggable(Level.FINE)) {
136 ClassComparableComparisonResult.logger.logp(Level.FINE,
137 "ClassComparableComparisonResult", "fastCompute()", "Exception caught", e);
138 }
139
140 result = new DefaultComparisonResult();
141 }
142 result.setOriginal(a1);
143 result.setChanged(a2);
144 result.fastCompute();
145 if (result.getPrecision() < 1.0) {
146 i = Integer.MAX_VALUE - 1;
147 }
148 }
149 if (i < Integer.MAX_VALUE) {
150 setPrecision(1.0);
151 }
152 }
153 } else {
154 ComparisonResult<Object> result = null;
155 try {
156 final Class<? extends ComparisonResult<Object>> compResultClass =
157 ComparisonResult.findComparisonResultClass(getOriginal().getTheObject());
158 result = compResultClass.newInstance();
159 } catch (final Exception e) {
160 if (ClassComparableComparisonResult.logger.isLoggable(Level.FINE)) {
161 ClassComparableComparisonResult.logger.logp(Level.FINE, "ClassComparableComparisonResult",
162 "fastCompute()", "Exception caught", e);
163 }
164
165 result = new DefaultComparisonResult();
166 }
167 result.setOriginal(getOriginal().getTheObject());
168 result.setChanged(getChanged().getTheObject());
169 result.fastCompute();
170 setPrecision(result.getPrecision());
171 }
172 }
173
174 if (ClassComparableComparisonResult.logger.isLoggable(Level.FINER)) {
175 ClassComparableComparisonResult.logger.exiting("ClassComparableComparisonResult", "fastCompute()", "end");
176 }
177 }
178
179 }