Example usage for java.lang ClassCastException ClassCastException

List of usage examples for java.lang ClassCastException ClassCastException

Introduction

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

Prototype

public ClassCastException(String s) 

Source Link

Document

Constructs a ClassCastException with the specified detail message.

Usage

From source file:cz.lbenda.dataman.db.RowDesc.java

/** Return set value for given column */
public <T> void setColumnValue(ColumnDesc column, T value) {
    value = repairClassOfValue(column, value);

    if (AbstractHelper.nullEquals(oldValues[column.getPosition() - 1], value)) {
        return;/*from   w  ww.j  a v  a  2s .  c o  m*/
    }
    if (column.getDataType() == ColumnType.ARRAY) {
        LOG.warn("The editing of ARRAY isn't implemented yet");
        return;
    }

    if (value instanceof SingleSelectionModel) {
        throw new ClassCastException("The value of column can't be selection model type.");
    }
    setPropertyValue(column, value);

    if (RowDescState.LOADED == state
            && !AbstractHelper.nullEquals(getColumnValue(column), oldValues[column.getPosition() - 1])) {
        this.setState(RowDescState.CHANGED);
    } else if (state == RowDescState.CHANGED) {
        boolean noChanged = true;
        for (int i = 0; i < oldValues.length; i++) {
            noChanged = noChanged && AbstractHelper.nullEquals(newValues[i].getValue(), oldValues[i]);
        }
        if (noChanged) {
            setState(RowDescState.LOADED);
        }
    }
}

From source file:com.citrus.sample.WalletPaymentFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {/*from ww w.j  a v  a 2  s.  c  o  m*/
        mListener = (WalletFragmentListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement WalletFragmentListener");
    }
}

From source file:com.androzic.MapFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.e(TAG, "onAttach()");

    dimView = new RelativeLayout(getActivity());

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception
    try {/*from  w w  w .j a  v a 2 s.  c  om*/
        waypointActionsCallback = (OnWaypointActionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnWaypointActionListener");
    }
    try {
        routeActionsCallback = (OnRouteActionListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnRouteActionListener");
    }
}

From source file:org.apache.syncope.core.persistence.beans.user.SyncopeUser.java

@Override
public <T extends AbstractDerAttr> boolean removeDerivedAttribute(T derivedAttribute) {
    if (!(derivedAttribute instanceof UDerAttr)) {
        throw new ClassCastException(
                "attribute is expected to be typed UDerAttr: " + derivedAttribute.getClass().getName());
    }/*from   ww  w.  ja v  a2 s .  c  o m*/
    return derivedAttributes.remove((UDerAttr) derivedAttribute);
}

From source file:com.deliciousdroid.fragment.BrowseBookmarkFeedFragment.java

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {// w w  w.j a  va2s.  c om
        bookmarkSelectedListener = (OnBookmarkSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString() + " must implement OnBookmarkSelectedListener");
    }
}

From source file:de.cebitec.readXplorer.util.GeneralUtils.java

/**
 * Converts a given number into a number of the given classType. If this is
 * not possible, it throws a ClassCastException
 * @param <T> one of the classes derived from Number
 * @param classType the type to convert the number into
 * @param number the number to convert//w  w w .j  a va  2  s. c om
 * @return The converted number
 */
public static <T extends Number> T convertNumber(Class<T> classType, Number number) throws ClassCastException {
    T convertedValue = null;
    if (classType.equals(Integer.class)) {
        convertedValue = classType.cast(number.intValue());
    } else if (classType.equals(Double.class)) {
        convertedValue = classType.cast(number.doubleValue());
    } else if (classType.equals(Long.class)) {
        convertedValue = classType.cast(number.longValue());
    } else if (classType.equals(Float.class)) {
        convertedValue = classType.cast(number.floatValue());
    } else if (classType.equals(Short.class)) {
        convertedValue = classType.cast(number.shortValue());
    } else if (classType.equals(Byte.class)) {
        convertedValue = classType.cast(number.byteValue());
    }

    if (convertedValue == null) {
        throw new ClassCastException("Cannot cast the given number into the given format.");
    }

    return convertedValue;
}

From source file:grails.plugins.jesque.AdminImpl.java

/**
 * Executes the given job./*from www . j  ava  2s.c o m*/
 *
 * @param job      the job to execute
 * @param curQueue the queue the job came from
 * @param instance the materialized job
 * @return the result of the job execution
 * @throws Exception if the instance is a {@link Callable} and throws an exception
 */
