Serial.java
001 package org.apache.qpid.util;
002 /*
003  
004  * Licensed to the Apache Software Foundation (ASF) under one
005  * or more contributor license agreements.  See the NOTICE file
006  * distributed with this work for additional information
007  * regarding copyright ownership.  The ASF licenses this file
008  * to you under the Apache License, Version 2.0 (the
009  * "License"); you may not use this file except in compliance
010  * with the License.  You may obtain a copy of the License at
011  
012  *   http://www.apache.org/licenses/LICENSE-2.0
013  
014  * Unless required by applicable law or agreed to in writing,
015  * software distributed under the License is distributed on an
016  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017  * KIND, either express or implied.  See the License for the
018  * specific language governing permissions and limitations
019  * under the License.
020  
021  */
022 
023 
024 import java.util.Comparator;
025 
026 import org.apache.qpid.SerialException;
027 
028 /**
029  * This class provides basic serial number comparisons as defined in
030  * RFC 1982.
031  */
032 
033 public class Serial
034 {
035 
036     public static final Comparator<Integer> COMPARATOR = new Comparator<Integer>()
037     {
038         public int compare(Integer s1, Integer s2)
039         {
040             return Serial.compare(s1, s2);
041         }
042     };
043 
044     /**
045      * Compares two numbers using serial arithmetic.
046      *
047      @param s1 the first serial number
048      @param s2 the second serial number
049      *
050      @return a negative integer, zero, or a positive integer as the
051      * first argument is less than, equal to, or greater than the
052      * second
053      */
054     public static final int compare(int s1, int s2)
055     {
056         return s1 - s2;
057     }
058 
059     public static final boolean lt(int s1, int s2)
060     {
061         return compare(s1, s20;
062     }
063 
064     public static final boolean le(int s1, int s2)
065     {
066         return compare(s1, s2<= 0;
067     }
068 
069     public static final boolean gt(int s1, int s2)
070     {
071         return compare(s1, s20;
072     }
073 
074     public static final boolean ge(int s1, int s2)
075     {
076         return compare(s1, s2>= 0;
077     }
078 
079     public static final boolean eq(int s1, int s2)
080     {
081         return s1 == s2;
082     }
083 
084     public static final int min(int s1, int s2)
085     {
086         if (lt(s1, s2))
087         {
088             return s1;
089         }
090         else
091         {
092             return s2;
093         }
094     }
095 
096     public static final int max(int s1, int s2)
097     {
098         if (gt(s1, s2))
099         {
100             return s1;
101         }
102         else
103         {
104             return s2;
105         }
106     }
107 
108 }