001package com.hfg.chem;
002
003
004import com.hfg.util.CompareUtil;
005import com.hfg.util.StringBuilderPlus;
006
007//------------------------------------------------------------------------------
008/**
009 Represents a covalent bond between atoms.
010
011 @author J. Alex Taylor, hairyfatguy.com
012 */
013//------------------------------------------------------------------------------
014// com.hfg XML/HTML Coding Library
015//
016// This library is free software; you can redistribute it and/or
017// modify it under the terms of the GNU Lesser General Public
018// License as published by the Free Software Foundation; either
019// version 2.1 of the License, or (at your option) any later version.
020//
021// This library is distributed in the hope that it will be useful,
022// but WITHOUT ANY WARRANTY; without even the implied warranty of
023// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024// Lesser General Public License for more details.
025//
026// You should have received a copy of the GNU Lesser General Public
027// License along with this library; if not, write to the Free Software
028// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029//
030// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
031// jataylor@hairyfatguy.com
032//------------------------------------------------------------------------------
033
034public class CovalentBond implements Comparable
035{
036   private Atom mFirstAtom;
037   private Integer mBondOrder; // i.e: 1, 2, 3, or 4
038   private Atom mSecondAtom;
039
040   // Indicates if this bond is part of an aromatic ring
041   private Boolean mAromatic;
042
043   // Used for indicating the configuration of atoms around double bonds (cis/trans).
044   private Boolean mUp;
045   private Boolean mDown;
046
047   private Atom[] mSortedAtoms = new Atom[2];
048
049   //##########################################################################
050   // CONSTRUCTORS
051   //##########################################################################
052
053   //--------------------------------------------------------------------------
054   public CovalentBond(Atom inAtom1, int inBondOrder)
055   {
056      this(inAtom1, inBondOrder, null);
057   }
058
059   //--------------------------------------------------------------------------
060   public CovalentBond(Atom inAtom1, Atom inAtom2)
061   {
062      this(inAtom1, null, inAtom2);
063   }
064
065   //--------------------------------------------------------------------------
066   public CovalentBond(Atom inAtom1, Integer inBondOrder, Atom inAtom2)
067   {
068      mFirstAtom = inAtom1;
069      mBondOrder = inBondOrder;
070      mSecondAtom = inAtom2;
071
072      orderAtoms();
073   }
074
075   //##########################################################################
076   // PUBLIC METHODS
077   //##########################################################################
078
079   //--------------------------------------------------------------------------
080   @Override
081   public String toString()
082   {
083      StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" ");
084      if (mFirstAtom != null)
085      {
086         buffer.append(mFirstAtom);
087      }
088
089      if (mBondOrder != null)
090      {
091         char bondChar;
092         switch (mBondOrder)
093         {
094            case 1:
095               bondChar = '-';
096               break;
097            case 2:
098               bondChar = '=';
099               break;
100            case 3:
101               bondChar = '≡';
102               break;
103            default:
104               bondChar = mBondOrder.toString().charAt(0);
105         }
106         buffer.delimitedAppend(bondChar);
107      }
108
109      if (mSecondAtom != null)
110      {
111         buffer.delimitedAppend(mSecondAtom);
112      }
113
114      return buffer.toString();
115   }
116   
117   //--------------------------------------------------------------------------
118   @Override
119   public int hashCode()
120   {
121      int hashcode = 31;
122      if (mFirstAtom != null)
123      {
124         hashcode += mFirstAtom.hashCode();
125      }
126
127      if (mSecondAtom != null)
128      {
129         hashcode += mSecondAtom.hashCode();
130      }
131
132      return hashcode;
133   }
134
135
136   //--------------------------------------------------------------------------
137   @Override
138   public boolean equals(Object inObj)
139   {
140      boolean result = false;
141
142      if (inObj != null)
143      {
144         result = (0 == compareTo(inObj));
145      }
146
147      return result;
148   }
149
150   //--------------------------------------------------------------------------
151   @Override
152   public int compareTo(Object inObj)
153   {
154      int result = -1;
155
156      if (inObj instanceof CovalentBond)
157      {
158         // The order of the atoms shouldn't matter for comparison purposes
159         // so we'll use our sorted array for the comparison for consistency.
160
161         result = CompareUtil.compare(mSortedAtoms[0], ((CovalentBond) inObj).mSortedAtoms[0]);
162
163         if (0 == result)
164         {
165            result = CompareUtil.compare(mSortedAtoms[1], ((CovalentBond) inObj).mSortedAtoms[1]);
166         }
167      }
168
169      return result;
170   }
171
172   //--------------------------------------------------------------------------
173   public Atom getFirstAtom()
174   {
175      return mFirstAtom;
176   }
177
178   //--------------------------------------------------------------------------
179   public Integer getSpecifiedBondOrder()
180   {
181      return mBondOrder;
182   }
183
184   //--------------------------------------------------------------------------
185   public CovalentBond setBondOrder(Integer inValue)
186   {
187      mBondOrder = inValue;
188      return this;
189   }
190
191   //--------------------------------------------------------------------------
192   public int getBondOrder()
193   {
194      return mBondOrder != null ? mBondOrder : 1;
195   }
196
197   //--------------------------------------------------------------------------
198   public CovalentBond setSecondAtom(Atom inValue)
199   {
200      mSecondAtom = inValue;
201      orderAtoms();
202      return this;
203   }
204
205   //--------------------------------------------------------------------------
206   public Atom getSecondAtom()
207   {
208      return mSecondAtom;
209   }
210
211
212   //--------------------------------------------------------------------------
213   public CovalentBond setIsAromatic()
214   {
215      mAromatic = true;
216      return this;
217   }
218
219   //--------------------------------------------------------------------------
220   public boolean isAromatic()
221   {
222      return mAromatic;
223   }
224
225
226   //--------------------------------------------------------------------------
227   public CovalentBond setIsUp()
228   {
229      mUp = true;
230      return this;
231   }
232
233   //--------------------------------------------------------------------------
234   public boolean isUp()
235   {
236      return mUp;
237   }
238
239
240   //--------------------------------------------------------------------------
241   public CovalentBond setIsDown()
242   {
243      mDown = true;
244      return this;
245   }
246
247   //--------------------------------------------------------------------------
248   public boolean isDown()
249   {
250      return mDown;
251   }
252
253   //--------------------------------------------------------------------------
254   public boolean eq()
255   {
256      return mDown;
257   }
258
259   //###########################################################################
260   // PRIVATE METHODS
261   //###########################################################################
262
263   //--------------------------------------------------------------------------
264   private void orderAtoms()
265   {
266      if (CompareUtil.compare(mFirstAtom, mSecondAtom) < 0)
267      {
268         mSortedAtoms[0] = mFirstAtom;
269         mSortedAtoms[1] = mSecondAtom;
270      }
271      else
272      {
273         mSortedAtoms[0] = mSecondAtom;
274         mSortedAtoms[1] = mFirstAtom;
275      }
276   }
277
278}