001// ***************************************************************************************************************************
002// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements.  See the NOTICE file *
003// * distributed with this work for additional information regarding copyright ownership.  The ASF licenses this file        *
004// * to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance            *
005// * with the License.  You may obtain a copy of the License at                                                              *
006// *                                                                                                                         *
007// *  http://www.apache.org/licenses/LICENSE-2.0                                                                             *
008// *                                                                                                                         *
009// * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an  *
010// * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the        *
011// * specific language governing permissions and limitations under the License.                                              *
012// ***************************************************************************************************************************
013package org.apache.juneau.assertions;
014
015import org.apache.juneau.internal.*;
016
017/**
018 * Used for fluent assertion calls against comparable objects.
019 *
020 * @param <R> The return type.
021 */
022@FluentSetters(returns="FluentComparableAssertion<R>")
023@SuppressWarnings("rawtypes")
024public class FluentComparableAssertion<R> extends FluentObjectAssertion<R> {
025
026   private final Comparable value;
027
028   /**
029    * Constructor.
030    *
031    * @param value The value being tested.
032    * @param returns The object to return after the test.
033    */
034   public FluentComparableAssertion(Comparable value, R returns) {
035      this(null, value, returns);
036   }
037
038   /**
039    * Constructor.
040    *
041    * @param creator The assertion that created this assertion.
042    * @param value The value being tested.
043    * @param returns The object to return after the test.
044    */
045   public FluentComparableAssertion(Assertion creator, Comparable value, R returns) {
046      super(creator, value, returns);
047      this.value = value;
048   }
049
050   /**
051    * Asserts that the value is greater than the specified value.
052    *
053    * @param value The values to check against.
054    * @return The response object (for method chaining).
055    * @throws AssertionError If assertion failed.
056    */
057   public R isGreaterThan(Comparable value) throws AssertionError {
058      exists();
059      assertNotNull("value", value);
060      if (compareTo(value) <= 0)
061         throw error("Value was not greater than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value);
062      return returns();
063   }
064
065   /**
066    * Asserts that the value is greater than the specified value.
067    *
068    * <p>
069    * Equivalent to {@link #isGreaterThan(Comparable)}
070    *
071    * @param value The values to check against.
072    * @return The response object (for method chaining).
073    * @throws AssertionError If assertion failed.
074    */
075   public R isGt(Comparable value) throws AssertionError {
076      return isGreaterThan(value);
077   }
078
079   /**
080    * Asserts that the value is greater than or equal to the specified value.
081    *
082    * @param value The values to check against.
083    * @return The response object (for method chaining).
084    * @throws AssertionError If assertion failed.
085    */
086   public R isGreaterThanOrEqual(Comparable value) throws AssertionError {
087      exists();
088      assertNotNull("value", value);
089      if (compareTo(value) < 0)
090            throw error("Value was not greater than or equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value);
091      return returns();
092   }
093
094   /**
095    * Asserts that the value is greater than or equal to the specified value.
096    *
097    * <p>
098    * Equivalent to {@link #isGreaterThanOrEqual(Comparable)}
099    *
100    * @param value The values to check against.
101    * @return The response object (for method chaining).
102    * @throws AssertionError If assertion failed.
103    */
104   public R isGte(Comparable value) throws AssertionError {
105      return isGreaterThanOrEqual(value);
106   }
107
108   /**
109    * Asserts that the value is less than the specified value.
110    *
111    * @param value The values to check against.
112    * @return The response object (for method chaining).
113    * @throws AssertionError If assertion failed.
114    */
115   public R isLessThan(Comparable value) throws AssertionError {
116      exists();
117      assertNotNull("value", value);
118      if (compareTo(value) >= 0)
119            throw error("Value was not less than expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value);
120      return returns();
121   }
122
123   /**
124    * Asserts that the value is less than the specified value.
125    *
126    * <p>
127    * Equivalent to {@link #isLessThan(Comparable)}
128    *
129    * @param value The values to check against.
130    * @return The response object (for method chaining).
131    * @throws AssertionError If assertion failed.
132    */
133   public R isLt(Comparable value) throws AssertionError {
134      return isLessThan(value);
135   }
136
137   /**
138    * Asserts that the value is less than or equals to the specified value.
139    *
140    * @param value The values to check against.
141    * @return The response object (for method chaining).
142    * @throws AssertionError If assertion failed.
143    */
144   public R isLessThanOrEqual(Comparable value) throws AssertionError {
145      exists();
146      assertNotNull("value", value);
147      if (compareTo(value) > 0)
148            throw error("Value was not less than or equals to expected.\n\tExpected=[{0}]\n\tActual=[{1}]", value, this.value);
149      return returns();
150   }
151
152   /**
153    * Asserts that the value is less than or equals to the specified value.
154    *
155    * <p>
156    * Equivalent to {@link #isLessThanOrEqual(Comparable)}
157    *
158    * @param value The values to check against.
159    * @return The response object (for method chaining).
160    * @throws AssertionError If assertion failed.
161    */
162   public R isLte(Comparable value) throws AssertionError {
163      return isLessThanOrEqual(value);
164   }
165
166   /**
167    * Asserts that the value is between (inclusive) the specified upper and lower values.
168    *
169    * @param lower The lower value to check against.
170    * @param upper The upper value to check against.
171    * @return The response object (for method chaining).
172    * @throws AssertionError If assertion failed.
173    */
174   public R isBetween(Comparable lower, Comparable upper) throws AssertionError {
175      exists();
176      assertNotNull("lower", lower);
177      assertNotNull("upper", upper);
178      isLessThanOrEqual(upper);
179      isGreaterThanOrEqual(lower);
180      return returns();
181   }
182
183   /**
184    * Perform a comparison with the specified object.
185    *
186    * @param value The object to compare against.
187    * @return The comparison value.
188    */
189   protected int compareTo(Object value) {
190      return this.value.compareTo(equivalent(value));
191   }
192
193   // <FluentSetters>
194
195   @Override /* GENERATED - Assertion */
196   public FluentComparableAssertion<R> msg(String msg, Object...args) {
197      super.msg(msg, args);
198      return this;
199   }
200
201   @Override /* GENERATED - Assertion */
202   public FluentComparableAssertion<R> stderr() {
203      super.stderr();
204      return this;
205   }
206
207   @Override /* GENERATED - Assertion */
208   public FluentComparableAssertion<R> stdout() {
209      super.stdout();
210      return this;
211   }
212
213   // </FluentSetters>
214}