001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021package org.biojava.nbio.structure.align.multiple;
022
023import java.util.Collections;
024import java.util.Map;
025import java.util.Set;
026import java.util.TreeMap;
027
028/**
029 * Abstact implementation of the {@link ScoresCache} with the shared code used
030 * in all objects with a variables cache.
031 *
032 * @author Spencer Bliven
033 * @since 4.1.0
034 *
035 */
036public abstract class AbstractScoresCache implements ScoresCache {
037
038        private Map<String,Double> scores = null;
039
040        protected AbstractScoresCache() {
041                scores = null;
042        }
043
044        protected AbstractScoresCache(AbstractScoresCache cache) {
045                this.scores = cache.scores;
046        }
047
048        @Override
049        public void putScore(String property, Double score) {
050                if(scores == null) {
051                        scores = new TreeMap<String, Double>();
052                }
053                scores.put(property, score);
054        }
055
056        @Override
057        public Double getScore(String property) {
058                if(scores != null && scores.containsKey(property)) {
059                        return scores.get(property);
060                }
061                return null;
062        }
063
064        @Override
065        public Set<String> getScores() {
066                if(scores == null) return Collections.emptySet();
067                return Collections.unmodifiableSet(scores.keySet());
068        }
069
070        /**
071         * Subclasses should override clone and use the copy constructor.
072         *
073         * @param e
074         * @return
075         * @throws CloneNotSupportedException
076         */
077        protected Object clone(Object e) throws CloneNotSupportedException {
078                throw new CloneNotSupportedException("Subclasses must override clone");
079        }
080
081        /**
082         * Clear the cached scores. This frees memory after the alignment changed.
083         */
084        public void clear() {
085                scores = null;
086        }
087}