Java Assert assertOid(final String s)

Here you can find the source of assertOid(final String s)

Description

assert Oid

License

Open Source License

Declaration

public final static void assertOid(final String s) 

Method Source Code

//package com.java2s;
/**//from   ww  w. j ava  2s .c om
 * The contents of this file are subject to the Regenstrief Public License
 * Version 1.0 (the "License"); you may not use this file except in compliance with the License.
 * Please contact Regenstrief Institute if you would like to obtain a copy of the license.
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 *
 * Copyright (C) Regenstrief Institute.  All Rights Reserved.
 */

public class Main {
    public final static void assertOid(final String s) {
        if ((s != null) && !isOid(s)) {
            throw new RuntimeException("Expected an OID but found " + s);
        }
    }

    /**
     * Retrieves whether the given String is an OID
     * 
     * @param s the String
     * @return whether the String is an OID
     **/
    public final static boolean isOid(final String s) {
        int size = length(s);
        boolean hasDot = false, prevDot;

        if (size == 0) {
            return false;
        } else if (!isDigit(s.charAt(0))) {
            return false;
        }
        prevDot = false;

        size--;
        if (!isDigit(s.charAt(size))) {
            return false;
        }

        for (int i = 1; i < size; i++) {
            final char c = s.charAt(i);
            if (c == '.') {
                if (prevDot) {
                    return false;
                }
                hasDot = true;
                prevDot = true;
            } else if (!isDigit(c)) {
                return false;
            } else {
                prevDot = false;
            }
        }

        return hasDot;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final Object array) {
        if (array == null) {
            return 0;
        } else if (array instanceof byte[]) {
            return ((byte[]) array).length;
        } else if (array instanceof short[]) {
            return ((short[]) array).length;
        } else if (array instanceof int[]) {
            return ((int[]) array).length;
        } else if (array instanceof long[]) {
            return ((long[]) array).length;
        } else if (array instanceof float[]) {
            return ((float[]) array).length;
        } else if (array instanceof double[]) {
            return ((double[]) array).length;
        } else if (array instanceof boolean[]) {
            return ((boolean[]) array).length;
        } else if (array instanceof char[]) {
            return ((char[]) array).length;
        }
        return ((Object[]) array).length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final Object[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final byte[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Returns the length of the given array
     * 
     * @param array the array for which to return the length
     * @return the length
     **/
    public final static int length(final int[] array) {
        return array == null ? 0 : array.length;
    }

    /**
     * Retrieves the length of the CharSequence
     * 
     * @param s the CharSequence
     * @return the length
     **/
    public final static int length(final CharSequence s) {
        return s == null ? 0 : s.length();
    }

    /**
     * Retrieves whether the given character is in the range from '0' to '9' (more restrictive than
     * Character.isDigit)
     * 
     * @param c the character
     * @return whether the given character is in the range from '0' to '9'
     **/
    public final static boolean isDigit(final char c) {
        return (c >= '0') && (c <= '9');
    }
}

Related

  1. assertNotTrue(boolean conditionThatMustNotBeTrue, String msgWhenTrue)
  2. assertNotTrue(boolean value, String message)
  3. assertObjectEmpty(Object value)
  4. assertObjEquals(Object expected, Object actual)
  5. assertOffsetLengthValid(int offset, int length, int arrayLength)
  6. assertOneBitMask(long mask)
  7. assertParameter(Class expectedClass, Object[] params, int index)
  8. assertParameterInRange(long param, long lower, long upper)
  9. assertParameterWithinBounds(String name, long lower, long upper, long parameter)