Jump to content

Simple Interval class


Botre

Recommended Posts

07dbdbcc8019af1e4bb058252b8ea51a.png

public class Interval {

	private final long firstEntry;
	private final long lastEntry;

	private final boolean includeFirstEntry;
	private final boolean includeLastEntry;
	
	public Interval(final boolean includeFirstEntry, final long firstEntry, final long lastEntry, final boolean includeLastEntry) {
		this.firstEntry = firstEntry;
		this.lastEntry = lastEntry;
		this.includeFirstEntry = includeFirstEntry;
		this.includeLastEntry = includeLastEntry;
	}
	
	public boolean contains(long n) {
		return (n == firstEntry && includeFirstEntry) || (n == lastEntry && includeLastEntry) || (n > firstEntry && n < lastEntry);
	}

}

ff5ca46a0535837505bc4da922ee7198.png

Edited by Botrepreneur
  • Like 1
Link to comment
Share on other sites

Can just make always not including firstEntry, lastEntry? And like if(includeFirstEntry) firstEntry--; if(includeLastEntry) lastEntry++;

 

I see what you mean, I could but I prefer the boolean specification because it mimics the mathematical notation for intervals.

 

[ 1, 40 [

 

=

 

a8855ed3e97ede4e0dd3d5c461319760.png

 

 

Edited by Botrepreneur
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...