Giter Site home page Giter Site logo

Comments (1)

highsource avatar highsource commented on August 12, 2024

Hi @kevinsdooapp,

sorry for the late reply.

I've reviewed your suggestion, thought it over and came to the conclusion that this can be already covered by the existing infrastructure. I don't think there is a need for another plugin.

The goal is to be able to customize, how enum is turned to string, this can already be done in the strategy. For instance, consider we want to print out value of the enum instead of its name. Here's our enum:

@XmlType(name = "issueGH31Type")
@XmlEnum
public enum IssueGH31Type
    implements EnumValue<String>
{

    @XmlEnumValue("Male")
    MALE("Male"),
    @XmlEnumValue("Female")
    FEMALE("Female");
    private final String value;

    IssueGH31Type(String v) {
        value = v;
    }

    public String value() {
        return value;
    }

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

    public String enumValue() {
        return this.value();
    }

}

In the strategy we can just implement the append method to check if the value is instance of EnumValue and use the value of the enum to convert it to string:

final ToStringStrategy2 s = new JAXBToStringStrategy() {
            // ...

            public StringBuilder append(ObjectLocator locator,
                    StringBuilder buffer, Object value) {

                if (value instanceof EnumValue<?>) {
                    return super.append(locator, buffer,
                            ((EnumValue<?>) value).enumValue());
                } else {
                    return super.append(locator, buffer, value);
                }
            };
        };

This produces Male/Female instead of MALE/FEMALE for IssueGH31Type.
You can argue whether instanceof is good or not, but that'd be better solved with the visitor pattern, making enums implement ToString won't help much with that.

Enums don't need to implement the ToString interface because they are atomic values. And it's the responsibility of the strategy to decide how individual values should be converted to strings.

So I don't see any necessity for enums to implement ToString.

Accordingly I'm intending to close this issue and the pull request #32 as wontfix. I'd be grateful to know what you think, if maybe I'm missing something.

And thank you for your effort. I am sorry it took me so long to et to this issue.

Best wishes,
Alexey

from jaxb2-basics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.