icfp-model/src/main/java/ru/bosony/model/cellscontents/CellContent.java
author indvdum (gotoindvdum[at]gmail[dot]com)
Sun, 15 Jul 2012 21:20:04 +0400
changeset 27 505c25412b16
parent 6 0dbbc8983926
child 29 66aa96e8fd92
permissions -rw-r--r--
Add trampolines and targets
     1 package ru.bosony.model.cellscontents;
     2 
     3 import java.util.HashSet;
     4 import java.util.Set;
     5 
     6 import ru.bosony.model.io.TextRepresentable;
     7 
     8 /**
     9  * @author indvdum (gotoindvdum[at]gmail[dot]com) <br>
    10  *         13.07.2012 21:25:49
    11  * 
    12  */
    13 public enum CellContent implements TextRepresentable {
    14 
    15 	ClosedLambdaLift("L"),
    16 	Earth("."),
    17 	Empty(" "),
    18 	Lambda("\\"),
    19 	MiningRobot("R"),
    20 	OpenLambdaLift("O"),
    21 	Rock("*"),
    22 	Wall("#"),
    23 	TrampolineA("A"),
    24 	TrampolineB("B"),
    25 	TrampolineC("C"),
    26 	TrampolineD("D"),
    27 	TrampolineE("E"),
    28 	TrampolineF("F"),
    29 	TrampolineG("G"),
    30 	TrampolineH("H"),
    31 	TrampolineI("I"),
    32 	Target1("1"),
    33 	Target2("2"),
    34 	Target3("3"),
    35 	Target4("4"),
    36 	Target5("5"),
    37 	Target6("6"),
    38 	Target7("7"),
    39 	Target8("8"),
    40 	Target9("9");
    41 
    42 	private String					text;
    43 	private CellContent				trampolineTarget	= null;
    44 	private static Set<CellContent>	trampolines			= new HashSet<CellContent>();
    45 	private static Set<CellContent>	targets				= new HashSet<CellContent>();
    46 	static {
    47 		trampolines.add(TrampolineA);
    48 		trampolines.add(TrampolineB);
    49 		trampolines.add(TrampolineC);
    50 		trampolines.add(TrampolineD);
    51 		trampolines.add(TrampolineE);
    52 		trampolines.add(TrampolineF);
    53 		trampolines.add(TrampolineG);
    54 		trampolines.add(TrampolineH);
    55 		trampolines.add(TrampolineI);
    56 		targets.add(Target1);
    57 		targets.add(Target2);
    58 		targets.add(Target3);
    59 		targets.add(Target4);
    60 		targets.add(Target5);
    61 		targets.add(Target6);
    62 		targets.add(Target7);
    63 		targets.add(Target8);
    64 		targets.add(Target9);
    65 	}
    66 
    67 	private CellContent(String text) {
    68 		this.text = text;
    69 	}
    70 
    71 	@Override
    72 	public String toText() {
    73 		return text;
    74 	}
    75 
    76 	public static CellContent fromText(String text) {
    77 		for (CellContent content : values()) {
    78 			if (text != null && text.equals(content.toText()))
    79 				return content;
    80 		}
    81 		return null;
    82 	}
    83 
    84 	public CellContent getTrampolineTarget() {
    85 		return trampolineTarget;
    86 	}
    87 
    88 	public void setTrampolineTarget(CellContent trampolineTarget) {
    89 		if (!trampolines.contains(this) || trampolineTarget != null && !targets.contains(trampolineTarget))
    90 			throw new RuntimeException("Only references from Trampolines to Targets allowed");
    91 		this.trampolineTarget = trampolineTarget;
    92 	}
    93 
    94 	public static Set<CellContent> getTrampolines() {
    95 		return trampolines;
    96 	}
    97 
    98 	public static Set<CellContent> getTargets() {
    99 		return targets;
   100 	}
   101 
   102 }