Skip to content

Commit b98f650

Browse files
egonwrajarshi
authored andcommitted
A shared IChemObject implementation for QueryAtom and QueryBond
Signed-off-by: Rajarshi Guha <rajarshi.guha@gmail.com>
1 parent 3708c8d commit b98f650

File tree

1 file changed

+357
-0
lines changed

1 file changed

+357
-0
lines changed
Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
/* Copyright (C) 1997-2007 Christoph Steinbeck <steinbeck@users.sf.net>
2+
* 2010 Egon Willighagen <egonw@users.sf.net>
3+
*
4+
* Contact: cdk-devel@lists.sourceforge.net
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public License
8+
* as published by the Free Software Foundation; either version 2.1
9+
* of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19+
*/
20+
package org.openscience.cdk.isomorphism.matchers;
21+
22+
import java.util.ArrayList;
23+
import java.util.HashMap;
24+
import java.util.Iterator;
25+
import java.util.List;
26+
import java.util.Map;
27+
28+
import org.openscience.cdk.CDKConstants;
29+
import org.openscience.cdk.event.ChemObjectChangeEvent;
30+
import org.openscience.cdk.interfaces.IAtom;
31+
import org.openscience.cdk.interfaces.IChemObjectBuilder;
32+
import org.openscience.cdk.interfaces.IChemObjectChangeEvent;
33+
import org.openscience.cdk.interfaces.IChemObjectListener;
34+
35+
/**
36+
* @cdk.module isomorphism
37+
* @cdk.githash
38+
*/
39+
public class QueryChemObject {
40+
41+
/**
42+
* List for listener administration.
43+
*/
44+
private List<IChemObjectListener> chemObjectListeners;
45+
46+
/**
47+
* A hashtable for the storage of any kind of properties of this IChemObject.
48+
*/
49+
private Map<Object, Object> properties;
50+
51+
/**
52+
* String representing the identifier for this atom type with null as default.
53+
*/
54+
private String identifier = (String) CDKConstants.UNSET;
55+
56+
/**
57+
* You will frequently have to use some flags on a IChemObject. For example, if
58+
* you want to draw a molecule and see if you've already drawn an atom, or in
59+
* a ring search to check whether a vertex has been visited in a graph
60+
* traversal. Use these flags while addressing particular positions in the
61+
* flag array with self-defined constants (flags[VISITED] = true). 100 flags
62+
* per object should be more than enough.
63+
*/
64+
private boolean[] flags;
65+
66+
public QueryChemObject() {
67+
super();
68+
}
69+
70+
/**
71+
* Lazy creation of chemObjectListeners List.
72+
*
73+
*@return List with the ChemObjects associated.
74+
*/
75+
private List<IChemObjectListener> lazyChemObjectListeners()
76+
{
77+
if (chemObjectListeners == null) {
78+
chemObjectListeners = new ArrayList<IChemObjectListener>();
79+
}
80+
return chemObjectListeners;
81+
}
82+
83+
84+
/**
85+
* Use this to add yourself to this IChemObject as a listener. In order to do
86+
* so, you must implement the ChemObjectListener Interface.
87+
*
88+
*@param col the ChemObjectListener
89+
*@see #removeListener
90+
*/
91+
public void addListener(IChemObjectListener col)
92+
{
93+
List<IChemObjectListener> listeners = lazyChemObjectListeners();
94+
95+
if (!listeners.contains(col))
96+
{
97+
listeners.add(col);
98+
}
99+
// Should we throw an exception if col is already in here or
100+
// just silently ignore it?
101+
}
102+
103+
104+
/**
105+
* Returns the number of ChemObjectListeners registered with this object.
106+
*
107+
*@return the number of registered listeners.
108+
*/
109+
public int getListenerCount() {
110+
if (chemObjectListeners == null) {
111+
return 0;
112+
}
113+
return lazyChemObjectListeners().size();
114+
}
115+
116+
117+
/**
118+
* Use this to remove a ChemObjectListener from the ListenerList of this
119+
* IChemObject. It will then not be notified of change in this object anymore.
120+
*
121+
*@param col The ChemObjectListener to be removed
122+
*@see #addListener
123+
*/
124+
public void removeListener(IChemObjectListener col) {
125+
if (chemObjectListeners == null) {
126+
return;
127+
}
128+
129+
List<IChemObjectListener> listeners = lazyChemObjectListeners();
130+
if (listeners.contains(col)) {
131+
listeners.remove(col);
132+
}
133+
}
134+
135+
136+
/**
137+
* This should be triggered by an method that changes the content of an object
138+
* to that the registered listeners can react to it.
139+
*/
140+
public void notifyChanged() {
141+
if (getNotification() && getListenerCount() > 0) {
142+
List<IChemObjectListener> listeners = lazyChemObjectListeners();
143+
for (Object listener : listeners) {
144+
((IChemObjectListener) listener).stateChanged(
145+
new ChemObjectChangeEvent(this)
146+
);
147+
}
148+
}
149+
}
150+
151+
152+
/**
153+
* This should be triggered by an method that changes the content of an object
154+
* to that the registered listeners can react to it. This is a version of
155+
* notifyChanged() which allows to propagate a change event while preserving
156+
* the original origin.
157+
*
158+
*@param evt A ChemObjectChangeEvent pointing to the source of where
159+
* the change happend
160+
*/
161+
public void notifyChanged(IChemObjectChangeEvent evt) {
162+
if (getNotification() && getListenerCount() > 0) {
163+
List<IChemObjectListener> listeners = lazyChemObjectListeners();
164+
for (Object listener : listeners) {
165+
((IChemObjectListener) listener).stateChanged(evt);
166+
}
167+
}
168+
}
169+
170+
171+
/**
172+
* Lazy creation of properties hash.
173+
*
174+
* @return Returns in instance of the properties
175+
*/
176+
private Map<Object, Object> lazyProperties()
177+
{
178+
if (properties == null)
179+
{
180+
properties = new HashMap<Object, Object>();
181+
}
182+
return properties;
183+
}
184+
185+
186+
/**
187+
* Sets a property for a IChemObject.
188+
*
189+
*@param description An object description of the property (most likely a
190+
* unique string)
191+
*@param property An object with the property itself
192+
*@see #getProperty
193+
*@see #removeProperty
194+
*/
195+
public void setProperty(Object description, Object property)
196+
{
197+
lazyProperties().put(description, property);
198+
notifyChanged();
199+
}
200+
201+
202+
/**
203+
* Removes a property for a IChemObject.
204+
*
205+
*@param description The object description of the property (most likely a
206+
* unique string)
207+
*@see #setProperty
208+
*@see #getProperty
209+
*/
210+
public void removeProperty(Object description)
211+
{
212+
if (properties == null) {
213+
return;
214+
}
215+
if (lazyProperties().remove(description) != null)
216+
notifyChanged();
217+
}
218+
219+
/**
220+
* Returns a property for the IChemObject.
221+
*
222+
*@param description An object description of the property (most likely a
223+
* unique string)
224+
*@return The object containing the property. Returns null if
225+
* propert is not set.
226+
*@see #setProperty
227+
*@see #removeProperty
228+
*/
229+
public Object getProperty(Object description)
230+
{
231+
if (properties != null) {
232+
return lazyProperties().get(description);
233+
}
234+
return null;
235+
}
236+
237+
/**
238+
* Returns a Map with the IChemObject's properties.
239+
*
240+
*@return The object's properties as an Hashtable
241+
*@see #setProperties
242+
*/
243+
public Map<Object,Object> getProperties()
244+
{
245+
return lazyProperties();
246+
}
247+
248+
/**
249+
* Returns the identifier (ID) of this object.
250+
*
251+
*@return a String representing the ID value
252+
*@see #setID
253+
*/
254+
public String getID()
255+
{
256+
return this.identifier;
257+
}
258+
259+
/**
260+
* Sets the identifier (ID) of this object.
261+
*
262+
*@param identifier a String representing the ID value
263+
*@see #getID
264+
*/
265+
public void setID(String identifier)
266+
{
267+
this.identifier = identifier;
268+
notifyChanged();
269+
}
270+
271+
/**
272+
* Sets the value of some flag.
273+
*
274+
*@param flag_type Flag to set
275+
*@param flag_value Value to assign to flag
276+
*@see #getFlag
277+
*/
278+
public void setFlag(int flag_type, boolean flag_value)
279+
{
280+
flags[flag_type] = flag_value;
281+
notifyChanged();
282+
}
283+
284+
/**
285+
* Returns the value of some flag.
286+
*
287+
*@param flag_type Flag to retrieve the value of
288+
*@return true if the flag <code>flag_type</code> is set
289+
*@see #setFlag
290+
*/
291+
public boolean getFlag(int flag_type)
292+
{
293+
return flags[flag_type];
294+
}
295+
296+
/**
297+
* Sets the properties of this object.
298+
*
299+
*@param properties a Hashtable specifying the property values
300+
*@see #getProperties
301+
*/
302+
public void setProperties(Map<Object,Object> properties)
303+
{
304+
Iterator<Object> keys = properties.keySet().iterator();
305+
while (keys.hasNext())
306+
{
307+
Object key = keys.next();
308+
lazyProperties().put(key, properties.get(key));
309+
}
310+
notifyChanged();
311+
}
312+
313+
private boolean doNotification = true;
314+
315+
/**
316+
* Sets the whole set of flags.
317+
*
318+
* @param flagsNew the new flags.
319+
* @see #getFlags
320+
*/
321+
public void setFlags(boolean[] flagsNew){
322+
flags=flagsNew;
323+
}
324+
325+
/**
326+
* Returns the whole set of flags.
327+
*
328+
*@return the flags.
329+
*@see #setFlags
330+
*/
331+
public boolean[] getFlags(){
332+
return(flags);
333+
}
334+
335+
public void setNotification(boolean bool) {
336+
this.doNotification = bool;
337+
}
338+
339+
public boolean getNotification() {
340+
return this.doNotification;
341+
}
342+
343+
public IChemObjectBuilder getBuilder() {
344+
throw new IllegalAccessError();
345+
}
346+
347+
public boolean matches(IAtom atom) {
348+
// TODO Auto-generated method stub
349+
return false;
350+
}
351+
352+
@Override
353+
public Object clone() throws CloneNotSupportedException {
354+
// TODO Auto-generated method stub
355+
return super.clone();
356+
}
357+
}

0 commit comments

Comments
 (0)