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 getBins().clear();
00160 for (Bin b : bins) addBin(b);
00161 return this;
00162 }
00164 public XAxis setBins(Collection<Bin> bins) {
00165 getBins().clear();
00166 for (Bin b : bins) addBin(b);
00167 return this;
00168 }
00170 public XAxis setBins(Bin[] bins) {
00171 log().debug("Setting bins from array");
00172 setBins(Arrays.asList(bins));
00173 return this;
00174 }
00175
00177 public XAxis addBin(Bin bin) {
00178 if (bin != null) {
00179 bin.setXAxis(this);
00180 } else {
00181 log().warn("Tried to add a null Bin to an XAxis");
00182 }
00183 return this;
00184 }
00186 public XAxis removeBin(Bin bin) {
00187 if (bin != null) {
00188 bin.setXAxis(null);
00189 _bins.remove(bin);
00190 } else {
00191 log().warn("Tried to remove a null Bin from an XAxis");
00192 }
00193 return this;
00194 }
00195
00196
00198 public XAxis setBins(double[] binposns) {
00199 log().debug("Setting bins from array");
00200 getBins().clear();
00201 for (Double bp : binposns) addBin(bp);
00202 return this;
00203 }
00205 public XAxis addBin(double binpos) {
00206 return addBin(new Bin(binpos));
00207 }
00209 public XAxis removeBin(double binpos) {
00210 return removeBin(new Bin(binpos));
00211 }
00212
00213
00214
00216
00217
00218 public int compareTo(XAxis other) {
00219 log().debug("Comparing x-axes...");
00220 if (getId() == null) {
00221 log().warn("Null XAxis ID");
00222 return 1;
00223 } else if (getId() > other.getId()) {
00224 log().debug("Greater than");
00225 return 1;
00226 } else if (getId() < other.getId()) {
00227 log().debug("Less than");
00228 return -1;
00229 } else {
00230 log().debug("Equal to");
00231 return 0;
00232 }
00233 }
00234
00235
00237
00238
00240 public String toString() {
00241 return toString(0);
00242 }
00243
00245 public String toString(Integer indentBy) {
00246 log().debug("Writing out x-axis as a string");
00247 StringBuffer s = new StringBuffer();
00248 String indent = "";
00249 for (int i = 0; i < indentBy; ++i) indent += " ";
00250
00251 s.append(indent + "X-axis ID: " + getId() + "\n");
00252 s.append(indent + "Header: " + getHeader());
00253 for (Bin bin : getBins()) {
00254 s.append("\n" + bin.toString(indentBy + 2));
00255 }
00256 return s.toString();
00257 }
00258
00259
00261
00262
00264 public boolean equals(Object other) {
00265 if (this == other) return true;
00266 if (! (other instanceof XAxis)) return false;
00267 return hashCode() == other.hashCode();
00268 }
00269
00270
00272 public int hashCode() {
00273 int code = 341;
00274 if (getDataset() != null) code ^= getDataset().hashCode();
00275 if (getHeader() != null) code ^= getHeader().hashCode();
00276 if (getUnit() != null) code ^= getUnit().hashCode();
00277 code = 2*code + 1;
00278 return code;
00279 }
00280 }