/** * Returns the wrapped data converted to a string or <tt>defaultValue</tt> if the wrapped value is <tt>null</tt> * <p> * The conversion method used is {@link NLS#toMachineString(Object)} * * @param defaultValue the value to use if the wrapped object was <tt>null</tt> * @return a string representation of the wrapped object * or <tt>defaultValue</tt> if the wrapped value is <tt>null</tt> */ @Nonnull public String asString(@Nonnull String defaultValue) { return isNull() ? defaultValue : NLS.toMachineString(data); }
/** * Returns the data converted to a string, or <tt>null</tt> if the wrapped value is null * <p> * The conversion method used is {@link NLS#toMachineString(Object)} * * @return a string representation of the wrapped object or <tt>null</tt> if the wrapped value is <tt>null</tt> */ @Nullable public String getString() { return isNull() ? null : NLS.toMachineString(data); }
/** * Returns the length of the string representation of the wrapped value. * * @return the length of the string representation of the wrapped value or 0, if the wrapped value is <tt>null</tt> */ public int length() { if (isNull()) { return 0; } return asString().length(); }
/** * Returns an lowercase version of the string representation of the wrapped value. * * @return an lowercase version of the string representation of the wrapped value or {@code ""} if the * wrapped value is <tt>null</tt> */ @Nonnull public String toLowerCase() { if (isNull()) { return ""; } return asString().toLowerCase(); }
/** * Returns an uppercase version of the string representation of the wrapped value. * * @return an uppercase version of the string representation of the wrapped value or {@code ""} if the * wrapped value is <tt>null</tt> */ @Nonnull public String toUpperCase() { if (isNull()) { return ""; } return asString().toUpperCase(); }
@SuppressWarnings({"unchecked", "raw", "rawtypes"}) @Override protected Object transformFromDatasource(Class<? extends BaseMapper<?, ?, ?>> mapperType, Value data) { if (data.isNull()) { return null; } if (ordinal) { Object[] values = field.getType().getEnumConstants(); int index = data.asInt(0); index = Math.min(index, values.length - 1); return values[index]; } return data.asEnum((Class<Enum>) field.getType()); }
/** * Returns a substring of the string representation of the wrapped value. * <p> * Returns the substring starting at <tt>startIndex</tt> and ending at <tt>endIndex</tt>. If the given * end index is greater than the string length, the complete substring from <tt>startIndex</tt> to the end of * the string is returned. If the <tt>startIndex</tt> is greater than the string length, {@code ""} is * returned. * * @param startIndex the index of the first character to be included in the sub string * @param endIndex the index of the last character to be included in the sub string * @return a substring like {@link String#substring(int, int)} or {@code ""} if the wrapped value */ @Nonnull public String substring(int startIndex, int endIndex) { if (isNull()) { return ""; } String value = asString(); if (startIndex > value.length()) { return ""; } return value.substring(startIndex, Math.min(value.length(), endIndex)); }
/** * Returns the long value for the wrapped value or <tt>null</tt> if the wrapped value isn't a long and * cannot be converted to one. * <p> * If the wrapped value is a <tt>Long</tt>, <tt>Integer</tt> or <tt>BigDecimal</tt>, it is either directly * returned or by calling {@link java.math.BigDecimal#longValue()}. * <p> * Otherwise {@link Long#parseLong(String)} is called on the string representation of the wrapped value. If * parsing fails, or if the wrapped value was <tt>null</tt>, <tt>null</tt> will be returned. * * @return the wrapped value casted or converted to <tt>Long</tt> or <tt>null</tt> * if no conversion is possible. */ @Nullable public Long getLong() { try { if (isNull()) { return null; } if (data instanceof Long) { return (Long) data; } return Long.parseLong(String.valueOf(data).trim()); } catch (NumberFormatException e) { Exceptions.ignore(e); return null; } }
public Integer getInteger() { try { if (isNull()) { return null;
if (isNull()) { return "";
if (isNull()) { return defaultValue;
if (isNull()) { return defaultValue;
if (isNull()) { return defaultValue;
if (isNull()) { return "";
/** * Converts the wrapped value to a <tt>boolean</tt> or returns the given <tt>defaultValue</tt> * if no conversion is possible. * <p> * To convert a value, {@link Boolean#parseBoolean(String)} is used, where {@code toString} is called on all * non-string objects. * * @param defaultValue the value to be used if the wrapped value cannot be converted to a boolean. * @return <tt>true</tt> if the wrapped value is <tt>true</tt> * or if the string representation of it is {@code "true"}. Returns <tt>false</tt> otherwise, * especially if the wrapped value is <tt>null</tt> */ public boolean asBoolean(boolean defaultValue) { if (isNull() || Strings.isEmpty(data)) { return defaultValue; } if (data instanceof Boolean) { return (Boolean) data; } return NLS.parseUserString(Boolean.class, String.valueOf(data).trim()); }
@SuppressWarnings("unchecked") @Override protected Object transformFromDatasource(Class<? extends BaseMapper<?, ?, ?>> mapperType, Value object) { if (mapperType != Mango.class) { throw new UnsupportedOperationException( "MultiPointLocationProperty currently only supports Mango as mapper!"); } List<Tuple<Double, Double>> locations = new ArrayList<>(); if (!object.isNull()) { Object coordinates = ((Document) object.get()).get("coordinates"); if (coordinates instanceof List<?>) { ((List<List<Double>>) coordinates).stream() .map(entry -> Tuple.create(entry.get(0), entry.get(1))) .collect(Lambdas.into(locations)); } } return locations; }
@Override public void readFromRequest(Entity entity, WebContext ctx) { try { if (ctx.get(getName()).isNull()) { getField().set(entity, false); } else { getField().set(entity, transformFromRequest(getName(), ctx)); } } catch (IllegalAccessException e) { Exceptions.handle(IndexAccess.LOG, e); } }
/** * Returns and converts the field value from the given request and writes it into the given entity. * * @param entity the entity to update * @param ctx the request to read the data from */ public void readFromRequest(Entity entity, WebContext ctx) { try { if (ctx.get(getName()).isNull()) { return; } field.set(entity, transformFromRequest(getName(), ctx)); } catch (IllegalAccessException e) { Exceptions.handle(IndexAccess.LOG, e); } }
@Override public void readFromRequest(Entity entity, WebContext ctx) { try { if (ctx.get(getName()).isNull()) { return; } ((EntityRef<?>) getField().get(entity)).setId(ctx.get(getName()).asString()); } catch (IllegalAccessException e) { throw Exceptions.handle(IndexAccess.LOG, e); } }
/** * Returns the {@link Value} defined for the given key or throws a <tt>HandledException</tt> if no value was found * <p> * If this extension doesn't provide a value for this key, but there is an extension with the name * <tt>default</tt> which provides a value, this is used. * <p> * Returning a {@link Value} instead of a plain object provides lots of conversion methods on the one hand * and also guarantees a non null result on the other hand, since a <tt>Value</tt> can be empty. * * @param path the access path to retrieve the value * @return the value wrapping the contents for the given path. This will never by <tt>null</tt>. * @throws sirius.kernel.health.HandledException if no value was found for the given <tt>path</tt> */ @Nonnull public Value require(String path) { Value result = get(path); if (result.isNull()) { throw Exceptions.handle() .to(LOG) .withSystemErrorMessage("The extension '%s' of type '%s' doesn't provide a value for: '%s'", id, type, path) .handle(); } return result; }