Skip to content

Commit

Permalink
Add more nullability annotations to Java models (#228)
Browse files Browse the repository at this point in the history
* Add more nullability annotations to Java models
* sort annotations before writing them out
  • Loading branch information
shashachu authored and RicoYao committed Jul 30, 2019
1 parent f683102 commit 730f8e1
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 54 deletions.
27 changes: 21 additions & 6 deletions Examples/Java/Sources/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,18 @@ private Board(
this._bits = _bits;
}

@NonNull
public static Board.Builder builder() {
return new Board.Builder();
}

@NonNull
public Board.Builder toBuilder() {
return new Board.Builder(this);
}

public Board mergeFrom(Board model) {
@NonNull
public Board mergeFrom(@NonNull Board model) {
Board.Builder builder = this.toBuilder();
builder.mergeFrom(model);
return builder.build();
Expand Down Expand Up @@ -243,6 +246,7 @@ private Builder(@NonNull Board model) {
this._bits = model._bits;
}

@NonNull
public Builder setUid(@Nullable String value) {
this.uid = value;
if (this._bits.length > ID_INDEX) {
Expand All @@ -251,6 +255,7 @@ public Builder setUid(@Nullable String value) {
return this;
}

@NonNull
public Builder setContributors(@Nullable Set<User> value) {
this.contributors = value;
if (this._bits.length > CONTRIBUTORS_INDEX) {
Expand All @@ -259,6 +264,7 @@ public Builder setContributors(@Nullable Set<User> value) {
return this;
}

@NonNull
public Builder setCounts(@Nullable Map<String, Integer> value) {
this.counts = value;
if (this._bits.length > COUNTS_INDEX) {
Expand All @@ -267,6 +273,7 @@ public Builder setCounts(@Nullable Map<String, Integer> value) {
return this;
}

@NonNull
public Builder setCreatedAt(@Nullable Date value) {
this.createdAt = value;
if (this._bits.length > CREATED_AT_INDEX) {
Expand All @@ -275,6 +282,7 @@ public Builder setCreatedAt(@Nullable Date value) {
return this;
}

@NonNull
public Builder setCreator(@Nullable Map<String, String> value) {
this.creator = value;
if (this._bits.length > CREATOR_INDEX) {
Expand All @@ -283,6 +291,7 @@ public Builder setCreator(@Nullable Map<String, String> value) {
return this;
}

@NonNull
public Builder setCreatorURL(@Nullable String value) {
this.creatorURL = value;
if (this._bits.length > CREATOR_URL_INDEX) {
Expand All @@ -291,6 +300,7 @@ public Builder setCreatorURL(@Nullable String value) {
return this;
}

@NonNull
public Builder setDescription(@Nullable String value) {
this.description = value;
if (this._bits.length > DESCRIPTION_INDEX) {
Expand All @@ -299,6 +309,7 @@ public Builder setDescription(@Nullable String value) {
return this;
}

@NonNull
public Builder setImage(@NonNull Image value) {
this.image = value;
if (this._bits.length > IMAGE_INDEX) {
Expand All @@ -307,6 +318,7 @@ public Builder setImage(@NonNull Image value) {
return this;
}

@NonNull
public Builder setName(@Nullable String value) {
this.name = value;
if (this._bits.length > NAME_INDEX) {
Expand All @@ -315,6 +327,7 @@ public Builder setName(@Nullable String value) {
return this;
}

@NonNull
public Builder setUrl(@Nullable String value) {
this.url = value;
if (this._bits.length > URL_INDEX) {
Expand Down Expand Up @@ -380,7 +393,7 @@ public Board build() {
);
}

public void mergeFrom(Board model) {
public void mergeFrom(@NonNull Board model) {
if (model.getUidIsSet()) {
this.uid = model.uid;
if (this._bits.length > ID_INDEX) {
Expand Down Expand Up @@ -446,8 +459,9 @@ public void mergeFrom(Board model) {

public static class BoardTypeAdapterFactory implements TypeAdapterFactory {

@Nullable
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> typeToken) {
public <T> TypeAdapter<T> create(@NonNull Gson gson, @NonNull TypeToken<T> typeToken) {
if (!Board.class.isAssignableFrom(typeToken.getRawType())) {
return null;
}
Expand All @@ -469,23 +483,24 @@ public static class BoardTypeAdapter extends TypeAdapter<Board> {
private TypeAdapter<Set<User>> set_User_TypeAdapter;
private TypeAdapter<String> stringTypeAdapter;

public BoardTypeAdapter(Gson gson, BoardTypeAdapterFactory factory, TypeToken typeToken) {
public BoardTypeAdapter(@NonNull Gson gson, BoardTypeAdapterFactory factory, TypeToken typeToken) {
this.factory = factory;
this.gson = gson;
this.typeToken = typeToken;
}

@Override
public void write(JsonWriter writer, Board value) throws IOException {
public void write(@NonNull JsonWriter writer, Board value) throws IOException {
if (this.delegateTypeAdapter == null) {
this.delegateTypeAdapter = this.gson.getDelegateAdapter(this.factory, this.typeToken);
}
writer.setSerializeNulls(false);
this.delegateTypeAdapter.write(writer, value);
}

@Nullable
@Override
public Board read(JsonReader reader) throws IOException {
public Board read(@NonNull JsonReader reader) throws IOException {
if (reader.peek() == JsonToken.NULL) {
reader.nextNull();
return null;
Expand Down
Loading

0 comments on commit 730f8e1

Please sign in to comment.