001    package util.wavelet;
002    
003    /** An enumeration of boundary conditions. When an array index exceeds
004     * the bounds of its array it must be converted back into the legal range.
005     *
006     * @author John Talbot
007     */
008    public enum BoundaryCondition {
009        Continuity() {  // Continuity (hard clipping to boudary value)
010            public int test(int index, int upperBoundary) {
011                if (index < 0) return 0;
012                else {
013                    if (index >= upperBoundary) return upperBoundary - 1;
014                    else return index;
015                }
016            }      
017        },
018        Mirror() {  // mirrored relative to boundary
019            public int test(int index, int upperBoundary) {
020                // if |index| > 2*upperBoundary then switch to periodic boundary for code simplicity
021                if (index < 0) return (-index % upperBoundary);
022                else {
023                    if (index >= upperBoundary) return (2 * (upperBoundary - 1) - index) % upperBoundary;
024                    else return index;
025                } 
026            }
027        },
028        Periodic() {  // periodic relative to boundary
029            public int test(int index, int upperBoundary) {
030                if (index < 0) return (index % upperBoundary) + upperBoundary;
031                else return index % upperBoundary;
032            }
033        };
034        
035        /** 
036         * Test the boundary conditions. Default returns perdiodic boundary.
037         * The lower boundary is 0 by default.
038         *
039         * @param   index          value to test against upper boundary
040         * @param   upperBoundary  upper boundary
041         * @return    the index value if it is within the upper boundary, periodic otherwise
042         */
043        public int test(int index, int upperBoundary) {
044            if (index < 0) return (index % upperBoundary) + upperBoundary;
045            else return index % upperBoundary;
046        }
047    }