Misc { public static <A, B> B cast(A v, Class<B> to) { if (Number.class.isAssignableFrom(v.getClass())) { if (eq(Integer.class, to)) { return (B) ((Object) (((Number) v).intValue())); } else if (eq(Long.class, to)) { return (B) ((Object) (((Number) v).longValue())); } else if (eq(Double.class, to)) { return (B) ((Object) (((Number) v).doubleValue())); } else if (eq(Float.class, to)) { return (B) ((Object) (((Number) v).floatValue())); } else if (eq(Short.class, to)) { return (B) ((Object) (((Number) v).shortValue())); } else if (eq(Byte.class, to)) { return (B) ((Object) (((Number) v).byteValue())); } else { return (B) v; } } else if (to.isAssignableFrom(v.getClass())) { return (B) v; } else { throw new ClassCastException(v.getClass().getName() + " is not of type " + to.getName()); } } private  Misc(); static A chuck(Throwable t); static Function<A, B> chuck(); static B cast(A v, Class<B> to); static Function<A, B> cast(); static List<A> widen(List<? extends A> xs); static Option<A> widen(Option<? extends A> xs); static Function<A, A> ifThen(final A predicate, final A b);  }
@Test public void testCast() { Misc.cast(1, Integer.class); Misc.cast(1.0, Double.class); Misc.cast(1, Double.class); Misc.cast(1, Short.class); Misc.cast(1, Byte.class); Misc.cast(1.0, Float.class); Misc.cast(1, Float.class); Misc.cast(1, Double.class); Misc.cast(1, Number.class); Misc.cast("1", String.class); }