Source Code : An assertion utility just for simple checks.

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

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(1, 2, 3);
    } 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"));
  }
}

   
    
    
    
    
    
    
    
  

Thank with us