R
Rhino
I need a sanity check on a design I am considering.
I am trying to design a simple class called Range, which subclasses Object.
I want Range to store a two-element array of Objects that are both of the
exact same class. However, I only want to store Objects if it is possible to
determine which of the two is the lower value. Integers have intrinsic order
so two Integers can obviously can be compared to see which is lower, however
two JPanels do not have intrinsic order (aside from creation sequence which
I consider irrelevant for my purposes) so they have no intrinsic order.
Therefore, I have no problem in creating a Range of Integers but don't want
to create a Range of JPanels.
It seems to me that my best approach is to see if the Objects that are
passed to my Range constructor implement the Comparable interface. If they
do, I can compare the two Objects in the constructor and determine which is
the lower value.
I can use the getInterfaces() method in Class to determine the interfaces
implemented by the incoming Objects and then use the compareTo() method
defined for that class to find out which is lower, then store it as a class
variable called 'lowValue'; the other Object gets stored in 'highValue'.
Then I can write a between() method to determine if a third value is within
the Range defined by the two Objects in the Range.
Is this design basically sound?
Followup question: If this design is sound, how do I compare the actual
values of the Objects? In my constructor, I know that they belong to the
same class and that the class implements Comparable but how do I actually
see which is lower? I have to cast them from Object to their actual class
and then do compareTo() right? If so, how do I cast them from Object to
their actual class? I know the following does NOT compile because the
firstObject.getClass() is not an actual class name, just something that
resolves to a class name, which isn't good enough for the compiler:
if ( ((firstObject.getClass()) firstObject).compareTo(
((secondObject.getClass()) secondObject)) > 0) {
}
So how do I compare the two values to see which is lower? I assume there is
some standard idiom that will let me do this but I don't know what it is.
--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare
I am trying to design a simple class called Range, which subclasses Object.
I want Range to store a two-element array of Objects that are both of the
exact same class. However, I only want to store Objects if it is possible to
determine which of the two is the lower value. Integers have intrinsic order
so two Integers can obviously can be compared to see which is lower, however
two JPanels do not have intrinsic order (aside from creation sequence which
I consider irrelevant for my purposes) so they have no intrinsic order.
Therefore, I have no problem in creating a Range of Integers but don't want
to create a Range of JPanels.
It seems to me that my best approach is to see if the Objects that are
passed to my Range constructor implement the Comparable interface. If they
do, I can compare the two Objects in the constructor and determine which is
the lower value.
I can use the getInterfaces() method in Class to determine the interfaces
implemented by the incoming Objects and then use the compareTo() method
defined for that class to find out which is lower, then store it as a class
variable called 'lowValue'; the other Object gets stored in 'highValue'.
Then I can write a between() method to determine if a third value is within
the Range defined by the two Objects in the Range.
Is this design basically sound?
Followup question: If this design is sound, how do I compare the actual
values of the Objects? In my constructor, I know that they belong to the
same class and that the class implements Comparable but how do I actually
see which is lower? I have to cast them from Object to their actual class
and then do compareTo() right? If so, how do I cast them from Object to
their actual class? I know the following does NOT compile because the
firstObject.getClass() is not an actual class name, just something that
resolves to a class name, which isn't good enough for the compiler:
if ( ((firstObject.getClass()) firstObject).compareTo(
((secondObject.getClass()) secondObject)) > 0) {
}
So how do I compare the two values to see which is lower? I assume there is
some standard idiom that will let me do this but I don't know what it is.
--
Rhino
---
rhino1 AT sympatico DOT ca
"There are two ways of constructing a software design. One way is to make it
so simple that there are obviously no deficiencies. And the other way is to
make it so complicated that there are no obvious deficiencies." - C.A.R.
Hoare