~ubuntu-branches/ubuntu/trusty/cdk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/RingSet.java

  • Committer: Bazaar Package Importer
  • Author(s): Paul Cager
  • Date: 2008-04-09 21:17:53 UTC
  • Revision ID: james.westby@ubuntu.com-20080409211753-46lmjw5z8mx5pd8d
Tags: upstream-1.0.2
ImportĀ upstreamĀ versionĀ 1.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $RCSfile$
 
2
 * $Author: egonw $    
 
3
 * $Date: 2007-08-28 11:21:42 +0200 (Tue, 28 Aug 2007) $    
 
4
 * $Revision: 8736 $
 
5
 * 
 
6
 * Copyright (C) 1997-2007  Christoph Steinbeck <steinbeck@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
 * 
 
15
 * This program is distributed in the hope that it will be useful,
 
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
18
 * GNU Lesser General Public License for more details.
 
19
 * 
 
20
 * You should have received a copy of the GNU Lesser General Public License
 
21
 * along with this program; if not, write to the Free Software
 
22
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 
 
23
 */
 
24
 
 
25
package org.openscience.cdk;
 
26
 
 
27
import java.io.Serializable;
 
28
import java.util.ArrayList;
 
29
import java.util.List;
 
30
 
 
31
import org.openscience.cdk.interfaces.IAtom;
 
32
import org.openscience.cdk.interfaces.IAtomContainer;
 
33
import org.openscience.cdk.interfaces.IRing;
 
34
import org.openscience.cdk.interfaces.IRingSet;
 
35
 
 
36
/**
 
37
 * Maintains a set of Ring objects.
 
38
 *
 
39
 * @cdk.module data
 
40
 *
 
41
 * @cdk.keyword     ring, set of
 
42
 */
 
