From: Roedy Green on
On Mon, 17 May 2010 12:37:27 +0000 (UTC), Rhino
<no.offline.contact.please(a)example.com> wrote, quoted or indirectly
quoted someone who said :

>1. If I create a JSpinner with a SpinnerNumberModel and specify the numbers
>in the model explicitly as short, why can't I rely on the getValue() from
>the JSpinner always returning shorts? In other words, if I define it all
>with shorts, what actions on my part will result in getValue() returning an
>Integer or something other than Short? For some reason, the results from
>getValue() are sometimes Short but sometimes Integer and I don't understand
>why. I'm trying to figure out how to make getValue() return ONLY Short (or,
>failing that, to return only Integer.) This is the statement that defined
>the model:

Here is the code for SpinnerNumberModel.setValue

public void setValue(Object value) {
if ((value == null) || !(value instanceof Number)) {
throw new IllegalArgumentException("illegal value");
}
if (!value.equals(this.value)) {
this.value = (Number)value;
fireStateChanged();
}

Note how it accepts any sort of Number, e.g. Short, Integer, Float,
Long ... and uses that for the internal representation. What ever you
put in will be what you get back out.
}
--
Roedy Green Canadian Mind Products
http://mindprod.com

Beauty is our business.
~ Edsger Wybe Dijkstra (born: 1930-05-11 died: 2002-08-06 at age: 72)

Referring to computer science.
From: Lew on
On 05/18/2010 06:41 PM, Roedy Green wrote:
> Here is the code for SpinnerNumberModel.setValue
>
> public void setValue(Object value) {
> if ((value == null) || !(value instanceof Number)) {

It's interesting that they use a redundant check in that condition.

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2

I believe I detect an error in the JLS in that section. They refer to the
type of the left-hand operand of the 'instanceof' operator as a
/RelationalExpression/, which it is not. At least not as I read the document.
The text explains what they mean there, so the rules are clear though the
syntax diagram not be.

--
Lew
From: John B. Matthews on
In article <hsv7o1$ah4$1(a)news.albasani.net>, Lew <noone(a)lewscanon.com>
wrote:

> On 05/18/2010 06:41 PM, Roedy Green wrote:
> > Here is the code for SpinnerNumberModel.setValue
> >
> > public void setValue(Object value) {
> > if ((value == null) || !(value instanceof Number)) {
>
> It's interesting that they use a redundant check in that condition.
>
> http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.20.2
>
> I believe I detect an error in the JLS in that section. They refer
> to the type of the left-hand operand of the 'instanceof' operator as
> a /RelationalExpression/, which it is not. At least not as I read
> the document.

I'm puzzled. It looks like RelationalExpression in

RelationalExpression instanceof ReferenceType

refers back through ShiftExpression, AdditiveExpression,
MultiplicativeExpression, and UnaryExpression to CastExpression
via UnaryExpressionNotPlusMinus. For example,

Object i = 1;
if ((Comparable)i instanceof Number) ...

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.16

> The text explains what they mean there, so the rules are clear though
> the syntax diagram not be.

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>