00001 package cedar.hepdata.model;
00002
00003 import java.util.Set;
00004 import java.util.SortedSet;
00005 import java.util.TreeSet;
00006 import java.util.HashSet;
00007
00008 import javax.persistence.*;
00009
00016 @Entity
00017 @Table(name="Bins")
00018 public class Bin extends Value implements Comparable<Bin> {
00020 @Column(name="LocalId", nullable=false)
00021 private Integer _localId;
00022
00025 @Column(name="Focus", nullable=true)
00026 private Double _focus = null;
00027
00029
00030 @Column(name="Width", nullable=true)
00031 private Double _width = null;
00032
00034 @Column(name="LowValue", nullable=true)
00035 private Double _lowValue = null;
00036
00038 @Column(name="HighValue", nullable=true)
00039 private Double _highValue = null;
00040
00044 @Column(name="Relation", nullable=false)
00045 @Enumerated(EnumType.STRING)
00046 private Relation _relation = Relation.EQUALS;
00047
00049 @ManyToOne
00050 private XAxis _xAxis;
00051
00052
00053
00055
00056
00057
00059 public Bin() {
00060 super();
00061 }
00062
00063 public Bin(Double centralValue) {
00064 setFocus(centralValue);
00065 }
00066
00067 public Bin(Double lowValue, Double highValue) {
00068 setLowValue(lowValue);
00069 setHighValue(highValue);
00070 }
00071
00072 public Bin(Double lowValue, Double highValue, Double focus) {
00073 this(lowValue, highValue);
00074 setFocus(focus);
00075 }
00076
00077 public Bin(XAxis xAxis) {
00078 setXAxis(xAxis);
00079 }
00080
00081 public Bin(XAxis xAxis, Double centralValue) {
00082 this(centralValue);
00083 setXAxis(xAxis);
00084 }
00085
00086 public Bin(XAxis xAxis, Double lowValue, Double highValue) {
00087 this(lowValue, highValue);
00088 setXAxis(xAxis);
00089 }
00090
00091 public Bin(XAxis xAxis, Double lowValue, Double highValue, Double focus) {
00092 this(lowValue, highValue, focus);
00093 setXAxis(xAxis);
00094 }
00095
00096
00098
00099
00101 public Integer getId() {
00102 return _localId;
00103 }
00105 public Bin setId(Integer binId) {
00106 _localId = binId;
00107 return this;
00108 }
00109
00110
00113 public Double getFocus() {
00114 if (_focus != null) return _focus;
00115
00116
00117 log().debug("Asked for focus, but focus is null.");
00118 if (getLowValue() != null && getHighValue() != null) {
00119 log().info("Calculating central value from high and low edges");
00120 return (getLowValue() + getHighValue()) / 2.0;
00121 } else {
00122 log().warn("Could neither retrieve nor calculate bin focus.");
00123 return null;
00124 }
00125 }
00126
00128 public Bin setFocus(Double focus) {
00129 _focus = focus;
00130 return this;
00131 }
00132
00133
00147 public Relation getRelation() {
00148 return _relation;
00149 }
00150
00152 public Bin setRelation(Relation relation) {
00153 _relation = relation;
00154 return this;
00155 }
00156
00157
00159 public Double getLowValue() {
00160 return _lowValue;
00161 }
00163 public Bin setLowValue(Double lowValue) {
00164 _lowValue = lowValue;
00165 return this;
00166 }
00167
00168
00170 public Double getHighValue() {
00171 return _highValue;
00172 }
00174 public Bin setHighValue(Double highValue) {
00175 _highValue = highValue;
00176 return this;
00177 }
00178
00179
00181
00182 public Double getWidth() {
00183 if (_width == null) {
00184 log().info("Asked for width, but Width is null.");
00185 if (getHighValue() != null && getLowValue() != null) {
00186 log().info("Calculating width from high and low edges");
00187 return Math.abs(getHighValue() - getLowValue());
00188 }
00189 }
00190 return _width;
00191 }
00192
00194
00195 public Bin setWidth(Double width) {
00196 _width = width;
00197 return this;
00198 }
00199
00200
00202 public XAxis getXAxis() {
00203 return _xAxis;
00204 }
00206 public Bin setXAxis(XAxis xAxis) {
00207 _xAxis = xAxis;
00208 if (getXAxis() != null) getXAxis().getBins().add(this);
00209 return this;
00210 }
00211
00212
00214
00215
00217 public SortedSet<Bin> getBins() {
00218 SortedSet<Bin> matchingBins = null;
00219 XAxis x = getXAxis();
00220 if (x != null) {
00221 Dataset d = x.getDataset();
00222 if (d != null) {
00223 matchingBins = d.getBins(getId());
00224 }
00225 }
00226 return matchingBins;
00227 }
00228
00230 public SortedSet<Point> getPoints() {
00231 SortedSet<Point> matchingPoints = null;
00232 XAxis x = getXAxis();
00233 if (x != null) {
00234 Dataset d = x.getDataset();
00235 if (d != null) {
00236 matchingPoints = d.getPoints(getId());
00237 }
00238 }
00239 return matchingPoints;
00240 }
00241
00242
00244
00245
00247 public String toString() {
00248 return toString(0);
00249 }
00250
00252 public String toString(Integer indentBy) {
00253 log().debug("Writing out x-axis as a string");
00254 StringBuffer s = new StringBuffer();
00255 String indent = "";
00256 for (int i = 0; i < indentBy; ++i) indent += " ";
00257
00258 s.append(indent + "Bin " + getId() + ": ");
00259 if (getLowValue() != null && getHighValue() != null) {
00260 s.append(getLowValue() + " - " + getHighValue());
00261 } else if (getFocus() != null) {
00262 s.append(getFocus());
00263 if (getWidth() != null) {
00264 s.append(" (width = " + getWidth() + ")");
00265 }
00266 }
00267 return s.toString();
00268 }
00269
00270
00272
00273
00275 public boolean equals(Object other) {
00276 if (this == other) return true;
00277 if (! (other instanceof Bin)) return false;
00278
00279 final Bin test = (Bin) other;
00280 if (test.getXAxis() != null && !test.getXAxis().equals(getXAxis()) ) return false;
00282 if (test.getFocus() != null && getFocus() != null &&
00283 (test.getFocus() - getFocus())/getFocus() > 1e-6 ) return false;
00284
00285
00286 return true;
00287 }
00288
00289
00291 public int hashCode() {
00292 int code = 0;
00293 if (getXAxis() != null) code += 100000 * getXAxis().hashCode();
00294 if (getFocus() != null) code += (int) (1000 * getFocus());
00295 code *= 2; ++code;
00296 return code;
00297 }
00298
00299
00301
00302
00304 public int compareTo(Bin other) {
00305 log().debug("Comparing bins...");
00306 if (getId() == null) {
00307 log().warn("Null Bin ID");
00309 return 1;
00310 } else if (getId() > other.getId()) {
00311 log().debug("Greater than");
00312 return 1;
00313 } else if (getId() < other.getId()) {
00314 log().debug("Less than");
00315 return -1;
00316 } else {
00317 log().debug("Bin IDs are equal");
00318 if (getXAxis() != null && other.getXAxis() != null) {
00319 return getXAxis().compareTo(other.getXAxis());
00320 } else {
00321 return 0;
00322 }
00323 }
00324 }
00325
00326 }