Range.java
001 /*
002  *
003  * Licensed to the Apache Software Foundation (ASF) under one
004  * or more contributor license agreements.  See the NOTICE file
005  * distributed with this work for additional information
006  * regarding copyright ownership.  The ASF licenses this file
007  * to you under the Apache License, Version 2.0 (the
008  * "License"); you may not use this file except in compliance
009  * with the License.  You may obtain a copy of the License at
010  *
011  *   http://www.apache.org/licenses/LICENSE-2.0
012  *
013  * Unless required by applicable law or agreed to in writing,
014  * software distributed under the License is distributed on an
015  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016  * KIND, either express or implied.  See the License for the
017  * specific language governing permissions and limitations
018  * under the License.
019  *
020  */
021 package org.apache.qpid.transport;
022 
023 import java.util.ArrayList;
024 import java.util.List;
025 
026 import static org.apache.qpid.util.Serial.*;
027 
028 
029 /**
030  * Range
031  *
032  @author Rafael H. Schloming
033  */
034 
035 public final class Range
036 {
037     private final int lower;
038     private final int upper;
039 
040     public Range(int lower, int upper)
041     {
042         this.lower = lower;
043         this.upper = upper;
044     }
045 
046     public int getLower()
047     {
048         return lower;
049     }
050 
051     public int getUpper()
052     {
053         return upper;
054     }
055 
056     public boolean includes(int value)
057     {
058         return le(lower, value&& le(value, upper);
059     }
060 
061     public boolean includes(Range range)
062     {
063         return includes(range.lower&& includes(range.upper);
064     }
065 
066     public boolean intersects(Range range)
067     {
068         return (includes(range.lower|| includes(range.upper||
069                 range.includes(lower|| range.includes(upper));
070     }
071 
072     public boolean touches(Range range)
073     {
074         return (intersects(range||
075                 includes(range.upper + 1|| includes(range.lower - 1||
076                 range.includes(upper + 1|| range.includes(lower - 1));
077     }
078 
079     public Range span(Range range)
080     {
081         return new Range(min(lower, range.lower), max(upper, range.upper));
082     }
083 
084     public List<Range> subtract(Range range)
085     {
086         List<Range> result = new ArrayList<Range>();
087 
088         if (includes(range.lower&& le(lower, range.lower - 1))
089         {
090             result.add(new Range(lower, range.lower - 1));
091         }
092 
093         if (includes(range.upper&& le(range.upper + 1, upper))
094         {
095             result.add(new Range(range.upper + 1, upper));
096         }
097 
098         if (result.isEmpty() && !range.includes(this))
099         {
100             result.add(this);
101         }
102 
103         return result;
104     }
105 
106     public Range intersect(Range range)
107     {
108         int l = max(lower, range.lower);
109         int r = min(upper, range.upper);
110         if (gt(l, r))
111         {
112             return null;
113         }
114         else
115         {
116             return new Range(l, r);
117         }
118     }
119 
120     public String toString()
121     {
122         return "[" + lower + ", " + upper + "]";
123     }
124 
125 }