Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Subclass final class (Boolean) ?



Greg,

Not to worry - annoyance does not stop any technical solutions.

I have already looked at your solution, and nixed it for the very reason you state - java.lang.Booleans will not handle equals correctly.

I have opted for a different solution at this point - using a custom EOGenerator template to generate methods that interact with WebObjects with Integers (bits in the database), but return and take Booleans. Hopefully it works!

Ken

On Aug 27, 2006, at 12:24 PM, Greg Guerin wrote:

Ken Anderson wrote:

To give a little background, I would like to have a custom WebObjects type
where I can declare the factory method to create the instance, and the
method to call to get back the string instance. I want to make it a single
character so that I can use a string field with length=1 instead of
length=5 (saving a lot of space).

Create your own class to represent the boolean values exactly the way you
want. I've done this before for Integer, Long, etc. when I needed
mutability or other things. In my case, I had the abstract Number class to
build from, which Boolean doesn't have.


Since a boolean has only two possible states, you can restrict your
constructor to private, and require calling the factory method. Then
choose either one or the other of exactly two static instances. You can
also accept a Boolean object for equals().


public class MyBoolean
{
    public static final MyBoolean TRUE = new MyBoolean( true );
    public static final MyBoolean FALSE = new MyBoolean( false );

    public static MyBoolean get( boolean b )
    { ... }

    public static MyBoolean get( Boolean b )
    { ... }

    public static MyBoolean get( String s )
    { ... }


private final boolean value;

    private MyBoolean( boolean b )
    {  value = b;  }

    public boolean value()
    {  return value;  }

  /** Accepts Boolean or MyBoolean. */
   public boolean equals(Object obj) {
	if (obj instanceof Boolean) {
	    return value == ((Boolean)obj).booleanValue();
	}
	if (obj instanceof MyBoolean) {
	    return value == ((MyBoolean)obj).value();
	}
	return false;
    }

   ...etc...
}

You can't override Boolean's equals() to reciprocate with
MyBoolean.equals(), so there's a risk of non-transitivity if your code
happens to use Boolean directly or indirectly.

Don't get stuck at the emotional level: annoyance. Getting annoyed at Java
doesn't solve anything. All it does is prevent you from seeing a technical
solution.


  -- GG


_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden

_______________________________________________ Do not post admin requests to the list. They will be ignored. Java-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/java-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: Subclass final class (Boolean) ? (From: Greg Guerin <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.