D
Danno
I think the idea of generics is that your code should be "generic". In
other words, while IntRange is fine for a class, generics force to be
more generic in your approach, so I created a regular class called
Range. I provided an example below, using your code, that seems to
work ok. I included a main method so you can check it out using longs,
short, etc.
I am no badass with generics, I just know the regular stuff. You just
happened to post while I was researching some advanced uses for
generics.
Let me know if that works for ya through here or email.
package com.evolutionnext;
import java.util.TreeSet;
/**
*
* @author valued customer
*/
public class Range<E extends Comparable<E>> extends TreeSet<E>{
public Range(E first, E second) {
add(first);
add(second);
}
public Range(E[] range) {
if (range.length != 2) {
throw new IllegalArgumentException("The Range array must
have exactly two values.");
}
add(range[0]);
add(range[1]);
}
public boolean input(E element) {
return last().compareTo(element) > first().compareTo(element);
}
public Object[] getValues() {
Object[] result = new Object[2];
result[0] = first();
result[1] = last();
return result;
}
public static void main(String[] args) {
Range<Integer> range = new Range<Integer>(1, 4);
System.out.println(range.input(3));
System.out.println(range.input(5));
System.out.println(range.input(-2));
System.out.println(range.input(0));
System.out.println(range.input(1));
}
}
other words, while IntRange is fine for a class, generics force to be
more generic in your approach, so I created a regular class called
Range. I provided an example below, using your code, that seems to
work ok. I included a main method so you can check it out using longs,
short, etc.
I am no badass with generics, I just know the regular stuff. You just
happened to post while I was researching some advanced uses for
generics.
Let me know if that works for ya through here or email.
package com.evolutionnext;
import java.util.TreeSet;
/**
*
* @author valued customer
*/
public class Range<E extends Comparable<E>> extends TreeSet<E>{
public Range(E first, E second) {
add(first);
add(second);
}
public Range(E[] range) {
if (range.length != 2) {
throw new IllegalArgumentException("The Range array must
have exactly two values.");
}
add(range[0]);
add(range[1]);
}
public boolean input(E element) {
return last().compareTo(element) > first().compareTo(element);
}
public Object[] getValues() {
Object[] result = new Object[2];
result[0] = first();
result[1] = last();
return result;
}
public static void main(String[] args) {
Range<Integer> range = new Range<Integer>(1, 4);
System.out.println(range.input(3));
System.out.println(range.input(5));
System.out.println(range.input(-2));
System.out.println(range.input(0));
System.out.println(range.input(1));
}
}