00001 package cedar.hepdata.model;
00002
00003 import java.util.*;
00004 import javax.persistence.*;
00005
00006 import cedar.hepdata.util.Unit;
00007
00008
00017 @Entity
00018 @Table(name="YAxes")
00019 public class YAxis extends Axis implements Comparable<YAxis> {
00021 @Column(name="LocalId", nullable=false)
00022 private Integer _localId;
00023
00025 @OneToMany(mappedBy="_yAxis")
00026 @org.hibernate.annotations.Fetch(value=org.hibernate.annotations.FetchMode.JOIN)
00027 @org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL,
00028 org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
00029 @org.hibernate.annotations.Sort(type=org.hibernate.annotations.SortType.NATURAL)
00030 @org.hibernate.annotations.BatchSize(size=20)
00031 private SortedSet<Point> _points = new TreeSet<Point>();
00032
00034 @org.hibernate.annotations.CollectionOfElements
00035 @JoinTable(name="AxisErrors", joinColumns=@JoinColumn(name="AXIS_ID"))
00036 @org.hibernate.annotations.CollectionId(columns=@Column(name="AxisErrorId"),
00037 type=@org.hibernate.annotations.Type(type="long"),
00038 generator="sequence")
00039 @org.hibernate.annotations.Sort(type=org.hibernate.annotations.SortType.NATURAL)
00040 @org.hibernate.annotations.BatchSize(size=5)
00041 private SortedSet<AxisError> _errors = new TreeSet<AxisError>();
00042
00044 @OneToMany(mappedBy="_yAxis")
00045 @org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL,
00046 org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
00047 private Set<AxisProperty> _properties = new HashSet<AxisProperty>();
00048
00050 @OneToMany(mappedBy="_yAxis")
00051 @org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL,
00052 org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
00053 private Set<AxisReaction> _reactions = new HashSet<AxisReaction>();
00054
00056
00057
00058 @Column(name="Observable", nullable=true)
00059 private String _observable = null;
00060
00062 @org.hibernate.annotations.CollectionOfElements
00063 @JoinTable(name="YAxisComments", joinColumns=@JoinColumn(name="AXIS_ID"))
00064 @org.hibernate.annotations.IndexColumn(name="Posn")
00065 @Column(name="Comments", length=10000)
00066 private List<String> _comments = new Vector<String>();
00067
00069 @ManyToOne
00070 protected Dataset _dataset;
00071
00072
00074
00075
00077 public YAxis() {
00078 super();
00079 log().debug("Making a YAxis (0 arg constructor)");
00080 }
00081
00082 public YAxis(Dataset dataset) {
00083 this();
00084 log().debug("Making a YAxis (1 arg constructor)");
00085 setDataset(dataset);
00086 }
00087
00088 public YAxis(String header, String observable, Unit unit) {
00089 this();
00090 log().debug("Making a YAxis (3 arg constructor)");
00091 setHeader(header);
00092 setObservable(observable);
00093 setUnit(unit);
00094 }
00095
00096 public YAxis(Dataset dataset, String header, String observable, Unit unit) {
00097 this(header, observable, unit);
00098 log().debug("Making a YAxis (4 arg constructor)");
00099 setDataset(dataset);
00100 }
00101
00102
00104
00105
00109 public Paper getPaper() {
00110 if (getDataset() != null) {
00111 return getDataset().getPaper();
00112 } else {
00113 return null;
00114 }
00115 }
00116
00117
00118
00119 public List<String> getComments() {
00120 return _comments;
00121 }
00122 public YAxis setComments(List<String> comments) {
00123 _comments = comments;
00124 return this;
00125 }
00126 public YAxis addComment(String comment) {
00127 _comments.add(comment);
00128 return this;
00129 }
00130 public YAxis removeComment(String comment) {
00131 _comments.remove(comment);
00132 return this;
00133 }
00134
00135
00137
00138
00140 public Integer getId() {
00141 return _localId;
00142 }
00144 public YAxis setId(Integer yAxisId) {
00145 _localId = yAxisId;
00146 return this;
00147 }
00148
00149
00151 public Dataset getDataset() {
00152 return _dataset;
00153 }
00155 public YAxis setDataset(Dataset dataset) {
00156 log().debug("Calling setDataset()");
00157 _dataset = dataset;
00158 if (dataset != null) {
00159 if (getId() == null) {
00160 int highestId = 0;
00161 if (dataset.getYAxes().size() > 0) {
00162 highestId = dataset.getYAxes().last().getId();
00163 }
00164 log().debug("Incrementing y-axis ID: " + (highestId + 1));
00165 setId(highestId + 1);
00166 }
00167 log().debug("Adding myself to a Dataset");
00168 dataset.getYAxes().add(this);
00169 }
00170 return this;
00171 }
00172
00173
00175 public SortedSet<XAxis> getXAxes() {
00176 if (getDataset() != null) {
00177 return getDataset().getXAxes();
00178 } else {
00179 return null;
00180 }
00181 }
00182
00183
00185 public Set<AxisReaction> getReactions() {
00186 return _reactions;
00187 }
00189 public YAxis setReactions(Set<AxisReaction> reactions) {
00190 getReactions().clear();
00191 for (AxisReaction r : reactions) addReaction(r);
00192 return this;
00193 }
00195 public YAxis addReaction(AxisReaction reaction) {
00196 if (reaction != null) {
00197 reaction.setYAxis(this);
00198 } else {
00199 log().warn("Tried to add a null reaction to a y-axis");
00200 }
00201 return this;
00202 }
00204 public YAxis removeReaction(AxisReaction reaction) {
00205 if (reaction != null) {
00206 reaction.setYAxis(null);
00207 _reactions.remove(reaction);
00208 } else {
00209 log().warn("Tried to remove a null reaction from a y-axis");
00210 }
00211 return this;
00212 }
00213
00214
00216 public String getObservable() {
00217 return _observable;
00218 }
00219
00221 public YAxis setObservable(String observable) {
00222 _observable = observable;
00223 return this;
00224 }
00225
00226
00227
00229 public SortedSet<Point> getPoints() {
00230 return _points;
00231 }
00232
00234 public Point getPoint(Integer pointId) {
00235 Point thePoint = null;
00236 for (Point p : getPoints()) {
00237 if (p.getId().equals(pointId)) {
00238 thePoint = p;
00239 break;
00240 }
00241 }
00242 return thePoint;
00243 }
00244
00246 public YAxis setPoints(SortedSet<Point> points) {
00247 log().debug("Calling setPoints(SortedSet<Point>)");
00248 this.getPoints().clear();
00249 for (Point p : points) addPoint(p);
00250 return this;
00251 }
00252
00254 public YAxis setPoints(Collection<Point> points) {
00255 log().debug("Calling setPoints(Collection<Point>)");
00256 this.getPoints().clear();
00257 for (Point p : points) addPoint(p);
00258 return this;
00259 }
00260
00262 public YAxis setPoints(Point[] points) {
00263 setPoints(Arrays.asList(points));
00264 return this;
00265 }
00266
00268 public YAxis addPoint(Point point) {
00269 if (point != null) {
00270 point.setYAxis(this);
00271 } else {
00272 log().warn("Tried to add a null Point to a YAxis");
00273 }
00274 return this;
00275 }
00276
00278 public YAxis removePoint(Point point) {
00279 log().debug("Removing point: " + point);
00280 if (point != null) {
00281 getPoints().remove(point);
00282 } else {
00283 log().warn("Tried to remove a null Point from a YAxis");
00284 }
00285 log().debug("Removing point: done");
00286 return this;
00287 }
00288
00290 public YAxis setPoints(double[] pointvals) {
00291 this.getPoints().clear();
00292 for (Double pv : pointvals) addPoint(pv);
00293 return this;
00294 }
00295
00297 public YAxis addPoint(double pointval) {
00298 return addPoint(new Point(pointval));
00299 }
00300
00302 public YAxis removePoint(double pointval) {
00303 return removePoint(new Point(pointval));
00304 }
00305
00306
00308 public SortedSet<AxisError> getErrors() {
00309 return _errors;
00310 }
00312 public YAxis setErrors(SortedSet<AxisError> axisErrors) {
00313 getErrors().clear();
00314 for (AxisError e : axisErrors) addError(e);
00315 return this;
00316 }
00318 public YAxis setErrors(Collection<AxisError> axisErrors) {
00319 getErrors().clear();
00320 for (AxisError e : axisErrors) addError(e);
00321 return this;
00322 }
00324 public YAxis addError(AxisError axisError) {
00325 if (axisError != null) {
00326 axisError.setAxis(this);
00327 } else {
00328 log().warn("Tried to add a null AxisError to a YAxis");
00329 }
00330 return this;
00331 }
00333 public YAxis removeError(AxisError axisError) {
00334 if (axisError != null) {
00335 getErrors().remove(axisError);
00336 } else {
00337 log().warn("Tried to remove a null AxisError from a YAxis");
00338 }
00339 return this;
00340 }
00341
00342
00344 public SortedSet<Uncertainty> getDatasetErrors() {
00345 SortedSet<Uncertainty> dsErrors = new TreeSet<Uncertainty>();
00346 if (getDataset() != null) {
00347 dsErrors.addAll( getDataset().getErrors() );
00348 }
00349 return dsErrors;
00350 }
00352 public SortedSet<Uncertainty> getAllErrors() {
00353 SortedSet<Uncertainty> allErrors = new TreeSet<Uncertainty>();
00354 allErrors.addAll(getErrors());
00355 allErrors.addAll(getDatasetErrors());
00356 return allErrors;
00357 }
00358
00359
00361 public Set<AxisProperty> getProperties() {
00362 return _properties;
00363 }
00365 public YAxis setProperties(Set<AxisProperty> properties) {
00366 _properties = properties;
00367 return this;
00368 }
00370 public YAxis addProperty(AxisProperty property) {
00371 if (property != null) {
00372 property.setYAxis(this);
00373 _properties.add(property);
00374 } else {
00375 log().warn("Tried to add a null AxisProperty to a YAxis");
00376 }
00377 return this;
00378 }
00380 public YAxis removeProperty(AxisProperty property) {
00381 if (property != null) {
00382 property.setYAxis(null);
00383 _properties.remove(property);
00384 } else {
00385 log().warn("Tried to remove a null AxisProperty from a YAxis");
00386 }
00387 return this;
00388 }
00389
00390
00392
00393
00395 public int compareTo(YAxis other) {
00396 log().debug("Comparing y-axes...");
00397 if (getId() == null) {
00398 log().debug("Null YAxis ID");
00399 return 1;
00400 } else if (getId() > other.getId()) {
00401 log().debug("Greater than");
00402 return 1;
00403 } else if (getId() < other.getId()) {
00404 log().debug("Less than");
00405 return -1;
00406 } else {
00407 log().debug("Equal to");
00408 return 0;
00409 }
00410 }
00411
00412
00414
00415
00417 public String toString() {
00418 return toString(0);
00419 }
00420
00422 public String toString(Integer indentBy) {
00423 log().debug("Writing out y-axis as a string");
00424 StringBuffer s = new StringBuffer();
00425 String indent = "";
00426 for (int i = 0; i < indentBy; ++i) indent += " ";
00427
00428 s.append(indent + "Y-axis ID: " + getId() + "\n");
00429 s.append(indent + "Header: " + getHeader());
00430 s.append(indent + "Observable: " + getObservable());
00431 for (Point point : getPoints()) {
00432 s.append("\n" + point.toString(indentBy + 2));
00433 }
00434 return s.toString();
00435 }
00436
00437
00439
00440
00442 public boolean equals(Object other) {
00443 if (this == other) return true;
00444 if (! (other instanceof YAxis)) return false;
00445 return hashCode() == other.hashCode();
00446 }
00447
00448
00450 public int hashCode() {
00451 int code = 31;
00452 if (getDataset() != null) code ^= getDataset().hashCode();
00453 if (getHeader() != null) code ^= getHeader().hashCode();
00454 if (getUnit() != null) code ^= getUnit().hashCode();
00455 return code;
00456 }
00457
00458 }