ModelRegistry getModelRegistry(Class<? extends Model> modelClass) { return modelRegistries.computeIfAbsent(modelClass, k -> new ModelRegistry()); }
void callbackWith(CallbackListener... listeners) { callbackWith(Arrays.asList(listeners)); }
private void fireBeforeSave() { beforeSave(); for (CallbackListener callback : modelRegistryLocal.callbacks()) { callback.beforeSave(this); } }
/** * Gets attribute value as <code>Integer</code>. * If there is a {@link Converter} registered for the attribute that converts from Class <code>S</code> to Class * <code>java.lang.Integer</code>, given the attribute value is an instance of <code>S</code>, then it will be used, * otherwise performs a conversion using {@link Convert#toInteger(Object)}. * * @param attributeName name of attribute to convert * @return value converted to <code>Integer</code> */ public Integer getInteger(String attributeName) { Object value = getRaw(attributeName); Converter<Object, Integer> converter = modelRegistryLocal.converterForValue(attributeName, value, Integer.class); return converter != null ? converter.convert(value) : Convert.toInteger(value); }
/** * Registers converter for specified model attributes. */ void convertWith(Converter converter, String... attributes) { for (String attribute : attributes) { convertWith(converter, attribute); } }
/** * Registers timestamp converters (Date -> String -> java.sql.Timestamp) for specified model attributes. */ void timestampFormat(String pattern, String... attributes) { timestampFormat(new SimpleDateFormat(pattern), attributes); }
/** * Registers date converters (Date -> String -> java.sql.Date) for specified model attributes. */ void dateFormat(String pattern, String... attributes) { dateFormat(new SimpleDateFormat(pattern), attributes); }
/** * Returns converter for specified model attribute, able to convert value to an instance of destinationClass. * Returns null if no suitable converter was found. */ <T> Converter<Object, T> converterForValue(String attribute, Object value, Class<T> destinationClass) { return converterForClass(attribute, value != null ? (Class<Object>) value.getClass() : Object.class, destinationClass); }
public static void removeValidator(Class<? extends Model> clazz, Validator validator) { modelRegistryOf(clazz).removeValidator(validator); }
public static NumericValidationBuilder validateNumericalityOf(Class<? extends Model> clazz, String... attributeNames) { return modelRegistryOf(clazz).validateNumericalityOf(attributeNames); }
/** * Gets attribute value as <code>Float</code>. * If there is a {@link Converter} registered for the attribute that converts from Class <code>S</code> to Class * <code>java.lang.Float</code>, given the attribute value is an instance of <code>S</code>, then it will be used, * otherwise performs a conversion using {@link Convert#toFloat(Object)}. * * @param attributeName name of attribute to convert * @return value converted to <code>Float</code> */ public Float getFloat(String attributeName) { Object value = getRaw(attributeName); Converter<Object, Float> converter = modelRegistryLocal.converterForValue(attributeName, value, Float.class); return converter != null ? converter.convert(value) : Convert.toFloat(value); }
/** * Registers date converters (Date -> String -> java.sql.Date) for specified model attributes. */ void dateFormat(DateFormat format, String... attributes) { convertWith(new DateToStringConverter(format), attributes); convertWith(new StringToSqlDateConverter(format), attributes); }
public static void timestampFormat(Class<? extends Model> clazz, String pattern, String... attributeNames) { modelRegistryOf(clazz).timestampFormat(pattern, attributeNames); }
public static void dateFormat(Class<? extends Model> clazz, DateFormat format, String... attributeNames) { modelRegistryOf(clazz).dateFormat(format, attributeNames); }
/** * Returns converter for specified model attribute, able to convert value to an instance of destinationClass. * Returns null if no suitable converter was found. */ <T> Converter<Object, T> converterForValue(String attribute, Object value, Class<T> destinationClass) { return converterForClass(attribute, value != null ? (Class<Object>) value.getClass() : Object.class, destinationClass); }
public static void removeValidator(Class<? extends Model> clazz, Validator validator) { modelRegistryOf(clazz).removeValidator(validator); }
public static NumericValidationBuilder validateNumericalityOf(Class<? extends Model> clazz, String... attributeNames) { return modelRegistryOf(clazz).validateNumericalityOf(attributeNames); }
private void fireBeforeValidation() { beforeValidation(); for(CallbackListener callback: modelRegistryLocal.callbacks()) callback.beforeValidation(this); }
/** * Gets attribute value as <code>Boolean</code>. * If there is a {@link Converter} registered for the attribute that converts from Class <code>S</code> to Class * <code>java.lang.Boolean</code>, given the attribute value is an instance of <code>S</code>, then it will be used, * otherwise performs a conversion using {@link Convert#toBoolean(Object)}. * * @param attributeName name of attribute to convert * @return value converted to <code>Boolean</code> */ public Boolean getBoolean(String attributeName) { Object value = getRaw(attributeName); Converter<Object, Boolean> converter = modelRegistryLocal.converterForValue(attributeName, value, Boolean.class); return converter != null ? converter.convert(value) : Convert.toBoolean(value); }
/** * Registers timestamp converters (Date -> String -> java.sql.Timestamp) for specified model attributes. */ void timestampFormat(DateFormat format, String... attributes) { convertWith(new DateToStringConverter(format), attributes); convertWith(new StringToTimestampConverter(format), attributes); }