• 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: How To Use A Type Safe Enum As A Custom Value In EOF (was Re: (EOM) entity logic)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How To Use A Type Safe Enum As A Custom Value In EOF (was Re: (EOM) entity logic)


  • Subject: Re: How To Use A Type Safe Enum As A Custom Value In EOF (was Re: (EOM) entity logic)
  • From: Arturo PĂ©rez <email@hidden>
  • Date: Sun, 31 Jul 2005 11:28:12 -0400

Hi all,

I was looking at this Type Safe Enum pattern. Implemented it, works fine but I have a space-efficiency question.

Essentially, what I want is a enum in the C sense where I can specify the values; e.g. RED=1, BLUE=2, etc. But using the code below my choice of data storage is either a String or NSData. But for space efficiency I'ld like to use database smallints.

Is there anyway to get objectWithArchiveData to take anything other than strings and NSData? More specifically, is there any way to get it to take integer values?

TIA,
-arturo

On Nov 22, 2004, at 8:55 PM, John Bruce wrote:

Hi Benjamin,

Would you be willing to share your Gender entity class with me?

Sure no problem. If you want some background info on the Type Safe Enum pattern these a section on it in "Effective Java".


import com.webobjects.foundation.*;
import java.util.Enumeration;

public final class Gender extends Object implements Cloneable {

public static final Gender MALE = new Gender("Male", "M");
public static final Gender FEMALE = new Gender("Female", "F");

public static final NSArray TYPES = new NSArray(new Object[] {MALE, FEMALE});

private final String type;
private final String code;

private Gender(final String type, final String code) {
this.type = type;
this.code = code;
}

public String type() {
return type;
}

public String code() {
return code;
}

// see "Practical WebObjects" - have to implement clone to make EOF happy
// since immutable just return same object
public Object clone() {
return this;
}

public String toString() {
StringBuffer buffer = new StringBuffer();
buffer.append("(");
buffer.append(code());
buffer.append(") ");
buffer.append(type());

return buffer.toString();
}

public String archiveData() {
return code();
}

public static Gender objectWithArchiveData(final String value) {
Gender gender = null;

for (Enumeration e = TYPES.objectEnumerator(); e.hasMoreElements(); ) {
Gender g = (Gender)e.nextElement();

if (g.code().equals(value)) {
gender = g;
break;
}
}

if (gender == null) {
throw new IllegalArgumentException("Gender not found with code: " + value);
}

return gender;
}


}

Once you have this you can set up a custom type in EOModeler with these values:

external width: 1
class: Gender (remember to use the full package name)
Factory method: objectWithArchiveData (EOModeler uses this by default but it puts a ":" after it so remember to remove that")
Conversion method: archiveData
Init Argument: NSString



So now you can do things like:

customer.setGender(Gender.MALE);

if (customer.gender() == Gender.FEMALE) {
	doSomething();
}


Since we return the same static object we can compare by reference instead of String equality.


There is some good stuff on using constant objects in "Hardcore Java", it also has a good chapter on the use of "final".

Cheers,

John

_______________________________________________
WebObjects-dev mailing list
email@hidden
http://www.omnigroup.com/mailman/listinfo/webobjects-dev


_______________________________________________ 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
  • Prev by Date: Re: Malformed Bundle alert - Solved - Premature
  • Next by Date: Re: Setting Default Values EOModeler
  • Previous by thread: Re: Malformed Bundle alert - Solved - Premature
  • Index(es):
    • Date
    • Thread