Skip to content
Snippets Groups Projects
Commit fd3aa704 authored by Christopher Schankula's avatar Christopher Schankula :earth_africa:
Browse files

add Bound and Range

parent ec5372c0
No related branches found
No related tags found
No related merge requests found
package sort;
public enum Bound {
LOWER, UPPER, LOWHIGH, ANY;
}
package sort;
public class Range<Key extends Comparable<Key>> {
private final Bound boundType;
private final Key lower;
private final Key upper;
public Range(Bound bt) throws Exception {
if (bt == Bound.ANY) {
boundType = bt;
lower = null;
upper = null;
}
else
throw new Exception("Must provide ANY bound.");
}
public Range(Bound bt, Key upperOrLower) throws Exception {
if (bt == Bound.UPPER) {
upper = upperOrLower;
lower = null;
} else if (bt == Bound.LOWER) {
upper = upperOrLower;
lower = null;
}
else
throw new Exception("Must provide UPPER OR LOWER bound.");
boundType = bt;
}
public Range(Bound bt, Key lower, Key upper) throws Exception {
if (bt == Bound.LOWHIGH) {
this.lower = lower;
this.upper = upper;
} else
throw new Exception("Must provide LOWHIGH bound.");
this.boundType = bt;
}
public boolean inBounds(Key key, GeneralCompare<Key> gc) {
if (boundType == Bound.ANY) return true;
else if (boundType == Bound.LOWER) return gc.compare(lower, key) <= 0;
else if (boundType == Bound.UPPER) return gc.compare(upper, key) >= 0;
else return gc.compare(lower, key) <= 0 && gc.compare(upper, key) >= 0;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment