Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]
    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.kahadb.util;
   18   
   19   /**
   20    * Represents a range of numbers.
   21    * 
   22    * @author chirino
   23    */
   24   public class Sequence extends LinkedNode<Sequence> {
   25       long first;
   26       long last;
   27   
   28       public Sequence(long value) {
   29           first = last = value;
   30       }
   31   
   32       public Sequence(long first, long last) {
   33           this.first = first;
   34           this.last = last;
   35       }
   36   
   37       public boolean isAdjacentToLast(long value) {
   38           return last + 1 == value;
   39       }
   40   
   41       public boolean isAdjacentToFirst(long value) {
   42           return first - 1 == value;
   43       }
   44   
   45       public boolean contains(long value) {
   46           return first <= value && value <= last;
   47       }
   48   
   49       public long range() {
   50           return first == last ? 1 : (last - first) + 1;
   51       }
   52       
   53       @Override
   54       public String toString() {
   55           return first == last ? "" + first : first + "-" + last;
   56       }
   57   
   58       public long getFirst() {
   59           return first;
   60       }
   61   
   62       public void setFirst(long first) {
   63           this.first = first;
   64       }
   65   
   66       public long getLast() {
   67           return last;
   68       }
   69   
   70       public void setLast(long last) {
   71           this.last = last;
   72       }
   73       
   74       public interface Closure<T extends Throwable> {
   75           public void execute(long value) throws T;
   76       }
   77   
   78       public <T extends Throwable> void each(Closure<T> closure) throws T {
   79           for( long i=first; i<=last; i++ ) {
   80               closure.execute(i);
   81           }
   82       }
   83   
   84   }

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]