Skip to content

Commit c9ca0c9

Browse files
chore: miscellaneous code cleanups (#946)
Addresses some Android Studio / IntelliJ code tips.
1 parent 0806388 commit c9ca0c9

File tree

9 files changed

+69
-143
lines changed

9 files changed

+69
-143
lines changed

aws-api-appsync/src/main/java/com/amplifyframework/api/aws/AppSyncGraphQLRequest.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ public final class AppSyncGraphQLRequest<R> extends GraphQLRequest<R> {
5050

5151
/**
5252
* Constructor for AppSyncGraphQLRequest.
53-
* @throw AmplifyException if a ModelSchema can't be derived from the provided model class
5453
*/
5554
private AppSyncGraphQLRequest(Builder builder) {
5655
super(builder.responseType, new GsonVariablesSerializer());
@@ -116,34 +115,30 @@ public String getQuery() {
116115
String modelName = Casing.capitalizeFirst(modelSchema.getName());
117116
if (QueryType.LIST.equals(operation)) {
118117
// The list operation name is pluralized by simply adding 's' to the end.
119-
modelName = modelName += "s";
118+
modelName += "s";
120119
} else if (QueryType.SYNC.equals(operation)) {
121120
// The sync operation name is pluralized using pluralize.js, which uses more complex pluralization rules
122121
// than simply adding an 's' at the end (e.g. baby > babies, person > people, etc). This pluralized name
123122
// is an annotation on the codegen'd model class, so we will just grab it from the ModelSchema.
124123
modelName = Casing.capitalizeFirst(modelSchema.getPluralName());
125124
}
126125

127-
String operationString = new StringBuilder()
128-
.append(Casing.from(Casing.CaseType.SCREAMING_SNAKE_CASE).to(Casing.CaseType.CAMEL_CASE)
129-
.convert(operation.toString()))
130-
.append(modelName)
131-
.append(inputParameterString)
132-
.append(selectionSet.toString(" "))
133-
.toString();
134-
135-
String queryString = new StringBuilder()
136-
.append(operation.getOperationType().getName())
137-
.append(" ")
138-
.append(Casing.from(Casing.CaseType.SCREAMING_SNAKE_CASE).to(Casing.CaseType.PASCAL_CASE)
139-
.convert(operation.toString()))
140-
.append(modelName)
141-
.append(inputTypeString)
142-
.append(Wrap.inPrettyBraces(operationString, "", " "))
143-
.append("\n")
144-
.toString();
145-
146-
return queryString;
126+
String operationString =
127+
Casing.from(Casing.CaseType.SCREAMING_SNAKE_CASE)
128+
.to(Casing.CaseType.CAMEL_CASE)
129+
.convert(operation.toString()) +
130+
modelName +
131+
inputParameterString +
132+
selectionSet.toString(" ");
133+
134+
return operation.getOperationType().getName() +
135+
" " +
136+
Casing.from(Casing.CaseType.SCREAMING_SNAKE_CASE).to(Casing.CaseType.PASCAL_CASE)
137+
.convert(operation.toString()) +
138+
modelName +
139+
inputTypeString +
140+
Wrap.inPrettyBraces(operationString, "", " ") +
141+
"\n";
147142
}
148143

149144
@Override

aws-api-appsync/src/main/java/com/amplifyframework/api/aws/SelectionSet.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class SelectionSet {
5555
* Copy constructor.
5656
* @param selectionSet node to copy
5757
*/
58+
@SuppressWarnings("CopyConstructorMissesField") // It is cloned, by recursion
5859
public SelectionSet(SelectionSet selectionSet) {
5960
this(selectionSet.value, new HashSet<>(selectionSet.nodes));
6061
}
@@ -206,7 +207,7 @@ public SelectionSet build() throws AmplifyException {
206207
* - "nextToken"
207208
*
208209
* @param node a root node, with a value of null, and pagination fields
209-
* @return
210+
* @return A selection set
210211
*/
211212
private SelectionSet wrapPagination(SelectionSet node) {
212213
return new SelectionSet(null, wrapPagination(node.getNodes()));
@@ -271,7 +272,7 @@ private Set<SelectionSet> getModelFields(Class<? extends Model> clazz, int depth
271272
/**
272273
* We handle customType fields differently as DEPTH does not apply here.
273274
* @param clazz class we wish to build selection set for
274-
* @return
275+
* @return A set of selection sets
275276
*/
276277
private Set<SelectionSet> getNestedCustomTypeFields(Class<?> clazz) {
277278
Set<SelectionSet> result = new HashSet<>();
@@ -289,7 +290,7 @@ private Set<SelectionSet> getNestedCustomTypeFields(Class<?> clazz) {
289290
/**
290291
* Helper to determine if field is a custom type. If custom types we need to build nested selection set.
291292
* @param field field we wish to check
292-
* @return
293+
* @return True if the field is of a custom type
293294
*/
294295
private static boolean isCustomType(@NonNull Field field) {
295296
Class<?> cls = getClassForField(field);
@@ -307,13 +308,13 @@ private static boolean isCustomType(@NonNull Field field) {
307308

308309
/**
309310
* Get the class of a field. If field is a collection, it returns the Generic type
310-
* @return
311+
* @return The class of the field
311312
*/
312313
static Class<?> getClassForField(Field field) {
313314
Class<?> typeClass;
314315
if (Collection.class.isAssignableFrom(field.getType())) {
315316
ParameterizedType listType = (ParameterizedType) field.getGenericType();
316-
typeClass = (Class) listType.getActualTypeArguments()[0];
317+
typeClass = (Class<?>) listType.getActualTypeArguments()[0];
317318
} else {
318319
typeClass = field.getType();
319320
}

aws-api/src/main/java/com/amplifyframework/api/aws/AppSyncGraphQLRequestFactory.java

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,13 @@ static <R, T extends Model> GraphQLRequest<R> buildQuery(
174174
* @return a valid {@link GraphQLRequest} instance.
175175
* @throws IllegalStateException when the model schema does not contain the expected information.
176176
*/
177-
@SuppressWarnings("unchecked")
178177
public static <R, T extends Model> GraphQLRequest<R> buildMutation(
179178
T model,
180179
QueryPredicate predicate,
181180
MutationType type
182181
) {
183182
try {
184-
// model is of type T so this is a safe cast - hence the warning suppression
185-
Class<T> modelClass = (Class<T>) model.getClass();
183+
Class<? extends Model> modelClass = model.getClass();
186184
ModelSchema schema = ModelSchema.fromModelClass(modelClass);
187185
String graphQlTypeName = schema.getName();
188186

@@ -192,11 +190,10 @@ public static <R, T extends Model> GraphQLRequest<R> buildMutation(
192190
.requestOptions(new ApiGraphQLRequestOptions())
193191
.responseType(modelClass);
194192

195-
String inputType = new StringBuilder()
196-
.append(Casing.capitalize(type.toString()))
197-
.append(Casing.capitalizeFirst(graphQlTypeName))
198-
.append("Input!")
199-
.toString(); // CreateTodoInput
193+
String inputType =
194+
Casing.capitalize(type.toString()) +
195+
Casing.capitalizeFirst(graphQlTypeName) +
196+
"Input!"; // CreateTodoInput
200197

201198
if (MutationType.DELETE.equals(type)) {
202199
builder.variable("input", inputType, Collections.singletonMap("id", model.getId()));
@@ -205,11 +202,10 @@ public static <R, T extends Model> GraphQLRequest<R> buildMutation(
205202
}
206203

207204
if (!QueryPredicates.all().equals(predicate)) {
208-
String conditionType = new StringBuilder()
209-
.append("Model")
210-
.append(Casing.capitalizeFirst(graphQlTypeName))
211-
.append("ConditionInput")
212-
.toString();
205+
String conditionType =
206+
"Model" +
207+
Casing.capitalizeFirst(graphQlTypeName) +
208+
"ConditionInput";
213209
builder.variable("condition", conditionType, parsePredicate(predicate));
214210
}
215211

@@ -232,7 +228,6 @@ public static <R, T extends Model> GraphQLRequest<R> buildMutation(
232228
* @return a valid {@link GraphQLRequest} instance.
233229
* @throws IllegalStateException when the model schema does not contain the expected information.
234230
*/
235-
@SuppressWarnings("SameParameterValue")
236231
public static <R, T extends Model> GraphQLRequest<R> buildSubscription(
237232
Class<T> modelClass,
238233
SubscriptionType subscriptionType
@@ -254,7 +249,7 @@ public static <R, T extends Model> GraphQLRequest<R> buildSubscription(
254249

255250
private static Map<String, Object> parsePredicate(QueryPredicate queryPredicate) {
256251
if (queryPredicate instanceof QueryPredicateOperation) {
257-
QueryPredicateOperation<?> qpo = (QueryPredicateOperation) queryPredicate;
252+
QueryPredicateOperation<?> qpo = (QueryPredicateOperation<?>) queryPredicate;
258253
QueryOperator<?> op = qpo.operator();
259254
return Collections.singletonMap(
260255
qpo.field(),
@@ -323,17 +318,17 @@ private static Object appSyncOpValue(QueryOperator<?> qOp) {
323318
case EQUAL:
324319
return ((EqualQueryOperator) qOp).value();
325320
case LESS_OR_EQUAL:
326-
return ((LessOrEqualQueryOperator) qOp).value();
321+
return ((LessOrEqualQueryOperator<?>) qOp).value();
327322
case LESS_THAN:
328-
return ((LessThanQueryOperator) qOp).value();
323+
return ((LessThanQueryOperator<?>) qOp).value();
329324
case GREATER_OR_EQUAL:
330-
return ((GreaterOrEqualQueryOperator) qOp).value();
325+
return ((GreaterOrEqualQueryOperator<?>) qOp).value();
331326
case GREATER_THAN:
332-
return ((GreaterThanQueryOperator) qOp).value();
327+
return ((GreaterThanQueryOperator<?>) qOp).value();
333328
case CONTAINS:
334329
return ((ContainsQueryOperator) qOp).value();
335330
case BETWEEN:
336-
BetweenQueryOperator<?> betweenOp = (BetweenQueryOperator) qOp;
331+
BetweenQueryOperator<?> betweenOp = (BetweenQueryOperator<?>) qOp;
337332
return Arrays.asList(betweenOp.start(), betweenOp.end());
338333
case BEGINS_WITH:
339334
return ((BeginsWithQueryOperator) qOp).value();

aws-api/src/test/java/com/amplifyframework/api/aws/AppSyncGraphQLRequestFactoryTest.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
package com.amplifyframework.api.aws;
1717

18-
import com.amplifyframework.AmplifyException;
19-
import com.amplifyframework.api.ApiException;
2018
import com.amplifyframework.api.graphql.GraphQLRequest;
2119
import com.amplifyframework.api.graphql.MutationType;
2220
import com.amplifyframework.api.graphql.SubscriptionType;
@@ -44,11 +42,10 @@ public final class AppSyncGraphQLRequestFactoryTest {
4442

4543
/**
4644
* Validate construction of a GraphQL query from a class and an object ID.
47-
* @throws ApiException from possible query builder failure
4845
* @throws JSONException from JSONAssert.assertEquals
4946
*/
5047
@Test
51-
public void buildQueryFromClassAndId() throws ApiException, JSONException {
48+
public void buildQueryFromClassAndId() throws JSONException {
5249
// Arrange a hard-coded ID as found int the expected data file.
5350
final String uuidForExpectedQuery = "9a1bee5c-248f-4746-a7da-58f703ec572d";
5451

@@ -66,11 +63,10 @@ public void buildQueryFromClassAndId() throws ApiException, JSONException {
6663

6764
/**
6865
* Validate construction of a GraphQL query from a class and a predicate.
69-
* @throws AmplifyException from buildQuery().
7066
* @throws JSONException from JSONAssert.assertEquals
7167
*/
7268
@Test
73-
public void buildQueryFromClassAndPredicate() throws AmplifyException, JSONException {
69+
public void buildQueryFromClassAndPredicate() throws JSONException {
7470
// Arrange - ID for predicate, hard-coded version of what's in the expected txt
7571
final String expectedId = "aca4a318-181e-445a-beb9-7656f5005c7b";
7672

@@ -89,12 +85,11 @@ public void buildQueryFromClassAndPredicate() throws AmplifyException, JSONExcep
8985
/**
9086
* Validates construction of a mutation query from a Person instance, a predicate,
9187
* and an {@link MutationType}.
92-
* @throws AmplifyException From buildMutation().
9388
* @throws JSONException from JSONAssert.assertEquals
9489
*/
9590
@SuppressWarnings("deprecation")
9691
@Test
97-
public void buildMutationFromPredicateAndMutationType() throws AmplifyException, JSONException {
92+
public void buildMutationFromPredicateAndMutationType() throws JSONException {
9893
// Arrange a person to delete, using UUID from test resource file
9994
final String expectedId = "dfcdac69-0662-41df-a67b-48c62a023f97";
10095
final Person tony = Person.builder()
@@ -122,11 +117,10 @@ public void buildMutationFromPredicateAndMutationType() throws AmplifyException,
122117
/**
123118
* Validates construction of a subscription request using a class and an
124119
* {@link SubscriptionType}.
125-
* @throws ApiException from subscription builder potential failure
126120
* @throws JSONException from JSONAssert.assertEquals
127121
*/
128122
@Test
129-
public void buildSubscriptionFromClassAndSubscriptionType() throws ApiException, JSONException {
123+
public void buildSubscriptionFromClassAndSubscriptionType() throws JSONException {
130124
GraphQLRequest<Person> subscriptionRequest = AppSyncGraphQLRequestFactory.buildSubscription(
131125
Person.class, SubscriptionType.ON_CREATE
132126
);
@@ -140,11 +134,10 @@ public void buildSubscriptionFromClassAndSubscriptionType() throws ApiException,
140134

141135
/**
142136
* Validates date serialization when creating GraphQLRequest.
143-
* @throws ApiException from buildMutation potential failure
144137
* @throws JSONException from JSONAssert.assertEquals JSON parsing error
145138
*/
146139
@Test
147-
public void validateDateSerializer() throws ApiException, JSONException {
140+
public void validateDateSerializer() throws JSONException {
148141
// Create expectation
149142
final Meeting meeting1 = Meeting.builder()
150143
.name("meeting1")

aws-datastore/src/main/java/com/amplifyframework/datastore/appsync/AppSyncRequestFactory.java

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,9 @@ static <M extends Model> AppSyncGraphQLRequest<ModelWithMetadata<M>> buildDeleti
132132
static <M extends Model> AppSyncGraphQLRequest<ModelWithMetadata<M>> buildUpdateRequest(
133133
M model, Integer version, QueryPredicate predicate) throws DataStoreException {
134134
Class<M> modelClass = (Class<M>) model.getClass();
135-
Map<String, Object> inputMap = new HashMap<>();
136135
try {
137136
ModelSchema schema = ModelSchema.fromModelClass(modelClass);
138-
inputMap.putAll(schema.getMapOfFieldNameAndValues(model));
137+
Map<String, Object> inputMap = new HashMap<>(schema.getMapOfFieldNameAndValues(model));
139138
inputMap.put("_version", version);
140139
return buildMutation(modelClass, inputMap, predicate, MutationType.UPDATE);
141140

@@ -162,7 +161,7 @@ static <M extends Model> AppSyncGraphQLRequest<ModelWithMetadata<M>> buildCreati
162161

163162
static Map<String, Object> parsePredicate(QueryPredicate queryPredicate) throws DataStoreException {
164163
if (queryPredicate instanceof QueryPredicateOperation) {
165-
QueryPredicateOperation<?> qpo = (QueryPredicateOperation) queryPredicate;
164+
QueryPredicateOperation<?> qpo = (QueryPredicateOperation<?>) queryPredicate;
166165
QueryOperator<?> op = qpo.operator();
167166
return Collections.singletonMap(
168167
qpo.field(),
@@ -234,17 +233,17 @@ private static Object appSyncOpValue(QueryOperator<?> qOp) throws DataStoreExcep
234233
case EQUAL:
235234
return ((EqualQueryOperator) qOp).value();
236235
case LESS_OR_EQUAL:
237-
return ((LessOrEqualQueryOperator) qOp).value();
236+
return ((LessOrEqualQueryOperator<?>) qOp).value();
238237
case LESS_THAN:
239-
return ((LessThanQueryOperator) qOp).value();
238+
return ((LessThanQueryOperator<?>) qOp).value();
240239
case GREATER_OR_EQUAL:
241-
return ((GreaterOrEqualQueryOperator) qOp).value();
240+
return ((GreaterOrEqualQueryOperator<?>) qOp).value();
242241
case GREATER_THAN:
243-
return ((GreaterThanQueryOperator) qOp).value();
242+
return ((GreaterThanQueryOperator<?>) qOp).value();
244243
case CONTAINS:
245244
return ((ContainsQueryOperator) qOp).value();
246245
case BETWEEN:
247-
BetweenQueryOperator<?> betweenOp = (BetweenQueryOperator) qOp;
246+
BetweenQueryOperator<?> betweenOp = (BetweenQueryOperator<?>) qOp;
248247
return Arrays.asList(betweenOp.start(), betweenOp.end());
249248
case BEGINS_WITH:
250249
return ((BeginsWithQueryOperator) qOp).value();
@@ -279,20 +278,18 @@ private static <M extends Model> AppSyncGraphQLRequest<ModelWithMetadata<M>> bui
279278
.requestOptions(new DataStoreGraphQLRequestOptions())
280279
.responseType(TypeMaker.getParameterizedType(ModelWithMetadata.class, modelClass));
281280

282-
String inputType = new StringBuilder()
283-
.append(Casing.capitalize(mutationType.toString()))
284-
.append(Casing.capitalizeFirst(graphQlTypeName))
285-
.append("Input!")
286-
.toString(); // CreateTodoInput
281+
String inputType =
282+
Casing.capitalize(mutationType.toString()) +
283+
Casing.capitalizeFirst(graphQlTypeName) +
284+
"Input!"; // CreateTodoInput
287285

288286
builder.variable("input", inputType, inputMap);
289287

290288
if (!QueryPredicates.all().equals(predicate)) {
291-
String conditionType = new StringBuilder()
292-
.append("Model")
293-
.append(Casing.capitalizeFirst(graphQlTypeName))
294-
.append("ConditionInput")
295-
.toString();
289+
String conditionType =
290+
"Model" +
291+
Casing.capitalizeFirst(graphQlTypeName) +
292+
"ConditionInput";
296293
builder.variable("condition", conditionType, parsePredicate(predicate));
297294
}
298295
return builder.build();

aws-datastore/src/test/java/com/amplifyframework/datastore/appsync/AppSyncRequestFactoryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -153,17 +153,17 @@ public void validateDeleteWithPredicateGeneration() throws DataStoreException, J
153153
/**
154154
* Checks that the predicate expression matches the expected value.
155155
* @throws DataStoreException If the output does not match.
156-
* @throws JSONException from JSONAssert.assertEquals.
157156
*/
158157
@Test
159158
public void validatePredicateGeneration() throws DataStoreException {
160-
Map<String, Object> predicate = AppSyncRequestFactory.parsePredicate(BlogOwner.NAME.eq("Test Dummy"));
159+
Map<String, Object> predicate =
160+
AppSyncRequestFactory.parsePredicate(BlogOwner.NAME.eq("Test Dummy"));
161161
assertEquals(
162162
"{name={eq=Test Dummy}}",
163163
predicate.toString()
164164
);
165165

166-
predicate = AppSyncRequestFactory.parsePredicate(
166+
AppSyncRequestFactory.parsePredicate(
167167
Blog.NAME.beginsWith("A day in the life of a...").and(Blog.OWNER.eq("DUMMY_OWNER_ID"))
168168
);
169169
}

core/src/main/java/com/amplifyframework/core/model/ModelAssociation.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public boolean equals(Object thatObject) {
131131

132132
@Override
133133
public int hashCode() {
134-
int result = name != null ? name.hashCode() : 0;
134+
int result = name.hashCode();
135135
result = 31 * result + (targetName != null ? targetName.hashCode() : 0);
136136
result = 31 * result + (associatedName != null ? associatedName.hashCode() : 0);
137137
result = 31 * result + (associatedType != null ? associatedType.hashCode() : 0);
@@ -141,11 +141,11 @@ public int hashCode() {
141141
@Override
142142
public String toString() {
143143
return "ModelAssociation{" +
144-
"name=\'" + name + "\'" +
145-
", targetName=\'" + targetName + "\'" +
146-
", associatedName=\'" + associatedName + "\'" +
147-
", associatedType=\'" + associatedType + "\'" +
148-
'}';
144+
"name='" + name + '\'' +
145+
", targetName='" + targetName + '\'' +
146+
", associatedName='" + associatedName + '\'' +
147+
", associatedType='" + associatedType + '\'' +
148+
'}';
149149
}
150150

151151
/**

0 commit comments

Comments
 (0)