001package votorola.g.util; // Copyright 2001-2003, 2006, Michael Allan.  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE.
002
003import votorola.g.lang.*;
004
005
006/** Index offsetter for a sequence of fixed size.  It implements a logical index
007  * whose start (logical zero) may be offset from actual zero, and whose increments
008  * will wrap around the end of sequence.
009  */
010public @ThreadRestricted("touch") final class IndexOffsetter implements java.io.Serializable
011{
012
013    private static final long serialVersionUID = 0L;
014
015
016
017    /** Constructs an IndexOffsetter.
018      *
019      *     @param s size per {@linkplain #size() size}()
020      */
021    public IndexOffsetter( int s ) { size = s; }
022
023
024
025   // ------------------------------------------------------------------------------------
026
027
028    /** Returns the actual index of a given logical index.
029      *
030      *     @param logicalIndex logical index in sequence (positive or negative)
031      *     @return actual index, wrapped as necessary around the end of the sequence
032      *
033      *     @throws ArrayIndexOutOfBoundsException if |logicalIndex| >= size()
034      */
035    public int actualIndex( int logicalIndex )
036    {
037        if( Math.abs(logicalIndex) >= size ) throw new ArrayIndexOutOfBoundsException( logicalIndex );
038
039        int actualIndex = actualIndexOfLogicalZero + logicalIndex;
040        if( actualIndex >= size ) actualIndex -= size;
041        else if( actualIndex < 0 ) actualIndex += size;
042        return actualIndex;
043    }
044
045
046
047    /** Sets the actual index of logical index zero.  When this is zero (the default),
048      * the logical index is identical to the actual index.
049      *
050      *     @throws ArrayIndexOutOfBoundsException if actualIndex &lt; 0
051      *       or actualIndex &gt;= size()
052      */
053    public @ThreadRestricted void setActualIndexOfLogicalZero( int actualIndex )
054    {
055        if( actualIndex < 0 || actualIndex >= size ) throw new ArrayIndexOutOfBoundsException( actualIndex );
056
057        actualIndexOfLogicalZero = actualIndex;
058    }
059
060
061        private int actualIndexOfLogicalZero = 0; // by default
062
063
064
065    /** Returns the size of the sequence.
066      */
067    public int size() { return size; }
068
069
070        private final int size;
071
072
073
074}