43
public class RingSet extends AtomContainerSet implements Serializable, IRingSet, Cloneable {
 
44
 
 
45
        private static final long serialVersionUID = 7168431521057961434L;
 
46
        
 
47
        /** Flag to denote that the set is order with the largest ring first? */
 
48
        public final static int LARGE_FIRST = 1;
 
49
    /** Flag to denote that the set is order with the smallest ring first? */
 
50
        public final static int SMALL_FIRST = 2;
 
51
        
 
52
        /**
 
53
         * The constructor.
 
54
         *
 
55
         */
 
56
        public RingSet()
 
57
        {
 
58
                super();
 
59
        }
 
60
        
 
61
        
 
62
        /**
 
63
         * Returns a vector of all rings that this bond is part of.
 
64
         *
 
65
         * @param   bond  The bond to be checked
 
66
         * @return   A vector of all rings that this bond is part of  
 
67
         */
 
68
 
 
69
        public List getRings(org.openscience.cdk.interfaces.IBond bond)
 
70
        {
 
71
                List rings = new ArrayList();
 
72
                Ring ring;
 
73
                for (int i = 0; i < getAtomContainerCount(); i++)
 
74
                {
 
75
                        ring = (Ring)getAtomContainer(i);
 
76
                        if (ring.contains(bond))
 
77
                        {
 
78
                                rings.add(ring);
 
79
                        }
 
80
                }
 
81
                return rings;
 
82
        }
 
83
        
 
84
        /**
 
85
         * Returns a vector of all rings that this atom is part of.
 
86
         *
 
87
         * @param   atom  The atom to be checked
 
88
         * @return   A vector of all rings that this bond is part of  
 
89
         */
 
90
 
 
91
        public IRingSet getRings(IAtom atom)
 
92
        {
 
93
                IRingSet rings = new RingSet();
 
94
                IRing ring;
 
95
                for (int i = 0; i < getAtomContainerCount();i++)
 
96
                {
 
97
                        ring = (Ring)getAtomContainer(i);
 
98
                        if (ring.contains(atom))
 
99
                        {
 
100
                                rings.addAtomContainer(ring);
 
101
                        }
 
102
                }
 
103
                return rings;
 
104
        }
 
105
 
 
106
        /**
 
107
         * Returns all the rings in the RingSet that share
 
108
         * one or more atoms with a given ring.
 
109
         *
 
110
         * @param   ring  A ring with which all return rings must share one or more atoms
 
111
         * @return  All the rings that share one or more atoms with a given ring.   
 
112
         */
 
113
 
 
114
        public List getConnectedRings(IRing ring)
 
115
        {
 
116
                List connectedRings = new ArrayList();
 
117
                IRing tempRing;
 
118
                IAtom atom;
 
119
                for (int i  = 0; i < ring.getAtomCount(); i++)
 
120
                {
 
121
                        atom = ring.getAtom(i);
 
122
                        for (int j = 0; j < getAtomContainerCount(); j++)
 
123
                        {       
 
124
                                tempRing = (IRing)getAtomContainer(j);
 
125
                                if (tempRing != ring && !connectedRings.contains(tempRing) && tempRing.contains(atom))
 
126
                                {
 
127
                                        connectedRings.add(tempRing);
 
128
                                }
 
129
                        }
 
130
                }
 
131
                return connectedRings;
 
132
        }
 
133
 
 
134
        /**
 
135
         * Adds all rings of another RingSet if they are not allready part of this ring set.
 
136
         *
 
137
         * @param   ringSet  the ring set to be united with this one.
 
138
         */
 
139
        public void add(IRingSet ringSet)
 
140
        {
 
141
                for (int f = 0; f < ringSet.getAtomContainerCount(); f++)
 
142
                {
 
143
                        if (!contains((IRing)ringSet.getAtomContainer(f)))
 
144
                        {
 
145
                                addAtomContainer(ringSet.getAtomContainer(f));
 
146
                        }
 
147
                }
 
148
        }
 
149
 
 
150
        /**
 
151
         * True, if at least one of the rings in the ringset cotains
 
152
         * the given atom.
 
153
         *
 
154
     * @param  atom Atom to check
 
155
         * @return      true, if the ringset contains the atom
 
156
         */
 
157
        public boolean contains(IAtom atom) {
 
158
                for (int i = 0; i < getAtomContainerCount(); i++) {
 
159
                        if (((IRing)getAtomContainer(i)).contains(atom)) {
 
160
                                return true;
 
161
                        }
 
162
                }
 
163
                return false;
 
164
        }
 
165
        
 
166
        /**
 
167
         * Checks for presence of a ring in this RingSet.
 
168
         * 
 
169
         * @param  ring  The ring to check
 
170
         * @return  true if ring is part of RingSet
 
171
         * 
 
172
         */
 
173
        public boolean contains(IAtomContainer ring) {
 
174
                for (int i = 0; i < getAtomContainerCount(); i++) {
 
175
                        if (ring == getAtomContainer(i)) {
 
176
                                return true;
 
177
                        }
 
178
                }
 
179
                return false;
 
180
        }
 
181
 
 
182
        /**
 
183
         * Clones this <code>RingSet</code> including the Rings.
 
184
         *
 
185
         * @return  The cloned object
 
186
         */
 
187
        public Object clone() throws CloneNotSupportedException {
 
188
                RingSet clone = (RingSet)super.clone();
 
189
                java.util.Iterator result = atomContainers();
 
190
                while (result.hasNext()) {
 
191
                        clone.addAtomContainer((IAtomContainer) ((IAtomContainer)result.next()).clone());
 
192
                }
 
193
                return clone;
 
194
        }
 
195
 
 
196
    /**
 
197
     * Returns the String representation of this RingSet.
 
198
     *
 
199
     * @return The String representation of this RingSet
 
200
     */
 
201
    public String toString() {
 
202
        StringBuffer buffer = new StringBuffer(32);
 
203
        buffer.append("RingSet(");
 
204
        buffer.append(this.hashCode());
 
205
        buffer.append(", R=").append(getAtomContainerCount()).append(", ");
 
206
        for (int i = 0; i < atomContainerCount; i++) {
 
207
            IRing possibleRing = (IRing)atomContainers[i];
 
208
            buffer.append(possibleRing.toString());
 
209
            if (i+1 < atomContainerCount) {
 
210
                buffer.append(", ");
 
211
            }
 
212
        }
 
213
        buffer.append(')');
 
214
        return buffer.toString();
 
215
    }
 
216
    
 
217
 }