Skip to content

Commit

Permalink
Cleanup unnecessary syntax (#1854)
Browse files Browse the repository at this point in the history
* Cleanup unnecessary syntax

* cleanup unnecessary else if

* use filter

Co-authored-by: wcekan <[email protected]>
Co-authored-by: Aaron Klish <[email protected]>
  • Loading branch information
3 people authored Feb 22, 2021
1 parent 852ce04 commit e2005d6
Show file tree
Hide file tree
Showing 61 changed files with 340 additions and 353 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ protected void executeHook(LifeCycleHookBinding.Operation operation, LifeCycleHo
//ReadPreSecurityHook - Those hooks get evaluated in line with the request processing.
executeAsync(query, queryWorker);
return;
} else if (operation.equals(CREATE)) {
}
if (operation.equals(CREATE)) {
if (phase.equals(POSTCOMMIT)) {
completeAsync(query, requestScope);
return;
} else if (phase.equals(PRESECURITY)) {
}
if (phase.equals(PRESECURITY)) {
updatePrincipalName(query, requestScope);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ public <T extends AsyncAPI> T updateStatus(String asyncAPIId, QueryStatus status
@Override
public <T extends AsyncAPI> Iterable<T> updateStatusAsyncAPIByFilter(FilterExpression filterExpression,
QueryStatus status, Class<T> type) {
return updateAsyncAPIIterable(filterExpression, (asyncAPI) -> {
asyncAPI.setStatus(status);
}, type);
return updateAsyncAPIIterable(filterExpression, asyncAPI -> asyncAPI.setStatus(status), type);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ public TableExport storeResults(TableExport tableExport, Observable<String> resu
throwable -> {
throw new IllegalStateException(STORE_ERROR, throwable);
},
() -> {
writer.flush();
}
() ->
writer.flush()
);
} catch (IOException e) {
throw new IllegalStateException(STORE_ERROR, e);
Expand All @@ -71,31 +70,28 @@ public Observable<String> getResultsByID(String asyncQueryID) {

return Observable.using(
() -> getReader(asyncQueryID),
reader -> {
return Observable.fromIterable(() -> {
return new Iterator<String>() {
private String record = null;
reader -> Observable.fromIterable(() -> new Iterator<String>() {
private String record = null;

@Override
public boolean hasNext() {
try {
record = reader.readLine();
return record != null;
} catch (IOException e) {
throw new IllegalStateException(RETRIEVE_ERROR, e);
}
}
@Override
public boolean hasNext() {
try {
record = reader.readLine();
return record != null;
} catch (IOException e) {
throw new IllegalStateException(RETRIEVE_ERROR, e);
}
}

@Override
public String next() {
if (record != null) {
return record;
}
throw new IllegalStateException("null line found.");
}
};
});
}, BufferedReader::close);
@Override
public String next() {
if (record != null) {
return record;
}
throw new IllegalStateException("null line found.");
}
}),
BufferedReader::close);
}

private BufferedReader getReader(String asyncQueryID) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,11 @@ protected <T extends AsyncAPI> void cancelAsyncAPI(Class<T> type) {

//AsyncAPI IDs that need to be cancelled
Set<String> queryIDsToCancel = queryUUIDsToCancel.stream()
.map(uuid -> {
return StreamSupport.stream(asyncAPIIterable.spliterator(), false)
.map(uuid -> StreamSupport
.stream(asyncAPIIterable.spliterator(), false)
.filter(query -> query.getRequestId().equals(uuid.toString()))
.map(T::getId)
.findFirst().orElseThrow(IllegalStateException::new);
})
.findFirst().orElseThrow(IllegalStateException::new))
.collect(Collectors.toSet());

//Cancel Transactions
Expand Down
8 changes: 4 additions & 4 deletions elide-core/src/main/java/com/yahoo/elide/core/Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,12 @@ private PathElement resolvePathAttribute(Type<?> entityClass,
|| fieldName.equals(dictionary.getIdFieldName(entityClass))) {
Type<?> attributeClass = dictionary.getType(entityClass, fieldName);
return new PathElement(entityClass, attributeClass, fieldName, alias, arguments);
} else if ("this".equals(fieldName)) {
}
if ("this".equals(fieldName)) {
return new PathElement(entityClass, null, fieldName);
} else {
String entityAlias = dictionary.getJsonAliasFor(entityClass);
throw new InvalidValueException(entityAlias + " does not contain the field " + fieldName);
}
String entityAlias = dictionary.getJsonAliasFor(entityClass);
throw new InvalidValueException(entityAlias + " does not contain the field " + fieldName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ public Resource toResource() {
* @return The Resource
*/
public Resource toResource(EntityProjection projection) {
return toResource(() -> { return getRelationships(projection); }, this::getAttributes);
return toResource(() -> getRelationships(projection), this::getAttributes);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,8 @@ private static MultivaluedMap<String, String> getFilterParams(MultivaluedMap<Str

queryParams.entrySet()
.stream()
.filter((entry) -> entry.getKey().startsWith("filter"))
.forEach((entry) -> {
returnMap.put(entry.getKey(), entry.getValue());
});
.filter(entry -> entry.getKey().startsWith("filter"))
.forEach(entry -> returnMap.put(entry.getKey(), entry.getValue()));
return returnMap;
}

Expand Down Expand Up @@ -506,8 +504,7 @@ private void registerPreSecurityObservers() {
public String getRequestHeaderByName(String headerName) {
if (this.requestHeaders.get(headerName) == null) {
return null;
} else {
return this.requestHeaders.get(headerName).get(0);
}
return this.requestHeaders.get(headerName).get(0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,17 @@ public class InMemoryStoreTransaction implements DataStoreTransaction {
private static final Comparator<Object> NULL_SAFE_COMPARE = (a, b) -> {
if (a == null && b == null) {
return 0;
} else if (a == null) {
}
if (a == null) {
return -1;
} else if (b == null) {
}
if (b == null) {
return 1;
} else if (a instanceof Comparable) {
}
if (a instanceof Comparable) {
return ((Comparable) a).compareTo(b);
} else {
throw new IllegalStateException("Trying to comparing non-comparable types!");
}
throw new IllegalStateException("Trying to comparing non-comparable types!");
};

/**
Expand Down Expand Up @@ -113,9 +115,8 @@ public Object loadObject(EntityProjection projection,
if (projection.getFilterExpression() == null
|| tx.supportsFiltering(scope, Optional.empty(), projection) == FeatureSupport.FULL) {
return tx.loadObject(projection, id, scope);
} else {
return DataStoreTransaction.super.loadObject(projection, id, scope);
}
return DataStoreTransaction.super.loadObject(projection, id, scope);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,8 @@ public static Type<?> getFieldType(Type<?> parentClass,
Type type;
if (fieldOrMethod instanceof Field) {
return ((Field) fieldOrMethod).getParameterizedType(parentClass, index);
} else {
return ((Method) fieldOrMethod).getParameterizedReturnType(parentClass, index);
}
return ((Method) fieldOrMethod).getParameterizedReturnType(parentClass, index);
}

private void bindTriggerIfPresent(AccessibleObject fieldOrMethod) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1832,12 +1832,10 @@ public String getAnnotatedColumnName(Type<?> cls, String fieldName) {
if (column == null || column.length == 0) {
if (joinColumn == null || joinColumn.length == 0) {
return fieldName;
} else {
return joinColumn[0].name();
}
} else {
return column[0].name();
return joinColumn[0].name();
}
return column[0].name();
}

/**
Expand Down Expand Up @@ -1870,9 +1868,8 @@ public static String getEntityName(Type<?> modelClass) {
Entity entity = (Entity) getFirstAnnotation(declaringClass, Arrays.asList(Entity.class));
if (entity == null || "".equals(entity.name())) {
return StringUtils.uncapitalize(declaringClass.getSimpleName());
} else {
return entity.name();
}
return entity.name();
}

public static <T> Type<T> getType(T object) {
Expand Down
25 changes: 11 additions & 14 deletions elide-core/src/main/java/com/yahoo/elide/core/filter/Operator.java
Original file line number Diff line number Diff line change
Expand Up @@ -444,11 +444,10 @@ private static <T> Predicate<T> getComparator(Path fieldPath, List<Object> value

if (fieldVal instanceof Collection) {
return ((Collection) fieldVal).stream()
.anyMatch((fieldValueElement) -> {
return fieldValueElement != null
&& values.stream()
.anyMatch(testVal -> condition.test(compare(fieldValueElement, testVal)));
});
.anyMatch(fieldValueElement ->
fieldValueElement != null
&& values.stream()
.anyMatch(testVal -> condition.test(compare(fieldValueElement, testVal))));
}

return fieldVal != null
Expand All @@ -474,16 +473,14 @@ private static boolean evaluate(Object entity, Path fieldPath, List<Object> valu

if (leftHandSide instanceof Collection && !valueClass.isAssignableFrom(COLLECTION_TYPE)) {
return ((Collection) leftHandSide).stream()
.anyMatch((leftHandSideElement) -> {
return values.stream()
.map(value -> CoerceUtil.coerce(value, valueClass))
.anyMatch(value -> predicate.test(leftHandSideElement, value));
});
} else {
return leftHandSide != null && values.stream()
.map(value -> CoerceUtil.coerce(value, valueClass))
.anyMatch(value -> predicate.test(leftHandSide, value));
.anyMatch(leftHandSideElement ->
values.stream()
.map(value -> CoerceUtil.coerce(value, valueClass))
.anyMatch(value -> predicate.test(leftHandSideElement, value)));
}
return leftHandSide != null && values.stream()
.map(value -> CoerceUtil.coerce(value, valueClass))
.anyMatch(value -> predicate.test(leftHandSide, value));
}

public Operator negate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,8 @@ private Path buildAttribute(Type rootEntityType, String attributeName) {
if (attribute != null) {
return new Path(rootEntityType, dictionary, attribute.getName(),
attribute.getAlias(), attribute.getArguments());
} else {
return buildPath(rootEntityType, attributeName);
}
return buildPath(rootEntityType, attributeName);
}

private Path buildPath(Type rootEntityType, String selector) {
Expand Down Expand Up @@ -435,13 +434,17 @@ public FilterExpression visit(ComparisonNode node, Type entityType) {
.collect(Collectors.toList());
if (op.equals(RSQLOperators.EQUAL) || op.equals(RSQLOperators.IN)) {
return equalityExpression(arguments.get(0), path, values, true);
} else if (op.equals(INI)) {
}
if (op.equals(INI)) {
return equalityExpression(arguments.get(0), path, values, false);
} else if (op.equals(RSQLOperators.NOT_EQUAL) || op.equals(RSQLOperators.NOT_IN)) {
}
if (op.equals(RSQLOperators.NOT_EQUAL) || op.equals(RSQLOperators.NOT_IN)) {
return new NotFilterExpression(equalityExpression(arguments.get(0), path, values, true));
} else if (op.equals(NOT_INI)) {
}
if (op.equals(NOT_INI)) {
return new NotFilterExpression(equalityExpression(arguments.get(0), path, values, false));
} else if (OPERATOR_MAP.containsKey(op)) {
}
if (OPERATOR_MAP.containsKey(op)) {
return new FilterPredicate(path, OPERATOR_MAP.get(op), values);
}

Expand Down Expand Up @@ -475,8 +478,8 @@ private FilterExpression equalityExpression(String argument, Path path,
}

Boolean isStringLike = path.lastElement()
.map(e -> e.getFieldType().isAssignableFrom(STRING_TYPE))
.orElse(false);
.filter(e -> e.getFieldType().isAssignableFrom(STRING_TYPE))
.isPresent();
if (isStringLike) {
Operator op = caseSensitive
? caseSensitivityStrategy.mapOperator(Operator.IN)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ public class AndFilterExpression implements FilterExpression {
public static FilterExpression fromPair(FilterExpression left, FilterExpression right) {
if (left != null && right != null) {
return new AndFilterExpression(left, right);
} else if (left == null) {
}
if (left == null) {
return right;
}
return left;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public class OrFilterExpression implements FilterExpression {
public static FilterExpression fromPair(FilterExpression left, FilterExpression right) {
if (left != null && right != null) {
return new OrFilterExpression(left, right);
} else if (left == null) {
}
if (left == null) {
return right;
}
return left;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public static boolean toManyInPathExceptLastPathElement(EntityDictionary diction

public static boolean isLastPathElementAssignableFrom(EntityDictionary dictionary, Path path, Type<?> clz) {
return path.lastElement()
.map(last ->
.filter(last ->
clz.isAssignableFrom(
dictionary.getType(last.getType(), last.getFieldName())
))
.orElse(false);
.isPresent();
}

public FilterPredicate(PathElement pathElement, Operator op, List<Object> values) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public class Common {
public static class FieldSetToNull<T> extends OperationCheck<T> {
@Override
public boolean ok(T record, RequestScope requestScope, Optional<ChangeSpec> changeSpec) {
return changeSpec.map((c) -> { return c.getModified() == null; })
.orElse(false);
return changeSpec.filter(c -> c.getModified() == null).isPresent();
}
}
}
Loading

0 comments on commit e2005d6

Please sign in to comment.