Skip to content

Instantly share code, notes, and snippets.

@honjo2
Created October 20, 2010 02:28
Show Gist options
  • Save honjo2/635653 to your computer and use it in GitHub Desktop.
Save honjo2/635653 to your computer and use it in GitHub Desktop.
[java] 階層型enum
package honjo;
import java.util.List;
import static honjo.OsType.*;
public class Client {
public static void main(String[] args) {
OsType parent = WindowsNT.parent();
System.out.println(parent); // Windows
System.out.println("----------");
boolean is = Windows7.is(Unix);
System.out.println(is); // false
System.out.println("----------");
List<? extends OsType> children = WindowsNT.children();
for (OsType child : children) {
System.out.println(child);
}
// WindowsNTWorkstation
// WindowsNTServer
System.out.println("----------");
List<? extends OsType> allChildren = Windows.allChildren();
for (OsType osType : allChildren) {
System.out.println(osType);
}
// WindowsNT
// WindowsNTWorkstation
// WindowsNTServer
// Windows2000
// Windows2000Server
// Windows2000Workstation
// WindowsXp
// WindowsVista
// Windows7
// Windows95
// Windows98
System.out.println("----------");
boolean support = Windows7.supportsXWindowSystem();
System.out.println(support); // false
boolean support2 = SunOs.supportsXWindowSystem();
System.out.println(support2); // true
System.out.println("----------");
boolean major = Windows2000.major();
System.out.println(major); // false
boolean major2 = WindowsXp.major();
System.out.println(major2); // true
boolean major3 = Linux.major();
System.out.println(major3); // true
}
}
package honjo;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public enum OsType {
OS(null),
Windows(OS),
WindowsNT(Windows),
WindowsNTWorkstation(WindowsNT),
WindowsNTServer(WindowsNT),
Windows2000(Windows),
Windows2000Server(Windows2000),
Windows2000Workstation(Windows2000),
WindowsXp(Windows) {
@Override
public boolean major() {
return true;
}
},
WindowsVista(Windows),
Windows7(Windows),
Windows95(Windows),
Windows98(Windows),
Unix(OS) {
@Override
public boolean supportsXWindowSystem() {
return true;
}
},
Linux(Unix) {
@Override
public boolean major() {
return true;
}
},
AIX(Unix),
HpUx(Unix),
SunOs(Unix),
;
private final OsType parent;
private final List<OsType> children = new ArrayList<OsType>();
private final List<OsType> allChildren = new ArrayList<OsType>();
OsType (OsType parent) {
this.parent = parent;
if (parent != null) {
parent.addChild(this);
}
}
public OsType parent() {
return parent;
}
public boolean is(OsType other) {
if (other == null) {
return false;
}
for (OsType osType = this; osType != null; osType = osType.parent()) {
if (other == osType) {
return true;
}
}
return false;
}
public List<? extends OsType> children() {
return Collections.unmodifiableList(children);
}
public List<? extends OsType> allChildren() {
return Collections.unmodifiableList(allChildren);
}
private void addChild(OsType child) {
this.children.add(child);
List<OsType> greatChildren = new ArrayList<OsType>();
greatChildren.add(child);
greatChildren.addAll(child.allChildren());
OsType currentAncestor = this;
while (currentAncestor != null) {
currentAncestor.allChildren.addAll(greatChildren);
currentAncestor = currentAncestor.parent;
}
}
public boolean supportsXWindowSystem() {
if (parent == null) {
return false;
}
return parent.supportsXWindowSystem();
}
public boolean major() {
if (parent == null) {
return false;
}
return parent.major();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment