/** * Stores the array of items in the configuration with the given keyName. * * @param <K> the class of the item * @param conf the configuration to use * @param items the objects to be stored * @param keyName the name of the key to use * @throws IndexOutOfBoundsException if the items array is empty * @throws IOException : forwards Exceptions from the underlying * {@link Serialization} classes. */ public static <K> void storeArray(Configuration conf, K[] items, String keyName) throws IOException { DefaultStringifier<K> stringifier = new DefaultStringifier<K>(conf, GenericsUtil.getClass(items[0])); try { StringBuilder builder = new StringBuilder(); for (K item : items) { builder.append(stringifier.toString(item)).append(SEPARATOR); } conf.set(keyName, builder.toString()); } finally { stringifier.close(); } }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
/** * Restores the array of objects from the configuration. * * @param <K> the class of the item * @param conf the configuration to use * @param keyName the name of the key to use * @param itemClass the class of the item * @return restored object * @throws IOException : forwards Exceptions from the underlying * {@link Serialization} classes. */ public static <K> K[] loadArray(Configuration conf, String keyName, Class<K> itemClass) throws IOException { DefaultStringifier<K> stringifier = new DefaultStringifier<K>(conf, itemClass); try { String itemStr = conf.get(keyName); ArrayList<K> list = new ArrayList<K>(); String[] parts = itemStr.split(SEPARATOR); for (String part : parts) { if (!part.isEmpty()) list.add(stringifier.fromString(part)); } return GenericsUtil.toArray(itemClass, list); } finally { stringifier.close(); } }
if (isLog4jLogger(NMErrorsAndWarningsPage.class)) { Log4jWarningErrorMetricsAppender appender = Log4jWarningErrorMetricsAppender.findAppender(); if (appender != null) {
T[] funcThatUsesToArray() { T[] arr = GenericsUtil.toArray(list); return arr; } }
@Override public void render(Block html) { boolean addErrorsAndWarningsLink = false; if (isLog4jLogger(NavBlock.class)) { Log4jWarningErrorMetricsAppender appender = Log4jWarningErrorMetricsAppender.findAppender();
/** * Stores the item in the configuration with the given keyName. * * @param <K> the class of the item * @param conf the configuration to store * @param item the object to be stored * @param keyName the name of the key to use * @throws IOException : forwards Exceptions from the underlying * {@link Serialization} classes. */ public static <K> void store(Configuration conf, K item, String keyName) throws IOException { DefaultStringifier<K> stringifier = new DefaultStringifier<K>(conf, GenericsUtil.getClass(item)); conf.set(keyName, stringifier.toString(item)); stringifier.close(); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
public void testWithEmptyList2() { List<String> list = new ArrayList<String>(); //this method should not throw IndexOutOfBoundsException String[] arr = GenericsUtil.<String>toArray(String.class, list); assertEquals(0, arr.length); }
public void testGetClass() { //test with Integer Integer x = new Integer(42); Class<Integer> c = GenericsUtil.getClass(x); assertEquals(Integer.class, c); //test with GenericClass<Integer> GenericClass<Integer> testSubject = new GenericClass<Integer>(); Class<GenericClass<Integer>> c2 = GenericsUtil.getClass(testSubject); assertEquals(GenericClass.class, c2); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
public void testWithEmptyList() { try { List<String> list = new ArrayList<String>(); String[] arr = GenericsUtil.toArray(list); fail("Empty array should throw exception"); System.out.println(arr); //use arr so that compiler will not complain }catch (IndexOutOfBoundsException ex) { //test case is successful } }
public static Map<String,String> parseParams(String serializedString) throws IOException { Configuration conf = new Configuration(); conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization," + "org.apache.hadoop.io.serializer.WritableSerialization"); Map<String,String> params = Maps.newHashMap(); DefaultStringifier<Map<String,String>> mapStringifier = new DefaultStringifier<Map<String,String>>(conf, GenericsUtil.getClass(params)); return mapStringifier.fromString(serializedString); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
T[] funcThatUsesToArray() { T[] arr = GenericsUtil.toArray(list); return arr; } }
public static Map<String,String> parseParams(String serializedString) throws IOException { Configuration conf = new Configuration(); conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization," + "org.apache.hadoop.io.serializer.WritableSerialization"); Map<String,String> params = Maps.newHashMap(); DefaultStringifier<Map<String,String>> mapStringifier = new DefaultStringifier<Map<String,String>>(conf, GenericsUtil.getClass(params)); return mapStringifier.fromString(serializedString); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }
T[] funcThatUsesToArray() { T[] arr = GenericsUtil.toArray(list); return arr; } }
public static Map<String,String> parseParams(String serializedString) throws IOException { Configuration conf = new Configuration(); conf.set("io.serializations", "org.apache.hadoop.io.serializer.JavaSerialization," + "org.apache.hadoop.io.serializer.WritableSerialization"); Map<String,String> params = Maps.newHashMap(); DefaultStringifier<Map<String,String>> mapStringifier = new DefaultStringifier<>(conf, GenericsUtil.getClass(params)); return mapStringifier.fromString(serializedString); }
/** * Converts the given <code>List<T></code> to a an array of * <code>T[]</code>. * @param list the list to convert * @throws ArrayIndexOutOfBoundsException if the list is empty. * Use {@link #toArray(Class, List)} if the list may be empty. */ public static <T> T[] toArray(List<T> list) { return toArray(getClass(list.get(0)), list); }