- according to the design of all other IQueryParameterXXX types changed the methods setUpperBound() and setLowerBound() to return this, supporting method chaining.

- changed the order in which the state of a DateRangeParam instance got first modified and then verified.
- renamed the private method validateAndThrowDataFormatExceptionIfInvalid() to validateAndSet(DateParam, DateParam).
- replaced the methods haveLowerBound() and haveUpperBound() with the stateless method hasBound(DateParam).
This commit is contained in:
Gaetano Gallo 2018-02-20 13:47:24 +01:00
parent 72cf22e3a9
commit 01ff2e7c3b
1 changed files with 60 additions and 64 deletions

View File

@ -1,5 +1,9 @@
package ca.uhn.fhir.rest.param; package ca.uhn.fhir.rest.param;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.EQUAL;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.GREATERTHAN_OR_EQUALS;
import static ca.uhn.fhir.rest.param.ParamPrefixEnum.LESSTHAN_OR_EQUALS;
import static java.lang.String.format;
import static org.apache.commons.lang3.StringUtils.isNotBlank; import static org.apache.commons.lang3.StringUtils.isNotBlank;
/* /*
@ -86,21 +90,18 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
case STARTS_AFTER: case STARTS_AFTER:
case GREATERTHAN: case GREATERTHAN:
case GREATERTHAN_OR_EQUALS: case GREATERTHAN_OR_EQUALS:
myLowerBound = theDateParam; validateAndSet(theDateParam, null);
myUpperBound = null;
break; break;
case ENDS_BEFORE: case ENDS_BEFORE:
case LESSTHAN: case LESSTHAN:
case LESSTHAN_OR_EQUALS: case LESSTHAN_OR_EQUALS:
myLowerBound = null; validateAndSet(null, theDateParam);
myUpperBound = theDateParam;
break; break;
default: default:
// Should not happen // Should not happen
throw new InvalidRequestException("Invalid comparator for date range parameter:" + theDateParam.getPrefix() + ". This is a bug."); throw new InvalidRequestException("Invalid comparator for date range parameter:" + theDateParam.getPrefix() + ". This is a bug.");
} }
} }
validateAndThrowDataFormatExceptionIfInvalid();
} }
/** /**
@ -155,7 +156,7 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
} }
private void addParam(DateParam theParsed) throws InvalidRequestException { private void addParam(DateParam theParsed) throws InvalidRequestException {
if (theParsed.getPrefix() == null || theParsed.getPrefix() == ParamPrefixEnum.EQUAL) { if (theParsed.getPrefix() == null || theParsed.getPrefix() == EQUAL) {
if (myLowerBound != null || myUpperBound != null) { if (myLowerBound != null || myUpperBound != null) {
throw new InvalidRequestException("Can not have multiple date range parameters for the same param without a qualifier"); throw new InvalidRequestException("Can not have multiple date range parameters for the same param without a qualifier");
} }
@ -164,8 +165,8 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
myLowerBound = theParsed; myLowerBound = theParsed;
myUpperBound = theParsed; myUpperBound = theParsed;
} else { } else {
myLowerBound = new DateParam(ParamPrefixEnum.EQUAL, theParsed.getValueAsString()); myLowerBound = new DateParam(EQUAL, theParsed.getValueAsString());
myUpperBound = new DateParam(ParamPrefixEnum.EQUAL, theParsed.getValueAsString()); myUpperBound = new DateParam(EQUAL, theParsed.getValueAsString());
} }
} else { } else {
@ -254,7 +255,7 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
@Override @Override
public List<DateParam> getValuesAsQueryTokens() { public List<DateParam> getValuesAsQueryTokens() {
ArrayList<DateParam> retVal = new ArrayList<DateParam>(); ArrayList<DateParam> retVal = new ArrayList<>();
if (myLowerBound != null && myLowerBound.getMissing() != null) { if (myLowerBound != null && myLowerBound.getMissing() != null) {
retVal.add((myLowerBound)); retVal.add((myLowerBound));
} else { } else {
@ -268,21 +269,17 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
return retVal; return retVal;
} }
private boolean haveLowerBound() { private boolean hasBound(DateParam bound) {
return myLowerBound != null && myLowerBound.isEmpty() == false; return bound != null && !bound.isEmpty();
}
private boolean haveUpperBound() {
return myUpperBound != null && myUpperBound.isEmpty() == false;
} }
public boolean isEmpty() { public boolean isEmpty() {
return (getLowerBoundAsInstant() == null) && (getUpperBoundAsInstant() == null); return (getLowerBoundAsInstant() == null) && (getUpperBoundAsInstant() == null);
} }
public void setLowerBound(DateParam theLowerBound) { public DateRangeParam setLowerBound(DateParam theLowerBound) {
myLowerBound = theLowerBound; validateAndSet(theLowerBound, myUpperBound);
validateAndThrowDataFormatExceptionIfInvalid(); return this;
} }
/** /**
@ -298,9 +295,11 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
* theUpperBound may both be populated, or one may be null, but it is not valid for both to be null. * theUpperBound may both be populated, or one may be null, but it is not valid for both to be null.
*/ */
public void setRangeFromDatesInclusive(Date theLowerBound, Date theUpperBound) { public void setRangeFromDatesInclusive(Date theLowerBound, Date theUpperBound) {
myLowerBound = theLowerBound != null ? new DateParam(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, theLowerBound) : null; DateParam lowerBound = theLowerBound != null
myUpperBound = theUpperBound != null ? new DateParam(ParamPrefixEnum.LESSTHAN_OR_EQUALS, theUpperBound) : null; ? new DateParam(GREATERTHAN_OR_EQUALS, theLowerBound) : null;
validateAndThrowDataFormatExceptionIfInvalid(); DateParam upperBound = theUpperBound != null
? new DateParam(LESSTHAN_OR_EQUALS, theUpperBound) : null;
validateAndSet(lowerBound, upperBound);
} }
/** /**
@ -316,9 +315,7 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
* theUpperBound may both be populated, or one may be null, but it is not valid for both to be null. * theUpperBound may both be populated, or one may be null, but it is not valid for both to be null.
*/ */
public void setRangeFromDatesInclusive(DateParam theLowerBound, DateParam theUpperBound) { public void setRangeFromDatesInclusive(DateParam theLowerBound, DateParam theUpperBound) {
myLowerBound = theLowerBound; validateAndSet(theLowerBound, theUpperBound);
myUpperBound = theUpperBound;
validateAndThrowDataFormatExceptionIfInvalid();
} }
/** /**
@ -345,10 +342,9 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
upperBound = temp; upperBound = temp;
} }
} }
validateAndSet(
myLowerBound = lowerBound != null ? new DateParam(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, lowerBound) : null; lowerBound != null ? new DateParam(GREATERTHAN_OR_EQUALS, lowerBound) : null,
myUpperBound = upperBound != null ? new DateParam(ParamPrefixEnum.LESSTHAN_OR_EQUALS, upperBound) : null; upperBound != null ? new DateParam(LESSTHAN_OR_EQUALS, upperBound) : null);
validateAndThrowDataFormatExceptionIfInvalid();
} }
/** /**
@ -364,23 +360,27 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
* theUpperBound may both be populated, or one may be null, but it is not valid for both to be null. * theUpperBound may both be populated, or one may be null, but it is not valid for both to be null.
*/ */
public void setRangeFromDatesInclusive(String theLowerBound, String theUpperBound) { public void setRangeFromDatesInclusive(String theLowerBound, String theUpperBound) {
myLowerBound = theLowerBound != null ? new DateParam(ParamPrefixEnum.GREATERTHAN_OR_EQUALS, theLowerBound) : null; DateParam lowerBound = theLowerBound != null
myUpperBound = theUpperBound != null ? new DateParam(ParamPrefixEnum.LESSTHAN_OR_EQUALS, theUpperBound) : null; ? new DateParam(GREATERTHAN_OR_EQUALS, theLowerBound)
//FIXME potential null access on theLowerBound : null;
DateParam upperBound = theUpperBound != null
? new DateParam(LESSTHAN_OR_EQUALS, theUpperBound)
: null;
if (isNotBlank(theLowerBound) && isNotBlank(theUpperBound) && theLowerBound.equals(theUpperBound)) { if (isNotBlank(theLowerBound) && isNotBlank(theUpperBound) && theLowerBound.equals(theUpperBound)) {
myLowerBound.setPrefix(ParamPrefixEnum.EQUAL); lowerBound.setPrefix(EQUAL);
myUpperBound.setPrefix(ParamPrefixEnum.EQUAL); upperBound.setPrefix(EQUAL);
} }
validateAndThrowDataFormatExceptionIfInvalid(); validateAndSet(lowerBound, upperBound);
} }
public void setUpperBound(DateParam theUpperBound) { public DateRangeParam setUpperBound(DateParam theUpperBound) {
myUpperBound = theUpperBound; validateAndSet(myLowerBound, theUpperBound);
validateAndThrowDataFormatExceptionIfInvalid(); return this;
} }
@Override @Override
public void setValuesAsQueryTokens(FhirContext theContext, String theParamName, List<QualifiedParamList> theParameters) throws InvalidRequestException { public void setValuesAsQueryTokens(FhirContext theContext, String theParamName, List<QualifiedParamList> theParameters)
throws InvalidRequestException {
boolean haveHadUnqualifiedParameter = false; boolean haveHadUnqualifiedParameter = false;
for (QualifiedParamList paramList : theParameters) { for (QualifiedParamList paramList : theParameters) {
@ -418,14 +418,14 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
StringBuilder b = new StringBuilder(); StringBuilder b = new StringBuilder();
b.append(getClass().getSimpleName()); b.append(getClass().getSimpleName());
b.append("["); b.append("[");
if (haveLowerBound()) { if (hasBound(myLowerBound)) {
if (myLowerBound.getPrefix() != null) { if (myLowerBound.getPrefix() != null) {
b.append(myLowerBound.getPrefix().getValue()); b.append(myLowerBound.getPrefix().getValue());
} }
b.append(myLowerBound.getValueAsString()); b.append(myLowerBound.getValueAsString());
} }
if (haveUpperBound()) { if (hasBound(myUpperBound)) {
if (haveLowerBound()) { if (hasBound(myLowerBound)) {
b.append(" "); b.append(" ");
} }
if (myUpperBound.getPrefix() != null) { if (myUpperBound.getPrefix() != null) {
@ -433,7 +433,7 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
} }
b.append(myUpperBound.getValueAsString()); b.append(myUpperBound.getValueAsString());
} else { } else {
if (!haveLowerBound()) { if (!hasBound(myLowerBound)) {
b.append("empty"); b.append("empty");
} }
} }
@ -441,50 +441,46 @@ public class DateRangeParam implements IQueryParameterAnd<DateParam> {
return b.toString(); return b.toString();
} }
private void validateAndThrowDataFormatExceptionIfInvalid() { private void validateAndSet(DateParam lowerBound, DateParam upperBound) {
boolean haveLowerBound = haveLowerBound(); if (hasBound(lowerBound) && hasBound(upperBound)) {
boolean haveUpperBound = haveUpperBound(); if (lowerBound.getValue().getTime() > upperBound.getValue().getTime()) {
if (haveLowerBound && haveUpperBound) { throw new DataFormatException(format(
if (myLowerBound.getValue().getTime() > myUpperBound.getValue().getTime()) { "Lower bound of %s is after upper bound of %s",
StringBuilder b = new StringBuilder(); lowerBound.getValueAsString(), upperBound.getValueAsString()));
b.append("Lower bound of ");
b.append(myLowerBound.getValueAsString());
b.append(" is after upper bound of ");
b.append(myUpperBound.getValueAsString());
throw new DataFormatException(b.toString());
} }
} }
if (haveLowerBound) { if (hasBound(lowerBound)) {
if (myLowerBound.getPrefix() == null) { if (lowerBound.getPrefix() == null) {
myLowerBound.setPrefix(ParamPrefixEnum.GREATERTHAN_OR_EQUALS); lowerBound.setPrefix(GREATERTHAN_OR_EQUALS);
} }
switch (myLowerBound.getPrefix()) { switch (lowerBound.getPrefix()) {
case GREATERTHAN: case GREATERTHAN:
case GREATERTHAN_OR_EQUALS: case GREATERTHAN_OR_EQUALS:
default: default:
break; break;
case LESSTHAN: case LESSTHAN:
case LESSTHAN_OR_EQUALS: case LESSTHAN_OR_EQUALS:
throw new DataFormatException("Lower bound comparator must be > or >=, can not be " + myLowerBound.getPrefix().getValue()); throw new DataFormatException("Lower bound comparator must be > or >=, can not be " + lowerBound.getPrefix().getValue());
} }
} }
if (haveUpperBound) { if (hasBound(upperBound)) {
if (myUpperBound.getPrefix() == null) { if (upperBound.getPrefix() == null) {
myUpperBound.setPrefix(ParamPrefixEnum.LESSTHAN_OR_EQUALS); upperBound.setPrefix(LESSTHAN_OR_EQUALS);
} }
switch (myUpperBound.getPrefix()) { switch (upperBound.getPrefix()) {
case LESSTHAN: case LESSTHAN:
case LESSTHAN_OR_EQUALS: case LESSTHAN_OR_EQUALS:
default: default:
break; break;
case GREATERTHAN: case GREATERTHAN:
case GREATERTHAN_OR_EQUALS: case GREATERTHAN_OR_EQUALS:
throw new DataFormatException("Upper bound comparator must be < or <=, can not be " + myUpperBound.getPrefix().getValue()); throw new DataFormatException("Upper bound comparator must be < or <=, can not be " + upperBound.getPrefix().getValue());
} }
} }
myLowerBound = lowerBound;
myUpperBound = upperBound;
} }
} }