protected Object execute(final Job job, final String curQueue, final Object instance) throws Exception {
    final Object result;
    if (instance instanceof WorkerAware) {
        ((WorkerAware) instance).setWorker(this.workerRef.get());
    }
    if (instance instanceof Callable) {
        result = ((Callable<?>) instance).call(); // The job is executing!
    } else if (instance instanceof Runnable) {
        ((Runnable) instance).run(); // The job is executing!
        result = null;
    } else { // Should never happen since we're testing the class earlier
        throw new ClassCastException("instance must be a Runnable or a Callable: "
                + instance.getClass().getName() + " - " + instance);
    }
    return result;
}

From source file:Util.java

/**
 * Checks an object implements the given class
 * /*from  w w  w  . j  a v a 2 s . co  m*/
 * @param context
 *          the context
 * @param name
 *          the name to lookup
 * @param object
 *          the object
 * @param clazz
 *          the expected type
 * @throws Exception
 *           for any error
 */
protected static void checkObject(Context context, String name, Object object, Class clazz) throws Exception {
    Class objectClass = object.getClass();
    if (clazz.isAssignableFrom(objectClass) == false) {
        StringBuffer buffer = new StringBuffer(100);
        buffer.append("Object at '").append(name);
        buffer.append("' in context ").append(context.getEnvironment());
        buffer.append(" is not an instance of ");
        appendClassInfo(buffer, clazz);
        buffer.append(" object class is ");
        appendClassInfo(buffer, object.getClass());
        throw new ClassCastException(buffer.toString());
    }
}

From source file:edu.berkeley.path.bots.core.Coordinate.java

/**
 * Defines ordering for Coordinate objects, SRID then lat then lon.<br/>
 * SRID is sorted with null larger than the integers, and for the lat and
 * lon values, {@link Double#NaN} is bigger than
 * {@link Double#POSITIVE_INFINITY} which is bigger than the integers
 * (including -0.0) with {@link Double#NEGATIVE_INFINITY} as the smallest.
 * <p/>/*from  w  ww .  j a  v  a2s .co m*/
 * Always returns one of -1, 0, or 1 to work within
 * {@link #equalsEpsilon(core.Coordinate)}.
 * 
 * @param otherCoord
 *            to compare to.
 * @return 0 if <code>srid</code>, <code>lat</code>, and <code>lon</code>
 *         (the combined fields, in that order) are all exactly equal.<br/>
 *         1 if combined fields (of this) are &ldquo;bigger&rdquo;.<br/>
 *         -1 if combined fields (of this) are &ldquo;smaller&rdquo;.
 * @throws ClassCastException
 *             if otherCoord is null
 */
@Override
public int compareTo(Coordinate otherCoord) {
    // Unlikely (very) to have two refs to the same instance,
    // so no need for the optimization of this == otherCoord.

    // Need to sort by SRID first, null is biggest
    if (!this.equalsSRID(otherCoord)) {
        // Only need to sort by SRID (unless otherCoord is null).
        if (null == otherCoord) {
            throw new ClassCastException("Arg otherCoord is null.");
        }
        if (null == this.srid()) {
            return 1;
        } else if (null == otherCoord.srid()) {
            return -1;
        }
        // Use .compareTo() in Integer.
        if (0 < this.srid().compareTo(otherCoord.srid())) {
            return 1;
        } else {
            return -1;
        }
    }
    // SRID the same, compare lat

    // otherCoord is gaurenteed to not be null by the above call
    // to .equalsSRID().

    // Need to use Double.compare() to deal with edge values (NaN and Inf).
    int cmpi = Double.compare(this.lat(), otherCoord.lat()); // otherCoord
                                                             // can't
                                                             // be null.

    if (0 < cmpi) {
        return 1;
    }
    if (0 > cmpi) {
        return -1;
    }

    // OK, now lat
    cmpi = Double.compare(this.lon(), otherCoord.lon());

    if (0 < cmpi) {
        return 1;
    }
    if (0 > cmpi) {
        return -1;
    }

    // Finally we can say they are the same.
    return 0;
}

From source file:com.ebaotech.salesplatform.commons.helper.SimpleSharedPreferences.java

@Override
public boolean getBoolean(String key, boolean defValue) throws ClassCastException {
    initiateSharedPreferences();//from  w w  w.  j  a v a 2  s. c o  m
    try {
        return mSharedPreferences.getBoolean(key, defValue);
    } catch (final ClassCastException e) {
        final String returnType = new Object() {
        }.getClass().getEnclosingMethod().getReturnType().toString();
        throw new ClassCastException("\n ======================================== \nClassCastException : " + key
                + "'s value is not a " + returnType + " \n ======================================== \n");
    }
}