/** Constructs a numeric refinement for the given attribute, operator and value. * @param attribute the attribute to refine on. * @param operator a {@link NumericRefinement#OPERATOR_LT comparison operator} to apply. * @param value the value to refine with. */ public NumericRefinement(@NonNull String attribute, int operator, double value) { checkOperatorIsValid(operator); this.operator = operator; this.value = value; this.attribute = attribute; }
/** * Gets the current numeric refinement for an attribute and an operator. * * @param attribute the attribute to refine on. * @param operator one of the {@link NumericRefinement#OPERATOR_EQ operators} defined in {@link NumericRefinement}. * @return a {@link NumericRefinement} describing the current refinement for these parameters, or {@code null} if there is none. */ @SuppressWarnings({"WeakerAccess", "unused"}) // For library users @Nullable public NumericRefinement getNumericRefinement(@NonNull String attribute, int operator) { NumericRefinement.checkOperatorIsValid(operator); final SparseArray<NumericRefinement> attributeRefinements = numericRefinements.get(attribute); return attributeRefinements == null ? null : attributeRefinements.get(operator); }
@Test public void checkOperatorIsValid() { int operator = NumericRefinement.OPERATOR_LT; try { NumericRefinement.checkOperatorIsValid(operator); operator = NumericRefinement.OPERATOR_LE; NumericRefinement.checkOperatorIsValid(operator); operator = NumericRefinement.OPERATOR_EQ; NumericRefinement.checkOperatorIsValid(operator); operator = NumericRefinement.OPERATOR_NE; NumericRefinement.checkOperatorIsValid(operator); operator = NumericRefinement.OPERATOR_GE; NumericRefinement.checkOperatorIsValid(operator); operator = NumericRefinement.OPERATOR_GT; NumericRefinement.checkOperatorIsValid(operator); } catch (IllegalStateException e) { fail("Operator " + operator + "should be considered valid"); } }
/** * Removes the given numeric refinement for the next queries. * * @param refinement a description of the refinement to remove. * @return this {@link Searcher} for chaining. */ @SuppressWarnings({"WeakerAccess", "unused"}) // For library users public Searcher removeNumericRefinement(@NonNull NumericRefinement refinement) { if (refinement.operator == NumericRefinement.OPERATOR_UNKNOWN) { numericRefinements.remove(refinement.attribute); } else { NumericRefinement.checkOperatorIsValid(refinement.operator); final SparseArray<NumericRefinement> attributeRefinements = numericRefinements.get(refinement.attribute); if (attributeRefinements != null) { attributeRefinements.remove(refinement.operator); } } rebuildQueryNumericFilters(); EventBus.getDefault().post(new NumericRefinementEvent(this, Operation.REMOVE, refinement)); return this; } //endregion
@Test(expected = IllegalStateException.class) public void checkOperatorIsInvalid() { int operator = -1; NumericRefinement.checkOperatorIsValid(operator); } }