~ubuntu-branches/ubuntu/maverick/cdk/maverick

« back to all changes in this revision

Viewing changes to src/org/openscience/cdk/qsar/AbstractAtomicDescriptor.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: 2006-03-29 10:27:08 +0200 (Wed, 29 Mar 2006) $
 
4
 * $Revision: 5855 $
 
5
 * 
 
6
 * Copyright (C) 2006-2007  The Chemistry Development Kit (CDK) project
 
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
package org.openscience.cdk.qsar;
 
25
 
 
26
import java.util.HashMap;
 
27
import java.util.Map;
 
28
 
 
29
import org.openscience.cdk.interfaces.IAtom;
 
30
import org.openscience.cdk.interfaces.IAtomContainer;
 
31
import org.openscience.cdk.qsar.result.IDescriptorResult;
 
32
 
 
33
/**
 
34
 * Abstract atomic descriptor class with helper functions for descriptors
 
35
 * that require the whole molecule to calculate the descriptor values,
 
36
 * which in turn need to be cached for all atoms, so that they can be
 
37
 * retrieved one by one.
 
38
 *
 
39
 * @cdk.module qsar
 
40
 */
 
41
public abstract class AbstractAtomicDescriptor implements IAtomicDescriptor {
 
42
    
 
43
        private static final String PREVIOUS_ATOMCONTAINER = "previousAtomContainer";
 
44
        
 
45
        private Map cachedDescriptorValues = null;
 
46
        
 
47
        /**
 
48
         * Returns true if the cached IDescriptorResult's are for the given IAtomContainer.
 
49
         * 
 
50
         * @param container
 
51
         * @return false, if the cache is for a different IAtomContainer
 
52
         */
 
53
        public boolean isCachedAtomContainer(IAtomContainer container) {
 
54
                if (cachedDescriptorValues == null) return false;
 
55
                return (cachedDescriptorValues.get(PREVIOUS_ATOMCONTAINER) == container);
 
56
        }
 
57
        
 
58
        /**
 
59
         * Returns the cached DescriptorValue for the given IAtom.
 
60
         * 
 
61
         * @param atom the IAtom for which the DescriptorValue is requested
 
62
         * @return     null, if no DescriptorValue was cached for the given IAtom
 
63
         */
 
64
        public IDescriptorResult getCachedDescriptorValue(IAtom atom) {
 
65
                if (cachedDescriptorValues == null) return null;
 
66
                return (IDescriptorResult)cachedDescriptorValues.get(atom);
 
67
        }
 
68
        
 
69
        /**
 
70
         * Caches a DescriptorValue for a given IAtom. This method may only
 
71
         * be called after setNewContainer() is called.
 
72
         * 
 
73
         * @param atom  IAtom to cache the value for
 
74
         * @param value DescriptorValue for the given IAtom
 
75
         */
 
76
        public void cacheDescriptorValue(IAtom atom, IAtomContainer container, IDescriptorResult value) {
 
77
                if (cachedDescriptorValues == null) {
 
78
                        cachedDescriptorValues = new HashMap();
 
79
                        cachedDescriptorValues.put(PREVIOUS_ATOMCONTAINER, container);
 
80
                } else if (cachedDescriptorValues.get(PREVIOUS_ATOMCONTAINER) != container) {
 
81
                        cachedDescriptorValues.clear();
 
82
                        cachedDescriptorValues.put(PREVIOUS_ATOMCONTAINER, container);
 
83
                }
 
84
                cachedDescriptorValues.put(atom, value);
 
85
        }
 
86
}
 
87