1 /* Copyright (C) 2001-2002  Oliver Horlacher <oliver.horlacher@therastrat.com>
2  *                    2002  Christoph Steinbeck <steinbeck@users.sf.net>
3  *          2003-2008,2011  Egon Willighagen <egonw@users.sf.net>
4  *                    2004  Stefan Kuhn <shk3@users.sf.net>
5  *                    2006  Kai Hartmann <kaihartmann@users.sf.net>
6  *               2008-2009  Rajarshi Guha <rajarshi@users.sf.net>
7  *
8  * Contact: cdk-devel@lists.sourceforge.net
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public License
12  * as published by the Free Software Foundation; either version 2.1
13  * of the License, or (at your option) any later version.
14  * All we ask is that proper credit is given for our work, which includes
15  * - but is not limited to - adding the above copyright notice to the beginning
16  * of your source code files, and to any copyright notice that you may distribute
17  * with programs based on this work.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
27  *  */
28 package org.openscience.cdk.smiles;
29 
30 import org.openscience.cdk.interfaces.IAtom;
31 import org.openscience.cdk.interfaces.IAtomContainer;
32 import org.openscience.cdk.math.Primes;
33 
34 /**
35  * This is used to hold the invariance numbers for the canonical labeling of
36  * {@link IAtomContainer}s.
37  *
38  * @cdk.module standard
39  * @cdk.githash
40  */
41 public class InvPair implements java.io.Serializable {
42 
43     private static final long  serialVersionUID = -1397634098919863122L;
44 
45     /** The description used to set the invariance numbers in the atom's property*/
46     public final static String INVARIANCE_PAIR  = "InvariancePair";
47     public final static String CANONICAL_LABEL  = "CanonicalLabel";
48 
49     private long               last             = 0;
50 
51     private long               curr             = 0;
52 
53     private IAtom              atom;
54 
55     private int                prime;
56 
InvPair()57     public InvPair() {}
58 
InvPair(long current, IAtom atom)59     public InvPair(long current, IAtom atom) {
60         curr = current;
61         this.atom = atom;
62         atom.setProperty(INVARIANCE_PAIR, this);
63     }
64 
getLast()65     public long getLast() {
66         return last;
67     }
68 
69     /**
70      * Set the value of the seed.
71      *
72      * Note that use of this method implies that a new prime number is desired.
73      * If so, make sure to call {@link #setPrime()} to ensure that a new prime
74      * number is obtained using the new seed.
75      *
76      * Todo make the following robust!
77      *
78      * @see #getCurr()
79      * @see #setPrime()
80      */
setCurr(long newCurr)81     public void setCurr(long newCurr) {
82         curr = newCurr;
83     }
84 
85     /**
86      * Get the current seed.
87      *
88      * @return The seed
89      * @see #setCurr(long)
90      * @see #setPrime()
91      * @see #getPrime()
92      */
getCurr()93     public long getCurr() {
94         return curr;
95     }
96 
97     /**
98      * Check whether this instance equals another instance.
99      *
100      * @param object An instance of InvPair
101      * @return true if they are equal, false otherwise
102      */
103     @Override
equals(Object object)104     public boolean equals(Object object) {
105         if (object instanceof InvPair) {
106             InvPair o = (InvPair) object;
107             //      logger.debug("Last " + last + "o.last " + o.getLast() + " curr " + curr + " o.curr " + o.getCurr() + " equals " +(last == o.getLast() && curr == o.getCurr()));
108             return (last == o.getLast() && curr == o.getCurr());
109         } else {
110             return false;
111         }
112     }
113 
setLast(long newLast)114     public void setLast(long newLast) {
115         last = newLast;
116     }
117 
setAtom(IAtom newAtom)118     public void setAtom(IAtom newAtom) {
119         atom = newAtom;
120     }
121 
getAtom()122     public IAtom getAtom() {
123         return atom;
124     }
125 
commit()126     public void commit() {
127         atom.setProperty(CANONICAL_LABEL, Long.valueOf(curr));
128     }
129 
130     /**
131      * String representation.
132      *
133      * @return The string representation of the class.
134      */
135     @Override
toString()136     public String toString() {
137         StringBuilder buff = new StringBuilder();
138         buff.append(curr);
139         buff.append('\t');
140         return buff.toString();
141     }
142 
143     /**
144      * Get the current prime number.
145      *
146      * @return The current prime number
147      * @see #setPrime()
148      */
getPrime()149     public int getPrime() {
150         return prime;
151     }
152 
153     /**
154      * Sets the prime number based on the current seed.
155      *
156      * Note that if you change the seed via {@link #setCurr(long)}, you should make
157      * sure to call this method so that a new prime number is available via
158      * {@link #getPrime()}
159      *
160      * @see #setCurr(long)
161      * @see #getPrime()
162      */
setPrime()163     public void setPrime() {
164         prime = Primes.getPrimeAt((int) curr - 1);
165     }
166 }
167