List of usage examples for java.lang ClassCastException ClassCastException
public ClassCastException(String s)
ClassCastException
with the specified detail message. From source file:ca.sqlpower.wabit.dao.WorkspaceXMLDAO.java
/** * This saves a layout. This will not close the print writer passed into the constructor. * If this save method is used to export the query cache somewhere then close should be * called on it to flush the print writer and close it. *///from w w w . j a v a 2s .c o m private void saveLayout(Layout layout) { xml.print(out, "<layout"); printCommonAttributes(layout); printAttribute("zoom", layout.getZoomLevel()); printAttribute("template", (layout instanceof Template)); xml.niprintln(out, ">"); xml.indent++; Page page = layout.getPage(); xml.print(out, "<layout-page"); printCommonAttributes(page); printAttribute("height", page.getHeight()); printAttribute("width", page.getWidth()); printAttribute("orientation", page.getOrientation().name()); xml.niprintln(out, ">"); xml.indent++; saveFont(page.getDefaultFont()); if (layout instanceof Report) { for (Selector selector : ((Report) layout).getSelectors()) { saveSelector(selector); } } for (WabitObject object : page.getChildren()) { if (object instanceof ContentBox) { ContentBox box = (ContentBox) object; xml.print(out, "<content-box"); printCommonAttributes(box); printAttribute("width", box.getWidth()); printAttribute("height", box.getHeight()); printAttribute("xpos", box.getX()); printAttribute("ypos", box.getY()); xml.niprintln(out, ">"); xml.indent++; saveFont(box.getFont()); // Save ContentBox selectors. for (Selector selector : box.getChildren(Selector.class)) { saveSelector(selector); } if (box.getContentRenderer() != null) { if (box.getContentRenderer() instanceof WabitLabel) { WabitLabel label = (WabitLabel) box.getContentRenderer(); xml.print(out, "<content-label"); printCommonAttributes(label); printAttribute("horizontal-align", label.getHorizontalAlignment().name()); printAttribute("vertical-align", label.getVerticalAlignment().name()); if (label.getBackgroundColour() != null) { printAttribute("bg-colour", label.getBackgroundColour().getRGB()); } xml.niprintln(out, ">"); xml.indent++; xml.print(out, "<text>"); xml.niprint(out, SQLPowerUtils.escapeXML(label.getText())); xml.niprintln(out, "</text>"); saveFont(label.getFont()); xml.indent--; xml.println(out, "</content-label>"); } else if (box.getContentRenderer() instanceof ResultSetRenderer) { ResultSetRenderer rsRenderer = (ResultSetRenderer) box.getContentRenderer(); xml.print(out, "<content-result-set"); printCommonAttributes(rsRenderer); printAttribute("query-id", rsRenderer.getContent().getUUID()); printAttribute("null-string", rsRenderer.getNullString()); printAttribute("border", rsRenderer.getBorderType().name()); printAttribute("grand-totals", rsRenderer.isPrintingGrandTotals()); if (rsRenderer.getBackgroundColour() != null) { printAttribute("bg-colour", rsRenderer.getBackgroundColour().getRGB()); } printAttribute("header-colour", rsRenderer.getHeaderColour().getRGB()); printAttribute("data-colour", rsRenderer.getDataColour().getRGB()); xml.niprintln(out, ">"); xml.indent++; saveFont(rsRenderer.getHeaderFont(), "header-font"); saveFont(rsRenderer.getBodyFont(), "body-font"); for (WabitObject rendererChild : rsRenderer.getChildren()) { ColumnInfo ci = (ColumnInfo) rendererChild; xml.print(out, "<column-info"); printCommonAttributes(ci); printAttribute("width", ci.getWidth()); if (ci.getColumnInfoItem() != null) { printAttribute("column-info-item-id", ci.getColumnInfoItem().getUUID()); } printAttribute("column-alias", ci.getColumnAlias()); printAttribute("horizontal-align", ci.getHorizontalAlignment().name()); printAttribute("data-type", ci.getDataType().name()); printAttribute("group-or-break", ci.getWillGroupOrBreak().name()); printAttribute("will-subtotal", Boolean.toString(ci.getWillSubtotal())); xml.niprintln(out, ">"); xml.indent++; if (ci.getFormat() instanceof SimpleDateFormat) { xml.print(out, "<date-format"); SimpleDateFormat dateFormat = (SimpleDateFormat) ci.getFormat(); printAttribute("format", dateFormat.toPattern()); xml.niprintln(out, "/>"); } else if (ci.getFormat() instanceof DecimalFormat) { xml.print(out, "<decimal-format"); DecimalFormat decimalFormat = (DecimalFormat) ci.getFormat(); printAttribute("format", decimalFormat.toPattern()); xml.niprintln(out, "/>"); } else if (ci.getFormat() == null) { // This is a default format } else { throw new ClassCastException("Cannot cast format of type " + ci.getFormat().getClass() + " to a known format type when saving."); } xml.indent--; xml.println(out, "</column-info>"); } xml.indent--; xml.println(out, "</content-result-set>"); } else if (box.getContentRenderer() instanceof ImageRenderer) { ImageRenderer imgRenderer = (ImageRenderer) box.getContentRenderer(); xml.print(out, "<image-renderer"); printCommonAttributes(imgRenderer); if (imgRenderer.getImage() != null) { printAttribute("wabit-image-uuid", imgRenderer.getImage().getUUID()); } printAttribute("preserving-aspect-ratio", imgRenderer.isPreservingAspectRatio()); printAttribute("h-align", imgRenderer.getHAlign().name()); printAttribute("v-align", imgRenderer.getVAlign().name()); xml.niprint(out, ">"); out.println("</image-renderer>"); } else if (box.getContentRenderer() instanceof ChartRenderer) { ChartRenderer chartRenderer = (ChartRenderer) box.getContentRenderer(); xml.print(out, "<chart-renderer"); printCommonAttributes(chartRenderer); printAttribute("chart-uuid", chartRenderer.getContent().getUUID()); xml.println(out, " />"); } else if (box.getContentRenderer() instanceof CellSetRenderer) { CellSetRenderer renderer = (CellSetRenderer) box.getContentRenderer(); xml.print(out, "<cell-set-renderer"); printCommonAttributes(renderer); printAttribute("olap-query-uuid", renderer.getContent().getUUID()); printAttribute("body-alignment", renderer.getBodyAlignment().toString()); if (renderer.getBodyFormat() != null) { printAttribute("body-format-pattern", renderer.getBodyFormat().toPattern()); } xml.println(out, ">"); xml.indent++; saveFont(renderer.getHeaderFont(), "olap-header-font"); saveFont(renderer.getBodyFont(), "olap-body-font"); this.saveOlapQuery(renderer.getModifiedOlapQuery()); xml.indent--; xml.println(out, "</cell-set-renderer>"); } else { throw new ClassCastException( "Cannot save a content renderer of class " + box.getContentRenderer().getClass()); } } xml.indent--; xml.println(out, "</content-box>"); } else if (object instanceof Guide) { Guide guide = (Guide) object; xml.print(out, "<guide"); printCommonAttributes(guide); printAttribute("axis", guide.getAxis().name()); printAttribute("offset", guide.getOffset()); xml.niprintln(out, "/>"); } else { throw new ClassCastException("Cannot save page element of type " + object.getClass()); } } xml.indent--; xml.println(out, "</layout-page>"); xml.indent--; xml.println(out, "</layout>"); }
From source file:edu.berkeley.path.bots.core.Coordinate.java
/** * Implementation of Thaddeus Vincenty's algorithms to solve the direct and * inverse geodetic problems.//from ww w.j a va2s. c o m * <p/> * This implementation may be as good as the PostGIS provided one, the * errors seem to be pretty small (10 ^ -3 meters), but a thorough * check/analysis has not been done. * <p/> * For more information, see Vincenty's original publication on the NOAA * web-site: <a href="http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf"> * http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf</a> * * @param otherCoord * @return the distance calculated this way. * @throws ClassCastException * if the SRID~s don't match or are null. * @throws NullPointerException * if otherCoord is null. */ public double distanceVincentyInMeters(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."); } // else we are sure they are the same, and not null. final double a; final double b; final double f; // Set a, b, and f. switch (this.srid()) { case 4326: a = 6378137.0; b = 6356752.3142; f = (a - b) / a; break; default: throw new ClassCastException("The given SRID is not entered in this function, you can" + " get it by running the DB query defined in a function" + " in the NETCONFIG project called " + "util.SpatialDatabaseQueries.getSpheroidStringCached()" + ". This should return a spheroid string like:\n" + "\tSPHEROID(\"WGS 84\",6378137,298.257223563)\n" + "which is SPHEROID( desc, d1, d2 ), so then\n" + "\ta = d1\n" + "\tb = ( d2 * d1 - d1 ) / d2\n" + "\tf = 1 / d2\n" + "Then you can add the numbers to the switch statement" + " in the Java."); } // CHECKSTYLE:OFF // Now a, b, and f should be set. // // All constants below refer to Vincenty's publication (see above). // // get parameters as radians final double PiOver180 = Math.PI / 180.0; final double phi1 = PiOver180 * this.lat(); final double lambda1 = PiOver180 * this.lon(); final double phi2 = PiOver180 * otherCoord.lat(); final double lambda2 = PiOver180 * otherCoord.lon(); // calculations final double a2 = a * a; final double b2 = b * b; final double a2b2b2 = (a2 - b2) / b2; final double omega = lambda2 - lambda1; final double tanphi1 = FastMath.tan(phi1); final double tanU1 = (1.0 - f) * tanphi1; final double U1 = FastMath.atan(tanU1); final double sinU1 = FastMath.sin(U1); final double cosU1 = FastMath.cos(U1); final double tanphi2 = FastMath.tan(phi2); final double tanU2 = (1.0 - f) * tanphi2; final double U2 = FastMath.atan(tanU2); final double sinU2 = FastMath.sin(U2); final double cosU2 = FastMath.cos(U2); final double sinU1sinU2 = sinU1 * sinU2; final double cosU1sinU2 = cosU1 * sinU2; final double sinU1cosU2 = sinU1 * cosU2; final double cosU1cosU2 = cosU1 * cosU2; // Below are the re-assignable fields // eq. 13 double lambda = omega; // intermediates we'll need to compute 's' double A = 0.0; double B = 0.0; double sigma = 0.0; double deltasigma = 0.0; double lambda0; final int num_iters = 5; // 20 final double change_threshold = 1e-5; // 0.0000000000001; for (int i = 0; i < num_iters; i++) { lambda0 = lambda; final double sinlambda = FastMath.sin(lambda); final double coslambda = FastMath.cos(lambda); // eq. 14 final double sin2sigma = (cosU2 * sinlambda * cosU2 * sinlambda) + (cosU1sinU2 - sinU1cosU2 * coslambda) * (cosU1sinU2 - sinU1cosU2 * coslambda); final double sinsigma = FastMath.sqrt(sin2sigma); // eq. 15 final double cossigma = sinU1sinU2 + (cosU1cosU2 * coslambda); // eq. 16 sigma = FastMath.atan2(sinsigma, cossigma); // eq. 17 Careful! sin2sigma might be almost 0! final double sinalpha = (sin2sigma == 0) ? 0.0 : cosU1cosU2 * sinlambda / sinsigma; final double alpha = FastMath.asin(sinalpha); final double cosalpha = FastMath.cos(alpha); final double cos2alpha = cosalpha * cosalpha; // eq. 18 Careful! cos2alpha might be almost 0! final double cos2sigmam = cos2alpha == 0.0 ? 0.0 : cossigma - 2 * sinU1sinU2 / cos2alpha; final double u2 = cos2alpha * a2b2b2; final double cos2sigmam2 = cos2sigmam * cos2sigmam; // eq. 3 A = 1.0 + u2 / 16384 * (4096 + u2 * (-768 + u2 * (320 - 175 * u2))); // eq. 4 B = u2 / 1024 * (256 + u2 * (-128 + u2 * (74 - 47 * u2))); // eq. 6 deltasigma = B * sinsigma * (cos2sigmam + B / 4 * (cossigma * (-1 + 2 * cos2sigmam2) - B / 6 * cos2sigmam * (-3 + 4 * sin2sigma) * (-3 + 4 * cos2sigmam2))); // eq. 10 final double C = f / 16 * cos2alpha * (4 + f * (4 - 3 * cos2alpha)); // eq. 11 (modified) lambda = omega + (1 - C) * f * sinalpha * (sigma + C * sinsigma * (cos2sigmam + C * cossigma * (-1 + 2 * cos2sigmam2))); // see how much improvement we got final double change = FastMath.abs((lambda - lambda0) / lambda); if ((i > 1) && (change < change_threshold)) { break; } } // for // eq. 19 return b * A * (sigma - deltasigma); // CHECKSTYLE:ON }
From source file:br.liveo.searchliveo.SearchLiveo.java
/** * Show SearchLiveo/* w w w . jav a 2 s . c o m*/ */ public SearchLiveo show() { setActive(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { try { showAnimation(); } catch (ClassCastException e) { throw new ClassCastException(mContext.getString(R.string.warning_with)); } } else { Animation mFadeIn = AnimationUtils.loadAnimation(mContext.getApplicationContext(), android.R.anim.fade_in); mViewSearch.setEnabled(true); mViewSearch.setVisibility(View.VISIBLE); mViewSearch.setAnimation(mFadeIn); mContext.runOnUiThread(new Runnable() { @Override public void run() { ((InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE)) .toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } }); } mEdtSearch.requestFocus(); return this; }
From source file:br.liveo.searchliveo.SearchCardLiveo.java
/** * Show SearchCardLiveo//from w ww.jav a 2s . com */ public SearchCardLiveo show() { setActive(true); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { try { showAnimation(); } catch (ClassCastException e) { throw new ClassCastException(mContext.getString(R.string.warning_with)); } } else { Animation mFadeIn = AnimationUtils.loadAnimation(mContext.getApplicationContext(), android.R.anim.fade_in); mCardSearch.setEnabled(true); mCardSearch.setVisibility(View.VISIBLE); mCardSearch.setAnimation(mFadeIn); mContext.runOnUiThread(new Runnable() { @Override public void run() { ((InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE)) .toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY); } }); } mEdtSearch.requestFocus(); return this; }
From source file:net.greghaines.jesque.worker.WorkerImpl.java
/** * Executes the given job.//from w w w.j a va 2 s . c o m * * @param job * the job to execute * @param curQueue * the queue the job came from * @param instance * the materialized job * @throws Exception * if the instance is a {@link Callable} and throws an exception * @return result of the execution */ protected Object execute(final Job job, final String curQueue, final Object instance) throws Exception { if (instance instanceof WorkerAware) { ((WorkerAware) instance).setWorker(this); } this.listenerDelegate.fireEvent(JOB_EXECUTE, this, curQueue, job, instance, null, null); final Object result; 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:com.dtolabs.utils.Mapper.java
/** * Create a mapper for bean properties/*from w w w . jav a 2 s .com*/ * @param property name of the bean property * @return */ public static Mapper beanMapper(final String property) { return new Mapper() { public Object map(Object a) { try { return BeanUtils.getProperty(a, property); } catch (Exception e) { throw new ClassCastException("Object was not the expected class: " + a.getClass()); } } }; }
From source file:com.avapira.bobroreader.BoardFragment.java
/** * @deprecated Use {@link #onAttach(Context)} instead. *///from www .j a va 2 s . c om @Deprecated public void onAttach(Activity activity) { Log.d(TAG, "attach"); super.onAttach(activity); try { SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(activity); recentListSize = Integer.parseInt(prefs.getString("pref_board_recent_list_size", "3")); supervisor = (Castor) activity; } catch (ClassCastException e) { e.printStackTrace(); throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } }
From source file:com.opensymphony.xwork2.util.finder.ResourceFinder.java
/** * Assumes the class specified points to a directory in the classpath that holds files * containing the name of a class that implements or is a subclass of the specfied class. * <p/>//from www.jav a2 s .com * Any class that cannot be loaded or assigned to the specified interface will be cause * an exception to be thrown. * <p/> * Example classpath: * <p/> * META-INF/java.net.URLStreamHandler/jar * META-INF/java.net.URLStreamHandler/file * META-INF/java.net.URLStreamHandler/http * <p/> * ResourceFinder finder = new ResourceFinder("META-INF/"); * Map map = finder.mapAllImplementations(java.net.URLStreamHandler.class); * Class jarUrlHandler = map.get("jar"); * Class fileUrlHandler = map.get("file"); * Class httpUrlHandler = map.get("http"); * * @param interfase a superclass or interface * @return * @throws IOException if the URL cannot be read * @throws ClassNotFoundException if the class found is not loadable * @throws ClassCastException if the class found is not assignable to the specified superclass or interface */ public Map<String, Class> mapAllImplementations(Class interfase) throws IOException, ClassNotFoundException { Map<String, Class> implementations = new HashMap<String, Class>(); Map<String, String> map = mapAllStrings(interfase.getName()); for (Map.Entry<String, String> entry : map.entrySet()) { String string = entry.getKey(); String className = entry.getValue(); Class impl = classLoaderInterface.loadClass(className); if (!interfase.isAssignableFrom(impl)) { throw new ClassCastException("Class not of type: " + interfase.getName()); } implementations.put(string, impl); } return implementations; }
From source file:ca.uhn.fhir.util.FhirTerser.java
private void visit(IdentityHashMap<Object, Object> theStack, IBaseResource theResource, IBase theElement, List<String> thePathToElement, BaseRuntimeChildDefinition theChildDefinition, BaseRuntimeElementDefinition<?> theDefinition, IModelVisitor theCallback) { List<String> pathToElement = addNameToList(thePathToElement, theChildDefinition); if (theStack.put(theElement, theElement) != null) { return;//from w w w.j a v a 2s.c o m } theCallback.acceptElement(theResource, theElement, pathToElement, theChildDefinition, theDefinition); BaseRuntimeElementDefinition<?> def = theDefinition; if (def.getChildType() == ChildTypeEnum.CONTAINED_RESOURCE_LIST) { def = myContext.getElementDefinition(theElement.getClass()); } if (theElement instanceof IBaseReference) { IBaseResource target = ((IBaseReference) theElement).getResource(); if (target != null) { if (target.getIdElement().hasIdPart() == false || target.getIdElement().isLocal()) { RuntimeResourceDefinition targetDef = myContext.getResourceDefinition(target); visit(theStack, target, target, pathToElement, null, targetDef, theCallback); } } } switch (def.getChildType()) { case ID_DATATYPE: case PRIMITIVE_XHTML_HL7ORG: case PRIMITIVE_XHTML: case PRIMITIVE_DATATYPE: // These are primitive types break; case RESOURCE: case RESOURCE_BLOCK: case COMPOSITE_DATATYPE: { BaseRuntimeElementCompositeDefinition<?> childDef = (BaseRuntimeElementCompositeDefinition<?>) def; for (BaseRuntimeChildDefinition nextChild : childDef.getChildrenAndExtension()) { List<?> values = nextChild.getAccessor().getValues(theElement); if (values != null) { for (Object nextValueObject : values) { IBase nextValue; try { nextValue = (IBase) nextValueObject; } catch (ClassCastException e) { String s = "Found instance of " + nextValueObject.getClass() + " - Did you set a field value to the incorrect type? Expected " + IBase.class.getName(); throw new ClassCastException(s); } if (nextValue == null) { continue; } if (nextValue.isEmpty()) { continue; } BaseRuntimeElementDefinition<?> childElementDef; childElementDef = nextChild.getChildElementDefinitionByDatatype(nextValue.getClass()); if (childElementDef == null) { childElementDef = myContext.getElementDefinition(nextValue.getClass()); } if (nextChild instanceof RuntimeChildDirectResource) { // Don't descend into embedded resources theCallback.acceptElement(theResource, nextValue, null, nextChild, childElementDef); } else { visit(theStack, theResource, nextValue, pathToElement, nextChild, childElementDef, theCallback); } } } } break; } case CONTAINED_RESOURCES: { BaseContainedDt value = (BaseContainedDt) theElement; for (IResource next : value.getContainedResources()) { def = myContext.getResourceDefinition(next); visit(theStack, next, next, pathToElement, null, def, theCallback); } break; } case CONTAINED_RESOURCE_LIST: case EXTENSION_DECLARED: case UNDECL_EXT: { throw new IllegalStateException("state should not happen: " + def.getChildType()); } } theStack.remove(theElement); }
From source file:com.s3d.webapps.util.time.DateUtils.java
/** * <p>Round this date, leaving the field specified as the most * significant field.</p>// w w w . jav a 2 s .co m * * <p>For example, if you had the datetime of 28 Mar 2002 * 13:45:01.231, if this was passed with HOUR, it would return * 28 Mar 2002 14:00:00.000. If this was passed with MONTH, it * would return 1 April 2002 0:00:00.000.</p> * * <p>For a date in a timezone that handles the change to daylight * saving time, rounding to Calendar.HOUR_OF_DAY will behave as follows. * Suppose daylight saving time begins at 02:00 on March 30. Rounding a * date that crosses this time would produce the following values: * <ul> * <li>March 30, 2003 01:10 rounds to March 30, 2003 01:00</li> * <li>March 30, 2003 01:40 rounds to March 30, 2003 03:00</li> * <li>March 30, 2003 02:10 rounds to March 30, 2003 03:00</li> * <li>March 30, 2003 02:40 rounds to March 30, 2003 04:00</li> * </ul> * </p> * * @param date the date to work with, either Date or Calendar * @param field the field from <code>Calendar</code> * or <code>SEMI_MONTH</code> * @return the rounded date * @throws IllegalArgumentException if the date is <code>null</code> * @throws ClassCastException if the object type is not a <code>Date</code> * or <code>Calendar</code> * @throws ArithmeticException if the year is over 280 million */ public static Date round(Object date, int field) { if (date == null) { throw new IllegalArgumentException("The date must not be null"); } if (date instanceof Date) { return round((Date) date, field); } else if (date instanceof Calendar) { return round((Calendar) date, field).getTime(); } else { throw new ClassCastException("Could not round " + date); } }