001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    package org.apache.geronimo.samples.daytrader.util;
018    
019    import java.util.AbstractSequentialList;
020    import java.util.ListIterator;
021    public class KeyBlock extends AbstractSequentialList 
022    {
023    
024            // min and max provide range of valid primary keys for this KeyBlock
025            private int min = 0;
026            private int max = 0;
027            private int index = 0;
028    
029            /**
030             * Constructor for KeyBlock
031             */
032            public KeyBlock() {
033                    super();
034                    min = 0;
035                    max = 0;
036                    index = min;
037            }
038    
039            /**
040             * Constructor for KeyBlock 
041             */
042            public KeyBlock(int min, int max) {
043                    super();
044                    this.min = min;
045                    this.max = max;
046                    index = min;
047            }
048    
049            /**
050             * @see AbstractCollection#size()
051             */
052            public int size() {
053                    return (max - min) + 1;
054            }
055    
056            /**
057             * @see AbstractSequentialList#listIterator(int)
058             */
059            public ListIterator listIterator(int arg0) {
060                    return new KeyBlockIterator();
061            }
062    
063            class KeyBlockIterator implements ListIterator {
064    
065                    /**
066                     * @see ListIterator#hasNext()
067                     */
068                    public boolean hasNext() {
069                            return index <= max;
070                    }
071    
072                    /**
073                     * @see ListIterator#next()
074                     */
075                    public synchronized Object next() {
076                            if (index > max)
077                                    throw new java.lang.RuntimeException("KeyBlock:next() -- Error KeyBlock depleted");
078                            return new Integer(index++);
079                    }
080    
081                    /**
082                     * @see ListIterator#hasPrevious()
083                     */
084                    public boolean hasPrevious() {
085                            return index > min;
086                    }
087    
088                    /**
089                     * @see ListIterator#previous()
090                     */
091                    public Object previous() {
092                            return new Integer(--index);
093                    }
094    
095                    /**
096                     * @see ListIterator#nextIndex()
097                     */
098                    public int nextIndex() {
099                            return index-min;
100                    }
101    
102                    /**
103                     * @see ListIterator#previousIndex()
104                     */
105                    public int previousIndex() {
106                            throw new UnsupportedOperationException("KeyBlock: previousIndex() not supported");
107                    }
108    
109                    /**
110                     * @see ListIterator#add()
111                     */
112                    public void add(Object o) {
113                            throw new UnsupportedOperationException("KeyBlock: add() not supported");
114                    }
115    
116                    /**
117                     * @see ListIterator#remove()
118                     */
119                    public void remove() {
120                            throw new UnsupportedOperationException("KeyBlock: remove() not supported");
121                    }
122    
123                    /**
124                     * @see ListIterator#set(Object)
125                     */
126                    public void set(Object arg0) {
127                    }
128            }
129    }