Bin.java

Go to the documentation of this file.
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         // Fall back to calculating the geometric centre
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) {
00209             if (getId() == null) {
00210                 int highestId = 0;
00211                 if (xAxis.getBins().size() > 0) {
00212                     highestId = xAxis.getBins().last().getId();
00213                 }
00214                 log().debug("Incrementing bin ID: " + (highestId + 1));
00215                 setId(highestId + 1);
00216             }
00217             log().debug("Adding bin to x-axis " + xAxis.getId() + ": ID = " + getId());
00218             getXAxis().getBins().add(this);
00219         }
00220         return this;
00221     }
00222 
00223 
00225 
00226 
00228     public SortedSet<Bin> getBins() {
00229         SortedSet<Bin> matchingBins = null;
00230         XAxis x = getXAxis();
00231         if (x != null) {
00232             Dataset d = x.getDataset();
00233             if (d != null) {
00234                 matchingBins = d.getBins(getId());
00235             }
00236         }
00237         return matchingBins;
00238     }
00239 
00241     public SortedSet<Point> getPoints() {
00242         SortedSet<Point> matchingPoints = null;
00243         XAxis x = getXAxis();
00244         if (x != null) {
00245             Dataset d = x.getDataset();
00246             if (d != null) {
00247                 matchingPoints = d.getPoints(getId());
00248             }
00249         }
00250         return matchingPoints;
00251     }
00252 
00253 
00255 
00256 
00258     public String toString() {
00259         return toString(0);
00260     }
00261 
00263     public String toString(Integer indentBy) {
00264         log().debug("Writing out x-axis as a string");
00265         StringBuffer s = new StringBuffer();
00266         String indent = "";
00267         for (int i  = 0; i < indentBy; ++i) indent += " ";
00268 
00269         s.append(indent + "Bin " + getId() + ": ");
00270         if (getLowValue() != null && getHighValue() != null) {
00271             s.append(getLowValue() + " - " + getHighValue());
00272         } else if (getFocus() != null) {
00273             s.append(getFocus());
00274             if (getWidth() != null) {
00275                 s.append(" (width = " + getWidth() + ")");
00276             }
00277         }
00278         return s.toString();
00279     }
00280 
00281 
00283 
00284 
00286     public boolean equals(Object other) {
00287         if (this == other) return true;
00288         if (! (other instanceof Bin)) return false;
00289         return hashCode() == other.hashCode();
00290     }
00291 
00292 
00294     public int hashCode() {
00295         int code = 79;
00296         if (getXAxis() != null) code ^= getXAxis().hashCode();
00297         if (getFocus() != null) code ^= getFocus().hashCode();
00298         code = 2*code + 1; // ensure this is even
00299         return code;
00300     }
00301 
00302 
00304 
00305 
00307     public int compareTo(Bin other) {
00308         log().debug("Comparing bins...");
00309         if (getId() == null) {
00310             log().warn("Null Bin ID");
00312             return 1; // Sort null bins at the end
00313         } else if (getId() > other.getId()) {
00314             log().debug("Greater than");
00315             return 1;
00316         } else if (getId() < other.getId()) {
00317             log().debug("Less than");
00318             return -1;
00319         } else {
00320             log().debug("Bin IDs are equal");
00321             if (getXAxis() != null && other.getXAxis() != null) {
00322                 return getXAxis().compareTo(other.getXAxis());
00323             } else {
00324                 return 0;
00325             }
00326         }
00327     }
00328 
00329 }

Generated on Tue Apr 21 15:54:38 2009 for HepData object model by  doxygen 1.5.5