From: Rhino on 17 May 2010 08:37 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: SpinnerNumberModel seasonNumberModel = new SpinnerNumberModel((short)1, (short)1, (short)20, (short)1); 2. If a given method can return various sorts of integer numbers, like Short, Integer, and Long, what is the simplest way to determine the exact type that is being returned by the method? I'm just trying to cover the worst-case scenario that factors beyond my control may make it impossible to predict whether getValue() is going to return Integer or Short from my JSpinner. If that turns out to be true, I'd like to know the best way to determine if a given value is a short, int, long or anything else. I think I used to know how to do that several years ago but I'm darned if I can remember that technique now. -- Rhino
From: Lew on 17 May 2010 09:36 Rhino wrote: > 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 Because that is not the defined behavior of the model. > 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 > I don't know, but I bet if you look at the source for 'SpinnerNumberModel' you'll find out. My guess is that it uses the subtype of 'Number' that most closely matches the returned value. Given the model class's contract, there's nothing you can do directly to change what it returns. You get a 'Number' and that's the best you can predict. > 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: > > SpinnerNumberModel seasonNumberModel = new SpinnerNumberModel((short)1, > (short)1, (short)20, (short)1); > This call feeds integers to the constructor. Did you look at the Javadocs for the constructor? You are taking 'int' values, casting them to 'short', then the language is widening them back to 'int' arguments. There is no constructor overload for that class that takes 'short' parameters. All you are guaranteed from 'SpinnerNumberModel' is that the returned value of 'getValue()' is a 'Number'. (The signature says 'Object', but the Javadocs tell you that it will be a 'Number'.) If you want different behavior, set your own model. > 2. If a given method can return various sorts of integer numbers, like > Short, Integer, and Long, what is the simplest way to determine the exact > type that is being returned by the method? I'm just trying to cover the > Don't. Just use the return type, which for 'SpinnerNumberModel' is an 'Object' that can be downcast to 'Number'. > worst-case scenario that factors beyond my control may make it impossible > to predict whether getValue() is going to return Integer or Short from my > JSpinner. If that turns out to be true, I'd like to know the best way to > determine if a given value is a short, int, long or anything else. I think > I used to know how to do that several years ago but I'm darned if I can > remember that technique now. > 'instanceof'? What difference does it make? Just use 'Number'. Use its 'longValue()', 'intValue()', etc., methods if you must. You're making a mountain out of a molehill. Read the Javadocs for the classes and methods you're using! They will tell you what guarantees you can count on, and that's all you get to count on. Live with it. -- Lew
From: RedGrittyBrick on 17 May 2010 10:00 On 17/05/2010 13:37, Rhino wrote: > 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: > > SpinnerNumberModel seasonNumberModel = new SpinnerNumberModel((short)1, > (short)1, (short)20, (short)1); The relevant 1.6 API signature is SpinnerNumberModel(int value, int minimum, int maximum, int stepSize) - there isn't a constructor for shorts, so your shorts are being extended to ints by the compiler before the constructor gets to use them. > 2. If a given method can return various sorts of integer numbers, like > Short, Integer, and Long, what is the simplest way to determine the exact > type that is being returned by the method? Your description has both 'short' and 'Short' types which is confusing. Reference types can be tested for using the instanceof operator. ------------------------------8<------------------------------------ public class ShortIntegers { public static void main(String[] args) { SpinnerNumberModel snm = new SpinnerNumberModel( (short)1, (short)1, (short)20, (short)1); Object o = snm.getValue(); if (o instanceof Short) { System.out.println("Short"); } if (o instanceof Integer) { System.out.println("Integer"); } System.out.println(o); } } ------------------------------8<------------------------------------ Integer 1 > I'm just trying to cover the > worst-case scenario that factors beyond my control may make it impossible > to predict whether getValue() is going to return Integer or Short from my > JSpinner. If that turns out to be true, I'd like to know the best way to > determine if a given value is a short, int, long or anything else. Which factors are beyond your control? -- RGB
From: Eric Sosman on 17 May 2010 11:29 On 5/17/2010 8:37 AM, Rhino wrote: > 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: > > SpinnerNumberModel seasonNumberModel = new SpinnerNumberModel((short)1, > (short)1, (short)20, (short)1); This is just like `new SpinnerNumberModel(1, 1, 20, 1)' -- the fact that your promoted-to-int arguments happen to be in range for `short' (or even for `byte') doesn't influence the constructor's behavior, nor that of the constructed SpinnerNumberModel. Also, even if the supposed range of the SpinnerNumberModel is 1 through 20 with an integer step, setValue(new Double(98.6)) is still possible and will work. It's a little odd that you use getValue() instead of getNumber(), but that shouldn't matter a lot. > 2. If a given method can return various sorts of integer numbers, like > Short, Integer, and Long, what is the simplest way to determine the exact > type that is being returned by the method? I'm just trying to cover the > worst-case scenario that factors beyond my control may make it impossible > to predict whether getValue() is going to return Integer or Short from my > JSpinner. If that turns out to be true, I'd like to know the best way to > determine if a given value is a short, int, long or anything else. I think > I used to know how to do that several years ago but I'm darned if I can > remember that technique now. You can use getClass() or instanceof on the returned Object(). If you really want to be draconian, you could do Number num = seasonNumberModel.getNumber(); if (! (num instanceof Short)) { // Accept no substitutes: num = Short.valueOf(num.shortValue()); } -- Eric Sosman esosman(a)ieee-dot-org.invalid
From: Rhino on 17 May 2010 12:30 RedGrittyBrick <RedGrittyBrick(a)spamweary.invalid> wrote in news:4bf14bfd$0$28004$db0fefd9(a)news.zen.co.uk: > On 17/05/2010 13:37, Rhino wrote: >> 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: >> >> SpinnerNumberModel seasonNumberModel = new >> SpinnerNumberModel((short)1, (short)1, (short)20, (short)1); > > The relevant 1.6 API signature is SpinnerNumberModel(int value, int > minimum, int maximum, int stepSize) - there isn't a constructor for > shorts, so your shorts are being extended to ints by the compiler > before the constructor gets to use them. > Sorry, my mistake. I've amended my code to supply ints instead of shorts. > >> 2. If a given method can return various sorts of integer numbers, >> like Short, Integer, and Long, what is the simplest way to determine >> the exact type that is being returned by the method? > > Your description has both 'short' and 'Short' types which is > confusing. Reference types can be tested for using the instanceof > operator. > > ------------------------------8<------------------------------------ > public class ShortIntegers { > public static void main(String[] args) { > SpinnerNumberModel snm = new SpinnerNumberModel( > (short)1, (short)1, (short)20, (short)1); > Object o = snm.getValue(); > if (o instanceof Short) { System.out.println("Short"); } > if (o instanceof Integer) { System.out.println("Integer"); } > System.out.println(o); > } > } > ------------------------------8<------------------------------------ > Integer > 1 > Sorry for not properly distinguishing between 'short' and 'Short' and 'int' and 'Integer'. I was a little confused myself. instanceof looks like it will do what I want. Thanks! > >> I'm just trying to cover the >> worst-case scenario that factors beyond my control may make it >> impossible to predict whether getValue() is going to return Integer >> or Short from my JSpinner. If that turns out to be true, I'd like to >> know the best way to determine if a given value is a short, int, long >> or anything else. > > Which factors are beyond your control? > According to Lew, getValue() in JSpinner could return various sorts of Numbers. However, the code I use to handle the Number returned by getValue() needs to know what it is getting back. At least I think it does. I'm going to reread Lew's reply again now; something he said there seemed to take me in the right direction but I just have to think on that a bit more. -- Rhino
|
Next
|
Last
Pages: 1 2 3 Prev: Integrating Google Maps in Java Swing application Next: Swing JDialog wrong cursor shapes |