00001 package cedar.hepdata.model;
00002
00003 import java.text.DecimalFormat;
00004 import cedar.hepdata.util.*;
00005
00006 import javax.persistence.*;
00007
00014 @MappedSuperclass
00015 public class Uncertainty extends Storeable implements Comparable<Uncertainty> {
00016
00018 @Column(name="PlusError", nullable=false)
00019 private Double _plus;
00020
00022 @Column(name="MinusError", nullable=false)
00023 private Double _minus;
00024
00026 @Column(name="Comment", nullable=true, length=10000)
00027 private String _comment = "";
00028
00030 @Column(name="Source", nullable=false)
00031 @Enumerated(EnumType.STRING)
00032 private ErrorSource _sourcetype;
00033
00035 @Column(name="Norm", nullable=false)
00036 @Enumerated(EnumType.STRING)
00037 private ErrorNorm _normtype;
00038
00039
00040
00042
00043
00044
00046 public Uncertainty() {}
00047
00048
00056 public Uncertainty(Double plus, Double minus, ErrorSource sourcetype, ErrorNorm normtype) {
00057 setPlus( (plus != null) ? plus : 0.0 );
00058 setMinus( (minus != null) ? minus : 0.0 );
00059 setSourceType(sourcetype);
00060 setNormType(normtype);
00061 }
00062
00063
00070 public Uncertainty(Double plusminus, ErrorSource sourcetype, ErrorNorm normtype) {
00071 this(plusminus, plusminus, sourcetype, normtype);
00072 }
00073
00074
00075
00077
00078
00079
00081 public boolean isSymmetric() {
00082 return (getPlus().equals(getMinus()));
00083 }
00084
00085
00087 public boolean hasZeroSize() {
00088 return (getPlus().equals(0.0) && getMinus().equals(0.0));
00089 }
00090
00091
00092
00094
00095
00096
00098 public Uncertainty getAbsoluteError(Point p) throws HDException {
00099 if (getNormType() == ErrorNorm.UNKNOWN) {
00100 throw new HDException("Attempted to get the absolute error from an unknown error type.");
00101 }
00102
00103 Double absplus = getPlus();
00104 Double absminus = getMinus();
00105 if (getNormType() != ErrorNorm.ABS) {
00106 if (getNormType() == ErrorNorm.FRAC) {
00107 absplus *= p.getValue();
00108 absminus *= p.getValue();
00109 } else if (getNormType() == ErrorNorm.PCT) {
00110 absplus *= (p.getValue() / 100.0);
00111 absminus *= (p.getValue() / 100.0);
00112 }
00113 }
00114 Uncertainty err = new Uncertainty(absplus, absminus, getSourceType(), ErrorNorm.ABS);
00115 return err;
00116 }
00117
00118
00119
00121
00122
00123
00125 public Double getPlus() {
00126 return _plus;
00127 }
00128
00130 public Uncertainty setPlus(Double plus) {
00131 _plus = plus;
00132 return this;
00133 }
00134
00135
00136
00138 public Double getMinus() {
00139 return _minus;
00140 }
00141
00143 public Uncertainty setMinus(Double minus) {
00144 _minus = minus;
00145 return this;
00146 }
00147
00148
00149
00151 public ErrorSource getSourceType() {
00152 return _sourcetype;
00153 }
00154
00156 public Uncertainty setSourceType(ErrorSource sourcetype) {
00157 _sourcetype = sourcetype;
00158 return this;
00159 }
00160
00161
00163 public ErrorNorm getNormType() {
00164 return _normtype;
00165 }
00167 public Uncertainty setNormType(ErrorNorm normtype) {
00168 _normtype = normtype;
00169 return this;
00170 }
00171
00172
00174 public String getComment() {
00175 return _comment;
00176 }
00178 public Uncertainty setComment(String comment) {
00179 _comment = comment;
00180 return this;
00181 }
00182
00183
00185
00186
00187 public int compareTo(Uncertainty other) {
00188 log().debug("Comparing errors...");
00189
00190
00191 if (this instanceof PointError) {
00192 if (other instanceof PointError) {
00193 PointError test = (PointError) this;
00194 int result = test.compareTo((PointError) other);
00195 if (result != 0) return result;
00196 } else return 1;
00197 }
00198
00199
00200 if (this instanceof AxisError) {
00201 if (other instanceof AxisError) {
00202 AxisError test = (AxisError) this;
00203 int result = test.compareTo((AxisError) other);
00204 if (result != 0) return result;
00205 } else return (other instanceof PointError) ? -1 : 1;
00206 }
00207
00208
00209 if (this instanceof DatasetError) {
00210 if (other instanceof DatasetError) {
00211 DatasetError test = (DatasetError) this;
00212 int result = test.compareTo((DatasetError) other);
00213 if (result != 0) return result;
00214 } else return 1;
00215 }
00216
00218 return 0;
00219 }
00220
00221
00222
00224
00225
00227 public String toString() {
00228 return toString(0);
00229 }
00230
00231
00233 public String toString(Integer indentBy) {
00234 log().debug("Writing out error as a string");
00235 StringBuffer s = new StringBuffer();
00236 String indent = "";
00237 for (int i = 0; i < indentBy; ++i) indent += " ";
00238
00239
00240 s.append(indent);
00241 Double plusval = getPlus();
00242 Double minusval = getMinus();
00243
00244 String numFormat = "###.##";
00245
00246 if (getNormType() == ErrorNorm.PCT) {
00247 plusval /= 100;
00248 minusval /= 100;
00249 }
00250 if (getNormType() == ErrorNorm.FRAC || getNormType() == ErrorNorm.PCT) {
00251 numFormat += "%";
00252 }
00253 DecimalFormat numFormatter = new DecimalFormat(numFormat);
00254 if (isSymmetric()) {
00255 s.append("+- " + numFormatter.format(plusval));
00256 } else {
00257 s.append("+ " + numFormatter.format(plusval));
00258 s.append(" - " + numFormatter.format(minusval));
00259 }
00260 s.append(" (");
00261
00262
00263 s.append(getSourceType().toString());
00264 s.append(")");
00265 if (getComment() != null && getComment().length() != 0) {
00266 s.append(" [" + getComment() + "]");
00267 }
00268
00269 return s.toString();
00270 }
00271
00272 }