Class Assert

  1  
  2  
  3 /*
  4  * Copyright (c) 2016 Vivid Solutions.
  5  *
  6  * All rights reserved. This program and the accompanying materials
  7  * are made available under the terms of the Eclipse Public License 2.0
  8  * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
  9  * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html
 10  * and the Eclipse Distribution License is available at
 11  *
 12  * http://www.eclipse.org/org/documents/edl-v10.php.
 13  */
 14 package org.locationtech.jts.util;
 15  
 16 /**
 17  *  A utility for making programming assertions.
 18  *
 19  *@version 1.7
 20  */
 21 public class Assert {
 22  
 23   /**
 24    *  Throws an <code>AssertionFailedException</code> if the given assertion is
 25    *  not true.
 26    *
 27    *@param  assertion                  a condition that is supposed to be true
 28    *@throws  AssertionFailedException  if the condition is false
 29    */
 30   public static void isTrue(boolean assertion) {
 31     isTrue(assertion, null);
 32   }
 33  
 34   /**
 35    *  Throws an <code>AssertionFailedException</code> with the given message if
 36    *  the given assertion is not true.
 37    *
 38    *@param  assertion                  a condition that is supposed to be true
 39    *@param  message                    a description of the assertion
 40    *@throws  AssertionFailedException  if the condition is false
 41    */
 42   public static void isTrue(boolean assertion, String message) {
 43     if (!assertion) {
 44       if (message == null) {
 45         throw new AssertionFailedException();
 46       }
 47       else {
 48         throw new AssertionFailedException(message);
 49       }
 50     }
 51   }
 52  
 53   /**
 54    *  Throws an <code>AssertionFailedException</code> if the given objects are
 55    *  not equal, according to the <code>equals</code> method.
 56    *
 57    *@param  expectedValue              the correct value
 58    *@param  actualValue                the value being checked
 59    *@throws  AssertionFailedException  if the two objects are not equal
 60    */
 61   public static void equals(Object expectedValue, Object actualValue) {
 62     equals(expectedValue, actualValue, null);
 63   }
 64  
 65   /**
 66    *  Throws an <code>AssertionFailedException</code> with the given message if
 67    *  the given objects are not equal, according to the <code>equals</code>
 68    *  method.
 69    *
 70    *@param  expectedValue              the correct value
 71    *@param  actualValue                the value being checked
 72    *@param  message                    a description of the assertion
 73    *@throws  AssertionFailedException  if the two objects are not equal
 74    */
 75   public static void equals(Object expectedValue, Object actualValue, String message) {
 76     if (!actualValue.equals(expectedValue)) {
 77       throw new AssertionFailedException("Expected " + expectedValue + " but encountered "
 78            + actualValue + (message != null ? ": " + message : ""));
 79     }
 80   }
 81  
 82   /**
 83    *  Always throws an <code>AssertionFailedException</code>.
 84    *
 85    *@throws  AssertionFailedException  thrown always
 86    */
 87   public static void shouldNeverReachHere() {
 88     shouldNeverReachHere(null);
 89   }
 90  
 91   /**
 92    *  Always throws an <code>AssertionFailedException</code> with the given
 93    *  message.
 94    *
 95    *@param  message                    a description of the assertion
 96    *@throws  AssertionFailedException  thrown always
 97    */
 98   public static void shouldNeverReachHere(String message) {
 99     throw new AssertionFailedException("Should never reach here"
100          + (message != null ? ": " + message : ""));
101   }
102 }
103  
104