@Test public void testGetAs() throws Exception { MutableList<String> list = MutableList.of("x"); assertEquals(Yamls.getAs(list.iterator(), List.class), list); assertEquals(Yamls.getAs(list.iterator(), Iterable.class), list); assertEquals(Yamls.getAs(list.iterator(), Iterator.class), list.iterator()); assertEquals(Yamls.getAs(list.iterator(), String.class), "x"); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T getAs(Object x, Class<T> type) { if (x==null) return null; if (x instanceof Iterable || x instanceof Iterator) { List result = new ArrayList(); Iterator xi; if (Iterator.class.isAssignableFrom(x.getClass())) { xi = (Iterator)x; } else { xi = ((Iterable)x).iterator(); } while (xi.hasNext()) { result.add( xi.next() ); } if (type.isAssignableFrom(List.class)) return (T)result; if (type.isAssignableFrom(Iterator.class)) return (T)result.iterator(); x = Iterables.getOnlyElement(result); } if (type.isInstance(x)) return (T)x; throw new ClassCastException("Cannot convert "+x+" ("+x.getClass()+") to "+type); }
Yamls { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T getAs(Object x, Class<T> type) { if (x==null) return null; if (x instanceof Iterable || x instanceof Iterator) { List result = new ArrayList(); Iterator xi; if (Iterator.class.isAssignableFrom(x.getClass())) { xi = (Iterator)x; } else { xi = ((Iterable)x).iterator(); } while (xi.hasNext()) { result.add( xi.next() ); } if (type.isAssignableFrom(List.class)) return (T)result; if (type.isAssignableFrom(Iterator.class)) return (T)result.iterator(); x = Iterables.getOnlyElement(result); } if (type.isInstance(x)) return (T)x; throw new ClassCastException("Cannot convert "+x+" ("+x.getClass()+") to "+type); } }
Yamls { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T getAs(Object x, Class<T> type) { if (x==null) return null; if (x instanceof Iterable || x instanceof Iterator) { List result = new ArrayList(); Iterator xi; if (Iterator.class.isAssignableFrom(x.getClass())) { xi = (Iterator)x; } else { xi = ((Iterable)x).iterator(); } while (xi.hasNext()) { result.add( xi.next() ); } if (type.isAssignableFrom(List.class)) return (T)result; if (type.isAssignableFrom(Iterator.class)) return (T)result.iterator(); x = Iterables.getOnlyElement(result); } if (type.isInstance(x)) return (T)x; throw new ClassCastException("Cannot convert "+x+" ("+x.getClass()+") to "+type); }  }
Yamls { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T getAs(Object x, Class<T> type) { if (x==null) return null; if (x instanceof Iterable || x instanceof Iterator) { List result = new ArrayList(); Iterator xi; if (Iterator.class.isAssignableFrom(x.getClass())) { xi = (Iterator)x; } else { xi = ((Iterable)x).iterator(); } while (xi.hasNext()) { result.add( xi.next() ); } if (type.isAssignableFrom(List.class)) return (T)result; if (type.isAssignableFrom(Iterator.class)) return (T)result.iterator(); x = Iterables.getOnlyElement(result); } if (type.isInstance(x)) return (T)x; throw new ClassCastException("Cannot convert "+x+" ("+x.getClass()+") to "+type); }  @SuppressWarnings({ "unchecked", "rawtypes" }) static T getAs(Object x, Class<T> type); @Beta static Object getAt(String yaml, List<String> path); @Beta @SuppressWarnings("unchecked") static Object getAtPreParsed(Object current, List<String> path); @SuppressWarnings("rawtypes") static void dump(int depth, Object r); @SuppressWarnings("unchecked") static Iterable<Object> parseAll(String yaml); @SuppressWarnings("unchecked") static Iterable<Object> parseAll(Reader yaml); static Object removeMultinameAttribute(Map<String,Object> obj, String ...equivalentNames); static Object getMultinameAttribute(Map<String,Object> obj, String ...equivalentNames); static YamlExtract getTextOfYamlAtPath(String yaml, Object ...path); }
Yamls { @SuppressWarnings({ "unchecked", "rawtypes" }) public static <T> T getAs(Object x, Class<T> type) { if (x==null) return null; if (x instanceof Iterable || x instanceof Iterator) { List result = new ArrayList(); Iterator xi; if (Iterator.class.isAssignableFrom(x.getClass())) { xi = (Iterator)x; } else { xi = ((Iterable)x).iterator(); } while (xi.hasNext()) { result.add( xi.next() ); } if (type.isAssignableFrom(List.class)) return (T)result; if (type.isAssignableFrom(Iterator.class)) return (T)result.iterator(); x = Iterables.getOnlyElement(result); } if (type.isInstance(x)) return (T)x; throw new ClassCastException("Cannot convert "+x+" ("+x.getClass()+") to "+type); }  @SuppressWarnings({ "unchecked", "rawtypes" }) static T getAs(Object x, Class<T> type); @Beta static Object getAt(String yaml, List<String> path); @Beta @SuppressWarnings("unchecked") static Object getAtPreParsed(Object current, List<String> path); @SuppressWarnings("rawtypes") static void dump(int depth, Object r); @SuppressWarnings("unchecked") static Iterable<Object> parseAll(String yaml); @SuppressWarnings("unchecked") static Iterable<Object> parseAll(Reader yaml); static Object removeMultinameAttribute(Map<String,Object> obj, String ...equivalentNames); static Object getMultinameAttribute(Map<String,Object> obj, String ...equivalentNames); static YamlExtract getTextOfYamlAtPath(String yaml, Object ...path);  }