Uncertainty.java

Go to the documentation of this file.
00001 package cedar.hepdata.model;
00002 
00003 import java.text.DecimalFormat;
00004 import cedar.hepdata.util.*;
00005 
00006 import javax.persistence.*;
00007 
00014 //@Entity
00015 //@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
00016 //@Table(name="BaseErrors")
00017 @MappedSuperclass
00018 public class Uncertainty extends Storeable implements Comparable<Uncertainty> {
00019     // /** Unique internal identifier */
00020     // @Id @GeneratedValue(strategy=GenerationType.TABLE)
00021     // @Column(name="ERROR_ID")
00022     // private Long _id = null;
00023 
00025     @Column(name="PlusError", nullable=false)
00026     private Double _plus;
00027 
00029     @Column(name="MinusError", nullable=false)
00030     private Double _minus;
00031 
00033     @Column(name="Comment", nullable=true, length=10000)
00034     private String _comment = "";
00035 
00037     @Column(name="Source", nullable=false)
00038     @Enumerated(EnumType.STRING)
00039     private ErrorSource _sourcetype;
00040 
00042     @Column(name="Norm", nullable=false)
00043     @Enumerated(EnumType.STRING)
00044     private ErrorNorm _normtype;
00045 
00046 
00047 
00049 
00050 
00051 
00053     public Uncertainty() {}
00054 
00055 
00063     public Uncertainty(Double plus, Double minus, ErrorSource sourcetype, ErrorNorm normtype) {
00064         setPlus( (plus != null)  ? plus  : 0.0 );
00065         setMinus( (minus != null) ? minus : 0.0 );
00066         setSourceType(sourcetype);
00067         setNormType(normtype);
00068     }
00069 
00070 
00077     public Uncertainty(Double plusminus, ErrorSource sourcetype, ErrorNorm normtype) {
00078         this(plusminus, plusminus, sourcetype, normtype);
00079     }
00080 
00081 
00082 
00084 
00085 
00086 
00088     public boolean isSymmetric() {
00089         return (getPlus().equals(getMinus()));
00090     }
00091 
00092 
00094     public boolean hasZeroSize() {
00095         return (getPlus().equals(0.0) && getMinus().equals(0.0));
00096     }
00097 
00098 
00099 
00101 
00102 
00103 
00105     public Uncertainty getAbsoluteError(Point p) throws HDException {
00106         if (getNormType() == ErrorNorm.UNKNOWN) {
00107             throw new HDException("Attempted to get the absolute error from an unknown error type.");
00108         }
00109 
00110         Double absplus = getPlus();
00111         Double absminus = getMinus();
00112         if (getNormType() != ErrorNorm.ABS) {
00113             if (getNormType() == ErrorNorm.FRAC) {
00114                 absplus *= p.getValue();
00115                 absminus *= p.getValue();
00116             } else if (getNormType() == ErrorNorm.PCT) {
00117                 absplus *= (p.getValue() / 100.0);
00118                 absminus *= (p.getValue() / 100.0);
00119             }
00120         }
00121         Uncertainty err = new Uncertainty(absplus, absminus, getSourceType(), ErrorNorm.ABS);
00122         return err;
00123     }
00124 
00125 
00126 
00128 
00129 
00130 
00132     public Double getPlus() {
00133         return _plus;
00134     }
00135 
00137     public Uncertainty setPlus(Double plus) {
00138         _plus = plus;
00139         return this;
00140     }
00141 
00142 
00143 
00145     public Double getMinus() {
00146         return _minus;
00147     }
00148 
00150     public Uncertainty setMinus(Double minus) {
00151         _minus = minus;
00152         return this;
00153     }
00154 
00155 
00156 
00158     public ErrorSource getSourceType() {
00159         return _sourcetype;
00160     }
00161 
00163     public Uncertainty setSourceType(ErrorSource sourcetype) {
00164         _sourcetype = sourcetype;
00165         return this;
00166     }
00167 
00168 
00170     public ErrorNorm getNormType() {
00171         return _normtype;
00172     }
00174     public Uncertainty setNormType(ErrorNorm normtype) {
00175         _normtype = normtype;
00176         return this;
00177     }
00178 
00179 
00181     public String getComment() {
00182         return _comment;
00183     }
00185     public Uncertainty setComment(String comment) {
00186         _comment = comment;
00187         return this;
00188     }
00189 
00190 
00192 
00193 
00194     public int compareTo(Uncertainty other) {
00195         log().debug("Comparing errors...");
00196 
00197         // For point errors
00198         if (this instanceof PointError) {
00199             if (other instanceof PointError) {
00200                 PointError test = (PointError) this;
00201                 int result = test.compareTo((PointError) other);
00202                 if (result != 0) return result;
00203             } else return 1;
00204         }
00205 
00206         // For axis errors
00207         if (this instanceof AxisError) {
00208             if (other instanceof AxisError) {
00209                 AxisError test = (AxisError) this;
00210                 int result = test.compareTo((AxisError) other);
00211                 if (result != 0) return result;
00212             } else return (other instanceof PointError) ? -1 : 1;
00213         }
00214 
00215         // For dataset errors
00216         if (this instanceof DatasetError) {
00217             if (other instanceof DatasetError) {
00218                 DatasetError test = (DatasetError) this;
00219                 int result = test.compareTo((DatasetError) other);
00220                 if (result != 0) return result;
00221             } else return 1;
00222         }
00223 
00225         return 0;
00226     }
00227 
00228 
00229 
00231 
00232 
00234     public String toString() {
00235         return toString(0);
00236     }
00237 
00238 
00240     public String toString(Integer indentBy) {
00241         log().debug("Writing out error as a string");
00242         StringBuffer s = new StringBuffer();
00243         String indent = "";
00244         for (int i  = 0; i < indentBy; ++i) indent += " ";
00245 
00246         //if (! hasZeroSize()) {
00247         s.append(indent);
00248         Double plusval = getPlus();
00249         Double minusval = getMinus();
00250 
00251         String numFormat = "###.##";
00252         // Use the number formatter to recalculate and display everything as a percentage
00253         if (getNormType() == ErrorNorm.PCT) {
00254             plusval /= 100;
00255             minusval /= 100;
00256         }
00257         if (getNormType() == ErrorNorm.FRAC || getNormType() == ErrorNorm.PCT) {
00258             numFormat += "%"; // multiplies by 100 and sticks "%" on the end
00259         }
00260         DecimalFormat numFormatter = new DecimalFormat(numFormat);
00261         if (isSymmetric()) {
00262             s.append("+- " + numFormatter.format(plusval));
00263         } else {
00264             s.append("+ " + numFormatter.format(plusval));
00265             s.append(" - " + numFormatter.format(minusval));
00266         }
00267         s.append(" (");
00268         //s.append(getNormType().toString());
00269         //s.append(", ");
00270         s.append(getSourceType().toString());
00271         s.append(")");
00272         if (getComment() != null && getComment().length() != 0) {
00273             s.append(" [" + getComment() + "]");
00274         }
00275         //}
00276         return s.toString();
00277     }
00278 
00279 }

Generated on Thu Sep 20 11:41:37 2007 by  doxygen 1.5.3