/** * Update given Object attribute by reading it from provided map properties. * * @param properties * which key-value pair of properties to assign those values to given object * @param obj * object which needs to be updated * @throws IllegalArgumentException * if the properties key-value contains incorrect value type */ public static <T> void update(Map<String, String> properties, T obj) throws IllegalArgumentException { Field[] fields = obj.getClass().getDeclaredFields(); Arrays.stream(fields).forEach(f -> { if (properties.containsKey(f.getName())) { try { f.setAccessible(true); String v = (String) properties.get(f.getName()); if (!StringUtils.isBlank(v)) { f.set(obj, value(v, f)); } } catch (Exception e) { throw new IllegalArgumentException(format("failed to initialize %s field while setting value %s", f.getName(), properties.get(f.getName())), e); } } }); }
/** * Converts value as per appropriate DataType of the field. * * @param strValue * : string value of the object * @param field * : field of the attribute * @return */ public static Object value(String strValue, Field field) { checkNotNull(field); // if field is not primitive type if (field.getGenericType() instanceof ParameterizedType) { Class<?> clazz = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; // convert to list if (field.getType().equals(List.class)) return stringToList(strValue, clazz); // convert to set else if (field.getType().equals(Set.class)) return stringToSet(strValue, clazz); else throw new IllegalArgumentException( format("unsupported field-type %s for %s", field.getType(), field.getName())); } else { return convert(strValue, field.getType()); } }
/** * Converts comma separated string to Set * * @param <T> * type of set * @param value * comma separated values. * @return The converted set with type <T>. */ public static <T> Set<T> stringToSet(String val, Class<T> type) { String[] tokens = trim(val).split(","); return Arrays.stream(tokens).map(t -> { return convert(t, type); }).collect(Collectors.toSet()); }
/** * Converts String to float. * * @param value * The String to be converted. * @return The converted Double value. */ public static Float stringToFloat(String val) { return Float.valueOf(trim(val)); }
to = (Class<T>) wrap(to);
@SuppressWarnings({ "rawtypes", "unchecked" }) protected static <T extends PulsarConfiguration> T create(Properties properties, Class<? extends PulsarConfiguration> clazz) throws IOException, IllegalArgumentException { checkNotNull(properties); T configuration = null; try { configuration = (T) clazz.newInstance(); configuration.setProperties(properties); update((Map) properties, configuration); } catch (InstantiationException | IllegalAccessException e) { throw new IllegalArgumentException("Failed to instantiate " + clazz.getName(), e); } return configuration; }
/** * Converts String to Long. * * @param value * The String to be converted. * @return The converted Long value. */ public static Long stringToLong(String val) { return Long.valueOf(trim(val)); }
to = (Class<T>) wrap(to);
/** * Converts value as per appropriate DataType of the field. * * @param strValue * : string value of the object * @param field * : field of the attribute * @return */ public static Object value(String strValue, Field field) { checkNotNull(field); // if field is not primitive type if (field.getGenericType() instanceof ParameterizedType) { Class<?> clazz = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]; // convert to list if (field.getType().equals(List.class)) return stringToList(strValue, clazz); // convert to set else if (field.getType().equals(Set.class)) return stringToSet(strValue, clazz); else throw new IllegalArgumentException( format("unsupported field-type %s for %s", field.getType(), field.getName())); } else { return convert(strValue, field.getType()); } }
/** * Converts comma separated string to List * * @param <T> * type of list * @param value * comma separated values. * @return The converted list with type <T>. */ public static <T> List<T> stringToList(String val, Class<T> type) { String[] tokens = trim(val).split(","); return Arrays.stream(tokens).map(t -> { return convert(t, type); }).collect(Collectors.toList()); }
/** * Update given Object attribute by reading it from provided map properties. * * @param properties * which key-value pair of properties to assign those values to given object * @param obj * object which needs to be updated * @throws IllegalArgumentException * if the properties key-value contains incorrect value type */ public static <T> void update(Map<String, String> properties, T obj) throws IllegalArgumentException { Field[] fields = obj.getClass().getDeclaredFields(); Arrays.stream(fields).forEach(f -> { if (properties.containsKey(f.getName())) { try { f.setAccessible(true); String v = (String) properties.get(f.getName()); if (!StringUtils.isBlank(v)) { f.set(obj, value(v, f)); } } catch (Exception e) { throw new IllegalArgumentException(format("failed to initialize %s field while setting value %s", f.getName(), properties.get(f.getName())), e); } } }); }
/** * Converts String to Double. * * @param value * The String to be converted. * @return The converted Double value. */ public static Double stringToDouble(String val) { String v = trim(val); if (StringUtil.isNullOrEmpty(v)) { return null; } else { return Double.valueOf(v); } }
/** * Converts comma separated string to List * * @param <T> * type of list * @param value * comma separated values. * @return The converted list with type <T>. */ public static <T> List<T> stringToList(String val, Class<T> type) { String[] tokens = trim(val).split(","); return Arrays.stream(tokens).map(t -> { return convert(t, type); }).collect(Collectors.toList()); }
if (field != null && field.isAnnotationPresent(FieldContext.class)) { field.setAccessible(true); field.set(pulsar().getConfiguration(), FieldParser.value(value, field)); log.info("Successfully updated {}/{}", key, value);
/** * Converts String to Integer. * * @param value * The String to be converted. * @return The converted Integer value. */ public static Integer stringToInteger(String val) { String v = trim(val); if (StringUtil.isNullOrEmpty(v)) { return null; } else { return Integer.valueOf(v); } }
/** * Converts comma separated string to Set * * @param <T> * type of set * @param value * comma separated values. * @return The converted set with type <T>. */ public static <T> Set<T> stringToSet(String val, Class<T> type) { String[] tokens = trim(val).split(","); return Arrays.stream(tokens).map(t -> { return convert(t, type); }).collect(Collectors.toSet()); }
/** * Converts String to float. * * @param value * The String to be converted. * @return The converted Double value. */ public static Float stringToFloat(String val) { return Float.valueOf(trim(val)); }
/** * Converts String to Long. * * @param value * The String to be converted. * @return The converted Long value. */ public static Long stringToLong(String val) { return Long.valueOf(trim(val)); }
/** * Converts String to Double. * * @param value * The String to be converted. * @return The converted Double value. */ public static Double stringToDouble(String val) { String v = trim(val); if (StringUtil.isNullOrEmpty(v)) { return null; } else { return Double.valueOf(v); } }
/** * Converts String to Integer. * * @param value * The String to be converted. * @return The converted Integer value. */ public static Integer stringToInteger(String val) { String v = trim(val); if (StringUtil.isNullOrEmpty(v)) { return null; } else { return Integer.valueOf(v); } }