View Javadoc

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.io.Serializable;
19  import java.util.logging.Level;
20  import java.util.logging.Logger;
21  
22  import org.ov4j.data.Version;
23  
24  /**
25   * This class is used to compare versions.
26   * 
27   * @author smolloy
28   * 
29   */
30  public class VersionComparisonResult<T extends Comparable<? super T> & Cloneable & Serializable> extends
31  	ComparisonResult<Version<T>> {
32  	/**
33  	 * Logger for this class
34  	 */
35  	private static final Logger	logger	= Logger.getLogger(VersionComparisonResult.class.getName());
36  
37  	/**
38  	 * @see org.ov4j.comp.ComparisonResult#compute()
39  	 */
40  	@Override
41  	public void compute() {
42  		if (VersionComparisonResult.logger.isLoggable(Level.FINER)) {
43  			VersionComparisonResult.logger.entering("VersionComparisonResult", "compute()", "start");
44  		}
45  
46  		if (getChanged() != null && getOriginal() != null) {
47  			try {
48  				final ComparisonResult<T> compRes =
49  					ComparisonResult.<T, ComparisonResult<T>> findComparisonResultClass(
50  						(getOriginal()).getVersionedObject()).newInstance();
51  				compRes.setChanged(getChanged().getVersionedObject());
52  				compRes.setOriginal(getOriginal().getVersionedObject());
53  				compRes.compute();
54  				final NumericComparisonResult<Integer> numComp = new NumericComparisonResult<Integer>();
55  				numComp.setChanged((getChanged()).getVersionNumber());
56  				numComp.setOriginal((getOriginal()).getVersionNumber());
57  				numComp.compute();
58  				final StringComparisonResult autComp = new StringComparisonResult();
59  				autComp.setChanged((getChanged()).getAuthor());
60  				autComp.setOriginal((getOriginal()).getAuthor());
61  				autComp.compute();
62  				final StringComparisonResult comComp = new StringComparisonResult();
63  				comComp.setChanged((getChanged()).getComment());
64  				comComp.setOriginal((getOriginal()).getComment());
65  				comComp.compute();
66  				setPrecision(0.6 * compRes.getPrecision() + 0.2 * numComp.getPrecision() + 0.1 * autComp.getPrecision()
67  					+ 0.1 * comComp.getPrecision());
68  				setRecall(0.6 * compRes.getRecall() + 0.2 * numComp.getRecall() + 0.1 * autComp.getRecall() + 0.1
69  					* comComp.getRecall());
70  			} catch (final InstantiationException e) {
71  				if (VersionComparisonResult.logger.isLoggable(Level.FINE)) {
72  					VersionComparisonResult.logger.logp(Level.FINE, "VersionComparisonResult", "compute()",
73  						"exception ignored", e);
74  				}
75  			} catch (final IllegalAccessException e) {
76  				if (VersionComparisonResult.logger.isLoggable(Level.FINE)) {
77  					VersionComparisonResult.logger.logp(Level.FINE, "VersionComparisonResult", "compute()",
78  						"exception ignored", e);
79  				}
80  			} catch (final ClassNotFoundException e) {
81  				if (VersionComparisonResult.logger.isLoggable(Level.FINE)) {
82  					VersionComparisonResult.logger.logp(Level.FINE, "VersionComparisonResult", "compute()",
83  						"exception ignored", e);
84  				}
85  			}
86  		}
87  
88  		if (VersionComparisonResult.logger.isLoggable(Level.FINER)) {
89  			VersionComparisonResult.logger.exiting("VersionComparisonResult", "compute()", "end");
90  		}
91  	}
92  
93  	/**
94  	 * @see org.ov4j.comp.ComparisonResult#fastCompute()
95  	 */
96  	@Override
97  	public void fastCompute() {
98  		if (VersionComparisonResult.logger.isLoggable(Level.FINER)) {
99  			VersionComparisonResult.logger.entering("VersionComparisonResult", "fastCompute()", "start");
100 		}
101 
102 		if (getOriginal() != null && getChanged() != null
103 			&& getOriginal().getVersionNumber() == getChanged().getVersionNumber()) {
104 			try {
105 				final ComparisonResult<T> compRes =
106 					ComparisonResult.<T, ComparisonResult<T>> findComparisonResultClass(
107 						(getOriginal()).getVersionedObject()).newInstance();
108 				compRes.setChanged(getChanged().getVersionedObject());
109 				compRes.setOriginal(getOriginal().getVersionedObject());
110 				compRes.fastCompute();
111 				setPrecision(compRes.getPrecision());
112 			} catch (final ClassNotFoundException e) {
113 				if (VersionComparisonResult.logger.isLoggable(Level.FINE)) {
114 					VersionComparisonResult.logger.logp(Level.FINE, "VersionComparisonResult", "fastCompute()",
115 						"exception ignored", e);
116 				}
117 			} catch (final InstantiationException e) {
118 				if (VersionComparisonResult.logger.isLoggable(Level.FINE)) {
119 					VersionComparisonResult.logger.logp(Level.FINE, "VersionComparisonResult", "fastCompute()",
120 						"exception ignored", e);
121 				}
122 			} catch (final IllegalAccessException e) {
123 				if (VersionComparisonResult.logger.isLoggable(Level.FINE)) {
124 					VersionComparisonResult.logger.logp(Level.FINE, "VersionComparisonResult", "fastCompute()",
125 						"exception ignored", e);
126 				}
127 			}
128 		}
129 
130 		if (VersionComparisonResult.logger.isLoggable(Level.FINER)) {
131 			VersionComparisonResult.logger.exiting("VersionComparisonResult", "fastCompute()", "end");
132 		}
133 	}
134 
135 }