00001 package cedar.hepdata.model;
00002
00003 import java.util.SortedSet;
00004 import java.util.TreeSet;
00005 import java.util.Set;
00006 import java.util.Collection;
00007 import java.util.Arrays;
00008
00009 import javax.persistence.*;
00010
00011 import cedar.hepdata.util.Unit;
00012
00013
00021 @Entity
00022 @Table(name="XAxes")
00023 public class XAxis extends Axis implements Comparable<XAxis> {
00025 @Column(name="LocalId", nullable=false)
00026 private Integer _localId;
00027
00029 @OneToMany(mappedBy="_xAxis")
00030 @org.hibernate.annotations.Fetch(value=org.hibernate.annotations.FetchMode.JOIN)
00031 @org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL,
00032 org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
00033 @org.hibernate.annotations.Sort(type=org.hibernate.annotations.SortType.NATURAL)
00034 @org.hibernate.annotations.BatchSize(size=20)
00035 private SortedSet<Bin> _bins = new TreeSet<Bin>();
00036
00038 @ManyToOne
00039 protected Dataset _dataset;
00040
00041
00043
00044
00046 public XAxis() {
00047 super();
00048 log().debug("Making an XAxis (No dataset, 0 arg constructor)");
00049 }
00050
00051 public XAxis(String header) {
00052 super(header);
00053 log().debug("Making an XAxis (No dataset, 1 arg constructor)");
00054 }
00055
00056 public XAxis(String header, Unit unit) {
00057 super(header, unit);
00058 log().debug("Making an XAxis (No dataset, 2 arg constructor)");
00059 }
00060
00061 public XAxis(Dataset dataset) {
00062 this();
00063 setDataset(dataset);
00064 log().debug("Making an XAxis (With dataset, 1 arg constructor)");
00065 }
00066
00067 public XAxis(Dataset dataset, String header) {
00068 this(header);
00069 setDataset(dataset);
00070 log().debug("Making an XAxis (With dataset, 2 arg constructor)");
00071 }
00072
00073 public XAxis(Dataset dataset, String header, Unit unit) {
00074 this(header, unit);
00075 setDataset(dataset);
00076 log().debug("Making an XAxis (With dataset, 3 arg constructor)");
00077 }
00078
00079
00081
00082
00086 public Paper getPaper() {
00087 if (getDataset() != null) {
00088 return getDataset().getPaper();
00089 } else {
00090 return null;
00091 }
00092 }
00093
00094
00095
00096 public Integer getId() {
00097 return _localId;
00098 }
00099 public XAxis setId(Integer xAxisId) {
00100 _localId = xAxisId;
00101 return this;
00102 }
00103
00104
00106 public Dataset getDataset() {
00107 return _dataset;
00108 }
00110 public XAxis setDataset(Dataset dataset) {
00111 log().debug("Calling setDataset()");
00112 _dataset = dataset;
00113 if (dataset != null) {
00114 if (getId() == null) {
00115 int highestId = 0;
00116 if (dataset.getXAxes().size() > 0) {
00117 highestId = dataset.getXAxes().last().getId();
00118 }
00119 log().debug("Incrementing x-axis ID: " + (highestId + 1));
00120 setId(highestId + 1);
00121
00122 }
00123 log().debug("Adding myself to a Dataset");
00124 dataset.getXAxes().add(this);
00125 } else {
00126 log().warn("Tried to attach a XAxis to a null Dataset");
00127 }
00128 return this;
00129 }
00130
00131
00133 public SortedSet<YAxis> getYAxes() {
00134 if (getDataset() != null) {
00135 return getDataset().getYAxes();
00136 } else {
00137 return null;
00138 }
00139 }
00140
00141
00143 public SortedSet<Bin> getBins() {
00144 return _bins;
00145 }
00147 public Bin getBin(Integer binId) {
00148 Bin theBin = null;
00149 for (Bin b : getBins()) {
00150 if (b.getId() == binId) {
00151 theBin = b;
00152 break;
00153 }
00154 }
00155 return theBin;
00156 }
00158 public XAxis setBins(SortedSet<Bin> bins) {
00159 _bins = bins;
00160 return this;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00176 public XAxis addBin(Bin bin) {
00177 if (bin != null) {
00178 if (bin.getId() == null) {
00179 int highestId = 0;
00180 if (getBins().size() > 0) {
00181 highestId = getBins().last().getId();
00182 }
00183 log().debug("Incrementing bin ID: " + (highestId + 1));
00184 bin.setId(highestId + 1);
00185 }
00186 log().debug("Adding bin: ID = " + bin.getId());
00187 bin.setXAxis(this);
00188 } else {
00189 log().warn("Tried to add a null Bin to an XAxis");
00190 }
00191 return this;
00192 }
00194 public XAxis removeBin(Bin bin) {
00195 if (bin != null) {
00196 bin.setXAxis(null);
00197 _bins.remove(bin);
00198 } else {
00199 log().warn("Tried to remove a null Bin from an XAxis");
00200 }
00201 return this;
00202 }
00203
00204
00206
00207
00208 public int compareTo(XAxis other) {
00209 log().debug("Comparing x-axes...");
00210 if (getId() == null) {
00211 log().warn("Null XAxis ID");
00212 return 1;
00213 } else if (getId() > other.getId()) {
00214 log().debug("Greater than");
00215 return 1;
00216 } else if (getId() < other.getId()) {
00217 log().debug("Less than");
00218 return -1;
00219 } else {
00220 log().debug("Equal to");
00221 return 0;
00222 }
00223 }
00224
00225
00227
00228
00230 public String toString() {
00231 return toString(0);
00232 }
00233
00235 public String toString(Integer indentBy) {
00236 log().debug("Writing out x-axis as a string");
00237 StringBuffer s = new StringBuffer();
00238 String indent = "";
00239 for (int i = 0; i < indentBy; ++i) indent += " ";
00240
00241 s.append(indent + "X-axis ID: " + getId() + "\n");
00242 s.append(indent + "Header: " + getHeader());
00243 for (Bin bin : getBins()) {
00244 s.append("\n" + bin.toString(indentBy + 2));
00245 }
00246 return s.toString();
00247 }
00248
00249
00251
00252
00254 public boolean equals(Object other) {
00255 if (this == other) return true;
00256 if (! (other instanceof XAxis)) return false;
00257
00258 final XAxis test = (XAxis) other;
00259 if (! test.getPaper().equals(getPaper()) ) return false;
00260 if (! test.getDataset().equals(getDataset()) ) return false;
00261 if (! test.getHeader().equals(getHeader()) ) return false;
00262 if (! test.getUnit().equals(getUnit()) ) return false;
00263 return true;
00264 }
00265
00266
00268 public int hashCode() {
00269 int code = 0;
00270 if (getPaper() != null) code += 1000000 * getPaper().hashCode();
00271 if (getDataset() != null) code += 100 * getDataset().hashCode();
00272 if (getHeader() != null) code += 10 * getHeader().hashCode();
00273 if (getUnit() != null) code += getUnit().hashCode();
00274 code *= 2; ++code;
00275 return code;
00276 }
00277 }