cn.hutool.core.lang.Assert.isTrue()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(8.7k)|赞(0)|评价(0)|浏览(429)

本文整理了Java中cn.hutool.core.lang.Assert.isTrue()方法的一些代码示例,展示了Assert.isTrue()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Assert.isTrue()方法的具体详情如下:
包路径:cn.hutool.core.lang.Assert
类名称:Assert
方法名:isTrue

Assert.isTrue介绍

[英]断言是否为真,如果为 false 抛出 IllegalArgumentException 异常

Assert.isTrue(i > 0, "The value must be greater than zero");

[中]断言是否为真,如果为 错误的抛出 非法辩论例外异常

Assert.isTrue(i > 0, "The value must be greater than zero");

代码示例

代码示例来源:origin: looly/hutool

/**
 * 断言是否为真,如果为 {@code false} 抛出 {@code IllegalArgumentException} 异常<br>
 * 
 * <pre class="code">
 * Assert.isTrue(i &gt; 0, "The value must be greater than zero");
 * </pre>
 * 
 * @param expression 波尔值
 * @throws IllegalArgumentException if expression is {@code false}
 */
public static void isTrue(boolean expression) throws IllegalArgumentException {
  isTrue(expression, "[Assertion failed] - this expression must be true");
}

代码示例来源:origin: looly/hutool

/**
 * 断言是否为真,如果为 {@code false} 抛出 {@code IllegalArgumentException} 异常<br>
 * 
 * <pre class="code">
 * Assert.isTrue(i &gt; 0, "The value must be greater than zero");
 * </pre>
 * 
 * @param expression 波尔值
 * @throws IllegalArgumentException if expression is {@code false}
 */
public static void isTrue(boolean expression) throws IllegalArgumentException {
  isTrue(expression, "[Assertion failed] - this expression must be true");
}

代码示例来源:origin: looly/hutool

/**
 * <p>
 * Two randomly chosen, odd numbers must be passed in. Ideally these should be different for each class,
 * however this is not vital.
 * </p>
 *
 * <p>
 * Prime numbers are preferred, especially for the multiplier.
 * </p>
 *
 * @param initialOddNumber
 *            an odd number used as the initial value
 * @param multiplierOddNumber
 *            an odd number used as the multiplier
 * @throws IllegalArgumentException
 *             if the number is even
 */
public HashCodeBuilder(final int initialOddNumber, final int multiplierOddNumber) {
  Assert.isTrue(initialOddNumber % 2 != 0, "HashCodeBuilder requires an odd initial value");
  Assert.isTrue(multiplierOddNumber % 2 != 0, "HashCodeBuilder requires an odd multiplier");
  iConstant = multiplierOddNumber;
  iTotal = initialOddNumber;
}

代码示例来源:origin: looly/hutool

/**
 * <p>
 * Two randomly chosen, odd numbers must be passed in. Ideally these should be different for each class,
 * however this is not vital.
 * </p>
 *
 * <p>
 * Prime numbers are preferred, especially for the multiplier.
 * </p>
 *
 * @param initialOddNumber
 *            an odd number used as the initial value
 * @param multiplierOddNumber
 *            an odd number used as the multiplier
 * @throws IllegalArgumentException
 *             if the number is even
 */
public HashCodeBuilder(final int initialOddNumber, final int multiplierOddNumber) {
  Assert.isTrue(initialOddNumber % 2 != 0, "HashCodeBuilder requires an odd initial value");
  Assert.isTrue(multiplierOddNumber % 2 != 0, "HashCodeBuilder requires an odd multiplier");
  iConstant = multiplierOddNumber;
  iTotal = initialOddNumber;
}

代码示例来源:origin: looly/hutool

/**
 * 是否是质数(素数)<br>
 * 质数表的质数又称素数。指整数在一个大于1的自然数中,除了1和此整数自身外,没法被其他自然数整除的数。
 * 
 * @param n 数字
 * @return 是否是质数
 */
