001:
002: /*
003: * The JTS Topology Suite is a collection of Java classes that
004: * implement the fundamental operations required to validate a given
005: * geo-spatial data set to a known topological specification.
006: *
007: * Copyright (C) 2001 Vivid Solutions
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022: *
023: * For more information, contact:
024: *
025: * Vivid Solutions
026: * Suite #1A
027: * 2328 Government Street
028: * Victoria BC V8T 5G5
029: * Canada
030: *
031: * (250)385-6040
032: * www.vividsolutions.com
033: */
034: package com.vividsolutions.jts.util;
035:
036: import com.vividsolutions.jts.util.AssertionFailedException;
037:
038: /**
039: * A utility for making programming assertions.
040: *
041: *@version 1.7
042: */
043: public class Assert {
044:
045: /**
046: * Throws an <code>AssertionFailedException</code> if the given assertion is
047: * not true.
048: *
049: *@param assertion a condition that is supposed to be true
050: *@throws AssertionFailedException if the condition is false
051: */
052: public static void isTrue(boolean assertion) {
053: isTrue(assertion, null);
054: }
055:
056: /**
057: * Throws an <code>AssertionFailedException</code> with the given message if
058: * the given assertion is not true.
059: *
060: *@param assertion a condition that is supposed to be true
061: *@param message a description of the assertion
062: *@throws AssertionFailedException if the condition is false
063: */
064: public static void isTrue(boolean assertion, String message) {
065: if (!assertion) {
066: if (message == null) {
067: throw new AssertionFailedException();
068: } else {
069: throw new AssertionFailedException(message);
070: }
071: }
072: }
073:
074: /**
075: * Throws an <code>AssertionFailedException</code> if the given objects are
076: * not equal, according to the <code>equals</code> method.
077: *
078: *@param expectedValue the correct value
079: *@param actualValue the value being checked
080: *@throws AssertionFailedException if the two objects are not equal
081: */
082: public static void equals(Object expectedValue, Object actualValue) {
083: equals(expectedValue, actualValue, null);
084: }
085:
086: /**
087: * Throws an <code>AssertionFailedException</code> with the given message if
088: * the given objects are not equal, according to the <code>equals</code>
089: * method.
090: *
091: *@param expectedValue the correct value
092: *@param actualValue the value being checked
093: *@param message a description of the assertion
094: *@throws AssertionFailedException if the two objects are not equal
095: */
096: public static void equals(Object expectedValue, Object actualValue,
097: String message) {
098: if (!actualValue.equals(expectedValue)) {
099: throw new AssertionFailedException("Expected "
100: + expectedValue + " but encountered " + actualValue
101: + (message != null ? ": " + message : ""));
102: }
103: }
104:
105: /**
106: * Always throws an <code>AssertionFailedException</code>.
107: *
108: *@throws AssertionFailedException thrown always
109: */
110: public static void shouldNeverReachHere() {
111: shouldNeverReachHere(null);
112: }
113:
114: /**
115: * Always throws an <code>AssertionFailedException</code> with the given
116: * message.
117: *
118: *@param message a description of the assertion
119: *@throws AssertionFailedException thrown always
120: */
121: public static void shouldNeverReachHere(String message) {
122: throw new AssertionFailedException("Should never reach here"
123: + (message != null ? ": " + message : ""));
124: }
125: }
|