Project Wonder 5.0.0.8787

er.extensions.eof
Class ERXConstant

java.lang.Object
  extended by er.extensions.eof.ERXConstant

public abstract class ERXConstant
extends Object

General purpose constant class, useful when you want reference object that are not bytes or strings in the DB like what you get with the factory classes.
If you use objects of this class, you might be able to completely remove the EOSharedEditingContext (the google search term for "why does my app lock up").

To use the Number constants, you need to add an entry ERXConstantClassName=Test.Status to the attribute's userInfo in question and your EO's class description needs to be a ERXEntityClassDescription, also you must enable the er.extension.ERXJDBCAdaptor.

The String and Byte based constants can be used with a custom class type:


 
 ERCMailMessage.plist:
 ...
 {
     columnName = MAIL_STATE_ID;
     name = state;
     prototypeName = osType;
     adaptorValueConversionMethodName = value;
     factoryMethodArgumentType = EOFactoryMethodArgumentIsNSString;
     valueClassName = er.corebusinesslogic.ERCMailState;
     valueFactoryMethodName = mailState;
 }
 ...
 
 public class ERCMailMessage extends EOGenericRecord {
 ...
   public ERCMailState state() {
       return (ERCMailState) storedValueForKey("state");
   }
   
   public void setState(ERCMailState value) {
       takeStoredValueForKey(value, "state");
   }
 ...
 }
 
 public class ERCMailState extends ERXConstant.StringConstant {

     public ERCMailState(String key, String name) {
         super(key, name);
     }
     
     public static ERCMailState mailState(String key) {
      return (ERCMailState) constantForClassNamed(key, ERCMailState.class.getName());
     }
 
     public static ERCMailState EXCEPTION_STATE = new ERCMailState ("xcpt", "Exception");
     public static ERCMailState READY_TO_BE_SENT_STATE = new ERCMailState("rtbs", "Ready to be sent");
     public static ERCMailState SENT_STATE = new ERCMailState("sent", "Sent");
     public static ERCMailState RECEIVED_STATE = new ERCMailState("rcvd", "Received");
     public static ERCMailState WAIT_STATE = new ERCMailState("wait", "Wait");
     public static ERCMailState PROCESSING_STATE = new ERCMailState("proc", "Processing");
 }
 

An example would be:

 public class Test extends EOGenericRecord {
        // your "status" attribute need a userInfo entry 
        // "ERXConstantClassName" = "Test.Status";
  // Normally, the class name would be "Test$Status", this form is used to help you use EOGenerator
        public static class Status extends ERXConstant.NumberConstant {
                protected Status(int value, String name) {
                        super(value, name);
                }
        }
        
        public Status OFF = new Status(0, "Off");
        public Status ON = new Status(1, "On");
        
     public Test() {
         super();
     }
 
     public Status status() {
         return (Status)storedValueForKey("status");
     }
 
     public void setStatus(Constant aValue) {
         takeStoredValueForKey(aValue, "status");
     }
     
     public boolean isOn() {
        return status() == ON;
     }
 }
 
 Test test = (Test)EOUtilities.createAndInsertInstance(ec, "Test");
 test.setTest(Test.Status.OFF);
 test = (Test)EOUtilities.createAndInsertInstance(ec, "Test");
 test.setStatus(Test.Status.ON);
 ec.saveChanges();
 
 NSArray objects;
 NSArray all = EOUtilities.objectsForEntityNamed(ec, "Test");
 EOQualifier q;
 
 objects = EOUtilities.objectsMatchingKeyAndValue(ec, "Test", "status", Test.Status.OFF);
 log.info("Test.Status.OFF: " + objects);
 q = new EOKeyValueQualifier("status", EOQualifier.QualifierOperatorEqual, Test.Status.OFF);
 log.info("Test.Status.OFF: " + EOQualifier.filteredArrayWithQualifier(all, q));
 
 // this might be a problem: equal number values match in the DB, but not in memory
 objects = EOUtilities.objectsMatchingKeyAndValue(ec, "Test", "status", ERXConstant.OneInteger);
 log.info("Number.OFF: " + objects);
 q = new EOKeyValueQualifier("status", EOQualifier.QualifierOperatorEqual, ERXConstant.OneInteger);
 log.info("Number.OFF: " + EOQualifier.filteredArrayWithQualifier(all, q));
 
 // you can compare by equality
 test.getStatus() == Test.Status.ON
 
