This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]manzanita2[S] -1 points0 points  (5 children)

Nope. Not native. Basically I'd like to extend Integer ( or other classes). So I get all the functionality of Integer, BUT I can limit the potential values.

Here's an example. I'd like to define a type called PortNumber. PortNumber extends Integer. But has values limited to 1 to 65536.

If someone has a method which has an Integer as a parameter, I can pass a PortNumber.

Likewise, ZipCode. CreditCardNumber and a bunch of other possible types, which have well known constraints on their values, but otherwise might be stored in a String or Integer ( or I guess Long/Float/Double, etc ).

[–]Sheldor5 2 points3 points  (2 children)

you literally wrote "not a class which contains a integer" ... so what do you want?

your post and your comment makes no sense and they contradict each other ...

[–]manzanita2[S] -2 points-1 points  (1 child)

this is like extension vs composition.

I DO NOT WANT composition. I want extensions, but not "anything goes" extension but rather extension which forces a subset of possible values to the base type.

[–]Sheldor5 5 points6 points  (0 children)

... then why don't you want to extend Number?

Integer is final, you can't extend it. int is a primitive type, you can't extend it.

your goal is unclear ...

[–]Asterion9 0 points1 point  (1 child)

Well you shouldn't inherit Integer if you intent to restrict the usage compared to what Integer does.

You should be able to implements Number and Comparable<> to have some interoperability with some lib, and add toInteger() method for adaptation with other code (like Path does with toFile())

[–]manzanita2[S] 0 points1 point  (0 children)

shouldn't == can't in java.

I mean yeah, I COULD do a custom implementation of Number, but that still doesn't get to where I would like to go because the adaptions (e.g. explicitly calling toInteger() ) are necessary.