A Range represents an interval—a set of values with a beginning and an end. Java does not have a Range util class. Here is an implementation of this class. It uses generics so, any object that implements the comparable interface should work well with it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.rushis.util;
 
public class Range<T extends Comparable<T>> {
	private T start=null;
	private T end=null;
 
	public Range(T start, T end) {
		if(start.compareTo(end)!=-1){
			throw new IllegalArgumentException();
		}
		this.start = start;
		this.end = end;
	}
 
	public T getStart() {
		return this.start;
	}
 
	public T getEnd() {
		return this.end;
	}
 
	public boolean contains(T obj) {
		return (start.compareTo(obj)<=0 && end.compareTo(obj)>=0);
	}
}
“Scratch your own itch, and work on things you like”
-OpenSource Philosophy

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>