Page MenuHomeHEPForge

YAxis.java
No OneTemporary

YAxis.java

package cedar.hepdata.model;
import java.util.Collection;
import java.util.Arrays;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Set;
import java.util.HashSet;
import cedar.hepdata.util.Unit;
/**
* A YAxis contains header information, a set of {@link Point}s, a set of {@link
* AxisError}s and a description, which includes {@link AxisProperty}s, a {@link
* Reaction} and an {@link Observable}. A {@link Dataset} has one or more YAxes.
*
* @author Andy Buckley
* @version $Date: 2006-12-13 16:47:20 +0000 (Wed, 13 Dec 2006) $ $Revision: 842 $
*/
public class YAxis extends Axis implements Comparable<YAxis> {
/** Unique-to-dataset y-axis ID */
private Integer yAxisId;
/** Collection of points */
private SortedSet<Point> points = new TreeSet<Point>();
/** Collection of axis-level errors */
private Set<AxisError> axisErrors = new HashSet<AxisError>();
/** Collection of properties, such as sqrt(s), Q^2 etc. ranges */
private Set<AxisProperty> properties = new HashSet<AxisProperty>();
/** The reaction being measured */
private Set<Reaction> reactions = new HashSet<Reaction>();
/** The observable being measured */
private Observable observable = null;
////////////////////////////////////////////////////////
/** No-arg constructor */
public YAxis() {
super();
log().debug("Making a YAxis (0 arg constructor)");
}
public YAxis(String header) {
super(header);
log().debug("Making a YAxis (1 arg constructor)");
}
public YAxis(String header, Unit unit) {
super(header, unit);
log().debug("Making a YAxis (2 arg constructor)");
}
public YAxis(Dataset dataset) {
super(dataset);
log().debug("Making a YAxis (1 arg constructor)");
}
public YAxis(Dataset dataset, String header) {
super(dataset, header);
log().debug("Making a YAxis (2 arg constructor)");
}
public YAxis(Dataset dataset, String header, Unit unit) {
super(dataset, header, unit);
log().debug("Making a YAxis (3 arg constructor)");
}
public YAxis(Observable observable) {
this();
log().debug("Making a YAxis (1 arg constructor)");
setObservable(observable);
}
public YAxis(Observable observable, String header) {
this(header);
log().debug("Making a YAxis (2 arg constructor)");
setObservable(observable);
}
public YAxis(Observable observable, String header, Unit unit) {
this(header, unit);
log().debug("Making a YAxis (3 arg constructor)");
setObservable(observable);
}
public YAxis(Dataset dataset, Observable observable) {
this(dataset);
log().debug("Making a YAxis (2 arg constructor)");
setObservable(observable);
}
public YAxis(Dataset dataset, Observable observable, String header) {
this(dataset, header);
log().debug("Making a YAxis (3 arg constructor)");
setObservable(observable);
}
public YAxis(Dataset dataset, Observable observable, String header, Unit unit) {
this(dataset, header, unit);
log().debug("Making a YAxis (4 arg constructor)");
setObservable(observable);
}
////////////////////////////////////////////////////////
// YAxis ID
public Integer getYAxisId() {
return yAxisId;
}
public YAxis setYAxisId(Integer yAxisId) {
this.yAxisId = yAxisId;
return this;
}
/** Over-ride dataset assignment, to get null IDs right */
public YAxis setDataset(Dataset dataset) {
log().debug("Calling setDataset()");
this.dataset = dataset;
if (dataset != null) {
if (getYAxisId() == null) {
int highestId = 0;
if (dataset.getYAxes().size() > 0) {
highestId = dataset.getYAxes().last().getYAxisId();
}
log().debug("Incrementing y-axis ID: " + (highestId + 1));
setYAxisId(highestId + 1);
}
log().debug("Adding myself to a Dataset");
dataset.getYAxes().add(this);
} else {
log().warn("Tried to attach a YAxis to a null Dataset");
}
return this;
}
/** Get associated x-axes */
public SortedSet<XAxis> getXAxes() {
if (getDataset() != null) {
return getDataset().getXAxes();
} else {
/// @todo Is this ideal? Return null or throw instead? Null could produce runtime error.
return new TreeSet<XAxis>();
}
}
// Reactions
public Set<Reaction> getReactions() {
return reactions;
}
public YAxis setReactions(Set<Reaction> reactions) {
Set<Reaction> toAdd = new HashSet<Reaction>();
Set<Reaction> toRemove = new HashSet<Reaction>();
for (Reaction r : this.reactions) {
if (! reactions.contains(r)) toRemove.add(r);
}
for (Reaction r : reactions) {
if (! this.reactions.contains(r)) toAdd.add(r);
}
for (Reaction r : toRemove) removeReaction(r);
for (Reaction r : toAdd) addReaction(r);
return this;
}
public YAxis addReaction(Reaction reaction) {
if (reaction != null) {
reaction.getYAxes().add(this);
this.reactions.add(reaction);
} else {
log().warn("Tried to add a null reaction to a y-axis");
}
return this;
}
public YAxis removeReaction(Reaction reaction) {
if (reaction != null) {
reaction.getYAxes().remove(this);
this.reactions.remove(reaction);
} else {
log().warn("Tried to remove a null reaction from a y-axis");
}
return this;
}
// Observable
public Observable getObservable() {
return observable;
}
public YAxis setObservable(Observable observable) {
if (observable != null) observable.getYAxes().add(this);
this.observable = observable;
return this;
}
/** Get Points */
public SortedSet<Point> getPoints() {
return points;
}
/** Get a specific point from this axis by point ID code */
public Point getPoint(Integer pointId) {
Point thePoint = null;
for (Point p : getPoints()) {
if (p.getPointId() == pointId) {
thePoint = p;
break;
}
}
return thePoint;
}
/** Explicit set-points-from-SortedSet to keep reflection apps happy */
public YAxis setPoints(SortedSet<Point> points) {
log().debug("Calling setPoints(SortedSet<Point>)");
this.points.clear();
for (Point p : points) addPoint(p);
return this;
}
/** Set points from a Collection */
public YAxis setPoints(Collection<Point> points) {
log().debug("Calling setPoints(Collection<Point>)");
this.points.clear();
for (Point p : points) addPoint(p);
return this;
}
/** Set points from an array */
public YAxis setPoints(Point[] points) {
setPoints(Arrays.asList(points));
return this;
}
/** Add a point */
public YAxis addPoint(Point point) {
if (point != null) {
if (point.getPointId() == null) {
int highestId = 0;
if (getPoints().size() > 0) {
highestId = getPoints().last().getPointId();
}
log().debug("Incrementing point ID: " + (highestId + 1));
point.setPointId(highestId + 1);
}
log().debug("Adding point: ID = " + point.getPointId());
point.setYAxis(this);
this.points.add(point);
} else {
log().warn("Tried to add a null Point to a YAxis");
}
return this;
}
/** Remove a point */
public YAxis removePoint(Point point) {
log().debug("Removing point: " + point);
if (point != null) {
this.points.remove(point);
point.setYAxis(null); /// @todo Data orphaning: CHECK CASCADE
} else {
log().warn("Tried to remove a null Point from a YAxis");
}
log().debug("Removing point: done");
return this;
}
// Axis errors
public Set<AxisError> getErrors() {
return axisErrors;
}
public YAxis setErrors(Set<AxisError> axisErrors) {
this.axisErrors.clear();
for (AxisError e : axisErrors) addError(e);
return this;
}
public YAxis addError(AxisError axisError) {
if (axisError != null) {
axisError.setYAxis(this);
this.axisErrors.add(axisError);
} else {
log().warn("Tried to add a null AxisError to a YAxis");
}
return this;
}
public YAxis removeError(AxisError axisError) {
if (axisError != null) {
this.axisErrors.remove(axisError);
axisError.setYAxis(null); // @todo Data orphaning: cascade should handle this. CHECK
} else {
log().warn("Tried to remove a null AxisError from a YAxis");
}
return this;
}
// Other errors
public Set<Error> getDatasetErrors() {
Set<Error> dsErrors = new HashSet<Error>();
if (getDataset() != null) {
dsErrors.addAll( getDataset().getErrors() );
}
return dsErrors;
}
public Set<Error> getAllErrors() {
Set<Error> allErrors = new HashSet<Error>();
allErrors.addAll(getErrors());
allErrors.addAll(getDatasetErrors());
return allErrors;
}
// Properties
public Set<AxisProperty> getProperties() {
return properties;
}
public YAxis setProperties(Set<AxisProperty> properties) {
this.properties.clear();
for (AxisProperty p : properties) addProperty(p);
return this;
}
public YAxis addProperty(AxisProperty property) {
if (property != null) {
property.setYAxis(this);
this.properties.add(property);
} else {
log().warn("Tried to add a null AxisProperty to a YAxis");
}
return this;
}
public YAxis removeProperty(AxisProperty property) {
if (property != null) {
property.setYAxis(null); /// @todo Data orphaning: CHECK THAT CASCADE WORKS
this.properties.remove(property);
} else {
log().warn("Tried to remove a null AxisProperty from a YAxis");
}
return this;
}
///////////////////////////////////////////////////////
public int compareTo(YAxis other) {
log().debug("Comparing y-axes...");
if (getYAxisId() == null) {
log().warn("Null YAxis ID");
return 1; // Sort null y-axes at the end
} else if (getYAxisId() > other.getYAxisId()) {
log().debug("Greater than");
return 1;
} else if (getYAxisId() < other.getYAxisId()) {
log().debug("Less than");
return -1;
} else {
log().debug("Equal to");
return 0;
}
}
///////////////////////////////////////////////////////
/** String representation */
public String toString() {
return toString(0);
}
/** String representation with indent */
public String toString(Integer indentBy) {
log().debug("Writing out y-axis as a string");
StringBuffer s = new StringBuffer();
String indent = "";
for (int i = 0; i < indentBy; ++i) indent += " ";
s.append(indent + "Y-axis ID: " + getYAxisId() + "\n");
s.append(indent + "Header: " + getHeader());
for (Point point : points) {
s.append("\n" + point.toString(indentBy + 2));
}
return s.toString();
}
///////////////////////////////////////////////////////
public boolean equals(Object other) {
if (this == other) return true;
if (! (other instanceof YAxis)) return false;
final YAxis test = (YAxis) other;
if (! test.getPaper().equals(getPaper()) ) return false;
if (! test.getDataset().equals(getDataset()) ) return false;
if (! test.getHeader().equals(getHeader()) ) return false;
if (! test.getUnit().equals(getUnit()) ) return false;
return true;
}
public int hashCode() {
int code = 0;
if (getPaper() != null) code += 1000000 * getPaper().hashCode();
if (getDataset() != null) code += 100 * getDataset().hashCode();
if (getHeader() != null) code += 10 * getHeader().hashCode();
if (getUnit() != null) code += getUnit().hashCode();
code *= 2; // ensure this is even
return code;
}
}

File Metadata

Mime Type
text/plain
Expires
Sun, Feb 23, 2:40 PM (22 h, 18 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4486671
Default Alt Text
YAxis.java (12 KB)

Event Timeline