Skip to content

Commit

Permalink
Use standard Java enum types for enum properties (#184)
Browse files Browse the repository at this point in the history
- Migrate generated code away from @IntDef @stringdef annotations since they are premature optimizations that will be handled anyways by ProGuard
  • Loading branch information
rahul-malik authored Mar 14, 2019
1 parent e63f15c commit 06925b8
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 127 deletions.
2 changes: 0 additions & 2 deletions Examples/Java/Sources/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

package com.pinterest.models;

import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
Expand Down
113 changes: 61 additions & 52 deletions Examples/Java/Sources/Everything.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

package com.pinterest.models;

import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
Expand Down Expand Up @@ -55,26 +53,27 @@ interface EverythingPolymorphicPropMatcher<R> {

public class Everything {

@Retention(RetentionPolicy.SOURCE)
@IntDef({EverythingIntEnum.INT_CASE_1, EverythingIntEnum.INT_CASE_2, EverythingIntEnum.INT_CASE_3})
public @interface EverythingIntEnum {
int INT_CASE_1 = 1;
int INT_CASE_2 = 2;
int INT_CASE_3 = 3;
public enum EverythingIntEnum {
INT_CASE_1(1),
INT_CASE_2(2),
INT_CASE_3(3);
private final int value;
EverythingIntEnum(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}

@Retention(RetentionPolicy.SOURCE)
@StringDef({EverythingStringEnum.CASE1, EverythingStringEnum.CASE2, EverythingStringEnum.CASE3})
public @interface EverythingStringEnum {
String CASE1 = "case1";
String CASE2 = "case2";
String CASE3 = "case3";
public enum EverythingStringEnum {
@SerializedName("case1") CASE1, @SerializedName("case2") CASE2, @SerializedName("case3") CASE3;
}

@SerializedName("array_prop") private @Nullable List<Object> arrayProp;
@SerializedName("boolean_prop") private @Nullable Boolean booleanProp;
@SerializedName("date_prop") private @Nullable Date dateProp;
@SerializedName("int_enum") private @Nullable @EverythingIntEnum int intEnum;
@SerializedName("int_enum") private @Nullable EverythingIntEnum intEnum;
@SerializedName("int_prop") private @Nullable Integer intProp;
@SerializedName("list_polymorphic_values") private @Nullable List<Object> listPolymorphicValues;
@SerializedName("list_with_list_and_other_model_values") private @Nullable List<List<User>> listWithListAndOtherModelValues;
Expand All @@ -96,7 +95,7 @@ public class Everything {
@SerializedName("set_prop_with_other_model_values") private @Nullable Set<User> setPropWithOtherModelValues;
@SerializedName("set_prop_with_primitive_values") private @Nullable Set<Integer> setPropWithPrimitiveValues;
@SerializedName("set_prop_with_values") private @Nullable Set<String> setPropWithValues;
@SerializedName("string_enum") private @Nullable @EverythingStringEnum String stringEnum;
@SerializedName("string_enum") private @Nullable EverythingStringEnum stringEnum;
@SerializedName("string_prop") private @Nullable String stringProp;
@SerializedName("type") private @Nullable String type;
@SerializedName("uri_prop") private @Nullable String uriProp;
Expand Down Expand Up @@ -137,7 +136,7 @@ private Everything(
@Nullable List<Object> arrayProp,
@Nullable Boolean booleanProp,
@Nullable Date dateProp,
@Nullable @EverythingIntEnum int intEnum,
@Nullable EverythingIntEnum intEnum,
@Nullable Integer intProp,
@Nullable List<Object> listPolymorphicValues,
@Nullable List<List<User>> listWithListAndOtherModelValues,
Expand All @@ -159,7 +158,7 @@ private Everything(
@Nullable Set<User> setPropWithOtherModelValues,
@Nullable Set<Integer> setPropWithPrimitiveValues,
@Nullable Set<String> setPropWithValues,
@Nullable @EverythingStringEnum String stringEnum,
@Nullable EverythingStringEnum stringEnum,
@Nullable String stringProp,
@Nullable String type,
@Nullable String uriProp,
Expand Down Expand Up @@ -296,7 +295,7 @@ public int hashCode() {
return this.dateProp;
}

public @Nullable @EverythingIntEnum int getIntEnum() {
public @Nullable EverythingIntEnum getIntEnum() {
return this.intEnum;
}

Expand Down Expand Up @@ -384,7 +383,7 @@ public int hashCode() {
return this.setPropWithValues;
}

public @Nullable @EverythingStringEnum String getStringEnum() {
public @Nullable EverythingStringEnum getStringEnum() {
return this.stringEnum;
}

Expand Down Expand Up @@ -521,7 +520,7 @@ public static class Builder {
@SerializedName("array_prop") private @Nullable List<Object> arrayProp;
@SerializedName("boolean_prop") private @Nullable Boolean booleanProp;
@SerializedName("date_prop") private @Nullable Date dateProp;
@SerializedName("int_enum") private @Nullable @EverythingIntEnum int intEnum;
@SerializedName("int_enum") private @Nullable EverythingIntEnum intEnum;
@SerializedName("int_prop") private @Nullable Integer intProp;
@SerializedName("list_polymorphic_values") private @Nullable List<Object> listPolymorphicValues;
@SerializedName("list_with_list_and_other_model_values") private @Nullable List<List<User>> listWithListAndOtherModelValues;
Expand All @@ -543,7 +542,7 @@ public static class Builder {
@SerializedName("set_prop_with_other_model_values") private @Nullable Set<User> setPropWithOtherModelValues;
@SerializedName("set_prop_with_primitive_values") private @Nullable Set<Integer> setPropWithPrimitiveValues;
@SerializedName("set_prop_with_values") private @Nullable Set<String> setPropWithValues;
@SerializedName("string_enum") private @Nullable @EverythingStringEnum String stringEnum;
@SerializedName("string_enum") private @Nullable EverythingStringEnum stringEnum;
@SerializedName("string_prop") private @Nullable String stringProp;
@SerializedName("type") private @Nullable String type;
@SerializedName("uri_prop") private @Nullable String uriProp;
Expand Down Expand Up @@ -604,7 +603,7 @@ public Builder setDateProp(@Nullable Date value) {
return this;
}

public Builder setIntEnum(@Nullable @EverythingIntEnum int value) {
public Builder setIntEnum(@Nullable EverythingIntEnum value) {
this.intEnum = value;
this._bits |= INT_ENUM_SET;
return this;
Expand Down Expand Up @@ -736,7 +735,7 @@ public Builder setSetPropWithValues(@Nullable Set<String> value) {
return this;
}

public Builder setStringEnum(@Nullable @EverythingStringEnum String value) {
public Builder setStringEnum(@Nullable EverythingStringEnum value) {
this.stringEnum = value;
this._bits |= STRING_ENUM_SET;
return this;
Expand Down Expand Up @@ -772,7 +771,7 @@ public Builder setUriProp(@Nullable String value) {
return this.dateProp;
}

public @Nullable @EverythingIntEnum int getIntEnum() {
public @Nullable EverythingIntEnum getIntEnum() {
return this.intEnum;
}

Expand Down Expand Up @@ -860,7 +859,7 @@ public Builder setUriProp(@Nullable String value) {
return this.setPropWithValues;
}

public @Nullable @EverythingStringEnum String getStringEnum() {
public @Nullable EverythingStringEnum getStringEnum() {
return this.stringEnum;
}

Expand Down Expand Up @@ -1132,16 +1131,21 @@ public Everything read(JsonReader reader) throws IOException {

public static final class EverythingMapPolymorphicValues<R> {

@Retention(RetentionPolicy.SOURCE)
@IntDef({InternalStorage.USER, InternalStorage.BOARD, InternalStorage.IMAGE, InternalStorage.PIN, InternalStorage.EVERYTHING, InternalStorage.LISTOBJECT, InternalStorage.MAPSTRING_OBJECT})
public @interface InternalStorage {
int USER = 0;
int BOARD = 1;
int IMAGE = 2;
int PIN = 3;
int EVERYTHING = 4;
int LISTOBJECT = 5;
int MAPSTRING_OBJECT = 6;
public enum InternalStorage {
USER(0),
BOARD(1),
IMAGE(2),
PIN(3),
EVERYTHING(4),
LISTOBJECT(5),
MAPSTRING_OBJECT(6);
private final int value;
InternalStorage(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}

private @Nullable User value0;
Expand All @@ -1152,7 +1156,7 @@ public static final class EverythingMapPolymorphicValues<R> {
private @Nullable List<Object> value5;
private @Nullable Map<String, Object> value6;

static private @InternalStorage int internalStorage;
static private InternalStorage internalStorage;

private EverythingMapPolymorphicValues() {
}
Expand All @@ -1165,20 +1169,25 @@ public R matchEverythingMapPolymorphicValues(EverythingMapPolymorphicValuesMatch

public static final class EverythingPolymorphicProp<R> {

@Retention(RetentionPolicy.SOURCE)
@IntDef({InternalStorage.USER, InternalStorage.BOARD, InternalStorage.IMAGE, InternalStorage.PIN, InternalStorage.EVERYTHING, InternalStorage.STRING, InternalStorage.BOOLEAN, InternalStorage.INTEGER, InternalStorage.DOUBLE, InternalStorage.DATE, InternalStorage.STRING})
public @interface InternalStorage {
int USER = 0;
int BOARD = 1;
int IMAGE = 2;
int PIN = 3;
int EVERYTHING = 4;
int STRING = 5;
int BOOLEAN = 6;
int INTEGER = 7;
int DOUBLE = 8;
int DATE = 9;
int STRING = 10;
public enum InternalStorage {
USER(0),
BOARD(1),
IMAGE(2),
PIN(3),
EVERYTHING(4),
STRING(5),
BOOLEAN(6),
INTEGER(7),
DOUBLE(8),
DATE(9),
STRING(10);
private final int value;
InternalStorage(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}

private @Nullable User value0;
Expand All @@ -1193,7 +1202,7 @@ public static final class EverythingPolymorphicProp<R> {
private @Nullable Date value9;
private @Nullable String value10;

static private @InternalStorage int internalStorage;
static private InternalStorage internalStorage;

private EverythingPolymorphicProp() {
}
Expand Down
2 changes: 0 additions & 2 deletions Examples/Java/Sources/Image.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

package com.pinterest.models;

import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
Expand Down
2 changes: 0 additions & 2 deletions Examples/Java/Sources/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

package com.pinterest.models;

import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
Expand Down
48 changes: 28 additions & 20 deletions Examples/Java/Sources/Pin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@

package com.pinterest.models;

import android.support.annotation.IntDef;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StringDef;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.TypeAdapter;
Expand All @@ -36,12 +34,17 @@ interface PinAttributionObjectsMatcher<R> {

public class Pin {

@Retention(RetentionPolicy.SOURCE)
@IntDef({PinInStock.UNKNOWN, PinInStock.OUT_OF_STOCK, PinInStock.IN_STOCK})
public @interface PinInStock {
int UNKNOWN = -1;
int OUT_OF_STOCK = 0;
int IN_STOCK = 1;
public enum PinInStock {
UNKNOWN(-1),
OUT_OF_STOCK(0),
IN_STOCK(1);
private final int value;
PinInStock(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}

@SerializedName("attribution") private @Nullable Map<String, String> attribution;
Expand All @@ -54,7 +57,7 @@ public class Pin {
@SerializedName("description") private @Nullable String descriptionText;
@SerializedName("id") private @NonNull String identifier;
@SerializedName("image") private @Nullable Image image;
@SerializedName("in_stock") private @Nullable @PinInStock int inStock;
@SerializedName("in_stock") private @Nullable PinInStock inStock;
@SerializedName("link") private @Nullable String link;
@SerializedName("media") private @Nullable Map<String, String> media;
@SerializedName("note") private @Nullable String note;
Expand Down Expand Up @@ -93,7 +96,7 @@ private Pin(
@Nullable String descriptionText,
@NonNull String identifier,
@Nullable Image image,
@Nullable @PinInStock int inStock,
@Nullable PinInStock inStock,
@Nullable String link,
@Nullable Map<String, String> media,
@Nullable String note,
Expand Down Expand Up @@ -225,7 +228,7 @@ public int hashCode() {
return this.image;
}

public @Nullable @PinInStock int getInStock() {
public @Nullable PinInStock getInStock() {
return this.inStock;
}

Expand Down Expand Up @@ -333,7 +336,7 @@ public static class Builder {
@SerializedName("description") private @Nullable String descriptionText;
@SerializedName("id") private @NonNull String identifier;
@SerializedName("image") private @Nullable Image image;
@SerializedName("in_stock") private @Nullable @PinInStock int inStock;
@SerializedName("in_stock") private @Nullable PinInStock inStock;
@SerializedName("link") private @Nullable String link;
@SerializedName("media") private @Nullable Map<String, String> media;
@SerializedName("note") private @Nullable String note;
Expand Down Expand Up @@ -427,7 +430,7 @@ public Builder setImage(@Nullable Image value) {
return this;
}

public Builder setInStock(@Nullable @PinInStock int value) {
public Builder setInStock(@Nullable PinInStock value) {
this.inStock = value;
this._bits |= IN_STOCK_SET;
return this;
Expand Down Expand Up @@ -509,7 +512,7 @@ public Builder setVisualSearchAttrs(@Nullable Map<String, Object> value) {
return this.image;
}

public @Nullable @PinInStock int getInStock() {
public @Nullable PinInStock getInStock() {
return this.inStock;
}

Expand Down Expand Up @@ -709,17 +712,22 @@ public Pin read(JsonReader reader) throws IOException {

public static final class PinAttributionObjects<R> {

@Retention(RetentionPolicy.SOURCE)
@IntDef({InternalStorage.BOARD, InternalStorage.USER})
public @interface InternalStorage {
int BOARD = 0;
int USER = 1;
public enum InternalStorage {
BOARD(0),
USER(1);
private final int value;
InternalStorage(int value) {
this.value = value;
}
public int getValue() {
return this.value;
}
}

private @Nullable Board value0;
private @Nullable User value1;

static private @InternalStorage int internalStorage;
static private InternalStorage internalStorage;

private PinAttributionObjects() {
}
Expand Down
Loading

0 comments on commit 06925b8

Please sign in to comment.