Right,
then I would do
// helper fnc to create integers....
Integer createKlangedIntegers(int i) {
if (i >= -128 && i <= 127) {
return new Integer(i);
}
return Integer.valueOf(i);
}
And, btw, even if you'll change the internal cache at cell 4, it
still would be immutable ;).
KlaasJan
On Dec 4, 11:19 am, "Viktor Klang" <[EMAIL PROTECTED]> wrote:
> On Thu, Dec 4, 2008 at 8:26 AM, klaasjan <[EMAIL PROTECTED]> wrote:
>
> > I do Integer.valueOf(3);
>
> But what you do not know is that I've already replaced cell no 4 in the
> internal cache in Integer.class with the value 1337
>
>
>
>
>
> > On Dec 4, 4:58 am, hlovatt <[EMAIL PROTECTED]> wrote:
> > > In 219 there was a long discussion about setting up an object and then
> > > making it immutable. There are a couple of ways of doing this in Java
> > > already:
>
> > > public class FooArgs {
> > > public int x = 1; // 1 is default
> > > public int y = 2;
> > > public int z = 3;
> > > ...
>
> > > }
>
> > > public class Foo {
> > > public Foo( FooArgs args ) { ... }
> > > ...
>
> > > }
>
> > > Foo foo = new Foo( new FooArgs() {{ x = 2 }} );
>
> > > Another option is an immutable and mutable versions that inherit from
> > > a common base and implement a standard conversion interface. First the
> > > standard interface:
>
> > > public interface ImmutableValueConversions {
> > > Immutable toImmutable() throws IllegalStateException;
> > > Value toValue();
>
> > > }
>
> > > Below is an integer example that uses an abstract class as the base of
> > > the hierarchy. You can also use interfaces. The abstract class is:
>
> > > 8:public abstract class AbstractInteger extends AbstractValue
> > > 9: implements ImmutableValueConversions, InstanceFactory {
> > > 10: public abstract int getValue();
> > > 11:
> > > 12: public abstract AbstractInteger instanceOfSelf( int value );
> > > 13:
> > > 14: public AbstractInteger add( final int rhs ) {
> > > 15: return instanceOfSelf( getValue() + rhs );
> > > 16: }
> > > 17:}
>
> > > Then the immutable version:
>
> > > 9:public final class ImmutableInteger2 extends AbstractInteger
> > > 10: implements Immutable {
> > > 11: private final int value;
> > > 12:
> > > 13: public ImmutableInteger2( final int value ) {
> > > 14: this.value = value;
> > > 15: }
> > > 16:
> > > 17: public int getValue() {
> > > 18: return value;
> > > 19: }
> > > 20:
> > > 21: public Immutable toImmutable() {
> > > 22: return this;
> > > 23: }
> > > 24:
> > > 25: public Value toValue() {
> > > 26: return new ValueInteger( value );
> > > 27: }
> > > 28:
> > > 29: public AbstractInteger instanceOfSelf( final int value ) {
> > > 30: return new ImmutableInteger2( value );
> > > 31: }
> > > 38:}
>
> > > And the value version:
>
> > > 9:public final class ValueInteger extends AbstractInteger {
> > > 10: // Not final, this is the value version
> > > 11: private int value;
> > > 12:
> > > 13: public ValueInteger( final int value ) {
> > > 14: this.value = value;
> > > 15: }
> > > 16:
> > > 17: public int getValue() {
> > > 18: return value;
> > > 19: }
> > > 20:
> > > 21: // Setter, this is the value version!
> > > 22: public void setValue( final int value ) {
> > > 23: this.value = value;
> > > 24: }
> > > 25:
> > > 26: public Immutable toImmutable() {
> > > 27: return new ImmutableInteger2( value );
> > > 28: }
> > > 29:
> > > 30: public Value toValue() {
> > > 31: return new ValueInteger( value );
> > > 32: }
> > > 33:
> > > 34: public AbstractInteger instanceOfSelf( final int value ) {
> > > 35: return new ValueInteger( value );
> > > 36: }
> > > 37:
> > > 38: // Set and add combined (+=), this is the value version!
> > > 39: public ValueInteger setAdd( final int rhs ) {
> > > 40: value += rhs;
> > > 41: return this;
> > > 42: }
> > > 49:}
>
> > > Then you can use these classes like this:
>
> > > ImmutableInteger i = (ImmutableInteger)( new ValueInteger().setValue
> > > ( 2 ).toValue() );
>
> > > My own pet project, PEC, is an extensible compiler that can enforce
> > > patterns, the above immutable pattern can be enforced. More details
> > > from:
>
> > > http://pec.dev.java.net/nonav/frontpage.html
>
> > > What do others do?
>
> --
> Viktor Klang
> Senior Systems Analyst
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "The
Java Posse" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/javaposse?hl=en
-~----------~----~----~----~------~----~------~--~---