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 */
017package org.apache.activemq.store.kahadb.disk.util;
018
019/**
020 * Represents a range of numbers.
021 * 
022 * @author chirino
023 */
024public 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 isBiggerButNotAdjacentToLast(long value) {
042        return last + 1 < value;
043    }
044
045    public boolean isAdjacentToFirst(long value) {
046        return first - 1 == value;
047    }
048
049    public boolean contains(long value) {
050        return first <= value && value <= last;
051    }
052
053    public long range() {
054        return first == last ? 1 : (last - first) + 1;
055    }
056    
057    @Override
058    public String toString() {
059        return first == last ? "" + first : first + ".." + last;
060    }
061
062    public long getFirst() {
063        return first;
064    }
065
066    public void setFirst(long first) {
067        this.first = first;
068    }
069
070    public long getLast() {
071        return last;
072    }
073
074    public void setLast(long last) {
075        this.last = last;
076    }
077    
078    public interface Closure<T extends Throwable> {
079        public void execute(long value) throws T;
080    }
081
082    public <T extends Throwable> void each(Closure<T> closure) throws T {
083        for( long i=first; i<=last; i++ ) {
084            closure.execute(i);
085        }
086    }
087
088}