Example usage for java.lang IllegalStateException IllegalStateException

List of usage examples for java.lang IllegalStateException IllegalStateException

Introduction

In this page you can find the example usage for java.lang IllegalStateException IllegalStateException.

Prototype

public IllegalStateException(Throwable cause) 

Source Link

Document

Constructs a new exception with the specified cause and a detail message of (cause==null ?

Usage

From source file:Main.java

public static String getChildText(Element parent, String childName) {
    NodeList list = parent.getElementsByTagName(childName);
    if (list.getLength() > 1) {
        throw new IllegalStateException("Multiple child elements with name " + childName);
    } else if (list.getLength() == 0) {
        return null;
    }/*from w  ww  .j a v  a 2  s .  co  m*/
    Element child = (Element) list.item(0);
    return getText(child);
}

From source file:Main.java

/**
 * Get the current application context/*from   w w  w .jav a  2s  . c  o m*/
 * 
 * @return
 */
public static Context getApplicationContext() {
    if (sApplicationContext == null) {
        throw new IllegalStateException(
                "Context is empty. Don't forget to call setContext in the Application.onCreate() method");
    }
    return sApplicationContext;
}

From source file:Main.java

/**
 * Throws an IllegalStateException if this thread is the Swing Event Thread.
 * @throws IllegalStateException if this thread is the Swing Event Thread
 *//*from ww w  .  j  ava2 s  . c o  m*/
public static void throwIfEventThread() {
    if (SwingUtilities.isEventDispatchThread())
        throw new IllegalStateException("This method should NOT be called from the Swing Event Thread");
}

From source file:Main.java

private static DOMImplementationRegistry makeRegistry() {
    try {//w  ww .  j a  va 2 s  .co m
        return DOMImplementationRegistry.newInstance();
    } catch (ClassCastException e) {
        throw new IllegalStateException(e);
    } catch (ClassNotFoundException e) {
        throw new IllegalStateException(e);
    } catch (InstantiationException e) {
        throw new IllegalStateException(e);
    } catch (IllegalAccessException e) {
        throw new IllegalStateException(e);
    }
}

From source file:Main.java

private static void a(Context context) {
    Looper looper = Looper.myLooper();//from w w w .  j av a2 s. com
    if (looper != null && looper == context.getMainLooper()) {
        throw new IllegalStateException("calling this from your main thread can lead to deadlock");
    } else {
        return;
    }
}

From source file:Main.java

public static JFrame getOnlyVisibleFrame() {
    JFrame f = null;/* ww  w  . j av  a  2 s . co  m*/
    for (Window w : Window.getWindows())
        if (w instanceof JFrame && w.isShowing()) {
            if (f != null)
                throw new IllegalStateException("num frames > 1");
            f = (JFrame) w;
        }
    return f;
}

From source file:Main.java

/**
 * Finds the value that belongs to the given key in the given map.
 * If the key is null or no value is found, an {@link IllegalStateException} is thrown.
 *///ww w .  jav  a 2  s . c om
public static <T1, T2> T2 find(T1 key, Map<T1, T2> map) throws IllegalStateException {
    if (key == null)
        throw new IllegalStateException("Key is null");
    T2 ret = map.get(key);
    if (ret == null)
        throw new IllegalStateException("No value for key " + key);
    return ret;
}

From source file:Main.java

/**
 * verify that the object is not null. If it is, throws a new
 * IllegalStateException with the given message
 * /*from w  ww .j av a  2 s  . c om*/
 * @param object
 * @param message
 */
public static void notNull(Object object, String message) {
    if (object == null) {
        throw new IllegalStateException(message);
    }
}

From source file:Main.java

public static File getDestinationInExternalPublicDir(String dirType, String fileName) {
    File file = Environment.getExternalStoragePublicDirectory(dirType);
    if (file.exists()) {
        if (!file.isDirectory()) {
            throw new IllegalStateException(file.getAbsolutePath() + " already exists and is not a directory");
        }/*  w ww  .  ja  v  a2  s .co m*/
    } else {
        if (!file.mkdirs()) {
            throw new IllegalStateException("Unable to create directory: " + file.getAbsolutePath());
        }
    }

    //mDestinationUri = Uri.withAppendedPath(Uri.fromFile(file), subPath);
    /*File destFolder =  new File(file.getAbsolutePath() + File.separator + DOWNLOAD_FOLDER);
    if (destFolder.exists()) {
    if (!destFolder.isDirectory()) {
        throw new IllegalStateException(file.getAbsolutePath() +
                " already exists and is not a directory");
    }
    } else {
    if (!destFolder.mkdirs()) {
        throw new IllegalStateException("Unable to create directory: "+
                destFolder.getAbsolutePath());
    }
    }
            
    File destFile = new File(destFolder.getAbsolutePath() + File.separator + fileName);*/
    return new File(file.getAbsolutePath() + File.separator + fileName);
}

From source file:Main.java

public static <E> E first(Collection<E> collection) {

    if (isEmpty(collection)) {
        return null;
    }/*w ww. j  av a 2  s. c  om*/

    Iterator<E> iterator = collection.iterator();

    if (iterator.hasNext()) {
        return iterator.next();
    }

    throw new IllegalStateException("Should not happen");
}