title: Coding tip - intersection types and java enums
body: <p>Intersection types allow you to (kinda sorta) do enums that have an inheritance hierarchy. You can't inherit implementation, but you can delegate it to a helper class.</p>
  
  <pre><code>enum Foo1 implements Bar {}
  enum Foo2 implements Bar {}
  
  class HelperClass {
     static &lt;T extends Enum&lt;T&gt; &amp; Bar&gt; void fooBar(T the enum) {}
  }
  </code></pre>
  
  <p>This is useful when you have a number of different enums that implement some sort of pattern. For instance, a number of pairs of enums that have a parent-child relationship.</p>
  
  <pre><code>enum PrimaryColor {Red, Green, Blue;}
  enum PastelColor {Pink, HotPink, Rockmelon, SkyBlue, BabyBlue;}
  
  enum TransportMedium {Land, Sea, Air;}
  enum Vehicle {Car, Truck, BigBoat, LittleBoat, JetFighter, HotAirBaloon;}
  </code></pre>
  
  <p>You can write generic methods that say "Ok, given an enum value thats a parent of some other enum values, what percentage of all the possible child enums of the child type have this particular parent value as their parent?", and have it all typesafe and done without casting. (eg: that "Sea" is 33% of all possible vehicles, and "Green" 20% of all possible Pastels).</p>
  
  <p>The code look like this. Note in particular that the "leaf" classes themselves are quite neat - but the generic classes have declarations that are horribly ugly. That's ok: you only write them once. Once the generic classes are there, then using them is easy.</p>
  
  <p>The helper class below just has some static methods. Other ways to go include </p>
  
  <ul>
  <li>providing an instance that returns a
  singleton, but typed according to the
  parent/child </li>
  <li>returning an instance
  for each paren/child, typed
  appropriately, and including one in
  each parent enum</li>
  </ul>
  
  <p>With this second option, the "children" object would actually be inside the helper, so reducing the amount of code needed in the enumerations. They'd all instantiate a helper, and delegate anything difficult. </p>
  
  <pre><code>import java.util.EnumSet;
  
  import javax.swing.JComponent;
  
  public class zz extends JComponent {
  
      public static void main(String[] args) {
          System.out.println(PrimaryColor.Green + " " + ParentUtil.pctOf(PrimaryColor.Green) + "%");
          System.out.println(TransportMedium.Air + " " + ParentUtil.pctOf(TransportMedium.Air) + "%");
      }
  
  
  }
  
  interface Parent&lt;P extends Enum&lt;P&gt; &amp; Parent&lt;P, C&gt;, C extends Enum&lt;C&gt; &amp; Child&lt;P, C&gt;&gt; {
      Class&lt;C&gt; getChildClass();
  
      EnumSet&lt;C&gt; getChildren();
  }
  
  interface Child&lt;P extends Enum&lt;P&gt; &amp; Parent&lt;P, C&gt;, C extends Enum&lt;C&gt; &amp; Child&lt;P, C&gt;&gt; {
      Class&lt;P&gt; getParentClass();
  
      P getParent();
  }
  
  enum PrimaryColor implements Parent&lt;PrimaryColor, PastelColor&gt; {
      Red, Green, Blue;
  
      private EnumSet&lt;PastelColor&gt;    children;
  
      public Class&lt;PastelColor&gt; getChildClass() {
          return PastelColor.class;
      }
  
      public EnumSet&lt;PastelColor&gt; getChildren() {
          if(children == null) children=ParentUtil.loadChildrenOf(this);
          return children;
      }
  }
  
  enum PastelColor implements Child&lt;PrimaryColor, PastelColor&gt; {
      Pink(PrimaryColor.Red), HotPink(PrimaryColor.Red), //
      Rockmelon(PrimaryColor.Green), //
      SkyBlue(PrimaryColor.Blue), BabyBlue(PrimaryColor.Blue);
  
      final PrimaryColor  parent;
  
      private PastelColor(PrimaryColor parent) {
          this.parent = parent;
      }
  
      public Class&lt;PrimaryColor&gt; getParentClass() {
          return PrimaryColor.class;
      }
  
      public PrimaryColor getParent() {
          return parent;
      }
  }
  
  enum TransportMedium implements Parent&lt;TransportMedium, Vehicle&gt; {
      Land, Sea, Air;
  
      private EnumSet&lt;Vehicle&gt;    children;
  
      public Class&lt;Vehicle&gt; getChildClass() {
          return Vehicle.class;
      }
  
      public EnumSet&lt;Vehicle&gt; getChildren() {
          if(children == null) children=ParentUtil.loadChildrenOf(this);
          return children;
      }
  }
  
  enum Vehicle implements Child&lt;TransportMedium, Vehicle&gt; {
      Car(TransportMedium.Land), Truck(TransportMedium.Land), //
      BigBoat(TransportMedium.Sea), LittleBoat(TransportMedium.Sea), //
      JetFighter(TransportMedium.Air), HotAirBaloon(TransportMedium.Air);
  
      private final TransportMedium   parent;
  
      private Vehicle(TransportMedium parent) {
          this.parent = parent;
      }
  
      public Class&lt;TransportMedium&gt; getParentClass() {
          return TransportMedium.class;
      }
  
      public TransportMedium getParent() {
          return parent;
      }
  }
  
  class ParentUtil {
      private ParentUtil(){}
      static &lt;P extends Enum&lt;P&gt; &amp; Parent&lt;P, C&gt;, C extends Enum&lt;C&gt; &amp; Child&lt;P, C&gt;&gt; //
      float pctOf(P parent) {
          return (float) parent.getChildren().size() / //
                  (float) EnumSet.allOf(parent.getChildClass()).size() //
                  * 100f;
      }
      public static &lt;P extends Enum&lt;P&gt; &amp; Parent&lt;P, C&gt;, C extends Enum&lt;C&gt; &amp; Child&lt;P, C&gt;&gt; //
      EnumSet&lt;C&gt; loadChildrenOf(P p) {
          EnumSet&lt;C&gt; cc = EnumSet.noneOf(p.getChildClass());
          for(C c: EnumSet.allOf(p.getChildClass())) {
              if(c.getParent() == p) {
                  cc.add(c);
              }
          }
          return cc;
      }
  }
  </code></pre>