Guava Preconditions

Preconditions

断言,相同的还有Objects、Java assert key word、hamcrest。

使用

  • 构造方法

构造私有,与Joiner、Splitter不同,Preconditions提供了大量的静态方法,直接类名.方法名调用节课,不需要获取Preconditions对象。

private Preconditions() {}
  • 其他方法

checkArgument方法,检查参数,如果参数为false,抛出IllegalArgumentException。

public static void checkArgument(boolean expression) {
    if (!expression) {
        throw new IllegalArgumentException();
    }
}

checkState方法,检查状态,状态为false,抛出IllegalStateException。

public static void checkState(boolean expression, @Nullable Object errorMessage) {
    if (!expression) {
        throw new IllegalStateException(String.valueOf(errorMessage));
    }
}

checkNotNull方法,不能为null,为null抛出NullPointerException。

// 返回值可以忽略
@CanIgnoreReturnValue
public static <T> T checkNotNull(T reference) {
    if (reference == null) {
        throw new NullPointerException();
    }
    return reference;
}

checkElementIndex方法,检查元素索引值是否有效,越界抛出IndexOutOfBoundsException。

@CanIgnoreReturnValue
public static int checkElementIndex(int index, int size) {
    return checkElementIndex(index, size, "index");
}
@CanIgnoreReturnValue
public static int checkElementIndex(int index, int size, @Nullable String desc) {
    // 越界
    if (index < 0 || index >= size) {
        throw new IndexOutOfBoundsException(badElementIndex(index, size, desc));
    }
    return index;
}


private static String badElementIndex(int index, int size, String desc) {
    if (index < 0) {// index为负数,格式化index越界报错信息
        return format("%s (%s) must not be negative", desc, index);
    } else if (size < 0) {// size为负数,抛出size参数异常
        throw new IllegalArgumentException("negative size: " + size);
    } else { // index >= size,越界报错信息
        return format("%s (%s) must be less than size (%s)", desc, index, size);
    }
}

checkPositionIndex方法,检查下标索引值是否有效。

@CanIgnoreReturnValue
public static int checkPositionIndex(int index, int size) {
    return checkPositionIndex(index, size, "index");
}
@CanIgnoreReturnValue
public static int checkPositionIndex(int index, int size, @Nullable String desc) {
    // Carefully optimized for execution by hotspot (explanatory comment above)
    if (index < 0 || index > size) {
        throw new IndexOutOfBoundsException(badPositionIndex(index, size, desc));
    }
    return index;
}


private static String badPositionIndex(int index, int size, String desc) {
    if (index < 0) {
        return format("%s (%s) must not be negative", desc, index);
    } else if (size < 0) {
        throw new IllegalArgumentException("negative size: " + size);
    } else { // index > size
        return format("%s (%s) must not be greater than size (%s)", desc, index, size);
    }
}

format方法,将可变参数列表中的参数替换到template的占位符%s中,如同System.out.println方法。

static String format(String template, @Nullable Object... args) {
    template = String.valueOf(template); // null -> "null"

    // 将变长参数替换到%s占位符中
    StringBuilder builder = new StringBuilder(template.length() + 16 * args.length);
    int templateStart = 0;
    int i = 0;
    while (i < args.length) {
    	// 获取占位符"%s"的下标赋予placeholderStart
        int placeholderStart = template.indexOf("%s", templateStart);
      	// 无占位符退出
      	if (placeholderStart == -1) {
            break;
        }
        // 将template追加到builder的[template, placeholderStart)序列中
      	builder.append(template, templateStart, placeholderStart);
      	// 追加参数
      	builder.append(args[i++]);
        templateStart = placeholderStart + 2;
    }
    builder.append(template, templateStart, template.length());

    // if we run out of placeholders, append the extra args in square braces
    if (i < args.length) {
        builder.append(" [");
        builder.append(args[i++]);
        while (i < args.length) {
        	builder.append(", ");
            builder.append(args[i++]);
        }
        builder.append(']');
    }

    return builder.toString();
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值