• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Subclass final class (Boolean) ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Subclass final class (Boolean) ?


  • Subject: Re: Subclass final class (Boolean) ?
  • From: Ken Anderson <email@hidden>
  • Date: Mon, 28 Aug 2006 07:23:44 -0400

You're absolutely right Kieran - it's my concern too, and I plan on doing a lot of testing to see what occurs before actually using it :)

Ken

On Aug 27, 2006, at 10:59 PM, Kieran Kelleher wrote:

Correct me if I am wrong, but I have a hunch that one caveat of using this strategy instead of the custom attribute with factory and conversion methods is that EOQualifiers (for example in a Fetch Specification) on this attribute may not "just work" .... it might be worth verifying before you become too dependent on this strategy only to find later you might not be able write effective EOQualifiers on this attribute. With the "true/false" custom attribute as described in Chuck's book, you can use Boolean.TRUE in a qualifier on the attribute and it does "just work" .... keeping in mind that the same Boolean EOQualifier works in an in-memory array of EO's and a database fetch where EOF auto-generates the SQL WHERE x='true' or x='false' on the CHAR(5) field.

Regards, Kieran

On Aug 27, 2006, at 1:32 PM, Ken Anderson wrote:

In case anyone else is interested, here's my current solution. I modified the EOGenerator template for attributes like this:

<$foreach Attribute classAttributes.@sortedNameArray do$>
<$if Attribute.prototypeName eq 'boolean'$>
public Boolean <$Attribute.name$>() {
Integer storedValue = (Integer) storedValueForKey("< $Attribute.name$>");
if (storedValue.intValue() > 0)
return new Boolean(true);
else
return new Boolean(false);
}
public void set<$Attribute.name.initialCapitalString$>(Boolean aVal) {
if (aVal.booleanValue())
takeStoredValueForKey(new Integer(1), "<$Attribute.name$>");
else
takeStoredValueForKey(new Integer(0), "<$Attribute.name$>");
}
<$else$>
public <$Attribute.javaValueClassName$> <$Attribute.name$>() {
return (<$Attribute.javaValueClassName$>)storedValueForKey ("<$Attribute.name$>");
}
public void set<$Attribute.name.initialCapitalString$>(< $Attribute.javaValueClassName$> aValue) {
takeStoredValueForKey(aValue, "<$Attribute.name$>");
}<$endif$>



This change assumes that you have a prototype called boolean, but besides that, it's pretty generic and has worked well so far :)


Ken

On Aug 27, 2006, at 11:47 AM, Ken Anderson wrote:

Hey Steve...

I think you miss my intention. I don't want to have tons of subclasses, I simply want to have a boolean in the database that returns a boolean object in EOGenerator without writing code for all of them. I'd really rather not hold my nose :)

I always liked Chuck's solution of creating a custom type in EOModeler that could go to/from booleans using a varchar(5) field in the database, but just can't get around the amount of space it's taking up. This endeavor was to try and get the no-code solution without storing 5 characters in the database.

If you're not familiar, the solution in Practical WebObjects is to create a custom internal data type with a class of java.lang.Boolean, a factory method of valueOf, and a conversion method of toString(). This gets you booleans the way you want them in java from your DB, no code.

Ken

On Aug 27, 2006, at 11:40 AM, Steve Quirk wrote:



On Sun, 27 Aug 2006, Ken Anderson wrote:

OK, I'm a little annoyed here because I find the whole concept of 'final' to be ridiculous.

I would like to subclass Boolean so that I can initialize it (class method) with a short string (valueOf:"t" instead of valueOf:"f") and also to return "t" and "f" for toString (or some other method like toShortString()).

Unfortunately, java.lang.Boolean is marked as final. Is there any way to get around that?

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).

Oh, and yet another reason I want categories from Objective-C in Java...

Thanks,
Ken

Hi Ken,

For a more complex class with several values, it might make sense (a single letter indicating eye color, for instance). But DBs have lots of examples with a two-value true/false column - all in the same schema.

What do you do for the other tables in your DB that use 1 and 0 (Number) or 'Y'/'N' (char(1)) or 'A'/'D' (char(1) - "Alive"/"Dead") to represent true and false? A dozen subclasses of Boolean in the same app? Your coworkers and those who follow you to maintain your app will be grabbing the pitchforks and lighting the torches to come & hunt you.

Just give in and do what everyone else does. In your entity class, hold your nose and code something like:

   private static final Character TRUE  = new Character('t');
   private static final Character FALSE = new Character('f');

   boolean rancid;  // column RANCID char(1), 't' = TRUE

   public void setRancid(boolean aBool) {
       value = aBool;
   }
   public void setRancid(Character aChar) {
       setRancid(TRUE.equals(aChar));
   }
   public Character getRancid() {
       return (rancid) ? TRUE : FALSE;
   }
   public boolean isRancid() {
       return rancid;
   }

   ...

TRUE/FALSE is just too easy to code around. Keeping the simple things final might not be a bad idea.

- sq



_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40anderhome.com


This email sent to email@hidden

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40mac.com


This email sent to email@hidden


_______________________________________________ Do not post admin requests to the list. They will be ignored. Webobjects-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
References: 
 >Subclass final class (Boolean) ? (From: Ken Anderson <email@hidden>)
 >Re: Subclass final class (Boolean) ? (From: Steve Quirk <email@hidden>)
 >Re: Subclass final class (Boolean) ? (From: Ken Anderson <email@hidden>)
 >Re: Subclass final class (Boolean) ? (From: Ken Anderson <email@hidden>)
 >Re: Subclass final class (Boolean) ? (From: Kieran Kelleher <email@hidden>)

  • Prev by Date: Re: Why Ruby is popular?
  • Next by Date: Re: Why Ruby is popular?
  • Previous by thread: Re: Subclass final class (Boolean) ?
  • Next by thread: Re: Subclass final class (Boolean) ?
  • Index(es):
    • Date
    • Thread