/** * Limits the length of the given string to the given length. * * @param input the object which string representation should be limited to the given length * @param length the max. number of characters to return * @return a part of the string representation of the given <tt>input</tt>. If input is shorter * than <tt>length</tt>, the full value is returned. If input is <tt>null</tt>, "" is returned. */ public static String limit(@Nullable Object input, int length) { return limit(input, length, false); }
/** * Sets the given value (its string representation), mit limits this to <tt>limit</tt> characters. * * @param key the key to which the value should be associated * @param value the value which string representation should be put into the context * @param limit the maximal number of characters to put into the map. Everything after that will be discarded */ public void putLimited(String key, Object value, int limit) { set(key, Strings.limit(value, limit)); }
@Override public String generateDropForeignKey(Table table, ForeignKey key) { return MessageFormat.format("ALTER TABLE `{0}` DROP FOREIGN KEY `{1}`", table.getName(), Strings.limit(key.getName(), getConstraintCharacterLimit())); }
/** * Returns a verbose representation of the entity containing all fields. * * @return a string representation of the entity containing all fields */ public String toDebugString() { StringBuilder sb = new StringBuilder(); sb.append(id + " (Version: " + version + ") {"); boolean first = true; for (Property p : index.getDescriptor(getClass()).getProperties()) { if (first) { first = false; } else { sb.append(", "); } sb.append(p.getName()); sb.append(": "); sb.append("'"); sb.append(Strings.limit(p.writeToSource(this), 50)); sb.append("'"); } sb.append("}"); return sb.toString(); }
@Override public String generateAddForeignKey(Table table, ForeignKey key) { return MessageFormat.format("ALTER TABLE `{0}` ADD CONSTRAINT `{1}` FOREIGN KEY ({2}) REFERENCES `{3}` ({4})", table.getName(), Strings.limit(key.getName(), getConstraintCharacterLimit()), String.join(", ", key.getColumns()), key.getForeignTable(), String.join(", ", key.getForeignColumns())); }
@Override protected Object transformToDatasource(Class<? extends BaseMapper<?, ?, ?>> mapperType, Object object) { if (length > 0 && !lob && object != null && ((String) object).length() > length) { throw Exceptions.handle() .to(Mixing.LOG) .withNLSKey("StringProperty.dataTruncation") .set("value", Strings.limit(object, 30)) .set("field", getLabel()) .set("length", ((String) object).length()) .set("maxLength", length) .handle(); } return object; }