io.joynr.generator.communicationmodel.EnumTemplate Maven / Gradle / Ivy
package io.joynr.generator.communicationmodel;
import com.google.common.base.Objects;
import com.google.inject.Inject;
import io.joynr.generator.util.JoynrJavaGeneratorExtensions;
import io.joynr.generator.util.TemplateBase;
import org.eclipse.emf.common.util.EList;
import org.eclipse.xtend2.lib.StringConcatenation;
import org.eclipse.xtext.xbase.lib.Extension;
import org.franca.core.franca.FEnumerationType;
import org.franca.core.franca.FEnumerator;
@SuppressWarnings("all")
public class EnumTemplate {
@Inject
@Extension
private JoynrJavaGeneratorExtensions _joynrJavaGeneratorExtensions;
@Inject
@Extension
private TemplateBase _templateBase;
public CharSequence generate(final FEnumerationType enumType) {
CharSequence _xblockexpression = null;
{
final String typeName = this._joynrJavaGeneratorExtensions.joynrName(enumType);
final String packagePath = this._joynrJavaGeneratorExtensions.getPackagePathWithJoynrPrefix(enumType, ".");
StringConcatenation _builder = new StringConcatenation();
_builder.append("\t\t");
CharSequence _warning = this._templateBase.warning();
_builder.append(_warning, " ");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.newLine();
_builder.append("package ");
_builder.append(packagePath, "");
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.newLine();
_builder.append("import java.util.HashMap;");
_builder.newLine();
_builder.append("import java.util.Map;");
_builder.newLine();
_builder.append("import java.util.Map.Entry;");
_builder.newLine();
_builder.newLine();
_builder.append("public enum ");
_builder.append(typeName, "");
_builder.append(" {");
_builder.newLineIfNotEmpty();
{
EList _enumElements = this._joynrJavaGeneratorExtensions.getEnumElements(enumType);
boolean _hasElements = false;
for(final FEnumerator enumValue : _enumElements) {
if (!_hasElements) {
_hasElements = true;
} else {
_builder.appendImmediate(",", " ");
}
_builder.append("\t");
String _joynrName = this._joynrJavaGeneratorExtensions.joynrName(enumValue);
_builder.append(_joynrName, " ");
_builder.newLineIfNotEmpty();
_builder.append("\t");
}
}
_builder.append(";");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("public static final Map ordinalToEnumValues = new HashMap();");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("static{");
_builder.newLine();
_builder.append("\t\t");
int i = (-1);
_builder.newLineIfNotEmpty();
{
EList _enumElements_1 = this._joynrJavaGeneratorExtensions.getEnumElements(enumType);
for(final FEnumerator enumValue_1 : _enumElements_1) {
_builder.append("\t\t");
_builder.append("ordinalToEnumValues.put(Integer.valueOf(");
{
boolean _or = false;
String _value = enumValue_1.getValue();
boolean _equals = Objects.equal(_value, null);
if (_equals) {
_or = true;
} else {
String _value_1 = enumValue_1.getValue();
boolean _equals_1 = _value_1.equals("");
_or = (_equals || _equals_1);
}
if (_or) {
int _plus = (i + 1);
int _i = i = _plus;
_builder.append(_i, " ");
} else {
String _value_2 = enumValue_1.getValue();
_builder.append(_value_2, " ");
}
}
_builder.append("), ");
String _joynrName_1 = this._joynrJavaGeneratorExtensions.joynrName(enumValue_1);
_builder.append(_joynrName_1, " ");
_builder.append(");");
_builder.newLineIfNotEmpty();
}
}
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("public static ");
_builder.append(typeName, " ");
_builder.append(" getEnumValue(Integer ordinal) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("return ordinalToEnumValues.get(ordinal);");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("public Integer getOrdinal() {");
_builder.newLine();
_builder.append("\t\t");
_builder.append("// TODO should we use a bidirectional map from a third-party library?");
_builder.newLine();
_builder.append("\t\t");
_builder.append("Integer ordinal = null;");
_builder.newLine();
_builder.append("\t\t");
_builder.append("for(Entry entry : ordinalToEnumValues.entrySet()) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t\t");
_builder.append("if(this == entry.getValue()) {");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("ordinal = entry.getKey();");
_builder.newLine();
_builder.append("\t\t\t\t");
_builder.append("break;");
_builder.newLine();
_builder.append("\t\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t\t");
_builder.append("return ordinal;");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.newLine();
_xblockexpression = (_builder);
}
return _xblockexpression;
}
}