1 package uk.ac.cam.ch.wwmm.opsin;
2 
3 import uk.ac.cam.ch.wwmm.opsin.BondStereo.BondStereoValue;
4 
5 /**A bond, between two atoms.
6  *
7  * @author ptc24
8  * @author dl387
9  *
10  */
11 class Bond {
12 	/** The Atom the bond comes from */
13 	private final Atom from;
14 	/** The Atom the bond goes to */
15 	private final Atom to;
16 	/** The bond order */
17 	private int order;
18 
19 	static enum SMILES_BOND_DIRECTION{
20 		RSLASH,
21 		LSLASH
22 	}
23 	/** If this bond was built from SMILES can be set to either RSLASH or LSLASH. Subsequently read to add a bondStereoElement
24 	 * null by default*/
25 	private SMILES_BOND_DIRECTION smilesBondDirection = null;
26 
27 	/**
28 	 * Holds the bondStereo object associated with this bond
29 	 * null by default
30 	 */
31 	private BondStereo bondStereo = null;
32 
33 	/** DO NOT CALL DIRECTLY EXCEPT FOR TESTING
34 	 * Creates a new Bond.
35 	 *
36 	 * @param from The Atom the bond comes from.
37 	 * @param to The Atom the bond goes to.
38 	 * @param order The bond order.
39 	 */
Bond(Atom from, Atom to, int order)40 	Bond(Atom from, Atom to, int order) {
41 		if (from == to){
42 			throw new IllegalArgumentException("Bonds must be made between different atoms");
43 		}
44 		if (order < 1 || order > 3){
45 			throw new IllegalArgumentException("Bond order must be 1, 2 or 3");
46 		}
47 		if (from == null){
48 			throw new IllegalArgumentException("From atom was null!");
49 		}
50 		if (to == null){
51 			throw new IllegalArgumentException("To atom was null!");
52 		}
53 		this.from = from;
54 		this.to = to;
55 		this.order = order;
56 	}
57 
58 	/**
59 	 * Gets from ID
60 	 * @return ID
61 	 */
getFrom()62 	int getFrom() {
63 		return from.getID();
64 	}
65 
66 	/**
67 	 * Gets to ID
68 	 * @return ID
69 	 */
getTo()70 	int getTo() {
71 		return to.getID();
72 	}
73 
74 	/**Gets order.
75     * @return*/
getOrder()76 	int getOrder() {
77 		return order;
78 	}
79 
80 	/**Sets order.
81     * @param order*/
setOrder(int order)82 	void setOrder(int order) {
83 		this.order = order;
84 	}
85 
86 	/**
87 	 * Gets from Atom
88 	 * @return Atom
89 	 */
getFromAtom()90 	Atom getFromAtom() {
91 		return from;
92 	}
93 
94 	/**
95 	 * Gets to Atom
96 	 * @return Atom
97 	 */
getToAtom()98 	Atom getToAtom() {
99 		return to;
100 	}
101 
102 	/**Adds to the bond order.
103 	 *
104 	 * @param o The value to be added to the bond order.
105 	 */
addOrder(int o)106 	void addOrder(int o) {
107 		order += o;
108 	}
109 
110 	/**
111 	 * Returns either null or RSLASH or LSLASH
112 	 * @return
113 	 */
getSmilesStereochemistry()114 	SMILES_BOND_DIRECTION getSmilesStereochemistry() {
115 		return smilesBondDirection;
116 	}
117 
setSmilesStereochemistry(SMILES_BOND_DIRECTION bondDirection)118 	void setSmilesStereochemistry(SMILES_BOND_DIRECTION bondDirection) {
119 		this.smilesBondDirection = bondDirection;
120 	}
121 
getBondStereo()122 	BondStereo getBondStereo() {
123 		return bondStereo;
124 	}
125 
setBondStereo(BondStereo bondStereo)126 	void setBondStereo(BondStereo bondStereo) {
127 		this.bondStereo = bondStereo;
128 	}
129 
setBondStereoElement(Atom[] atomRefs4, BondStereoValue cOrT)130 	void setBondStereoElement(Atom[] atomRefs4, BondStereoValue cOrT) {
131 		bondStereo = new BondStereo(atomRefs4, cOrT);
132 	}
133 
134 	/**
135 	 * Returns the atom at the other end of the bond to given atom
136 	 * @param atom
137 	 * @return
138 	 */
getOtherAtom(Atom atom)139 	Atom getOtherAtom(Atom atom) {
140 		if (from == atom){
141 			return to;
142 		}
143 		else if (to == atom){
144 			return from;
145 		}
146 		else{
147 			return null;
148 		}
149 	}
150 
151 	@Override
hashCode()152 	public int hashCode() {
153 		final int prime = 31;
154 		int result = 1;
155 		result = prime * result + from.getID();
156 		result = prime * result + to.getID();
157 		return result;
158 	}
159 
160 	@Override
equals(Object obj)161 	public boolean equals(Object obj) {
162 		if (this == obj) {
163 			return true;
164 		}
165 		if (obj == null) {
166 			return false;
167 		}
168 		if (getClass() != obj.getClass()) {
169 			return false;
170 		}
171 		Bond other = (Bond) obj;
172 
173 		if (from == other.from &&
174 				to == other.to){
175 			return true;
176 		}
177 		if (from == other.to &&
178 				to == other.from){
179 			return true;
180 		}
181 
182 		return false;
183 	}
184 }
185