/*
 *                    BioJava development code
 *
 * This code may be freely distributed and modified under the
 * terms of the GNU Lesser General Public Licence.  This should
 * be distributed with the code.  If you do not have a copy,
 * see:
 *
 *      http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual
 * authors.  These should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims,
 * or to join the biojava-l mailing list, visit the home page
 * at:
 *
 *      http://www.biojava.org/
 *
 * Created on May 23, 2010
 *
 */
package org.biojava.nbio.structure.io.mmcif;

import org.biojava.nbio.structure.*;
import org.biojava.nbio.structure.io.mmcif.chem.PolymerType;
import org.biojava.nbio.structure.io.mmcif.model.ChemComp;
import org.biojava.nbio.core.util.SoftHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class ChemCompGroupFactory {
	
	private static final Logger logger = LoggerFactory.getLogger(ChemCompGroupFactory.class);

	private static ChemCompProvider chemCompProvider = new DownloadChemCompProvider();

	private static SoftHashMap<String, ChemComp> cache = new SoftHashMap<String, ChemComp>(0);

	public static ChemComp getChemComp(String recordName){

		recordName = recordName.toUpperCase().trim();

		// we are using the cache, to avoid hitting the file system too often.
		ChemComp cc = cache.get(recordName);
		if ( cc != null) {
			logger.debug("Chem comp "+cc.getThree_letter_code()+" read from cache");
			return cc;
		}

		// not cached, get the chem comp from the provider
		logger.debug("Chem comp "+recordName+" read from provider "+chemCompProvider.getClass().getCanonicalName());
		cc = chemCompProvider.getChemComp(recordName);
		
		if (!cc.getOne_letter_code().equals("?")){
			cache.put(recordName, cc);
		}
		return cc;
	}

	public static void setChemCompProvider(ChemCompProvider provider) {
		logger.debug("Setting new chem comp provider to "+provider.getClass().getCanonicalName());
		chemCompProvider = provider;
	}

	public static ChemCompProvider getChemCompProvider(){
		return chemCompProvider;
	}

	public static Group getGroupFromChemCompDictionary(String recordName) {

		// make sure we work with upper case records		
		recordName = recordName.toUpperCase().trim();

		Group g = null;


		ChemComp cc =  getChemComp(recordName);

		if ( cc == null)
			return null;

		if ( PolymerType.PROTEIN_ONLY.contains( cc.getPolymerType() ) ){
			AminoAcid aa = new AminoAcidImpl();

			String one_letter = cc.getOne_letter_code();
			if ( one_letter == null || one_letter.equals("X") || one_letter.equals("?") || one_letter.length()==0){
				String parent = cc.getMon_nstd_parent_comp_id();
				if ( parent != null && parent.length() == 3){
					String parentid = cc.getMon_nstd_parent_comp_id() ;
					ChemComp parentCC = getChemComp(parentid);
					one_letter = parentCC.getOne_letter_code();
				}
			}

			if ( one_letter == null || one_letter.length()==0 || one_letter.equals("?")) {
				// e.g. problem with PRR, which probably should have a parent of ALA, but as of 20110127 does not.
				logger.warn("Problem with chemical component: " + recordName + "  Did not find one letter code! Setting it to 'X'");
				aa.setAminoType('X');

			} else  {
				aa.setAminoType(one_letter.charAt(0));
			}


			g = aa;
		} else if ( PolymerType.POLYNUCLEOTIDE_ONLY.contains(cc.getPolymerType())) {
			NucleotideImpl nuc = new NucleotideImpl();

			g = nuc;


		} else {

			g = new HetatomImpl();
		}

		g.setChemComp(cc);


		return g;
	}


	public  static String getOneLetterCode(ChemComp cc){
		String oneLetter = cc.getOne_letter_code();
		if ( oneLetter == null || oneLetter.equals("X") || oneLetter.equals("?")) {
			String parentId = cc.getMon_nstd_parent_comp_id() ;
			if ( parentId == null)
				return oneLetter;
			ChemComp parentCC = ChemCompGroupFactory.getChemComp(parentId);
			if ( parentCC == null)
				return oneLetter;
			oneLetter = parentCC.getOne_letter_code();
		}
		return oneLetter;
	}

	public static SoftHashMap<String, ChemComp> getCache() {
		return cache;
	}


}

@@ -1121,15 +1121,14 @@
 new 
-Downloa
+Reduce
 dChe
@@ -1880,24 +1880,490 @@
 urn cc;%0A%09%7D%0A%0A
+%09/**%0A%09 * The new ChemCompProvider will be set in the static variable, %0A%09 * so this provider will be used from now on until it is changed %0A%09 * again. Note that this change can have unexpected behavior of%0A%09 * code executed afterwards.%0A%09 * %3Cp%3E%0A%09 * Changing the provider does not reset the cache, so Chemical%0A%09 * Component definitions already downloaded from previous providers%0A%09 * will be used. To reset the cache see %7B@link #getCache()).%0A%09 * %0A%09 * @param provider%0A%09 */%0A
 %09public stat

Change to ReducedChemCompProvider as default and javadoc