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:co.tinode.tindroid.ContactsFragment.java

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    try {/*from   w w  w . j  a  v a 2  s .  co  m*/
        // Assign callback listener which the holding activity must implement. This is used
        // so that when a contact item is interacted with (selected by the user) the holding
        // activity will be notified and can take further action such as populating the contact
        // detail pane (if in multi-pane layout) or starting a new activity with the contact
        // details (single pane layout).
        mOnContactSelectedListener = (OnContactsInteractionListener) context;
    } catch (ClassCastException e) {
        throw new ClassCastException(context.toString() + " must implement OnContactsInteractionListener");
    }
}

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

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);
    try {/*from   w w w. ja  v  a2  s . c  o  m*/
        bookmarkActionListener = (OnBookmarkActionListener) activity;
        bookmarkSelectedListener = (OnBookmarkSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnBookmarkActionListener and OnBookmarkSelectedListener");
    }
}

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

/**
 * This calculates the Cartesian distance using whatever units this
 * coordinate is in (degrees, maybe), and <b>DOES NOT TAKE INTO ACCOUNT</b>
 * the ellipsoid. It does, however check that the <code>srid</code>'s of the
 * two coordinates match and throws an ClassCastException if they don't.
 * //from   w  w w .  j av  a  2 s.c  om
 * @param otherCoord
 *            to get distance to.
 * @return the Cartesian, planer distance in <code>srid</code> units.
 * @throws ClassCastException
 *             if the <code>srid</code>'s don't match, or otherCoord is
 *             <code>null</code>.
 */
public double distanceCartesianInSRUnits(Coordinate otherCoord) {
    // Has to work if either srid is null or the same object.
    // .equalsSRID returns false if that is null or not both SRID~s are.
    if (this.equalsSRID(otherCoord)) {
        double diffLat = otherCoord.lat() - this.lat();
        double diffLon = otherCoord.lon() - this.lon();
        return Math.sqrt((diffLat * diffLat) + (diffLon * diffLon));
    } else {
        throw new ClassCastException(String.format("SRID~s do not match: %s and %s", this.toString(),
                null == otherCoord ? "(null)" : otherCoord.toString()));
    }
}

From source file:com.bosscs.spark.commons.utils.Utils.java

/**
 * Cast number type.//from  w  w w . j a va  2s . c  o m
 *
 * @param object the object
 * @param clazz  the clazz
 * @return object
 */
public static Object castNumberType(Object object, Class clazz) {

    if (Number.class.isAssignableFrom(clazz)) {
        //            AtomicInteger, AtomicLong, BigDecimal, BigInteger, Byte, Double, Float, Integer, Long, Short
        if (Double.class.isAssignableFrom(clazz)) {
            return ((Number) object).doubleValue();
        } else if (Long.class.isAssignableFrom(clazz)) {
            return ((Number) object).longValue();

        } else if (Float.class.isAssignableFrom(clazz)) {
            return ((Number) object).floatValue();

        } else if (Integer.class.isAssignableFrom(clazz)) {
            return ((Number) object).intValue();

        } else if (Short.class.isAssignableFrom(clazz)) {
            return ((Number) object).shortValue();

        } else if (Byte.class.isAssignableFrom(clazz)) {
            return ((Number) object).byteValue();

        } else if (BigInteger.class.isAssignableFrom(clazz)) {
            return BigInteger.valueOf(((Number) object).longValue());

        } else if (BigDecimal.class.isAssignableFrom(clazz)) {
            return BigDecimal.valueOf(((Number) object).longValue());

        } else if (AtomicLong.class.isAssignableFrom(clazz)) {
            return new AtomicLong(((Number) object).longValue());

        } else if (AtomicInteger.class.isAssignableFrom(clazz)) {
            return new AtomicInteger(((Number) object).intValue());
        }
    }
    throw new ClassCastException("it is not a Number Type" + object.getClass() + "|" + clazz);
}

From source file:com.bluecloud.ioc.core.KernelComponentDepender.java

/**
 * <h3>List?Set</h3>//from   w w w  .j a v a 2 s  .  c  o m
 * 
 * @param collection
 *            List?Set?
 * @param services
 *            ??
 * @param context
 *            ??Component?
 * @param limitedClass
 *            ?Class
 * @param limited
 *            ???
 * @return
 * @throws KernelComponentDependerException
 */
private Collection<Object> collectionType(Collection<Object> collection, ServicesMetadata services,
        ComponentContext context, Class<?> limitedClass, String limited)
        throws KernelComponentDependerException {
    for (ServiceMetadata service : services.getServices()) {
        Object serviceObject = this.getServiceDepend(service, context);
        if (limitedClass.isAssignableFrom(serviceObject.getClass())) {
            collection.add(serviceObject);
        } else {
            if (log.isErrorEnabled()) {
                log.error("?" + limited);
            }
            throw new ClassCastException(service.getClazz());
        }
    }
    return collection;
}

From source file:com.kaichaohulian.baocms.ecdemo.ui.chatting.ChattingFragment.java

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

From source file:de.uniba.wiai.lspi.chord.data.ID.java

