00001 package cedar.hepdata.model;
00002
00003 import java.util.Set;
00004 import java.util.HashSet;
00005
00006 import javax.persistence.*;
00007
00008 import cedar.hepdata.util.*;
00009
00016 @Entity
00017 @Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
00018 @Table(name="BaseReactions")
00019 public class Reaction extends Storeable {
00021 @Id @GeneratedValue(strategy=GenerationType.TABLE)
00022 @Column(name="REACTION_ID")
00023 private Long _id;
00024
00026 @org.hibernate.annotations.CollectionOfElements
00027 @JoinTable(name="InitialStates", joinColumns=@JoinColumn(name="REACTION_ID"))
00028 @org.hibernate.annotations.CollectionId(columns=@Column(name="InitialStateId"),
00029 type=@org.hibernate.annotations.Type(type="long"),
00030 generator="sequence")
00031 private Set<Particle> _initialState = new HashSet<Particle>();
00032
00034 @org.hibernate.annotations.CollectionOfElements
00035 @JoinTable(name="FinalStates", joinColumns=@JoinColumn(name="REACTION_ID"))
00036 @org.hibernate.annotations.CollectionId(columns=@Column(name="FinalStateId"),
00037 type=@org.hibernate.annotations.Type(type="long"),
00038 generator="sequence")
00039 private Set<Particle> _finalState = new HashSet<Particle>();
00040
00041
00043
00044
00046 public Reaction() {}
00047
00048
00050
00052 public Set<Particle> getInitialState() {
00053 return _initialState;
00054 }
00056 public Reaction setInitialState(Set<Particle> initialState) {
00057 _initialState = initialState;
00058 return this;
00059 }
00064 public Reaction addToInitialState(Particle particle) {
00065 boolean foundType = false;
00066 for (Particle p : _initialState) {
00067 if (Particle.sameType(p, particle)) {
00068 foundType = true;
00069 p.setMultiplicity(p.getMultiplicity() + particle.getMultiplicity());
00070 break;
00071 }
00072 }
00073 if (!foundType) _initialState.add(particle);
00074 return this;
00075 }
00082 public Reaction removeFromInitialState(Particle particle) {
00083 for (Particle p : _initialState) {
00084 if (Particle.sameType(p, particle)) {
00085 int newMult = p.getMultiplicity() - particle.getMultiplicity();
00086 if (newMult <= 0) {
00087 _initialState.remove(p);
00088 } else {
00089 p.setMultiplicity(newMult);
00090 }
00091 break;
00092 }
00093 }
00094 return this;
00095 }
00097 public Reaction purgeFromInitialState(Particle particle) {
00098 for (Particle p : _initialState) {
00099 if (Particle.sameType(p, particle)) {
00100 _initialState.remove(p);
00101 }
00102 }
00103 return this;
00104 }
00105
00106
00108 public Set<Particle> getFinalState() {
00109 return _finalState;
00110 }
00112 public Reaction setFinalState(Set<Particle> finalState) {
00113 _finalState = finalState;
00114 return this;
00115 }
00120 public Reaction addToFinalState(Particle particle) {
00121 boolean foundType = false;
00122 for (Particle p : _finalState) {
00123 if (Particle.sameType(p, particle)) {
00124 foundType = true;
00125 p.setMultiplicity(p.getMultiplicity() + particle.getMultiplicity());
00126 break;
00127 }
00128 }
00129 if (!foundType) _finalState.add(particle);
00130 return this;
00131 }
00138 public Reaction removeFromFinalState(Particle particle) {
00139 for (Particle p : _finalState) {
00140 if (Particle.sameType(p, particle)) {
00141 int newMult = p.getMultiplicity() - particle.getMultiplicity();
00142 if (newMult <= 0) {
00143 _finalState.remove(p);
00144 } else {
00145 p.setMultiplicity(newMult);
00146 }
00147 break;
00148 }
00149 }
00150 return this;
00151 }
00153 public Reaction purgeFromFinalState(Particle particle) {
00154 for (Particle p : _finalState) {
00155 if (Particle.sameType(p, particle)) {
00156 _finalState.remove(p);
00157 }
00158 }
00159 return this;
00160 }
00161
00162
00164
00165
00167 public boolean equals(Object other) {
00168 log().debug("Comparing reactions...");
00169 if (this == other) return true;
00170 if (! (other instanceof Reaction)) return false;
00171 final Reaction test = (Reaction) other;
00172
00173 if (! this.getInitialState().equals( test.getInitialState() )) return false;
00174 if (test.getInitialState() != null && this.getInitialState() != null) {
00175 Set<Particle> is1 = this.getInitialState();
00176 Set<Particle> is2 = test.getInitialState();
00177 if (is1.size() != is2.size()) return false;
00178 for (Particle p : is1) {
00179 if (! is2.contains(p)) return false;
00180 }
00181 }
00182
00183 if (! this.getFinalState().equals( test.getFinalState() )) return false;
00184 if (test.getFinalState() != null && this.getFinalState() != null) {
00185 Set<Particle> fs1 = this.getFinalState();
00186 Set<Particle> fs2 = test.getFinalState();
00187 if (fs1.size() != fs2.size()) return false;
00188 for (Particle p : fs1) {
00189 if (! fs2.contains(p)) return false;
00190 }
00191 }
00192
00194
00195
00196 return true;
00197 }
00198
00199
00200
00202
00203
00204
00206 public String initialStateToString() {
00207 StringBuffer s = new StringBuffer();
00208 for (Particle ispart : getInitialState()) {
00209 s.append(" " + ispart.toString());
00210 }
00211 return s.toString().trim();
00212 }
00213
00214
00216 public String finalStateToString() {
00217 StringBuffer s = new StringBuffer();
00218 for (Particle fspart : getFinalState()) {
00219 s.append(" " + fspart.toString());
00220 }
00221 return s.toString().trim();
00222 }
00223
00224
00226 public String toString() {
00227 StringBuffer s = new StringBuffer();
00228 s.append(initialStateToString());
00229 s.append(" -> ");
00230 s.append(finalStateToString());
00231 return s.toString().trim();
00232 }
00233
00234 }