All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.biojava.bio.AnnotationRenamer Maven / Gradle / Ivy

There is a newer version: 1.9.7
Show newest version
/*
 *                    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/
 *
 */

package org.biojava.bio;

import java.util.AbstractMap;
import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.biojava.bio.program.tagvalue.PropertyChanger;

/**
 * AnnotationRenamer remaps the keys of an
 * Annotation to new keys specified by a
 * TagMapper. This will rename properties, but not alter their
 * values.
 * For writing light-weigth adaptors to project one type of
 * Annotation to another using a TagMapper.
 * @since 1.3
 * @author Matthew Pocock
 * @author Keith James (docs)
 *
 */
public class AnnotationRenamer extends AbstractAnnotation
{
    private final Annotation wrapped;
    private final PropertyChanger mapper;
    private final Map properties;
  
    /**
     * Creates a new AnnotationRenamer using the
     * specified TagMapper to remap its keys.
     *
     * @param wrapped an Annotation.
     * @param mapper a TagMapper.
     */
    public AnnotationRenamer(Annotation wrapped, PropertyChanger mapper) {
        this.wrapped = wrapped;
        this.mapper = mapper;
        this.properties = new MappedHash();
    }

    /**
     * getWrapped returns the Annotation
     * being remapped.
     *
     * @return an Annotation.
     */
    public Annotation getWrapped() {
        return wrapped;
    }

    /**
     * getMapper returns the TagMapper being
     * used to remap the Annotation.
     *
     * @return a TagMapper.
     */
    public PropertyChanger getMapper() {
        return mapper;
    }

    /**
     * getProperties returns the mapped contents of the
     * underlying Annotation as a Map.
     *
     * @return a Map.
     */
    public Map getProperties() {
        return properties;
    }

    /**
     * propertiesAllocated Javadoc FIXME - this overrides
     * a protected method and I'm not sure why (KJ).
     *
     * @return a boolean.
     */
    public boolean propertiesAllocated() {
        return true;
    }

    private class MappedHash extends AbstractMap {
        public int size() {
            return wrapped.asMap().size();
        }

        public Set entrySet() {
            return new WrappedSet(wrapped.asMap().entrySet());
        }
    }

    private class WrappedSet extends AbstractSet {
        private Set entrySet;

        public WrappedSet(Set entrySet) {
            this.entrySet = entrySet;
        }

        public int size() {
            return entrySet.size();
        }

        public Iterator iterator() {
            return new Iterator() {
                    Iterator i = entrySet.iterator();

                    public boolean hasNext() {
                        return i.hasNext();
                    }

                    public Object next() {
                        final Map.Entry entry = (Map.Entry) i.next();

                        return new Map.Entry() {
                                public Object getKey() {
                                    return mapper.getNewTag(entry.getKey());
                                }

                                public Object getValue() {
                                    return entry.getValue();
                                }

                                public Object setValue(Object value) {
                                    return entry.setValue(value);
                                }
                            };
                    }
        
                    public void remove() {
                        i.remove();
                    }
                };
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy