tag:blogger.com,1999:blog-5638372.post2418519255449735616..comments2023-10-10T05:22:56.347-05:00Comments on binkley's BLOG: More with Java enums: simulating inheritanceBrian Oxleyhttp://www.blogger.com/profile/06617364377560752378noreply@blogger.comBlogger7125tag:blogger.com,1999:blog-5638372.post-84630539884906918772011-06-25T07:53:49.306-05:002011-06-25T07:53:49.306-05:00Hi all,
the missing inheritance of enumerations i...Hi all,<br /><br />the missing inheritance of enumerations is a recurring (and pretty much arbitrary) limitation of the Java language, and for many instances a royal pain in the neck (especially in combination with annotations). And the forums are full of complaints about this limitation...<br /><br />We are joining forces now to propose at least a basic support for enumeration extendability for the upcoming Java-8 edition.<br /><br />Please help us in moving the Extended-Enum petition forward, and vote for us at http://www.extended-enums.org - this would be a minimal change with a maximum of impact and transcendence!<br /><br />cheers and many thnx, <br /><br />ChrisChris Mayhttp://www.extended-enums.orgnoreply@blogger.comtag:blogger.com,1999:blog-5638372.post-65025511202000289862010-07-15T06:12:26.410-05:002010-07-15T06:12:26.410-05:00You can say that, yes. But it's not the typica...You can say that, yes. But it's not the typical visitor pattern where you have a Visitor interface with a few implementations that visit an object hierarchy that accepts the Visitor with:<br /><br />accept(Visitor someVisitor);Loshiahttps://www.blogger.com/profile/05086538727337824799noreply@blogger.comtag:blogger.com,1999:blog-5638372.post-72869235480651871952010-07-14T10:43:08.763-05:002010-07-14T10:43:08.763-05:00@Loshia questions about visitor pattern:
Visitor ...@Loshia questions about visitor pattern:<br /><br />Visitor pulls the data structure apart from the algorithm using it.<br /><br />In this case, Men.valueOf(character):Men lets Convert.from(E[], character):E do the work, providing the E structure to operate over.<br /><br />Or so it looks to me.Brian Oxleyhttps://www.blogger.com/profile/06617364377560752378noreply@blogger.comtag:blogger.com,1999:blog-5638372.post-86604603617687297972010-07-14T09:21:49.793-05:002010-07-14T09:21:49.793-05:00Hi, I wanted to ask where exactly did you implemen...Hi, I wanted to ask where exactly did you implement the VIsitor pattern? All I see is using interfaces to allow enums inheritance.Loshiahttps://www.blogger.com/profile/05086538727337824799noreply@blogger.comtag:blogger.com,1999:blog-5638372.post-8761496678081194832009-10-20T05:41:54.947-05:002009-10-20T05:41:54.947-05:00Yep, String is an issue just as you point out. Pi...Yep, String is an issue just as you point out. Pick a better method name than "valueOf" which does not overload (I should have done this in the first place) and you will be good.Brian Oxleyhttps://www.blogger.com/profile/06617364377560752378noreply@blogger.comtag:blogger.com,1999:blog-5638372.post-39913948295540550532009-10-20T01:31:44.990-05:002009-10-20T01:31:44.990-05:00Great! still very useful after 2 years.
It's ...Great! still very useful after 2 years.<br /><br />It's actually too bad it doesn't work with a String type instead of character.<br /><br />I get the compiler error: valueOf(java.lang.String) is already defined. And I don't understand why it can't overload the original Enum.<br />Isn't it possible to overload static method?<br /><br />Anyway, no big deal, just have to name the 'valueOf' method something like 'value'.<br /><br />And it gives me a solution to an old unsolved problem. I'm gonna expose it here, if you don't mind, I think it should be useful to others.<br /><br />It solves a problem when working with legacy application/databases where you have a lot of String constants and you have to make test on these with values coming from the db.<br /><br />Here's an exemple for those interested:<br /><br />enum Model implements StringEnum.StringContent {<br /> MODEL1("long boring name1"), MODEL2("..."), MOdEL3("..."), ...;<br /><br /> Model(String value) {<br /> this.value = value;<br /> }<br /><br /> private final String value;<br /><br /> public String getValue() {<br /> return this.value;<br /> }<br /><br /> //can't be named valueOf, compiler error<br /> public static Model value(String value) {<br /> return StringEnum.from(values(), value);<br /> }<br /> }<br /><br />This allow, finally, to replace long if conditions that clutter legacy code:<br /><br />if (MODEL1.equals(modelName) || MODEL2.equals(modelName) ||<br />MODEL3.equals(modelName) ||<br />MODEL4.equals(modelName) ||<br />...<br />) performSomeStuff();<br /><br />with <br /><br />EnumSet<Mode>> subset = EnumSet.of(Model.MODEL1, Model.MODEL2);<br />if(subset.contains(Model.value("my model name coming from the database")))<br /> performSomeStuff();<br /> <br /><br />You can work with EnumSet methods with values of the Enum and not only with names.<br /><br />With static import you can reduce the code even more. You can reuse your subsets by putting them outside of the method, give them meaningful names, etc..<br /><br />Hope it makes sense. <br /><br />StringEnum.StringContent are the original Convert.Vivid changed to work with Strings. Just replace char with String<br /><br />Agreeably they could be better named ;)Unknownhttps://www.blogger.com/profile/08507177677519842022noreply@blogger.comtag:blogger.com,1999:blog-5638372.post-81098121717001161612008-08-04T01:51:00.000-05:002008-08-04T01:51:00.000-05:00Nice! Thanks a lot!Nice! Thanks a lot!Anonymousnoreply@blogger.com