<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: Groking Enum (aka Enum&#60;E extends Enum&#60;E&#62;&#62;)</title>
	<atom:link href="http://madbean.com/2004/mb2004-3/feed/" rel="self" type="application/rss+xml" />
	<link>http://madbean.com/2004/mb2004-3/</link>
	<description>Your zero step program</description>
	<pubDate>Sat, 22 Nov 2008 06:59:48 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.3</generator>
		<item>
		<title>By: Maik</title>
		<link>http://madbean.com/2004/mb2004-3/#comment-406</link>
		<dc:creator>Maik</dc:creator>
		<pubDate>Wed, 28 Sep 2005 14:17:45 +0000</pubDate>
		<guid isPermaLink="false">http://madbean.com/blog/2004-3#comment-406</guid>
		<description>&lt;p&gt;(more a reminder to self...)&lt;/p&gt;

&lt;p&gt;&#62;To say that another way: this idiom allows a superclass
&#62;(such as an Abstract Factory) to define methods whose
&#62;argument types and return types are in terms of the
&#62;subclass type, not the superclass type.&lt;/p&gt;

&lt;p&gt;More generically speaking: The idiom makes a available a type parameter to the superclass "containing" the type of the subclass.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>(more a reminder to self&#8230;)</p>
<p>&gt;To say that another way: this idiom allows a superclass<br />
&gt;(such as an Abstract Factory) to define methods whose<br />
&gt;argument types and return types are in terms of the<br />
&gt;subclass type, not the superclass type.</p>
<p>More generically speaking: The idiom makes a available a type parameter to the superclass &#8220;containing&#8221; the type of the subclass.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Garulfo</title>
		<link>http://madbean.com/2004/mb2004-3/#comment-377</link>
		<dc:creator>Garulfo</dc:creator>
		<pubDate>Fri, 12 Aug 2005 23:08:17 +0000</pubDate>
		<guid isPermaLink="false">http://madbean.com/blog/2004-3#comment-377</guid>
		<description>&lt;p&gt;I find something strange that i cannot explain :&lt;/p&gt;

&lt;p&gt;this constructor &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public DFA (Map&#38;gt; delta) {
    transitionFunction = new EnumMap&#38;gt;(delta);
    for (S state : delta.keySet()) {
        transitionFunction.put(state, new EnumMap(delta.get(state)));
    } 
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;cannot be used like this&lt;/p&gt;

&lt;p&gt;EnumMap&#62; delta;
   DFA automata;
   [...]
   automata = new DFA(delta);&lt;/p&gt;

&lt;p&gt;I need to create this constructor :&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public DFA (EnumMap&#38;gt; delta) {
    transitionFunction = new EnumMap&#38;gt;(delta);
    for (S state : delta.keySet()) {
        transitionFunction.put(state, new EnumMap(delta.get(state)));
    } 
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So I conclude that EnumMap&#62; do not inherit from Map&#62;.&lt;/p&gt;

&lt;p&gt;I've tried 
    public DFA (EnumMap&#62;&#62; delta) 
but it didn't work either, because ? is not allow in constructor. &lt;/p&gt;

&lt;p&gt;This is not more than annoying... but now I'd like to write something like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public NFA (EnumMap&#38;gt;&#38;gt; delta) {/* etc */ }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;and nor &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    transitionFunction = new EnumMap&#38;gt;&#38;gt;(delta);
    for (S state : delta.keySet()) {
        transitionFunction.put(state, new EnumMap&#38;gt;(delta.get(state)));
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;neither &lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    transitionFunction = new EnumMap&#38;gt;&#38;gt;(delta);
    for (S state : delta.keySet()) {
        transitionFunction.put(state, new EnumMap&#38;gt;(delta.get(state)));
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;works as body -_- &lt;/p&gt;

&lt;p&gt;The first tells me that there is not constructor in EnumMap that matchs.
The second tells me that there is incompatible types.&lt;/p&gt;

&lt;p&gt;Do you have an idea ?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>I find something strange that i cannot explain :</p>
<p>this constructor </p>
<pre><code>public DFA (Map&amp;gt; delta) {
    transitionFunction = new EnumMap&amp;gt;(delta);
    for (S state : delta.keySet()) {
        transitionFunction.put(state, new EnumMap(delta.get(state)));
    }
}
</code></pre>
<p>cannot be used like this</p>
<p>EnumMap&gt; delta;<br />
   DFA automata;<br />
   [...]<br />
   automata = new DFA(delta);</p>
<p>I need to create this constructor :</p>
<pre><code>public DFA (EnumMap&amp;gt; delta) {
    transitionFunction = new EnumMap&amp;gt;(delta);
    for (S state : delta.keySet()) {
        transitionFunction.put(state, new EnumMap(delta.get(state)));
    }
 }
</code></pre>
<p>So I conclude that EnumMap&gt; do not inherit from Map&gt;.</p>
<p>I&#8217;ve tried<br />
    public DFA (EnumMap&gt;&gt; delta)<br />
but it didn&#8217;t work either, because ? is not allow in constructor. </p>
<p>This is not more than annoying&#8230; but now I&#8217;d like to write something like</p>
<pre><code>public NFA (EnumMap&amp;gt;&amp;gt; delta) {/* etc */ }
</code></pre>
<p>and nor </p>
<pre><code>    transitionFunction = new EnumMap&amp;gt;&amp;gt;(delta);
    for (S state : delta.keySet()) {
        transitionFunction.put(state, new EnumMap&amp;gt;(delta.get(state)));
    }
</code></pre>
<p>neither </p>
<pre><code>    transitionFunction = new EnumMap&amp;gt;&amp;gt;(delta);
    for (S state : delta.keySet()) {
        transitionFunction.put(state, new EnumMap&amp;gt;(delta.get(state)));
    }
</code></pre>
<p>works as body -_- </p>
<p>The first tells me that there is not constructor in EnumMap that matchs.<br />
The second tells me that there is incompatible types.</p>
<p>Do you have an idea ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mattias Jiderhamn</title>
		<link>http://madbean.com/2004/mb2004-3/#comment-285</link>
		<dc:creator>Mattias Jiderhamn</dc:creator>
		<pubDate>Wed, 12 Jan 2005 09:39:12 +0000</pubDate>
		<guid isPermaLink="false">http://madbean.com/blog/2004-3#comment-285</guid>
		<description>&lt;p&gt;This doesn't work either (&#34;call to super not allowed in enum constructor&#34;).&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
enum SpecialService {
  CUSTOMS(&#34;ABW&#34;,1),
  ALLOWANCE(&#34;ACA&#34;,2),
  FEE(&#34;ACN&#34;,3),
  FREIGHT(&#34;FC&#34;,4),
  QUANTITY&lt;em&gt;DISCOUNT(&#34;QD&#34;,5),
  PENALTY(&#34;SC&#34;,6),
  SPECIAL&lt;/em&gt;HANDLING(&#34;SH&#34;,7);&lt;/p&gt;

&lt;p&gt;private SpecialService(String name, int ordinal) {
    super(name, ordinal);
  }
}
&lt;/code&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>This doesn&#8217;t work either (&quot;call to super not allowed in enum constructor&quot;).</p>
<p><code><br />
enum SpecialService {<br />
  CUSTOMS(&quot;ABW&quot;,1),<br />
  ALLOWANCE(&quot;ACA&quot;,2),<br />
  FEE(&quot;ACN&quot;,3),<br />
  FREIGHT(&quot;FC&quot;,4),<br />
  QUANTITY<em>DISCOUNT(&quot;QD&quot;,5),<br />
  PENALTY(&quot;SC&quot;,6),<br />
  SPECIAL</em>HANDLING(&quot;SH&quot;,7);</code></p>
<p>private SpecialService(String name, int ordinal) {<br />
    super(name, ordinal);<br />
  }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mattias Jiderhamn</title>
		<link>http://madbean.com/2004/mb2004-3/#comment-284</link>
		<dc:creator>Mattias Jiderhamn</dc:creator>
		<pubDate>Wed, 12 Jan 2005 09:34:12 +0000</pubDate>
		<guid isPermaLink="false">http://madbean.com/blog/2004-3#comment-284</guid>
		<description>&lt;p&gt;How would I go about if I want to map each enum to a &lt;em&gt;String&lt;/em&gt; value differing from it's name?&lt;/p&gt;

&lt;p&gt;I tried the following, but the compiler says &#34;valueOf(java.lang.String) is already defined in se.exder.logic.types.SpecialService&#34;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;
enum SpecialService {
  CUSTOMS(&#34;ABW&#34;),
  ALLOWANCE(&#34;ACA&#34;),
  FEE(&#34;ACN&#34;),
  FREIGHT(&#34;FC&#34;),
  QUANTITY&lt;em&gt;DISCOUNT(&#34;QD&#34;),
  PENALTY(&#34;SC&#34;),
  SPECIAL&lt;/em&gt;HANDLING(&#34;SH&#34;);&lt;/p&gt;

&lt;p&gt;private String code;&lt;/p&gt;

&lt;p&gt;private SpecialService(String c) {
    this.code = c;
  }&lt;/p&gt;

&lt;p&gt;public String toString() {
    return code;
  }&lt;/p&gt;

&lt;p&gt;public static SpecialService valueOf(String name) {
    for(SpecialService ss : values()) {
      if(ss.code.equals(name))
        return ss;
    }&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;throw new IllegalArgumentException(name);
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}
}
&lt;/code&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>How would I go about if I want to map each enum to a <em>String</em> value differing from it&#8217;s name?</p>
<p>I tried the following, but the compiler says &quot;valueOf(java.lang.String) is already defined in se.exder.logic.types.SpecialService&quot;</p>
<p><code><br />
enum SpecialService {<br />
  CUSTOMS(&quot;ABW&quot;),<br />
  ALLOWANCE(&quot;ACA&quot;),<br />
  FEE(&quot;ACN&quot;),<br />
  FREIGHT(&quot;FC&quot;),<br />
  QUANTITY<em>DISCOUNT(&quot;QD&quot;),<br />
  PENALTY(&quot;SC&quot;),<br />
  SPECIAL</em>HANDLING(&quot;SH&quot;);</code></p>
<p>private String code;</p>
<p>private SpecialService(String c) {<br />
    this.code = c;<br />
  }</p>
<p>public String toString() {<br />
    return code;<br />
  }</p>
<p>public static SpecialService valueOf(String name) {<br />
    for(SpecialService ss : values()) {<br />
      if(ss.code.equals(name))<br />
        return ss;<br />
    }</p>
<pre><code>throw new IllegalArgumentException(name);
</code></pre>
<p>}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Quail</title>
		<link>http://madbean.com/2004/mb2004-3/#comment-283</link>
		<dc:creator>Matt Quail</dc:creator>
		<pubDate>Mon, 11 Oct 2004 01:03:41 +0000</pubDate>
		<guid isPermaLink="false">http://madbean.com/blog/2004-3#comment-283</guid>
		<description>&lt;p&gt;Joe, oops... Enum already contains an int &#34;ordinal&#34;, so you can just do this (and you could build a static hashmap to make the valueOf() faster):&lt;/p&gt;

&lt;p&gt;&lt;code&gt;public class EnumTest2 {
    enum E {
        red, green, blue;
        public static E valueOf(int i) {
            for(E e : values()) {
                if (e.ordinal() == i) {
                    return e;
                }
            }
            throw new IllegalArgumentException(&#34;wtf is &#34; + i);
        }&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;}

public static void main(String[] args) {
    E e = E.red;
    System.out.println(e.name() + &#38;quot; &#38;quot; + e.ordinal());
    System.out.println(E.valueOf(1));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}&lt;/code&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Joe, oops&#8230; Enum already contains an int &quot;ordinal&quot;, so you can just do this (and you could build a static hashmap to make the valueOf() faster):</p>
<p><code>public class EnumTest2 {<br />
    enum E {<br />
        red, green, blue;<br />
        public static E valueOf(int i) {<br />
            for(E e : values()) {<br />
                if (e.ordinal() == i) {<br />
                    return e;<br />
                }<br />
            }<br />
            throw new IllegalArgumentException(&quot;wtf is &quot; + i);<br />
        }</code></p>
<pre><code>}

public static void main(String[] args) {
    E e = E.red;
    System.out.println(e.name() + &amp;quot; &amp;quot; + e.ordinal());
    System.out.println(E.valueOf(1));
}
</code></pre>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Quail</title>
		<link>http://madbean.com/2004/mb2004-3/#comment-282</link>
		<dc:creator>Matt Quail</dc:creator>
		<pubDate>Mon, 11 Oct 2004 00:52:01 +0000</pubDate>
		<guid isPermaLink="false">http://madbean.com/blog/2004-3#comment-282</guid>
		<description>&lt;p&gt;Joe, In java, enums can be &#34;externalized&#34; easily as strings as opposed to ints. Consider this example:
&lt;code&gt;public class EnumTest {
    enum E {
        red, green, blue;
    }&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;public static void main(String[] args) {
    E e = E.red;
    System.out.println(e.name());
    System.out.println(E.valueOf(&#38;quot;green&#38;quot;));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you really want to treat externalize as ints, maybe do something like this:
&lt;code&gt;public class EnumTest2 {
    enum E {
        red(1), green(2), blue(3);
        public final int i;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;    E(int i) {
        this.i = i;
    }

    public static E valueOf(int i) {
        for(E e : values()) {
            if (e.i == i) {
                return e;
            }
        }
        throw new IllegalArgumentException(&#38;quot;wtf is &#38;quot; + i);
    }
}

public static void main(String[] args) {
    E e = E.red;
    System.out.println(e.name() + &#38;quot; &#38;quot; + e.i);
    System.out.println(E.valueOf(2));
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;}&lt;/code&gt;&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Joe, In java, enums can be &quot;externalized&quot; easily as strings as opposed to ints. Consider this example:<br />
<code>public class EnumTest {<br />
    enum E {<br />
        red, green, blue;<br />
    }</code></p>
<pre><code>public static void main(String[] args) {
    E e = E.red;
    System.out.println(e.name());
    System.out.println(E.valueOf(&amp;quot;green&amp;quot;));
}
</code></pre>
<p>}</p>
<p>If you really want to treat externalize as ints, maybe do something like this:<br />
<code>public class EnumTest2 {<br />
    enum E {<br />
        red(1), green(2), blue(3);<br />
        public final int i;</code></p>
<pre><code>    E(int i) {
        this.i = i;
    }

    public static E valueOf(int i) {
        for(E e : values()) {
            if (e.i == i) {
                return e;
            }
        }
        throw new IllegalArgumentException(&amp;quot;wtf is &amp;quot; + i);
    }
}

public static void main(String[] args) {
    E e = E.red;
    System.out.println(e.name() + &amp;quot; &amp;quot; + e.i);
    System.out.println(E.valueOf(2));
}
</code></pre>
<p>}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Joe Clarke</title>
		<link>http://madbean.com/2004/mb2004-3/#comment-281</link>
		<dc:creator>Joe Clarke</dc:creator>
		<pubDate>Fri, 08 Oct 2004 19:53:08 +0000</pubDate>
		<guid isPermaLink="false">http://madbean.com/blog/2004-3#comment-281</guid>
		<description>&lt;p&gt;Matt, I enjoyed your article.  I read it in search of an answer to this question: how to I get a java enum into and out of a value that I can easily save to a database?  In C/C++ or the old handrolled java way, saving to and restoring from int was easy.  What have I missed?&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Matt, I enjoyed your article.  I read it in search of an answer to this question: how to I get a java enum into and out of a value that I can easily save to a database?  In C/C++ or the old handrolled java way, saving to and restoring from int was easy.  What have I missed?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Matt Quail</title>
		<link>http://madbean.com/2004/mb2004-3/#comment-280</link>
		<dc:creator>Matt Quail</dc:creator>
		<pubDate>Thu, 08 Apr 2004 12:57:48 +0000</pubDate>
		<guid isPermaLink="false">http://madbean.com/blog/2004-3#comment-280</guid>
		<description>&lt;p&gt;Daniel, I can't come up with an answer to your question... my brain just doesn't want to stretch that far at the moment :S&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>Daniel, I can&#8217;t come up with an answer to your question&#8230; my brain just doesn&#8217;t want to stretch that far at the moment :S</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Daniel Gronau</title>
		<link>http://madbean.com/2004/mb2004-3/#comment-279</link>
		<dc:creator>Daniel Gronau</dc:creator>
		<pubDate>Thu, 08 Apr 2004 12:46:07 +0000</pubDate>
		<guid isPermaLink="false">http://madbean.com/blog/2004-3#comment-279</guid>
		<description>&lt;p&gt;The trick with Foo&#60;SubClassOfFoo extends Foo&#60;SubClassOfFoo&#62;&#62; is really nice, but one thing is still unclear to me: How can you ensure that the trick works not only for direct subclasses of Foo (like Bar) but also for indirect subclasses. I tried to manage it, but I found no solution. Implementing that feature for a whole class tree would be a powerful tool.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>The trick with Foo&lt;SubClassOfFoo extends Foo&lt;SubClassOfFoo&gt;&gt; is really nice, but one thing is still unclear to me: How can you ensure that the trick works not only for direct subclasses of Foo (like Bar) but also for indirect subclasses. I tried to manage it, but I found no solution. Implementing that feature for a whole class tree would be a powerful tool.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zohar Melamed</title>
		<link>http://madbean.com/2004/mb2004-3/#comment-278</link>
		<dc:creator>Zohar Melamed</dc:creator>
		<pubDate>Fri, 13 Feb 2004 19:14:27 +0000</pubDate>
		<guid isPermaLink="false">http://madbean.com/blog/2004-3#comment-278</guid>
		<description>&lt;p&gt;All your enums are belong to us.&lt;/p&gt;
</description>
		<content:encoded><![CDATA[<p>All your enums are belong to us.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