Note that upon class initialization 2500 Integers will be created and cached, from 0 - 2499.


Nested Class Summary
static class ERXConstant.ByteConstant
          Constant class that can be used with bytes or NSData in the DB.
static interface ERXConstant.Constant
           
static class ERXConstant.NumberConstant
           
static class ERXConstant.StringConstant
          Constant class that can be used with strings in the DB.
 
Field Summary
static NSArray EmptyArray
           
static Class[] EmptyClassArray
           
static NSDictionary EmptyDictionary
           
static NSData EmptyImage
          an empty gif image
static Object EmptyObject
           
static Object[] EmptyObjectArray
           
static String EmptyString
           
protected static Integer[] INTEGERS
           
static int MAX_INT
           
static Integer MinusOneInteger
           
static Class[] NotificationClassArray
           
static Class[] ObjectClassArray
           
static BigDecimal OneBigDecimal
           
static Integer OneInteger
           
static NSArray SingleNullValueArray
           
static Class[] StringClassArray
           
static Integer ThreeInteger
           
static Integer TwoInteger
           
static BigDecimal ZeroBigDecimal
           
static Integer ZeroInteger
           
 
Constructor Summary
ERXConstant()
           
 
Method Summary
static ERXConstant.Constant constantForClassNamed(Object value, String clazzName)
          Retrieves the constant for the given class name and value.
static NSArray constantsForClassName(String clazzName)
          Retrieves all constants for the given class name ordered by value.
static Integer integerForInt(int i)
          Returns an Integer for a given int
static Integer integerForString(String s)
          Returns an Integer for a given String
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

MAX_INT

public static final int MAX_INT
See Also:
Constant Field Values

INTEGERS

protected static Integer[] INTEGERS

EmptyObject

public static final Object EmptyObject

EmptyString

public static final String EmptyString
See Also:
Constant Field Values

EmptyArray

public static final NSArray EmptyArray

SingleNullValueArray

public static final NSArray SingleNullValueArray

EmptyDictionary

public static final NSDictionary EmptyDictionary

MinusOneInteger

public static final Integer MinusOneInteger

OneInteger

public static final Integer OneInteger

ZeroInteger

public static final Integer ZeroInteger

TwoInteger

public static final Integer TwoInteger

ThreeInteger

public static final Integer ThreeInteger

ZeroBigDecimal

public static final BigDecimal ZeroBigDecimal

OneBigDecimal

public static final BigDecimal OneBigDecimal

EmptyClassArray

public static final Class[] EmptyClassArray

NotificationClassArray

public static final Class[] NotificationClassArray

ObjectClassArray

public static final Class[] ObjectClassArray

StringClassArray

public static final Class[] StringClassArray

EmptyObjectArray

public static final Object[] EmptyObjectArray

EmptyImage

public static final NSData EmptyImage
an empty gif image

Constructor Detail

ERXConstant

public ERXConstant()
Method Detail

constantsForClassName

public static NSArray constantsForClassName(String clazzName)
Retrieves all constants for the given class name ordered by value. An empty NSArray is returned if the class isn't found.

Parameters:
clazzName -

constantForClassNamed

public static ERXConstant.Constant constantForClassNamed(Object value,
                                                         String clazzName)
Retrieves the constant for the given class name and value. Null is returned if either class or value isn't found.

Parameters:
value -
clazzName -

integerForInt

public static Integer integerForInt(int i)
Returns an Integer for a given int

Returns:
potentially cache Integer for a given int

integerForString

public static Integer integerForString(String s)
                                throws NumberFormatException
Returns an Integer for a given String

Returns:
potentially cache Integer for a given String
Throws:
NumberFormatException - forwarded from the parseInt method off of Integer

Last updated: Wed, Jan 7, 2009 • 04:35 AM EST

Copyright © 2002 – 2007 Project Wonder.