下面列出了org.apache.commons.lang3.math.NumberUtils#LONG_ZERO 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 文字と数字の混ざった文字列から、数字のみを取り出してLongを作成します。
* 主に、外部で採番されたIDから数字のIDを作成するために使用します。
*
* @param stringWithNumber
* @return
*/
public static Long extractNumberString(Long headerNumber, String stringWithNumber) {
StringBuilder sb = new StringBuilder(30);
if (headerNumber != null) {
sb.append(headerNumber.longValue());
}
for (int i = 0; i < stringWithNumber.length(); i++) {
if (CharUtils.isAsciiNumeric(stringWithNumber.charAt(i))) {
sb.append(stringWithNumber.charAt(i));
}
}
if (sb.length() == 0) {
return NumberUtils.LONG_ZERO;
} else if(sb.length() >= 19) {
// 19桁以上の場合は先頭の18文字を使用する
return Long.valueOf(sb.substring(0, 18));
} else {
return Long.valueOf(sb.toString());
}
}
/**
* Compares the {@code quantityOnStock} values of an {@link InventoryEntry} and an {@link InventoryEntryDraft}
* and returns an {@link Optional} of update action, which would contain the {@code "changeQuantity"}
* {@link UpdateAction}. If both {@link InventoryEntry} and {@link InventoryEntryDraft} have the same
* {@code quantityOnStock} values, then no update action is needed and empty optional will be returned.
* If the {@code quantityOnStock} from the {@code newEntry} is {@code null}, the new {@code quantityOnStock} will
* have a value of 0L.
*
* @param oldEntry the inventory entry that should be updated
* @param newEntry the inventory entry draft which contains new quantity on stock
* @return optional containing update action or empty optional if quantities on stock are identical
*/
@Nonnull
public static Optional<UpdateAction<InventoryEntry>> buildChangeQuantityAction(@Nonnull final InventoryEntry
oldEntry,
@Nonnull final InventoryEntryDraft
newEntry) {
final Long oldQuantityOnStock = oldEntry.getQuantityOnStock();
final Long newQuantityOnStock = newEntry.getQuantityOnStock() == null ? NumberUtils.LONG_ZERO : newEntry
.getQuantityOnStock();
return buildUpdateAction(oldQuantityOnStock, newQuantityOnStock, () -> ChangeQuantity.of(newQuantityOnStock));
}