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