001    /**
002     * Copyright (c) 2010, SIB. All rights reserved.
003     * 
004     * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005     * https://sourceforge.net/projects/javaprotlib/
006     * 
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions are met:
009     * Redistributions of source code must retain the above copyright notice, this
010     * list of conditions and the following disclaimer. Redistributions in binary
011     * form must reproduce the above copyright notice, this list of conditions and
012     * the following disclaimer in the documentation and/or other materials provided
013     * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014     * its contributors may be used to endorse or promote products derived from this
015     * software without specific prior written permission.
016     * 
017     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020     * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021     * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023     * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     */
028    package org.expasy.jpl.commons.collection.symbol.seq;
029    
030    
031    import java.util.NoSuchElementException;
032    import org.expasy.jpl.commons.collection.IndexIterable;
033    
034    
035    /**
036     * This class provides a skeletal implementation of the PositionIterable
037     * interface, to minimize the effort required to implement this interface.
038     * 
039     * To implement a JPL positioner on a Sequence, the programmer needs only to
040     * extend this class and provide implementations for the iterator. (The iterator
041     * returned by the iterator method must implement the
042     * <code>foundNextPosition()</code> method).
043     * 
044     * @author nikitin
045     * 
046     * @version 1.0
047     * 
048     */
049    public abstract class AbstractSequencePositioner<T> implements
050        SequenceIterable<T> {
051            
052            /** Iterator will iterate over found positions on this sequence. */
053            protected SymbolSequence<T> sequence;
054            
055            protected int numberOfPositions;
056            
057            /**
058             * This inner class provides implementations for <code>next()</code> and
059             * <code>hasNext()</code> needed from <code>Iterator</code> interface. It
060             * announces and provides the mechanism of searching in a sequence and
061             * storing all positions matching an expression. (@see JPLRegExpPositioner
062             * and JPLSimpleAAPositioner).
063             */
064            public abstract class Iterator extends IndexIterable.Iterator {
065                    
066                    protected Integer nextPosition;
067                    
068                    public Iterator() {
069                            if (sequence == null) {
070                                    throw new IllegalStateException("sequence not defined");
071                            }
072                            
073                            numberOfPositions = 0;
074                            nextPosition = -1;
075                    }
076                    
077                    /**
078                     * Has found next match ? [method implemented to respect Iterator
079                     * contract].
080                     */
081                    public boolean hasNext() {
082                            return ((nextPosition == -1) ? foundNextPosition() : true);
083                    }
084                    
085                    /**
086                     * Returns the next <code>Integer</code> element [method implemented to
087                     * respect Iterator contract].
088                     * 
089                     */
090                    public Integer next() {
091                            if (nextPosition != -1) {
092                                    return consumePosition();
093                            }
094                            
095                            if (!foundNextPosition()) {
096                                    throw new NoSuchElementException("No more elements");
097                            }
098                            
099                            return consumePosition();
100                    }
101                    
102                    /**
103                     * Returns the next position found in the sequence.
104                     * 
105                     * @return Integer
106                     */
107                    private Integer consumePosition() {
108                            final Integer pos = nextPosition;
109                            
110                            nextPosition = -1;
111                            
112                            if (pos < sequence.length()) {
113                                    numberOfPositions++;
114                            }
115                            
116                            return pos;
117                    }
118                    
119                    /**
120                     * This function will have to set nextPosition to its value and will
121                     * return true if a valid position is found otherwise false.
122                     */
123                    public abstract boolean foundNextPosition();
124            }
125            
126            /**
127             * Set the sequence for future searches.
128             * 
129             * @param sequence the sequence where motif search will be done.
130             * 
131             * @throws IllegalArgumentException if sequence is not defined.
132             */
133            public void setSequence(final SymbolSequence<T> sequence) {
134                    if (sequence == null) {
135                            throw new IllegalArgumentException("sequence cannot be null.");
136                    }
137                    this.sequence = sequence;
138            }
139            
140            public int getNumberOfSites() {
141                    return numberOfPositions;
142            }
143            
144            public IndexIterable.Iterator iterator(SymbolSequence<T> sequence) {
145                    setSequence(sequence);
146                    return iterator();
147            }
148    }