Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (686)
Games in Android Showcase (198)
games submitted by our members
Games in WIP (758)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Stupid enum tricks  (Read 17851 times)
0 Members and 1 Guest are viewing this topic.
Offline Wiki Duke

?





« Posted 2014-01-28 08:18:03 »

Note: you are watching revision 5 of this wiki entry. ( view plain diff )
Number of elements
1  
2  
3  
4  
5  
6  
public enum Foo
{
  A,B,C;

  public static final int length = Foo.values().length;
}


As a singleton
Probably the easiest way to safely create a singleton in java if no parent is needed, but an enum can implement any number of interfaces:
1  
2  
3  
4  
5  
public enum Foo
{
  INSTANCE;
  // instance methods here
}

Note that the enum instance is constructed on class load:
1  
2  
3  
4  
5  
6  
7  
8  
enum E {
    $;
    {
        System.out.println("Whoa! Hello World?!"); //this will in fact print when E.class is loaded (main() is run)
    }
   
    public static void main(String[] a) {}
}


As a static utility class
1  
2  
3  
4  
5  
public enum Foo
{
  ;
  // static methods here
}


Tree relations
All kinds of stupid tricks you can do with this.  Minimal example here just shows a boolean test of hierarchy membership.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public enum TreeEnum {
  THING,
    SUB_THING_1(THING),
    SUB_THING_2(THING),
      SUB_SUB_THING_2(SUB_THING_2),
  OTHER,
    SOME_OTHER(OTHER),
    AND_YET_ANOTHER(OTHER)
  ;
 
  private final TreeEnum parent;
  private final int      depth;
 
  private TreeEnum() { this(null);}
 
  private TreeEnum(TreeEnum parent)
  {
    this.parent = parent;
   
    if (parent != null) {
      this.depth  = parent.depth+1;
    } else {
      this.depth  = 0;
    }
  }
 
  public boolean isA(TreeEnum other)
  {
    TreeEnum t = this;
   
    do {
      if (t == other) return true;
      t = t.parent;
    } while(t != null);
   
    return false;
  }
}
This wiki entry has had 5 revisions with contributions from 3 members. (more info)
Offline DrakosKai

Senior Newbie


Medals: 3



« Reply #1 - Posted 2016-04-14 14:35:47 »

Inheritance and polymorphism also apply to Enums

However an Enum cannot extend a class but it can implement an interface.

1  
2  
3  
public interface AGLThing {
    public int id();
}


1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
public enum GLBuffers implements AGLThing {

    VERTEX(0),
    ELEMENT(2);
    private final int id;

    private GLBuffers(final int id) {
        this.id = id;
    }

    @Override
    public int id() {
        return id;
    }

    @Override
    public String toString() {
        return this.name() + ",id: " + this.id();
    }
}


1  
2  
3  
4  
5  
6  
 public static void main(String[] args) {
        AGLThing vertexBufferInfo = GLBuffers.VERTEX;
        int vertexBufferId = vertexBufferInfo.id();
        System.out.println("Vertex Buffer id=" + vertexBufferId);
        System.out.println(vertexBufferInfo.toString());
    }


Output:

Vertex Buffer id=0
VERTEX,id: 0



Pages: [1]
  ignore  |  Print  
 
 

 
roseslayer (478 views)
2016-08-06 11:43:29

roseslayer (434 views)
2016-08-06 09:43:11

xTheGamerCodes (511 views)
2016-08-04 15:40:59

xTheGamerCodes (504 views)
2016-08-04 15:40:24

orrenravid (852 views)
2016-07-16 03:57:23

theagentd (929 views)
2016-07-11 14:28:54

Hydroque (1025 views)
2016-07-06 05:56:57

Hydroque (1018 views)
2016-07-03 08:52:54

GrandCastle (833 views)
2016-07-01 09:13:47

GrandCastle (640 views)
2016-07-01 09:09:45
Rendering resources
by Roquen
2016-08-08 05:55:21

Rendering resources
by Roquen
2016-08-08 05:52:42

Rendering resources
by Roquen
2016-08-08 05:50:38

Rendering resources
by Roquen
2016-08-08 05:49:53

Rendering resources
by Roquen
2016-08-08 05:32:39

Making a Dynamic Plugin System
by Hydroque
2016-06-25 00:13:25

Java Data structures
by BinaryMonkL
2016-06-13 21:22:09

Java Data structures
by BinaryMonkL
2016-06-13 21:20:42
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!