commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Doug_R...@percussion.com
Subject Re: [Betwixt] Java 5.0 enum
Date Mon, 09 Jan 2006 20:34:56 GMT
FWIW, I've handled this by hiding the enum properties and creating 
accessors that take and return integer values.  I'm sure the solution 
below is better, but I thought I'd offer the alternative.

Doug




Thomas Dudziak <tomdzk@gmail.com> 
01/07/2006 01:28 PM
Please respond to
"Jakarta Commons Users List" <commons-user@jakarta.apache.org>


To
Jakarta Commons Users List <commons-user@jakarta.apache.org>
cc

Subject
Re: [Betwixt] Java 5.0 enum






On 1/7/06, vachevil-basic@yahoo.com <vachevil-basic@yahoo.com> wrote:

> Is there any way to support a JDK 5.0 Enum in Betwixt?  At the moment it 
jus defines:
>     <entityType>
>       <declaringClass>net......EntityType</declaringClass>
>     </entityType>
>
> without any of the selected value.

I think, a converter will help you there:

for reading:

BeanReader reader = ...

reader.getBindingConfiguration().setObjectStringConverter(new
EnumStringConverter());


for writing:

BeanWriter writer = ...

writer.getBindingConfiguration().setObjectStringConverter(new
EntityTypeStringConverter());


and the converter itself something like:

public class EntityTypeStringConverter extends 
ConvertUtilsObjectStringConverter
{
  public String objectToString(Object object, Class type, Context context)
  {
    return EntityType.class.equals(type) ?
               ((EntityType)object).name() :
               super.objectToString(object, type, context);
  }

  public Object stringToObject(String value, Class type, Context context) 
{
    return EntityType.class.equals(type) ?
               Enum.valueOf(EntityType.class, value) :
               super.stringToObject(value, type, context);
  }
}

To use this, I think you have to define a .betwixt file or a mapping
file and map the property returning the enum to an XML attribute.

Tom

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org



Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message