View Javadoc
1   /*
2    * #%L
3    * Assertions.java - mongodb-async-driver - Allanbank Consulting, Inc.
4    * %%
5    * Copyright (C) 2011 - 2014 Allanbank Consulting, Inc.
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  
21  package com.allanbank.mongodb.util;
22  
23  /**
24   * Assertions provides common validation methods for the driver.
25   * 
26   * @api.no This class is <b>NOT</b> part of the drivers API. This class may be
27   *         mutated in incompatible ways between any two releases of the driver.
28   * @copyright 2012-2013, Allanbank Consulting, Inc., All Rights Reserved
29   */
30  public class Assertions {
31  
32      /**
33       * Throws an {@link IllegalArgumentException} if the {@code value} is
34       * <code>null</code> or an empty string.
35       * 
36       * @param value
37       *            The value to test.
38       * @param message
39       *            The message for the exception to throw.
40       * @throws IllegalArgumentException
41       *             In the case that the {@code value} is <code>null</code>.
42       */
43      public static void assertNotEmpty(final String value, final String message)
44              throws IllegalArgumentException {
45          if ((value == null) || value.trim().isEmpty()) {
46              throw new IllegalArgumentException(message);
47          }
48      }
49  
50      /**
51       * Throws an {@link IllegalArgumentException} if the {@code value} is
52       * <code>null</code>.
53       * 
54       * @param value
55       *            The value to test.
56       * @param message
57       *            The message for the exception to throw.
58       * @throws IllegalArgumentException
59       *             In the case that the {@code value} is <code>null</code>.
60       */
61      public static void assertNotNull(final Object value, final String message)
62              throws IllegalArgumentException {
63          if (value == null) {
64              throw new IllegalArgumentException(message);
65          }
66      }
67  
68      /**
69       * Throws an {@link IllegalArgumentException} if the {@code value} is
70       * <code>null</code>.
71       * 
72       * @param mustBeTrue
73       *            The value to test.
74       * @param message
75       *            The message for the exception to throw.
76       * @throws IllegalArgumentException
77       *             In the case that the {@code value} is <code>null</code>.
78       */
79      public static void assertThat(final boolean mustBeTrue, final String message)
80              throws IllegalArgumentException {
81          if (!mustBeTrue) {
82              throw new IllegalArgumentException(message);
83          }
84      }
85  
86      /**
87       * Creates a new Assertions.
88       */
89      private Assertions() {
90          // Static class.
91      }
92  }