Skip to content

Commit

Permalink
Added: Enum values can now be integer or string.
Browse files Browse the repository at this point in the history
  • Loading branch information
freezy committed Nov 11, 2012
1 parent 425806d commit 6aaf7ce
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHECKS.md
Expand Up @@ -11,6 +11,7 @@ Methods
* `Player.Open`: A real bitch. Two args, first one 4-way multitype (`item`),
second inner object (`options`) which second member `resume` is a 3-way
multitype.
* `PVR.GetChannels` returns an array of (referenced) enums.

Types
-----
Expand Down
Expand Up @@ -364,7 +364,16 @@ private boolean isGlobal() {
* @return Enum object
*/
public JavaEnum getEnum(Namespace ns, String enumName) {
final JavaEnum e = new JavaEnum(ns, enumName, name);
if (!property.getType().isNative()) {
throw new IllegalStateException("Enums with non-native type doesn't make any sense!");
}
final JavaEnum.NativeType t;
if (property.getType().getName().equals("integer")) {
t = JavaEnum.NativeType.INTEGER;
} else {
t = JavaEnum.NativeType.STRING;
}
final JavaEnum e = new JavaEnum(ns, enumName, name, t);
List<String> enums = property.isArray() ? property.getItems().getEnums() : property.getEnums();

if (property.obj().isArray()) {
Expand Down
30 changes: 29 additions & 1 deletion src/org/xbmc/android/jsonrpc/generator/model/JavaEnum.java
Expand Up @@ -37,16 +37,33 @@ public class JavaEnum {
private final String name;
private final Namespace namespace;
private final String apiType;
private final NativeType nativeType;
private final List<String> values = new ArrayList<String>();

private boolean isInner = false;
private boolean isArray = false;
private JavaClass outerType = null; // set if isInner == true

public enum NativeType {
STRING {
@Override
public String toString() {
return "String";
}
},
INTEGER {
@Override
public String toString() {
return "Integer";
}
}
}

public JavaEnum(Namespace namespace, String name, String apiType) {
public JavaEnum(Namespace namespace, String name, String apiType, NativeType nativeType) {
this.name = name;
this.namespace = namespace;
this.apiType = apiType;
this.nativeType = nativeType;

if (apiType != null) {
GLOBALS.put(apiType, this);
Expand All @@ -65,6 +82,9 @@ public void addValue(String value) {
public String getName() {
return name;
}
public String getTypeName() {
return nativeType.toString();
}

public Namespace getNamespace() {
return namespace;
Expand Down Expand Up @@ -99,6 +119,14 @@ public JavaEnum setArray() {
return this;
}

public boolean isInt() {
return nativeType == NativeType.INTEGER;
}

public boolean isString() {
return nativeType == NativeType.STRING;
}

/**
* Global enums sometimes are defined as array of enums. Since we render
* them as if they are normal enums when defining, it's important to know
Expand Down
29 changes: 24 additions & 5 deletions src/org/xbmc/android/jsonrpc/generator/view/EnumView.java
Expand Up @@ -55,16 +55,24 @@ public void render(StringBuilder sb, int indent, boolean force) {

// enumns
for (String enumValue : e.getValues()) {
sb.append(prefix).append(" public final String ");
sb.append(prefix).append(" public final ");
sb.append(e.getTypeName());
sb.append(" ");
sb.append(getName(enumValue));
sb.append(" = \"");
sb.append(" = ");
if (e.isString()) {
sb.append("\"");
}
sb.append(enumValue);
sb.append("\";\n");
if (e.isString()) {
sb.append("\"");
}
sb.append(";\n");
}

// array public final Set<String> values = new HashSet<String>(Arrays.asList(Type.UNKNOWN, Type.XBMC_ADDON_AUDIO));
sb.append("\n");
sb.append(prefix).append(" public final static Set<String> values = new HashSet<String>(Arrays.asList(");
sb.append(prefix).append(" public final static Set<").append(e.getTypeName()).append("> values = new HashSet<").append(e.getTypeName()).append(">(Arrays.asList(");
if (!e.getValues().isEmpty()) {
for (String enumValue : e.getValues()) {
sb.append(getName(enumValue));
Expand All @@ -77,7 +85,18 @@ public void render(StringBuilder sb, int indent, boolean force) {
}

private String getName(String enumValue) {
return enumValue.replaceAll("\\.", "_").toUpperCase();
if (e.isString()) {
return enumValue.replaceAll("\\.", "_").toUpperCase();
} else {
if (enumValue.equals("0")) {
return "ZERO";
}
if (enumValue.startsWith("-")) {
return "MINUS_" + enumValue.substring(1);
} else {
return "PLUS_" + enumValue;
}
}
}


Expand Down
Expand Up @@ -93,17 +93,19 @@ private void renderConstructor(StringBuilder sb, Namespace ns, JavaMethod method
while (it.hasNext()) {
final JavaAttribute p = it.next();
if (!it.hasNext() && p.isArray()) {
// if enum != null, we know it's an enum array, otherwise isArray() woulnd't have returned true.
// if enum != null, we know it's an enum array, otherwise isArray() wouldn't have returned true.
// in all other cases, p.getType() != null.
if (p.getEnum() != null || p.getType().isEnumArray()) {
sb.append("String...");
if (p.getEnum() != null) {
sb.append(p.getEnum().getTypeName());
} else if (p.getType().isEnumArray()) {
sb.append("String");
} else {
sb.append(getClassReference(ns, p.getType().getArrayType()));
sb.append("...");
}
sb.append("...");
} else {
if (p.isEnum()) {
sb.append("String");
sb.append(p.getEnum().getTypeName());
} else {
sb.append(getClassReference(ns, p.getType(), true));
}
Expand Down

0 comments on commit 6aaf7ce

Please sign in to comment.