/** {@inheritDoc} */ @Override public boolean create(@Nonnull final Object value) throws IOException { return create(AnnotationSupport.getContext(value), AnnotationSupport.getKey(value), AnnotationSupport.getValue(value), AnnotationSupport.getExpiration(value)); }
/** {@inheritDoc} */ @Override @Nullable public Object read(@Nonnull final Object value) throws IOException { final StorageRecord record = read(AnnotationSupport.getContext(value), AnnotationSupport.getKey(value)); if (record != null) { AnnotationSupport.setValue(value, record.getValue()); AnnotationSupport.setExpiration(value, record.getExpiration()); return value; } return null; }
/** {@inheritDoc} */ @Override public boolean updateExpiration(@Nonnull final Object value) throws IOException { return updateExpiration(AnnotationSupport.getContext(value), AnnotationSupport.getKey(value), AnnotationSupport.getExpiration(value)); }
/** {@inheritDoc} */ @Override public boolean delete(@Nonnull final Object value) throws IOException { return delete(AnnotationSupport.getContext(value), AnnotationSupport.getKey(value)); }
/** * Gets the value of the field indicated by the {@link Key} annotation on the given object. * * <p>If the value is not a {@link String}, then it is converted to one * by calling {@link Object#toString()} on the object.<p> * * @param target object from which to get key * * @return Key field value * @throws IllegalArgumentException if the target object doesn't declare a {@link Key} annotation * @throws RuntimeException if the field cannot be read on the target object */ @Nonnull @NotEmpty public static String getKey(@Nonnull final Object target) { final Key keyField = getAnnotation(target, Key.class); final Object value = getFieldValue(target, keyField.value()); if (value instanceof String) { return (String) value; } return value.toString(); }
final Expiration expField = getAnnotation(target, Expiration.class); if (expiration == null) { setFieldValue(target, expField.value(), null); final Class<?> type = getField(target, expField.value()).getType(); if (Long.class.isAssignableFrom(type)) { setFieldValue(target, expField.value(), expiration); } else if (Date.class.isAssignableFrom(type)) { setFieldValue(target, expField.value(), new Date(expiration)); } else if (ReadableInstant.class.isAssignableFrom(type)) { setFieldValue(target, expField.value(), new Instant(expiration)); } else { throw new RuntimeException(type + " is an unsupported data type for an expiration field.");
/** * Sets the value of the field indicated by the {@link Value} annotation on the given object. * * @param target object on which to set value * @param value Value field value * * @throws IllegalArgumentException if the target object doesn't declare a {@link Value} annotation * @throws RuntimeException if the field cannot be set on the target object */ public static void setValue(@Nonnull final Object target, @Nonnull @NotEmpty final String value) { final Value valueField = getAnnotation(target, Value.class); setFieldValue(target, valueField.value(), value); }
/** * Returns the value of a field from an object using reflection. * * @param target target object to examine * @param fieldName name of the field to retrieve * * @return the value of the field * @throws RuntimeException if the field cannot be read */ @Nullable private static Object getFieldValue(@Nonnull final Object target, @Nonnull final String fieldName) { try { return getField(target, fieldName).get(target); } catch (IllegalAccessException e) { throw new RuntimeException("Field " + fieldName + " cannot be read on " + target); } }
/** {@inheritDoc} */ @Override public boolean delete(@Nonnull final Object value) throws IOException { Constraint.isNotNull(value, "Value cannot be null"); return delete( AnnotationSupport.getContext(value), AnnotationSupport.getKey(value)); }
/** {@inheritDoc} */ @Override public boolean updateExpiration(@Nonnull final Object value) throws IOException { Constraint.isNotNull(value, "Value cannot be null"); return updateExpiration( AnnotationSupport.getContext(value), AnnotationSupport.getKey(value), AnnotationSupport.getExpiration(value)); }
/** * Gets the value of the field indicated by the {@link Context} annotation on the given object. * * <p>If the value is not a {@link String}, then it is converted to one * by calling {@link Object#toString()} on the object.<p> * * @param target object from which to get context * * @return Context field value * @throws IllegalArgumentException if the target object doesn't declare a {@link Context} annotation * @throws RuntimeException if the field cannot be read on the target object */ @Nonnull @NotEmpty public static String getContext(@Nonnull final Object target) { final Context ctxField = getAnnotation(target, Context.class); final Object value = getFieldValue(target, ctxField.value()); if (value instanceof String) { return (String) value; } return value.toString(); }
/** * Sets the value of the field indicated by the {@link Context} annotation on the given object. * * @param target Object on which to set context * @param context Context value * * @throws IllegalArgumentException if the target object doesn't declare a {@link Context} annotation * @throws RuntimeException if the field cannot be set on the target object */ public static void setContext(@Nonnull final Object target, @Nonnull @NotEmpty final String context) { final Context ctxField = getAnnotation(target, Context.class); setFieldValue(target, ctxField.value(), context); }
/** * Returns the value of a field from an object using reflection. * * @param target target object to update * @param fieldName name of the field to set * @param fieldValue value to set * * @throws RuntimeException if the field cannot be set */ private static void setFieldValue(@Nonnull final Object target, @Nonnull final String fieldName, @Nullable final Object fieldValue) { try { getField(target, fieldName).set(target, fieldValue); } catch (IllegalAccessException e) { throw new RuntimeException("Field " + fieldName + " cannot be set on " + target); } }
/** {@inheritDoc} */ @Override public boolean update(@Nonnull final Object value) throws IOException { return update(AnnotationSupport.getContext(value), AnnotationSupport.getKey(value), AnnotationSupport.getValue(value), AnnotationSupport.getExpiration(value)); }
/** {@inheritDoc} */ @Override public Object read(@Nonnull final Object value) throws IOException { Constraint.isNotNull(value, "Value cannot be null"); return read(AnnotationSupport.getContext(value), AnnotationSupport.getKey(value)); }
/** * Gets the value of the field indicated by the {@link Value} annotation on the given object. * * <p>If the value is not a {@link String}, then it is converted to one * by calling {@link Object#toString()} on the object.<p> * * @param target Object from which to get value * * @return Value field value * @throws IllegalArgumentException if the target object doesn't declare a {@link Value} annotation * @throws RuntimeException if the field cannot be read on the target object */ @Nonnull @NotEmpty public static String getValue(@Nonnull final Object target) { final Value valueField = getAnnotation(target, Value.class); final Object value = getFieldValue(target, valueField.value()); if (value instanceof String) { return (String) value; } return value.toString(); }
/** * Sets the value of the field indicated by the {@link Key} annotation on the given object. * * @param target Object on which to set key * @param key Key value * * @throws IllegalArgumentException if the target object doesn't declare a {@link Key} annotation * @throws RuntimeException if the field cannot be set on the target object */ public static void setKey(@Nonnull final Object target, @Nonnull @NotEmpty final String key) { final Key keyField = getAnnotation(target, Key.class); setFieldValue(target, keyField.value(), key); }
/** {@inheritDoc} */ @Override public boolean update(@Nonnull Object value) throws IOException { Constraint.isNotNull(value, "Value cannot be null"); return update( AnnotationSupport.getContext(value), AnnotationSupport.getKey(value), AnnotationSupport.getValue(value), AnnotationSupport.getExpiration(value)); }
/** {@inheritDoc} */ @Override public boolean deleteWithVersion(@Positive final long version, @Nonnull final Object value) throws IOException, VersionMismatchException { return deleteWithVersion(version, AnnotationSupport.getContext(value), AnnotationSupport.getKey(value)); }
final Expiration expField = getAnnotation(target, Expiration.class); final Object value = getFieldValue(target, expField.value()); if (value == null) { return null;