00001 package cedar.hepdata.model;
00002
00003 import java.util.Collection;
00004 import java.util.Set;
00005 import java.util.SortedSet;
00006 import java.util.TreeSet;
00007 import java.util.HashSet;
00008 import java.util.Arrays;
00009
00010 import javax.persistence.*;
00011
00012 import cedar.hepdata.util.HDException;
00013
00019 @Entity
00020 @Table(name="Points")
00021 public class Point extends Value implements Comparable<Point> {
00023 @Column(name="LocalId", nullable=false)
00024 private Integer _localId;
00025
00027 @Column(name="Value", nullable=false)
00028 private Double _value;
00029
00031 @org.hibernate.annotations.CollectionOfElements
00032 @JoinTable(name="PointErrors", joinColumns=@JoinColumn(name="VALUE_ID"))
00033 @org.hibernate.annotations.CollectionId(columns=@Column(name="PointErrorId"),
00034 type=@org.hibernate.annotations.Type(type="long"),
00035 generator="sequence")
00036 @org.hibernate.annotations.Sort(type=org.hibernate.annotations.SortType.NATURAL)
00037 @org.hibernate.annotations.BatchSize(size=5)
00038 private SortedSet<PointError> _errors = new TreeSet<PointError>();
00039
00041 @ManyToOne
00042 private YAxis _yAxis;
00043
00047 @Column(name="Relation", nullable=false)
00048 @Enumerated(EnumType.STRING)
00049 private Relation _relation = Relation.EQUALS;
00050
00052 @Column(name="ConfLevel", nullable=true)
00053 private Double _confidenceLevel = null;
00054
00055
00056
00058
00059
00060
00062 public Point() {}
00063
00065 public Point(Double value) {
00066 setValue(value);
00067 }
00068
00070 public Point(Double value, Relation relation, Double confidenceLevel) throws HDException {
00071 setLimit(relation, value, confidenceLevel);
00072 }
00073
00075 public Point(YAxis yAxis) {
00076 setYAxis(yAxis);
00077 }
00078
00080 public Point(YAxis yAxis, Double value) {
00081 this(yAxis);
00082 setValue(value);
00083 }
00084
00086 public Point(YAxis yAxis, Double value, Relation relation, Double confidenceLevel) throws HDException {
00087 this(yAxis);
00088 setLimit(relation, value, confidenceLevel);
00089 }
00090
00091
00093
00094
00095 public Point setLimit(Relation relation, Double value, Double confidenceLevel) throws HDException {
00096 setRelation(relation);
00097 setValue(value);
00098 setConfidenceLevel(confidenceLevel);
00099 return this;
00100 }
00101
00102
00104
00105
00106
00107 public Integer getId() {
00108 return _localId;
00109 }
00110 public Point setId(Integer pointId) {
00111 _localId = pointId;
00112 return this;
00113 }
00114
00115
00116
00117 public Double getValue() {
00118 return _value;
00119 }
00120 public Point setValue(Double value) {
00121 _value = value;
00122 return this;
00123 }
00124
00125
00129 public Relation getRelation() {
00130 return _relation;
00131 }
00132 public Point setRelation(Relation relation) {
00133 _relation = relation;
00134 return this;
00135 }
00136
00137
00138
00139 public Double getConfidenceLevel() {
00140 return _confidenceLevel;
00141 }
00142 public Point setConfidenceLevel(Double confidenceLevel) throws HDException {
00143 if (getRelation() != Relation.EQUALS) {
00144 _confidenceLevel = confidenceLevel;
00145 } else {
00146 _confidenceLevel = null;
00147 }
00148 return this;
00149 }
00150
00151
00152
00153 public SortedSet<PointError> getErrors() {
00154 return _errors;
00155 }
00156 public Point setErrors(SortedSet<PointError> errors) {
00157 getErrors().clear();
00158 for (PointError e : errors) addError(e);
00159 return this;
00160 }
00161 public Point setErrors(Collection<PointError> errors) {
00162 log().debug("Calling setErrors(Collection<PointError>)");
00163 getErrors().clear();
00164 for (PointError e : errors) addError(e);
00165 return this;
00166 }
00167 public Point setErrors(PointError[] errors) {
00168 setErrors(Arrays.asList(errors));
00169 return this;
00170 }
00171 public Point addError(PointError error) {
00172 if (error != null) {
00173 if (error.getId() == null) {
00174 int highestId = 0;
00175 if (getErrors().size() > 0) {
00176 highestId = getErrors().last().getId();
00177 }
00178 log().debug("Incrementing point error ID: " + (highestId + 1));
00179 error.setId(highestId + 1);
00180 }
00181 log().debug("Adding point error: ID = " + error.getId());
00182 _errors.add(error);
00183 } else {
00184 log().warn("Tried to add a null point error to a point");
00185 }
00186 return this;
00187 }
00188 public Point removeError(PointError error) {
00189 if (error != null) {
00190 _errors.remove(error);
00191 } else {
00192 log().warn("Tried to remove a null point error from a point");
00193 }
00194 return this;
00195 }
00196
00197
00199 public YAxis getYAxis() {
00200 return _yAxis;
00201 }
00203 public Point setYAxis(YAxis yAxis) {
00204 _yAxis = yAxis;
00205 if (yAxis != null) yAxis.getPoints().add(this);
00206 return this;
00207 }
00208
00209
00211
00212
00214 public Set<Uncertainty> getDatasetErrors() {
00215 if (getYAxis() != null) {
00216 return getYAxis().getDatasetErrors();
00217 } else {
00218 return new HashSet<Uncertainty>();
00219 }
00220 }
00222 public SortedSet<Uncertainty> getAxisErrors() {
00223 SortedSet<Uncertainty> axisErrors = new TreeSet<Uncertainty>();
00224 if (getYAxis() != null) {
00225 axisErrors.addAll( getYAxis().getErrors() );
00226 }
00227 return axisErrors;
00228 }
00230 public Set<Uncertainty> getPointErrors() {
00231 return new HashSet<Uncertainty>(getErrors());
00232 }
00234 public SortedSet<Uncertainty> getAllErrors() {
00235 SortedSet<Uncertainty> allErrors = new TreeSet<Uncertainty>();
00236 allErrors.addAll(getErrors());
00237 allErrors.addAll(getAxisErrors());
00238 allErrors.addAll(getDatasetErrors());
00239 return allErrors;
00240 }
00241
00242
00244
00245
00247 public SortedSet<Bin> getBins() {
00248 SortedSet<Bin> matchingBins = null;
00249 YAxis y = getYAxis();
00250 if (y != null) {
00251 Dataset d = y.getDataset();
00252 if (d != null) {
00253 matchingBins = d.getBins(getId());
00254 }
00255 }
00256 return matchingBins;
00257 }
00258
00260 public SortedSet<Point> getPoints() {
00261 SortedSet<Point> matchingPoints = null;
00262 YAxis y = getYAxis();
00263 if (y != null) {
00264 Dataset d = y.getDataset();
00265 if (d != null) {
00266 matchingPoints = d.getPoints(getId());
00267 }
00268 }
00269 return matchingPoints;
00270 }
00271
00272
00274
00275
00277 public String toString() {
00278 return toString(0);
00279 }
00280
00282 public String toString(Integer indentBy) {
00283 log().debug("Writing out point as a string");
00284 StringBuffer s = new StringBuffer();
00285 String indent = "";
00286 for (int i = 0; i < indentBy; ++i) indent += " ";
00287
00288 s.append(indent + "Point " + getId() + ": ");
00289 if (getAllErrors().size() > 0) s.append("( ");
00290 s.append(getValue());
00291 for (Uncertainty e : getAllErrors()) {
00292 s.append(" " + e.toString());
00293 }
00294 if (getAllErrors().size() > 0) s.append(" )");
00295 if (getYAxis() != null) {
00296 s.append(" " + getYAxis().getUnit().toString());
00297 }
00298 return s.toString();
00299 }
00300
00301
00303
00304
00306 public boolean equals(Object other) {
00307 if (this == other) return true;
00308 if (! (other instanceof Point)) return false;
00309
00310 final Point test = (Point) other;
00311 if (test.getYAxis() != null && !test.getYAxis().equals(getYAxis()) ) return false;
00313 if (test.getValue() != null && getValue() != null &&
00314 Math.abs((test.getValue() - getValue())/getValue()) > 1e-6 ) return false;
00315
00316
00317 return true;
00318 }
00319
00321 public int hashCode() {
00322 int code = 0;
00323 if (getYAxis() != null) code += 100000 * getYAxis().hashCode();
00324 if (getValue() != null) code += getValue().hashCode();
00325 code *= 2;
00326 return code;
00327 }
00328
00329
00331
00332
00334 public int compareTo(Point other) {
00335 log().debug("Comparing points...");
00336 if (getId() == null) {
00337 log().warn("Null Point ID");
00338 return 1;
00339 } else if (getId() > other.getId()) {
00340 log().debug("Greater than");
00341 return 1;
00342 } else if (getId() < other.getId()) {
00343 log().debug("Less than");
00344 return -1;
00345 } else {
00346 log().debug("Point IDs are equal");
00347 if (getYAxis() != null && other.getYAxis() != null) {
00348 return getYAxis().compareTo(other.getYAxis());
00349 } else {
00350 return 0;
00351 }
00352 }
00353 }
00354
00355 }