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.activemq.store.kahadb.disk.util;
018    
019    /**
020     * Represents a range of numbers.
021     * 
022     * @author chirino
023     */
024    public class Sequence extends LinkedNode<Sequence> {
025        long first;
026        long last;
027    
028        public Sequence(long value) {
029            first = last = value;
030        }
031    
032        public Sequence(long first, long last) {
033            this.first = first;
034            this.last = last;
035        }
036    
037        public boolean isAdjacentToLast(long value) {
038            return last + 1 == value;
039        }
040    
041        public boolean isAdjacentToFirst(long value) {
042            return first - 1 == value;
043        }
044    
045        public boolean contains(long value) {
046            return first <= value && value <= last;
047        }
048    
049        public long range() {
050            return first == last ? 1 : (last - first) + 1;
051        }
052        
053        @Override
054        public String toString() {
055            return first == last ? "" + first : first + ".." + last;
056        }
057    
058        public long getFirst() {
059            return first;
060        }
061    
062        public void setFirst(long first) {
063            this.first = first;
064        }
065    
066        public long getLast() {
067            return last;
068        }
069    
070        public void setLast(long last) {
071            this.last = last;
072        }
073        
074        public interface Closure<T extends Throwable> {
075            public void execute(long value) throws T;
076        }
077    
078        public <T extends Throwable> void each(Closure<T> closure) throws T {
079            for( long i=first; i<=last; i++ ) {
080                closure.execute(i);
081            }
082        }
083    
084    }