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
00045 @Column(name="Relation", nullable=false)
00046 @Enumerated(EnumType.STRING)
00047 private Relation _relation = Relation.EQUALS;
00048
00050 @Column(name="ConfLevel", nullable=true)
00051 private Double _confidenceLevel = null;
00052
00053
00054
00056
00057
00058
00060 public Point() {}
00061
00063 public Point(Double value) {
00064 setValue(value);
00065 }
00066
00068 public Point(Double value, Relation relation, Double confidenceLevel) throws HDException {
00069 setLimit(relation, value, confidenceLevel);
00070 }
00071
00073 public Point(YAxis yAxis) {
00074 setYAxis(yAxis);
00075 }
00076
00078 public Point(YAxis yAxis, Double value) {
00079 this(yAxis);
00080 setValue(value);
00081 }
00082
00084 public Point(YAxis yAxis, Double value, Relation relation, Double confidenceLevel) throws HDException {
00085 this(yAxis);
00086 setLimit(relation, value, confidenceLevel);
00087 }
00088
00089
00091
00092
00094 public Point setLimit(Relation relation, Double value, Double confidenceLevel) throws HDException {
00095 setRelation(relation);
00096 setValue(value);
00097 setConfidenceLevel(confidenceLevel);
00098 return this;
00099 }
00100
00101
00103
00104
00106 public Integer getId() {
00107 return _localId;
00108 }
00110 public Point setId(Integer pointId) {
00111 _localId = pointId;
00112 return this;
00113 }
00114
00115
00117 public Double getValue() {
00118 return _value;
00119 }
00121 public Point setValue(Double value) {
00122 _value = value;
00123 return this;
00124 }
00125
00126
00128 public Relation getRelation() {
00129 return _relation;
00130 }
00131
00133 public Point setRelation(Relation relation) {
00134 _relation = relation;
00135 return this;
00136 }
00137
00138
00140 public Double getConfidenceLevel() {
00141 return _confidenceLevel;
00142 }
00143
00145 public Point setConfidenceLevel(Double confidenceLevel) throws HDException {
00146 if (getRelation() != Relation.EQUALS) {
00147 _confidenceLevel = confidenceLevel;
00148 } else {
00149 _confidenceLevel = null;
00150 }
00151 return this;
00152 }
00153
00154
00156 public SortedSet<PointError> getErrors() {
00157 return _errors;
00158 }
00159
00161 public Point setErrors(SortedSet<PointError> errors) {
00162 getErrors().clear();
00163 for (PointError e : errors) addError(e);
00164 return this;
00165 }
00166
00168 public Point setErrors(Collection<PointError> errors) {
00169 log().debug("Calling setErrors(Collection<PointError>)");
00170 getErrors().clear();
00171 for (PointError e : errors) addError(e);
00172 return this;
00173 }
00174
00176 public Point setErrors(PointError[] errors) {
00177 setErrors(Arrays.asList(errors));
00178 return this;
00179 }
00180
00182 public Point addError(PointError error) {
00183 if (error != null) {
00184 error.setPoint(this);
00185 } else {
00186 log().warn("Tried to add a null point error to a point");
00187 }
00188 return this;
00189 }
00190
00192 public Point removeError(PointError error) {
00193 if (error != null) {
00194 _errors.remove(error);
00195 } else {
00196 log().warn("Tried to remove a null point error from a point");
00197 }
00198 return this;
00199 }
00200
00201
00203 public YAxis getYAxis() {
00204 return _yAxis;
00205 }
00207 public Point setYAxis(YAxis yAxis) {
00208 _yAxis = yAxis;
00209 if (yAxis != null) {
00210 if (getId() == null) {
00211 int highestId = 0;
00212 if (yAxis.getPoints().size() > 0) {
00213 highestId = yAxis.getPoints().last().getId();
00214 }
00215 log().debug("Incrementing point ID: " + (highestId + 1));
00216 setId(highestId + 1);
00217 }
00218 log().debug("Adding point to y-axis " + yAxis.getId() + ": ID = " + getId());
00219 yAxis.getPoints().add(this);
00220 }
00221 return this;
00222 }
00223
00224
00226
00227
00229 public Set<Uncertainty> getDatasetErrors() {
00230 if (getYAxis() != null) {
00231 return getYAxis().getDatasetErrors();
00232 } else {
00233 return new HashSet<Uncertainty>();
00234 }
00235 }
00237 public SortedSet<Uncertainty> getAxisErrors() {
00238 SortedSet<Uncertainty> axisErrors = new TreeSet<Uncertainty>();
00239 if (getYAxis() != null) {
00240 axisErrors.addAll( getYAxis().getErrors() );
00241 }
00242 return axisErrors;
00243 }
00245 public Set<Uncertainty> getPointErrors() {
00246 return new HashSet<Uncertainty>(getErrors());
00247 }
00249 public SortedSet<Uncertainty> getAllErrors() {
00250 SortedSet<Uncertainty> allErrors = new TreeSet<Uncertainty>();
00251 allErrors.addAll(getErrors());
00252 allErrors.addAll(getAxisErrors());
00253 allErrors.addAll(getDatasetErrors());
00254 return allErrors;
00255 }
00256
00257
00259
00260
00262 public SortedSet<Bin> getBins() {
00263 SortedSet<Bin> matchingBins = null;
00264 YAxis y = getYAxis();
00265 if (y != null) {
00266 Dataset d = y.getDataset();
00267 if (d != null) {
00268 matchingBins = d.getBins(getId());
00269 }
00270 }
00271 return matchingBins;
00272 }
00273
00275 public SortedSet<Point> getPoints() {
00276 SortedSet<Point> matchingPoints = null;
00277 YAxis y = getYAxis();
00278 if (y != null) {
00279 Dataset d = y.getDataset();
00280 if (d != null) {
00281 matchingPoints = d.getPoints(getId());
00282 }
00283 }
00284 return matchingPoints;
00285 }
00286
00287
00289
00290
00292 public String toString() {
00293 return toString(0);
00294 }
00295
00297 public String toString(Integer indentBy) {
00298 log().debug("Writing out point as a string");
00299 StringBuffer s = new StringBuffer();
00300 String indent = "";
00301 for (int i = 0; i < indentBy; ++i) indent += " ";
00302
00303 s.append(indent + "Point " + getId() + ": ");
00304 if (getAllErrors().size() > 0) s.append("( ");
00305 s.append(getValue());
00306 for (Uncertainty e : getAllErrors()) {
00307 s.append(" " + e.toString());
00308 }
00309 if (getAllErrors().size() > 0) s.append(" )");
00310 if (getYAxis() != null) {
00311 s.append(" " + getYAxis().getUnit().toString());
00312 }
00313 return s.toString();
00314 }
00315
00316
00318
00319
00321 public boolean equals(Object other) {
00322 if (this == other) return true;
00323 if (! (other instanceof Point)) return false;
00324 return hashCode() == other.hashCode();
00325 }
00326
00327
00329 public int hashCode() {
00330 int code = 57;
00331 if (getYAxis() != null) code ^= getYAxis().hashCode();
00332 if (getValue() != null) code ^= getValue().hashCode();
00333 if (getId() != null) code ^= getId().hashCode();
00334 return code;
00335 }
00336
00337
00339
00340
00342 public int compareTo(Point other) {
00343 log().debug("Comparing points...");
00344 if (getId() == null) {
00345 log().warn("Null Point ID");
00346 return 1;
00347 } else if (getId() > other.getId()) {
00348 log().debug("Greater than");
00349 return 1;
00350 } else if (getId() < other.getId()) {
00351 log().debug("Less than");
00352 return -1;
00353 } else {
00354 log().debug("Point IDs are equal");
00355 if (getYAxis() != null && other.getYAxis() != null) {
00356 return getYAxis().compareTo(other.getYAxis());
00357 } else {
00358 return 0;
00359 }
00360 }
00361 }
00362
00363 }