@SuppressWarnings("unchecked") private <T> T withDefault(Object value, T defaultValue) { if (value != null) { return (T) (defaultValue != null ? Cls.convert(value, Cls.of(defaultValue)) : value); } else { return defaultValue; } }
@Override public R processUnknown(T context, Object value) { throw U.rte("Unsupported type: " + Cls.of(value).getName()); }
public static <T extends Serializable> Var<T> var(String name, T defaultValue) { return var(name, Cls.of(defaultValue), defaultValue); }
@Override public Void processUnknown(ByteBuffer buf, Object value) { throw U.rte("Cannot serialize a value of type: " + Cls.of(value).getName()); }
@SuppressWarnings("rawtypes") public static Prop property(Object obj, String property, boolean mandatory) { if (obj instanceof Map) { Object value = ((Map) obj).get(property); return new MapProp(property, property, Cls.of(value)); } else { return property(obj.getClass(), property, mandatory); } }
public static <T> Map<String, T> toBeanMap(Map<String, Object> data, Class<T> type) { Map<String, T> map = U.map(); for (Map.Entry<String, Object> e : data.entrySet()) { T bean; Object value = e.getValue(); if (value instanceof Map) { bean = JSON.MAPPER.convertValue(value, type); } else if (value instanceof String) { bean = Cls.newInstance(type, value); } else { throw U.rte("Unsupported configuration type: %s", Cls.of(value)); } map.put(e.getKey(), bean); } return Collections.unmodifiableMap(map); }
public static BeanProperties from(Map<String, ?> map) { Map<String, Prop> properties = Coll.synchronizedMap(); for (Entry<?, ?> e : map.entrySet()) { Object key = e.getKey(); Prop prop = new MapProp(String.valueOf(key), key, Cls.of(e.getValue())); properties.put(prop.getName(), prop); } return new BeanProperties(properties); }
@Override public Item ifFitsIn(Item item) { U.must(fitsIn(item), "The item doesn't fit in the items, expected type '%s' but found: %s", beanType, Cls.of(item.value())); return item; }
public static <E> Comparator<E> comparator(final String orderBy) { final int sign = orderBy.startsWith("-") ? -1 : 1; final String order = sign == 1 ? orderBy : orderBy.substring(1); Comparator<E> comparator = (o1, o2) -> { try { E val1 = getPropValue(o1, order); E val2 = getPropValue(o2, order); U.must(val1 == null || val1 instanceof Comparable, "The property '%s' (%s) is not comparable!", order, Cls.of((val1))); U.must(val2 == null || val2 instanceof Comparable, "The property '%s' (%s) is not comparable!", order, Cls.of((val2))); return sign * U.compare(val1, val2); } catch (Exception e) { Log.error("Cannot compare values by: " + orderBy, e); return 0; } }; return comparator; }
private synchronized void ensureIsInitialized() { if (!initialized) { validate(); registerJDBCDriver(); if (this.dataSource == null) { this.dataSource = this.usePool ? createPool() : null; } String ds = dataSource != null ? Cls.of(dataSource).getSimpleName() : null; Log.info("Initialized JDBC API", "!url", url, "!driver", driver, "!username", username, "!password", maskedPassword(), "!dataSource", ds); initialized = true; } }
public static synchronized void reset() { for (ObjectMapper mapper : U.list(MAPPER, PRETTY_MAPPER)) { SerializerProvider serializerProvider = mapper.getSerializerProvider(); if (serializerProvider instanceof DefaultSerializerProvider) { DefaultSerializerProvider provider = (DefaultSerializerProvider) serializerProvider; provider.flushCachedSerializers(); } else { Log.warn("Couldn't clear the cache of Jackson serializers!", "class", Cls.of(serializerProvider)); } DeserializationContext deserializationContext = mapper.getDeserializationContext(); Object cache = Cls.getFieldValue(deserializationContext, "_cache"); if (cache instanceof DeserializerCache) { DeserializerCache deserializerCache = (DeserializerCache) cache; deserializerCache.flushCachedDeserializers(); } else { Log.warn("Couldn't clear the cache of Jackson deserializers!", "class", Cls.of(cache)); } } }
@SuppressWarnings("unchecked") private <T> T withDefault(Object value, T defaultValue) { if (value != null) { return (T) (defaultValue != null ? Cls.convert(value, Cls.of(defaultValue)) : value); } else { return defaultValue; } }
public static BeanProperties from(Map<String, ?> map) { Map<String, Prop> properties = new LinkedHashMap<String, Prop>(); for (Entry<?, ?> e : map.entrySet()) { Object key = e.getKey(); Prop prop = new MapProp(String.valueOf(key), key, Cls.of(e.getValue())); properties.put(prop.getName(), prop); } return new BeanProperties(properties); }
@SuppressWarnings("rawtypes") public static Prop property(Object obj, String property, boolean mandatory) { if (obj instanceof Map) { Object value = ((Map) obj).get(property); return new MapProp(property, property, Cls.of(value)); } else { return property(obj.getClass(), property, mandatory); } }
private void normalSet(Object target, Object value) { try { if (field != null) { field.setAccessible(true); field.set(target, Cls.convert(value, field.getType())); } else if (setter != null) { setter.setAccessible(true); setter.invoke(target, Cls.convert(value, setter.getParameterTypes()[0])); } else if (getter != null) { throw U.notExpected(); } } catch (Exception e) { throw U.rte("Cannot set %s %s.%s = %s (%s)", e, getType(), getDeclaringType(), getName(), value, Cls.of(value)); } }
private synchronized void ensureIsInitialized() { if (!initialized) { validate(); registerJDBCDriver(); if (this.dataSource == null) { this.dataSource = this.usePool ? createPool() : null; } String ds = dataSource != null ? Cls.of(dataSource).getSimpleName() : null; Log.info("Initialized JDBC API", "!url", url, "!driver", driver, "!username", username, "!password", maskedPassword(), "!dataSource", ds); initialized = true; } }
@Override public int compare(E o1, E o2) { try { E val1 = getPropValue(o1, order); E val2 = getPropValue(o2, order); U.must(val1 == null || val1 instanceof Comparable, "The property '%s' (%s) is not comparable!", order, Cls.of((val1))); U.must(val2 == null || val2 instanceof Comparable, "The property '%s' (%s) is not comparable!", order, Cls.of((val2))); return sign * U.compare(val1, val2); } catch (Exception e) { Log.error("Cannot compare values by: " + orderBy, e); return 0; } } };
public void delete(Object record) { U.argMust(Cls.kindOf(record) == TypeKind.OBJECT, "Invalid record type: %s", Cls.of(record)); delete(Beany.getLongId(record)); }