/**
 * Compare current ID with the given object. If either the object is not a
 * ID or both IDs' lengths do not match, a ClassCastException is thrown.
 * Otherwise both IDs are compared byte by byte.
 * //from   ww w.  j  av  a2  s .c om
 * @return -1, 0, or 1, if this ID is smaller, same size, or greater than
 *         the given object, respectively.
 */
public final int compareTo(ID otherKey) throws ClassCastException {
    assert (id.size() >= kTotalBitLen); // size >= given minimum at creation.
    if (id.size() != otherKey.id.size()) {
        throw new ClassCastException("Only ID objects with same length can be " + "compared! This ID is "
                + id.size() + " bits long while the other ID is " + otherKey.id.size() + " bits long.");
    }

    for (int i = 0; i < kTotalBitLen; ++i) {
        if (id.get(i) != otherKey.id.get(i))
            return id.get(i) ? 1 : -1;
    }

    return 0;
}

From source file:com.jayway.restassured.internal.http.URIBuilder.java

/**
 * Implementation of Groovy's <code>as</code> operator, to allow type
 * conversion./*www . j a  va  2 s .c  o m*/
 *
 * @param type <code>URL</code>, <code>URL</code>, or <code>String</code>.
 * @return a representation of this URIBuilder instance in the given type
 * @throws MalformedURLException if <code>type</code> is URL and this
 *                               URIBuilder instance does not represent a valid URL.
 */
public Object asType(Class<?> type) throws MalformedURLException {
    if (type == URI.class)
        return this.toURI();
    if (type == URL.class)
        return this.toURL();
    if (type == String.class)
        return this.toString();
    throw new ClassCastException("Cannot cast instance of URIBuilder to class " + type);
}

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

/**
 * Calculates the distance (in meters) between two coordinates (assumed to
 * be expressed in a Cartesian system of coordinates: SRID 0).
 * //from   www. java 2  s. c  o  m
 * @param otherCoord
 * @return
 */
public double distanceCartesianInMeters(Coordinate otherCoord) {
    // SRID's must match and not be null.
    // This can't be null, duh, and we don't check if otherCoord is, which
    // means the NullPointerException will be thrown.
    if (null == this.srid() || null == otherCoord.srid()) {
        throw new ClassCastException("This distance function uses the spheroid distance, but you "
                + "are using Coordinates with null SRID~s (from a PostgreSQL "
                + "point?).  This doesn't really make any sense and you "
                + "probably want to use .distanceCarteasianInSRUnits( other )" + "instead.");
    } else if (!this.srid().equals(otherCoord.srid())) {
        throw new ClassCastException("The SRID of otherCoord does't match this one.");
    }
    if (0 != this.srid() || 0 != otherCoord.srid()) {
        throw new ClassCastException("Only SRID 0 is supported by this function.");
    }
    return this.distanceCartesianInSRUnits(otherCoord);
}

From source file:com.almende.eve.protocol.jsonrpc.JSONRpc.java

/**
 * Cast a JSONArray or JSONObject params to the desired paramTypes.
 * // w  w  w  .ja va 2 s  .  c  o  m
 * @param params
 *            the params
 * @param annotatedParams
 *            the annotated params
 * @param requestParams
 *            the request params
 * @return the object[]
 */
private static Object[] castParams(final Object realDest, final ObjectNode params,
        final List<AnnotatedParam> annotatedParams, final RequestParams requestParams) {

    switch (annotatedParams.size()) {
    case 0:
        if (realDest != null) {
            return new Object[] { realDest };
        } else {
            return new Object[0];
        }
        /* -- Unreachable, explicit no break -- */
    case 1:
        if (annotatedParams.get(0).getType().equals(ObjectNode.class)
                && annotatedParams.get(0).getAnnotations().size() == 0) {
            // the method expects one parameter of type JSONObject
            // feed the params object itself to it.
            if (realDest != null) {
                return new Object[] { realDest, params };
            } else {
                return new Object[] { params };
            }
        }
        /* -- Explicit no break -- */
    default:
        final ObjectNode paramsObject = (ObjectNode) params;
        int offset = 0;
        if (realDest != null) {
            offset = 1;
        }
        final Object[] objects = new Object[annotatedParams.size() + offset];
        if (realDest != null) {
            objects[0] = realDest;
        }
        for (int i = 0; i < annotatedParams.size(); i++) {
            final AnnotatedParam p = annotatedParams.get(i);

            final Annotation a = getRequestAnnotation(p, requestParams);
            if (a != null) {
                // this is a systems parameter
                objects[i + offset] = requestParams.get(a);
            } else {
                final String name = getName(p);
                if (name != null) {
                    // this is a named parameter
                    if (paramsObject.has(name)) {
                        objects[i + offset] = TypeUtil.inject(paramsObject.get(name), p.getGenericType());
                    } else {
                        if (isRequired(p)) {
                            throw new ClassCastException("Required parameter '" + name + "' missing.");
                        } else if (p.getType().isPrimitive()) {
                            // TODO: should this test be moved to
                            // isAvailable()?
                            throw new ClassCastException("Parameter '" + name + "' cannot be both optional and "
                                    + "a primitive type (" + p.getType().getSimpleName() + ")");
                        } else {
                            objects[i + offset] = null;
                        }
                    }
                } else {
                    // this is a problem
                    throw new ClassCastException("Name of parameter " + i + " not defined");
                }
            }
        }
        return objects;
    }
}