Improved: Refactor boolean returns from methods

(OFBIZ-10725)
Improved boolean returns with a single statement, replacing if blocks with the explicit boolean return.
Improves code in methods:
isPrimaryKey() of GenericEntity
hasNext() and hasPrevious() of EntityListIterator
isValueActive() of EntityUtil

Additional change:
Improved return for getStartIndexFromViewIndex() method in EntityUtil. Instead of using if-else block, a ternary operator is used for improving readability.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/ofbiz-framework/trunk@1849741 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Aditya Sharma
2018-12-26 08:37:47 +00:00
parent f79f6fd797
commit aa44818396
3 changed files with 7 additions and 23 deletions
@@ -403,10 +403,7 @@ public class GenericEntity implements Map<String, Object>, LocalizedMap<Object>,
}
fieldKeys.remove(fieldName);
}
if (!fieldKeys.isEmpty()) {
return false;
}
return true;
return fieldKeys.isEmpty();
}
/** Returns true if the entity contains all of the primary key fields. */
@@ -292,12 +292,10 @@ public class EntityListIterator implements AutoCloseable, ListIterator<GenericVa
}
try {
if (resultSet.isLast() || resultSet.isAfterLast()) {
return false;
}
// do a quick game to see if the resultSet is empty:
// if we are not in the first or beforeFirst positions and we haven't made any values yet, the result set is empty so return false
return haveMadeValue || resultSet.isBeforeFirst() || resultSet.isFirst();
return !(resultSet.isLast() || resultSet.isAfterLast())
&& (haveMadeValue || resultSet.isBeforeFirst() || resultSet.isFirst());
} catch (SQLException e) {
tryCloseWithWarning("Warning: auto-closed EntityListIterator because of exception: " + e.toString());
throw new GeneralRuntimeException("Error while checking to see if this is the last result", e);
@@ -310,13 +308,10 @@ public class EntityListIterator implements AutoCloseable, ListIterator<GenericVa
*/
public boolean hasPrevious() {
try {
if (resultSet.isFirst() || resultSet.isBeforeFirst()) {
return false;
}
// do a quick game to see if the resultSet is empty:
// if we are not in the first or beforeFirst positions and we haven't made any values yet, the result set is
// empty so return false
return haveMadeValue || resultSet.isAfterLast() || resultSet.isLast();
return !(resultSet.isFirst() || resultSet.isBeforeFirst()) && (haveMadeValue || resultSet.isAfterLast() || resultSet.isLast());
} catch (SQLException e) {
tryCloseWithWarning("Warning: auto-closed EntityListIterator because of exception: " + e.toString());
throw new GeneralRuntimeException("Error while checking to see if this is the first result", e);
@@ -251,13 +251,8 @@ public final class EntityUtil {
public static boolean isValueActive(GenericValue datedValue, java.sql.Timestamp moment, String fromDateName, String thruDateName) {
java.sql.Timestamp fromDate = datedValue.getTimestamp(fromDateName);
java.sql.Timestamp thruDate = datedValue.getTimestamp(thruDateName);
if ((thruDate == null || thruDate.after(moment)) && (fromDate == null || fromDate.before(moment) || fromDate.equals(moment))) {
return true;
} else {
// else not active at moment
return false;
}
return (thruDate == null || thruDate.after(moment)) &&
(fromDate == null || fromDate.before(moment) || fromDate.equals(moment));
}
/**
@@ -517,10 +512,7 @@ public final class EntityUtil {
* @see EntityUtil#getPagedList
*/
public static int getStartIndexFromViewIndex(int viewIndex, int viewSize) {
if (viewIndex == 0) {
return 1;
}
return (viewIndex * viewSize) + 1;
return viewIndex == 0 ? 1 : (viewIndex * viewSize) + 1;
}
/**