Page MenuHomeHEPForge

XAxis.java
No OneTemporary

XAxis.java

package cedar.hepdata.model;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.Set;
import java.util.Collection;
import java.util.Arrays;
import javax.persistence.*;
import cedar.hepdata.util.Unit;
/**
* An XAxis contains header information and a set of {@link Bin}s.
* A {@link Dataset} has one or more XAxes.
*
* @author Andy Buckley
* @version $Date: 2009-11-06 12:09:24 +0000 (Fri, 06 Nov 2009) $ $Revision: 1305 $
*/
@Entity
@Table(name="XAxes")
public class XAxis extends Axis implements Comparable<XAxis> {
/** Unique-to-dataset x-axis ID */
@Column(name="LocalId", nullable=false)
private Integer _localId;
/** Collection of bins */
@OneToMany(mappedBy="_xAxis")
@org.hibernate.annotations.Fetch(value=org.hibernate.annotations.FetchMode.JOIN)
@org.hibernate.annotations.Cascade(value={org.hibernate.annotations.CascadeType.ALL,
org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@org.hibernate.annotations.Sort(type=org.hibernate.annotations.SortType.NATURAL)
@org.hibernate.annotations.BatchSize(size=20)
private SortedSet<Bin> _bins = new TreeSet<Bin>();
/** Parent dataset. */
@ManyToOne
protected Dataset _dataset;
////////////////////////////////////////////////
/** Default constructor */
public XAxis() {
super();
log().debug("Making an XAxis (No dataset, 0 arg constructor)");
}
public XAxis(String header) {
super(header);
log().debug("Making an XAxis (No dataset, 1 arg constructor)");
}
public XAxis(String header, Unit unit) {
super(header, unit);
log().debug("Making an XAxis (No dataset, 2 arg constructor)");
}
public XAxis(Dataset dataset) {
this();
setDataset(dataset);
log().debug("Making an XAxis (With dataset, 1 arg constructor)");
}
public XAxis(Dataset dataset, String header) {
this(header);
setDataset(dataset);
log().debug("Making an XAxis (With dataset, 2 arg constructor)");
}
public XAxis(Dataset dataset, String header, Unit unit) {
this(header, unit);
setDataset(dataset);
log().debug("Making an XAxis (With dataset, 3 arg constructor)");
}
////////////////////////////////////////////////
/** Get the Paper that this axis belongs to. Note two degrees of separation,
* since there's a dataset in-between: watch out for null returns during
* hierarchy construction, e.g. in unpersisting from XML or database. */
public Paper getPaper() {
if (getDataset() != null) {
return getDataset().getPaper();
} else {
return null;
}
}
// XAxis ID
public Integer getId() {
return _localId;
}
public XAxis setId(Integer xAxisId) {
_localId = xAxisId;
return this;
}
/** Get the Dataset that this axis belongs to. */
public Dataset getDataset() {
return _dataset;
}
/** Over-ride dataset assignment, to get null IDs right */
public XAxis setDataset(Dataset dataset) {
log().debug("Calling setDataset()");
_dataset = dataset;
if (dataset != null) {
if (getId() == null) {
int highestId = 0;
if (dataset.getXAxes().size() > 0) {
highestId = dataset.getXAxes().last().getId();
}
log().debug("Incrementing x-axis ID: " + (highestId + 1));
setId(highestId + 1);
}
log().debug("Adding myself to a Dataset");
dataset.getXAxes().add(this);
} else {
log().warn("Tried to attach a XAxis to a null Dataset");
}
return this;
}
/** Get associated y-axes */
public SortedSet<YAxis> getYAxes() {
if (getDataset() != null) {
return getDataset().getYAxes();
} else {
return null;
}
}
/** Get this axis' bins */
public SortedSet<Bin> getBins() {
return _bins;
}
/** Get a specific bin from this axis by bin ID code */
public Bin getBin(Integer binId) {
Bin theBin = null;
for (Bin b : getBins()) {
if (b.getId().equals(binId)) {
theBin = b;
break;
}
}
return theBin;
}
/** Set this axis' bins explicitly from a SortedSet (to keep reflection apps happy) */
public XAxis setBins(SortedSet<Bin> bins) {
getBins().clear();
for (Bin b : bins) addBin(b);
return this;
}
/** Set this axis' bins from a generic collection */
public XAxis setBins(Collection<Bin> bins) {
getBins().clear();
for (Bin b : bins) addBin(b);
return this;
}
/** Set this axis' bins from an array */
public XAxis setBins(Bin[] bins) {
log().debug("Setting bins from array");
setBins(Arrays.asList(bins));
return this;
}
/** Add a bin to this axis */
public XAxis addBin(Bin bin) {
if (bin != null) {
bin.setXAxis(this);
} else {
log().warn("Tried to add a null Bin to an XAxis");
}
return this;
}
/** Remove a bin from this axis */
public XAxis removeBin(Bin bin) {
if (bin != null) {
bin.setXAxis(null);
_bins.remove(bin);
} else {
log().warn("Tried to remove a null Bin from an XAxis");
}
return this;
}
/** Set bins from an array of doubles (for convenience). */
public XAxis setBins(double[] binposns) {
log().debug("Setting bins from array");
getBins().clear();
for (Double bp : binposns) addBin(bp);
return this;
}
/** Add a bin to this axis as a double (for convenience). */
public XAxis addBin(double binpos) {
return addBin(new Bin(binpos));
}
/** Remove a bin from this axis as a double (for convenience). */
public XAxis removeBin(double binpos) {
return removeBin(new Bin(binpos));
}
///////////////////////////////////////////////////////
public Double getMinValue() {
Double rtn = Double.MAX_VALUE;
for (Bin b : getBins()) {
rtn = Math.min(rtn, b.getLowValue());
}
return rtn;
}
public Double getMinPositiveValue() {
Double rtn = Double.MAX_VALUE;
for (Bin b : getBins()) {
if(b.getLowValue() > 0.0) rtn = Math.min(rtn, b.getLowValue());
}
return rtn;
}
public Double getMaxValue() {
Double rtn = -Double.MAX_VALUE;
for (Bin b : getBins()) {
rtn = Math.max(rtn, b.getHighValue());
}
return rtn;
}
///////////////////////////////////////////////////////
public int compareTo(XAxis other) {
log().debug("Comparing x-axes...");
if (getId() == null) {
log().warn("Null XAxis ID");
return 1; // Sort null x-axes at the end
} else if (getId() > other.getId()) {
log().debug("Greater than");
return 1;
} else if (getId() < other.getId()) {
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 x-axis as a string");
StringBuffer s = new StringBuffer();
String indent = "";
for (int i = 0; i < indentBy; ++i) indent += " ";
s.append(indent + "X-axis ID: " + getId() + "\n");
s.append(indent + "Header: " + getHeader());
for (Bin bin : getBins()) {
s.append("\n" + bin.toString(indentBy + 2));
}
return s.toString();
}
///////////////////////////////////////////////////////
/** Inheritance means that we need to override equals() */
public boolean equals(Object other) {
if (this == other) return true;
if (! (other instanceof XAxis)) return false;
return hashCode() == other.hashCode();
}
/** Inheritance means that we need to override equals() */
public int hashCode() {
int code = 341;
// if (getDataset() != null) {System.out.println("xaxis hashcode " + code);code ^= getDataset().hashCode();}
// if (getHeader() != null) code ^= getHeader().hashCode();
// if (getUnit() != null) code ^= getUnit().hashCode();
code = 2*code + 1; // ensure this is odd
return code;
}
}

File Metadata

Mime Type
text/plain
Expires
Sun, Feb 23, 2:56 PM (5 h, 13 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
4486741
Default Alt Text
XAxis.java (8 KB)

Event Timeline