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 */
021
022package org.biojava.bio.program.ssbind;
023
024import java.util.Iterator;
025import java.util.Map;
026import java.util.Set;
027
028import org.biojava.bio.Annotation;
029import org.biojava.bio.BioError;
030import org.biojava.bio.SimpleAnnotation;
031import org.biojava.bio.SmallAnnotation;
032import org.biojava.utils.ChangeVetoException;
033
034/**
035 * <code>AnnotationFactory</code> is a utility class for making
036 * <code>Annotation</code>s from <code>Map</code>s. Shared by the
037 * search and homology builders. Now public to allow use by anyone
038 * making custom handlers.
039 *
040 * @author Keith James
041 * @author Thomas Down
042 * @since 1.2
043 */
044public class AnnotationFactory
045{
046    /**
047     * <code>makeAnnotation</code> creates the annotation.
048     *
049     * @param m a <code>Map</code> of raw data.
050     * @return an <code>Annotation</code>.
051     */
052    public static Annotation makeAnnotation(Map m)
053    {
054        int elements = m.size();
055
056        if (elements == 0)
057        {
058            return Annotation.EMPTY_ANNOTATION;
059        }
060        else
061        {
062            Annotation annotation;
063
064            if (elements < 15)
065                annotation = new SmallAnnotation();
066            else
067                annotation = new SimpleAnnotation();
068
069            Set keySet = m.keySet();
070
071            try
072            {
073                for (Iterator ksi = keySet.iterator(); ksi.hasNext();)
074                {
075                    Object key = ksi.next();
076                    annotation.setProperty(key, m.get(key));
077                }
078            }
079            catch (ChangeVetoException cve)
080            {
081                throw new BioError("Assert failed: couldn't modify newly created Annotation",cve);
082            }
083
084            return annotation;
085        }
086    }
087}