public static boolean isPrimes(int n) {
  Assert.isTrue(n > 1, "The number must be > 1");
  for (int i = 2; i <= Math.sqrt(n); i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

代码示例来源:origin: looly/hutool

/**
 * 是否是质数(素数)<br>
 * 质数表的质数又称素数。指整数在一个大于1的自然数中,除了1和此整数自身外,没法被其他自然数整除的数。
 * 
 * @param n 数字
 * @return 是否是质数
 */
public static boolean isPrimes(int n) {
  Assert.isTrue(n > 1, "The number must be > 1");
  for (int i = 2; i <= Math.sqrt(n); i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

代码示例来源:origin: looly/hutool

/**
 * 限制字符串长度,如果超过指定长度,截取指定长度并在末尾加"..."
 * 
 * @param string 字符串
 * @param length 最大长度
 * @return 切割后的剩余的前半部分字符串+"..."
 * @since 4.0.10
 */
public static String maxLength(CharSequence string, int length) {
  Assert.isTrue(length > 0);
  if (null == string) {
    return null;
  }
  if (string.length() <= length) {
    return string.toString();
  }
  return sub(string, 0, length) + "...";
}

代码示例来源:origin: looly/hutool

/**
 * 限制字符串长度,如果超过指定长度,截取指定长度并在末尾加"..."
 * 
 * @param string 字符串
 * @param length 最大长度
 * @return 切割后的剩余的前半部分字符串+"..."
 * @since 4.0.10
 */
public static String maxLength(CharSequence string, int length) {
  Assert.isTrue(length > 0);
  if (null == string) {
    return null;
  }
  if (string.length() <= length) {
    return string.toString();
  }
  return sub(string, 0, length) + "...";
}

代码示例来源:origin: looly/hutool

final Class<?>[] types = method.getParameterTypes();
if (null != types && null != args) {
  Assert.isTrue(args.length == types.length, "Params length [{}] is not fit for param length [{}] of method !", args.length, types.length);
  Class<?> type;
  for (int i = 0; i < args.length; i++) {

代码示例来源:origin: looly/hutool

final Class<?>[] types = method.getParameterTypes();
if (null != types && null != args) {
  Assert.isTrue(args.length == types.length, "Params length [{}] is not fit for param length [{}] of method !", args.length, types.length);
  Class<?> type;
  for (int i = 0; i < args.length; i++) {

代码示例来源:origin: looly/hutool

/**
   * 列举指定日期范围内所有匹配表达式的日期
   * 
   * @param pattern 表达式
   * @param start 起始时间
   * @param end 结束时间
   * @param count 列举数量
   * @param isMatchSecond 是否匹配秒
   * @return 日期列表
   */
  public static List<Date> matchedDates(CronPattern pattern, long start, long end, int count, boolean isMatchSecond) {
    Assert.isTrue(start < end, "Start date is later than end !");

    final List<Date> result = new ArrayList<>(count);
    long step = isMatchSecond ? DateUnit.SECOND.getMillis() : DateUnit.MINUTE.getMillis();
    for (long i = start; i < end; i += step) {
      if (pattern.match(i, isMatchSecond)) {
        result.add(DateUtil.date(i));
        if (result.size() >= count) {
          break;
        }
      }
    }
    return result;
  }
}

代码示例来源:origin: looly/hutool

/**
   * 列举指定日期范围内所有匹配表达式的日期
   * 
   * @param pattern 表达式
   * @param start 起始时间
   * @param end 结束时间
   * @param count 列举数量
   * @param isMatchSecond 是否匹配秒
   * @return 日期列表
   */
  public static List<Date> matchedDates(CronPattern pattern, long start, long end, int count, boolean isMatchSecond) {
    Assert.isTrue(start < end, "Start date is later than end !");

    final List<Date> result = new ArrayList<>(count);
    long step = isMatchSecond ? DateUnit.SECOND.getMillis() : DateUnit.MINUTE.getMillis();
    for (long i = start; i < end; i += step) {
      if (pattern.match(i, isMatchSecond)) {
        result.add(DateUtil.date(i));
        if (result.size() >= count) {
          break;
        }
      }
    }
    return result;
  }
}

代码示例来源:origin: cn.hutool/hutool-all

/**
 * 断言是否为真,如果为 {@code false} 抛出 {@code IllegalArgumentException} 异常<br>
 * 
 * <pre class="code">
 * Assert.isTrue(i &gt; 0, "The value must be greater than zero");
 * </pre>
 * 
 * @param expression 波尔值
 * @throws IllegalArgumentException if expression is {@code false}
 */
public static void isTrue(boolean expression) throws IllegalArgumentException {
  isTrue(expression, "[Assertion failed] - this expression must be true");
}

代码示例来源:origin: cn.hutool/hutool-all

/**
 * <p>
 * Two randomly chosen, odd numbers must be passed in. Ideally these should be different for each class,
 * however this is not vital.
 * </p>
 *
 * <p>
 * Prime numbers are preferred, especially for the multiplier.
 * </p>
 *
 * @param initialOddNumber
 *            an odd number used as the initial value
 * @param multiplierOddNumber
 *            an odd number used as the multiplier
 * @throws IllegalArgumentException
 *             if the number is even
 */
public HashCodeBuilder(final int initialOddNumber, final int multiplierOddNumber) {
  Assert.isTrue(initialOddNumber % 2 != 0, "HashCodeBuilder requires an odd initial value");
  Assert.isTrue(multiplierOddNumber % 2 != 0, "HashCodeBuilder requires an odd multiplier");
  iConstant = multiplierOddNumber;
  iTotal = initialOddNumber;
}

代码示例来源:origin: cn.hutool/hutool-all

/**
 * 是否是质数(素数)<br>
 * 质数表的质数又称素数。指整数在一个大于1的自然数中,除了1和此整数自身外,没法被其他自然数整除的数。
 * 
 * @param n 数字
 * @return 是否是质数
 */
public static boolean isPrimes(int n) {
  Assert.isTrue(n > 1, "The number must be > 1");
  for (int i = 2; i <= Math.sqrt(n); i++) {
    if (n % i == 0) {
      return false;
    }
  }
  return true;
}

代码示例来源:origin: cn.hutool/hutool-all

/**
 * 限制字符串长度,如果超过指定长度,截取指定长度并在末尾加"..."
 * 
 * @param string 字符串
 * @param length 最大长度
 * @return 切割后的剩余的前半部分字符串+"..."
 * @since 4.0.10
 */
public static String maxLength(CharSequence string, int length) {
  Assert.isTrue(length > 0);
  if (null == string) {
    return null;
  }
  if (string.length() <= length) {
    return string.toString();
  }
  return sub(string, 0, length) + "...";
}

代码示例来源:origin: cn.hutool/hutool-all

final Class<?>[] types = method.getParameterTypes();
if (null != types && null != args) {
  Assert.isTrue(args.length == types.length, "Params length [{}] is not fit for param length [{}] of method !", args.length, types.length);
  Class<?> type;
  for (int i = 0; i < args.length; i++) {

代码示例来源:origin: cn.hutool/hutool-all

/**
   * 列举指定日期范围内所有匹配表达式的日期
   * 
   * @param pattern 表达式
   * @param start 起始时间
   * @param end 结束时间
   * @param count 列举数量
   * @param isMatchSecond 是否匹配秒
   * @return 日期列表
   */
  public static List<Date> matchedDates(CronPattern pattern, long start, long end, int count, boolean isMatchSecond) {
    Assert.isTrue(start < end, "Start date is later than end !");

    final List<Date> result = new ArrayList<>(count);
    long step = isMatchSecond ? DateUnit.SECOND.getMillis() : DateUnit.MINUTE.getMillis();
    for (long i = start; i < end; i += step) {
      if (pattern.match(i, isMatchSecond)) {
        result.add(DateUtil.date(i));
        if (result.size() >= count) {
          break;
        }
      }
    }
    return result;
  }
}

相关文章