#3 Parameterized Enum 2
Generation of an enum with a multiple boolean parameters, defined as an ASCII-art table.
- Download:
- source code Project Files in Zip (114 KB)
- mp4 Full Size H.264 Video (43 MB)
- m4v Smaller H.264 Video (24.8 MB)
- webm Full Size VP8 Video (25.4 MB)
- ogv Full Size Theora Video (47.6 MB)
Xtext grammer:
xtext
Model:
(
penums += PEnum
)*
;
PEnum:
'enum' name=ID
(
"|"
(
parameterNameList += ID
"|"
)+
)?
'{'
(
elements += PEnumElement
)*
'}'
;
PEnumElement:
name=ID
(
alternateText=STRING
)?
(
"|"
(
parameterValueList += PEnumElementParamaterValue
"|"
)+
)?
;
PEnumElementParamaterValue: {PEnumElementParamaterValue}
(
value ?= "+"
|
"-"
)
;
Model: ( penums += PEnum )* ; PEnum: 'enum' name=ID ( "|" ( parameterNameList += ID "|" )+ )? '{' ( elements += PEnumElement )* '}' ; PEnumElement: name=ID ( alternateText=STRING )? ( "|" ( parameterValueList += PEnumElementParamaterValue "|" )+ )? ; PEnumElementParamaterValue: {PEnumElementParamaterValue} ( value ?= "+" | "-" ) ;
Java validation method:
java
@Check
public void checkPEnumParametersNumber(PEnum penum) {
for (PEnumElement pEnumElement : penum.getElements()) {
if (pEnumElement.getParameterValueList().size() !=
penum.getParameterNameList().size()) {
error("Invalid number of parameters",
pEnumElement, PEnumPackage.Literals.PENUM_ELEMENT__NAME, -1);
}
}
}
@Check public void checkPEnumParametersNumber(PEnum penum) { for (PEnumElement pEnumElement : penum.getElements()) { if (pEnumElement.getParameterValueList().size() != penum.getParameterNameList().size()) { error("Invalid number of parameters", pEnumElement, PEnumPackage.Literals.PENUM_ELEMENT__NAME, -1); } } }
Xtend code generator:
xtend
class PEnumGenerator implements IGenerator {
override void doGenerate(Resource resource, IFileSystemAccess fsa) {
for (model : resource.contents.filter(typeof(Model))) {
for (penum : model.penums) {
fsa.generateFile(penum.name + ".java", penum.generatePEnum)
}
}
}
def generatePEnum(PEnum penum) {'''
public enum «penum.name» {
«FOR element : penum.elements»
«element.name»("«element.text»"«element.passParameters»),
«ENDFOR»
;
private String text;
«FOR name : penum.parameterNameList»
private boolean «name»;
«ENDFOR»
private «penum.name»(String text«penum.defParameters») {
this.text = text;
«FOR name : penum.parameterNameList»
this.«name» = «name»;
«ENDFOR»
}
public String getText() {
return text;
}
«FOR name : penum.parameterNameList»
public boolean is«name.toFirstUpper»() {
return «name»;
}
«ENDFOR»
}
'''}
def passParameters(PEnumElement element) {
// true, false, true, ...
// ", true, false, ...."
element.parameterValueList.map [ value |
", " + value.value
].join("")
}
def defParameters(PEnum penum) {
// p1, p2, p3
// ", boolean p1, boolean p2, ..."
penum.parameterNameList.map [ name |
", boolean " + name
].join("")
}
def text(PEnumElement element) {
if (element.alternateText != null) {
element.alternateText
} else {
element.name.toLowerCase.toFirstUpper.replace("_", " ")
}
}
}
class PEnumGenerator implements IGenerator { override void doGenerate(Resource resource, IFileSystemAccess fsa) { for (model : resource.contents.filter(typeof(Model))) { for (penum : model.penums) { fsa.generateFile(penum.name + ".java", penum.generatePEnum) } } } def generatePEnum(PEnum penum) {''' public enum «penum.name» { «FOR element : penum.elements» «element.name»("«element.text»"«element.passParameters»), «ENDFOR» ; private String text; «FOR name : penum.parameterNameList» private boolean «name»; «ENDFOR» private «penum.name»(String text«penum.defParameters») { this.text = text; «FOR name : penum.parameterNameList» this.«name» = «name»; «ENDFOR» } public String getText() { return text; } «FOR name : penum.parameterNameList» public boolean is«name.toFirstUpper»() { return «name»; } «ENDFOR» } '''} def passParameters(PEnumElement element) { // true, false, true, ... // ", true, false, ...." element.parameterValueList.map [ value | ", " + value.value ].join("") } def defParameters(PEnum penum) { // p1, p2, p3 // ", boolean p1, boolean p2, ..." penum.parameterNameList.map [ name | ", boolean " + name ].join("") } def text(PEnumElement element) { if (element.alternateText != null) { element.alternateText } else { element.name.toLowerCase.toFirstUpper.replace("_", " ") } } }
Example PEnum:
penum
enum Page | verifiable | skippable | {
INTRO "Introduction" | + | - |
NAME_AND_ADDRESS | + | + |
SUBMIT | - | - |
}
enum Page | verifiable | skippable | { INTRO "Introduction" | + | - | NAME_AND_ADDRESS | + | + | SUBMIT | - | - | }
Meta-data editor PEnum:
penum
enum FileType | size | bitrate | sterio | geotag | {
PLAIN "Unknown type" | - | - | - | - |
VIDEO | + | + | + | - |
AUDIO | - | + | + | - |
IMAGE | + | - | - | + |
}
enum FileType | size | bitrate | sterio | geotag | { PLAIN "Unknown type" | - | - | - | - | VIDEO | + | + | + | - | AUDIO | - | + | + | - | IMAGE | + | - | - | + | }