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.biojavax.bio.phylo.io.nexus;
022
023import java.util.ArrayList;
024import java.util.Iterator;
025import java.util.List;
026
027/**
028 * Represents Nexus files.
029 * 
030 * @author Richard Holland
031 * @author Tobias Thierer
032 * @author Jim Balhoff
033 * @since 1.6
034 */
035public class NexusFile {
036
037        private List objects = new ArrayList();
038
039        /**
040         * Appends an object to the end of the file.
041         * 
042         * @param object
043         *            the NexusObject to append.
044         */
045        public void addObject(final NexusObject object) {
046                this.objects.add(object);
047        }
048
049        /**
050         * Inserts an object at the given position.
051         * 
052         * @param object
053         *            the NexusObject to insert.
054         * @param pos
055         *            the position (0-indexed) to add it at.
056         */
057        public void insertObject(final NexusObject object, final int pos) {
058                this.objects.add(pos, object);
059        }
060
061        /**
062         * Removes an object from the file.
063         * 
064         * @param object
065         *            the NexusObject to remove.
066         */
067        public void removeObject(final NexusObject object) {
068                this.objects.remove(object);
069        }
070
071        /**
072         * Checks to see if we contain an object.
073         * 
074         * @param object
075         *            the NexusObject to check.
076         * @return <tt>true</tt> if we contain it.
077         */
078        public boolean containsObject(final NexusObject object) {
079                return this.objects.contains(object);
080        }
081
082        /**
083         * Iterate over all objects in the file in order.
084         * 
085         * @return an iterator of NexusObjects.
086         */
087        public Iterator objectIterator() {
088                return this.objects.iterator();
089        }
090
091        /**
092         * Iterate over all comments in the file in order.
093         * 
094         * @return an iterator of NexusComments.
095         */
096        public Iterator commentIterator() {
097                final List comments = new ArrayList();
098                for (final Iterator i = this.objectIterator(); i.hasNext();) {
099                        final NexusObject obj = (NexusObject) i.next();
100                        if (obj instanceof NexusComment)
101                                comments.add(obj);
102                }
103                return comments.iterator();
104        }
105
106        /**
107         * Iterate over all blocks in the file in order.
108         * 
109         * @return an iterator of NexusBlocks.
110         */
111        public Iterator blockIterator() {
112                final List blocks = new ArrayList();
113                for (final Iterator i = this.objectIterator(); i.hasNext();) {
114                        final NexusObject obj = (NexusObject) i.next();
115                        if (obj instanceof NexusBlock)
116                                blocks.add(obj);
117                }
118                return blocks.iterator();
119        }
120}