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