An assertion utility just for simple checks. : Assert « Language Basics « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Language Basics » Assert 




An assertion utility just for simple checks.
        
/*
 * Copyright 2008-2009 the T2 Project ant the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
//package org.t2framework.commons.util;

/**
 * An assertion utility just for simple checks.
 
 @author shot
 */
public class Assertion {

  /**
   * Assert whether the target is not null.If null, throw
   {@link NullPointerException}.
   
   @param <T>
   @param target
   @return
   */
  public static <T> T notNull(T target) {
    return notNull(target, null);
  }

  /**
   * Assert whether the target is not null with the message. If null, throw
   {@link NullPointerException}.
   
   @param <T>
   @param target
   @param message
   @return
   */
  public static <T> T notNull(T target, String message) {
    if (target == null) {
      throw new NullPointerException(message);
    }
    return target;
  }

  /**
   * Assert if the arguments are not null. If null, throw
   {@link NullPointerException}.
   
   @param <T>
   @param args
   @return
   */
  public static <T> T[] notNulls(T... args) {
    notNull(args);
    for (int i = 0; i < args.length; i++) {
      notNull(args[i]);
    }
    return args;
  }

  /**
   * Assert if the target is not null.This method returns true/false instead
   * of exception.
   
   @param <T>
   @param t
   @return
   */
  public static <T> boolean isNotNull(T t) {
    return (t != null);
  }

  /**
   * Assert if the target argument is not null.This method returns true/false
   * instead of exception.
   
   @param <T>
   @param args
   @return
   */
  public static <T> boolean hasNotNull(T... args) {
    return !hasNull(args);
  }

  /**
   * Assert if the target is null.This method returns true/false instead of
   * exception.
   
   @param <T>
   @param t
   @return
   */
  public static <T> boolean isNull(T t) {
    return (t == null);
  }

  /**
   * Assert if the argument contains null.This method returns true/false
   * instead of exception.
   
   @param <T>
   @param args
   @return
   */
  public static <T> boolean hasNull(T... args) {
    notNull(args);
    for (T t : args) {
      if (t == null) {
        return true;
      }
    }
    return false;
  }

  /**
   * Assert if the target argument is all null.
   
   @param <T>
   @param args
   @return
   */
  public static <T> boolean isAllNull(T... args) {
    notNull(args);
    for (T t : args) {
      if (t != null) {
        return false;
      }
    }
    return true;
  }

  public static void positive(int i) {
    if (i < 0) {
      throw new IllegalArgumentException();
    }
  }
}
---------------
/*
 * Copyright 2008-2009 the T2 Project ant the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.t2framework.commons.util;

import org.t2framework.commons.util.Assertion;

import junit.framework.TestCase;

/**
 @author shot
 */
public class AssertionTest extends TestCase {

  public void testNotNull() throws Exception {
    try {
      Assertion.notNull(null);
      fail();
    catch (NullPointerException e) {
    }
  }

  public void testNotNull2() throws Exception {
    try {
      String s = Assertion.notNull("hoge");
      assertEquals("hoge", s);
    catch (NullPointerException expected) {
      fail();
    }
  }

  public void testNotNulls() throws Exception {
    try {
      Assertion.notNulls("hoge""fuga"null);
      fail();
    catch (NullPointerException expected) {
    }
  }

  public void testNotNulls2() throws Exception {
    try {
      String s = null;
      Assertion.notNulls(s);
      fail();
    catch (NullPointerException expected) {
    }
  }

  public void testNotNulls3() throws Exception {
    try {
      Assertion.notNulls(123);
    catch (NullPointerException e) {
      fail();
    }
  }

  public void testIsNull1() throws Exception {
    assertTrue(Assertion.isNull(null));
    assertFalse(Assertion.isNull(""));
  }

  public void testHasNull1() throws Exception {
    assertTrue(Assertion.hasNull("a", null, "b"));
    assertTrue(Assertion.hasNull(null, "b"));
    assertTrue(Assertion.hasNull(null, null, null));
    assertTrue(Assertion.hasNull("a", null, null));
    assertFalse(Assertion.hasNull("a""b""c"));
  }
}

   
    
    
    
    
    
    
    
  














Related examples in the same category
1.A Program with Assertions
2.Enabling Assertions from the Command Line: -ea and -da enable and disable assertion in a package subtree or in a class.
3.Handling an Assertion Error
4.Catch assert exception with message
5.Assert with an informative message
6.Non-informative style of assert
7.Using the class loader to enable assertions
8.Class for checking syntax and comments for the assert
9.Assert Util
10.Assertion utility class that assists in validating arguments.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.