users@jaxb.java.net

Re: typesafe enumeration

From: Aleksei Valikov <valikov_at_gmx.net>
Date: Thu, 17 Jul 2008 10:22:28 +0200

Hi folks,

I've tries the following fragment:

        <xs:element name="SexType">
                <xs:complexType>
                        <xs:sequence>
                                <xs:element name="sex">
                                        <xs:simpleType>
                                                 <xs:annotation>
                                                         <xs:appinfo>
                                                                 <jaxb:typesafeEnumClass name="sex">
                        
<jaxb:typesafeEnumMember name="MALE" value="MA LE" />
                        
<jaxb:typesafeEnumMember name="FEMALE" value="FEM ALE" />
                                                                 </jaxb:typesafeEnumClass>
                                                         </xs:appinfo>
                                                 </xs:annotation>
                                                 <xs:restriction base="xs:string">
                                                         <xs:enumeration value="MA LE" />
                                                         <xs:enumeration value="FEM ALE" />
                                                 </xs:restriction>
                                        </xs:simpleType>
                                </xs:element>
                        </xs:sequence>
                </xs:complexType>
        </xs:element>


And got the following code generated:

/**
 * <p>Java class for anonymous complex type.
 *
 * <p>The following schema fragment specifies the expected content
contained within this class.
 *
 * <pre>
 * &lt;complexType>
 * &lt;complexContent>
 * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 * &lt;sequence>
 * &lt;element name="sex">
 * &lt;simpleType>
 * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string">
 * &lt;enumeration value="MA LE"/>
 * &lt;enumeration value="FEM ALE"/>
 * &lt;/restriction>
 * &lt;/simpleType>
 * &lt;/element>
 * &lt;/sequence>
 * &lt;/restriction>
 * &lt;/complexContent>
 * &lt;/complexType>
 * </pre>
 *
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
    "sex"
})
@XmlRootElement(name = "SexType")
@Entity(name = "org.jvnet.hyperjaxb3.ejb.tests.cuone.SexType")
@Table(name = "SEXTYPE")
@Inheritance(strategy = InheritanceType.JOINED)
public class SexType
    implements Equals, HashCode
{

    @XmlElement(required = true)
    protected SexType.Sex sex;
    @XmlAttribute(name = "Myid")
    protected Long myid;

    /**
     * Gets the value of the sex property.
     *
     * @return
     * possible object is
     * {_at_link SexType.Sex }
     *
     */
    @Basic
    @Column(name = "SEX")
    @Enumerated(EnumType.STRING)
    public SexType.Sex getSex() {
        return sex;
    }

    /**
     * Sets the value of the sex property.
     *
     * @param value
     * allowed object is
     * {_at_link SexType.Sex }
     *
     */
    public void setSex(SexType.Sex value) {
        this.sex = value;
    }

    /**
     * Gets the value of the myid property.
     *
     * @return
     * possible object is
     * {_at_link Long }
     *
     */
    @Id
    @Column(name = "MY_ID")
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getMyid() {
        return myid;
    }

    /**
     * Sets the value of the myid property.
     *
     * @param value
     * allowed object is
     * {_at_link Long }
     *
     */
    public void setMyid(Long value) {
        this.myid = value;
    }

    public void equals(Object object, EqualsBuilder equalsBuilder) {
        if (!(object instanceof SexType)) {
            equalsBuilder.appendSuper(false);
            return ;
        }
        if (this == object) {
            return ;
        }
        final SexType that = ((SexType) object);
        equalsBuilder.append(this.getSex(), that.getSex());
    }

    public boolean equals(Object object) {
        if (!(object instanceof SexType)) {
            return false;
        }
        if (this == object) {
            return true;
        }
        final EqualsBuilder equalsBuilder = new JAXBEqualsBuilder();
        equals(object, equalsBuilder);
        return equalsBuilder.isEquals();
    }

    public void hashCode(HashCodeBuilder hashCodeBuilder) {
        hashCodeBuilder.append(this.getSex());
    }

    public int hashCode() {
        final HashCodeBuilder hashCodeBuilder = new JAXBHashCodeBuilder();
        hashCode(hashCodeBuilder);
        return hashCodeBuilder.toHashCode();
    }


    /**
     * <p>Java class for null.
     *
     * <p>The following schema fragment specifies the expected content
contained within this class.
     * <p>
     * <pre>
     * &lt;simpleType>
     * &lt;restriction base="{http://www.w3.org/2001/XMLSchema}string">
     * &lt;enumeration value="MA LE"/>
     * &lt;enumeration value="FEM ALE"/>
     * &lt;/restriction>
     * &lt;/simpleType>
     * </pre>
     *
     */
    @XmlType(name = "")
    @XmlEnum
    public enum Sex {

        @XmlEnumValue("MA LE")
        MALE("MA LE"),
        @XmlEnumValue("FEM ALE")
        FEMALE("FEM ALE");
        private final String value;

        Sex(String v) {
            value = v;
        }

        public String value() {
            return value;
        }

        public static SexType.Sex fromValue(String v) {
            for (SexType.Sex c: SexType.Sex.values()) {
                if (c.value.equals(v)) {
                    return c;
                }
            }
            throw new IllegalArgumentException(v);
        }

    }

}


So from my perspective everything works just fine.

Bye.
/lexi