cn.hutool.core.lang.Assert类的使用及代码示例

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

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

Assert介绍

[英]断言
断言某些对象或值是否符合规定,否则抛出异常。经常用于做变量检查
[中]

代码示例

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

/**
 * 获得一个类中所有字段列表,直接反射获取,无缓存
 * 
 * @param beanClass 类
 * @return 字段列表
 * @throws SecurityException 安全检查异常
 */
public static Constructor<?>[] getConstructorsDirectly(Class<?> beanClass) throws SecurityException {
  Assert.notNull(beanClass);
  return beanClass.getDeclaredConstructors();
}

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

/**
 * 写出图像为PNG格式
 * 
 * @param targetImageStream 写出到的目标流
 * @return 是否成功写出,如果返回false表示未找到合适的Writer
 * @throws IORuntimeException IO异常
 */
public boolean write(ImageOutputStream targetImageStream) throws IORuntimeException {
  Assert.notBlank(this.targetImageType, "Target image type is blank !");
  Assert.notNull(targetImageStream, "Target output stream is null !");
  final BufferedImage targetImage = (null == this.targetImage) ? this.srcImage : this.targetImage;
  Assert.notNull(targetImage, "Target image is null !");
  
  return ImageUtil.write(targetImage, this.targetImageType, targetImageStream, this.quality);
}

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

/**
 * 检查是否未关闭状态
 */
private void checkNotClosed() {
  Assert.isFalse(this.isClosed, "ExcelReader has been closed!");
}
// ------------------------------------------------------------------------------------------------------- Private methods end

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

/**
 * 检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出 {@link IllegalArgumentException}
 * 
 * <pre class="code">
 * Assert.notBlank(name, "Name must not be blank");
 * </pre>
 * 
 * @param text 被检查字符串
 * @return 非空字符串
 * @see StrUtil#isNotBlank(CharSequence)
 * @throws IllegalArgumentException 被检查字符串为空白
 */
public static String notBlank(String text) throws IllegalArgumentException {
  return notBlank(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
}

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

/**
 * 图像类型转换:GIF=》JPG、GIF=》PNG、PNG=》JPG、PNG=》GIF(X)、BMP=》PNG
 * 
 * @param srcImageFile 源图像文件
 * @param destImageFile 目标图像文件
 */
public static void convert(File srcImageFile, File destImageFile) {
  Assert.notNull(srcImageFile);
  Assert.notNull(destImageFile);
  Assert.isFalse(srcImageFile.equals(destImageFile), "Src file is equals to dest file!");
  final String srcExtName = FileUtil.extName(srcImageFile);
  final String destExtName = FileUtil.extName(destImageFile);
  if (StrUtil.equalsIgnoreCase(srcExtName, destExtName)) {
    // 扩展名相同直接复制文件
    FileUtil.copy(srcImageFile, destImageFile, true);
  }
  ImageOutputStream imageOutputStream = null;
  try {
    imageOutputStream = getImageOutputStream(destImageFile);
    convert(read(srcImageFile), destExtName, imageOutputStream, StrUtil.equalsIgnoreCase(IMAGE_TYPE_PNG, srcExtName));
  } finally {
    IoUtil.close(imageOutputStream);
  }
}

代码示例来源: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

/**
 * 检查下标(数组、集合、字符串)是否符合要求,下标必须满足:
 * 
 * <pre>
 * 0 <= index < size
 * </pre>
 * 
 * @param index 下标
 * @param size 长度
 * @return 检查后的下标
 * @throws IllegalArgumentException 如果size < 0 抛出此异常
 * @throws IndexOutOfBoundsException 如果index < 0或者 index >= size 抛出此异常
 * @since 4.1.9
 */
public static int checkIndex(int index, int size) throws IllegalArgumentException, IndexOutOfBoundsException {
  return checkIndex(index, size, "[Assertion failed]");
}

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

/**
 * 检查下标(数组、集合、字符串)是否符合要求,下标必须满足:
 * 
 * <pre>
 * 0 <= index < size
 * </pre>
 * 
 * @param index 下标
 * @param size 长度
 * @param errorMsgTemplate 异常时的消息模板
 * @param params 参数列表
 * @return 检查后的下标
 * @throws IllegalArgumentException 如果size < 0 抛出此异常
 * @throws IndexOutOfBoundsException 如果index < 0或者 index >= size 抛出此异常
 * @since 4.1.9
 */
public static int checkIndex(int index, int size, String errorMsgTemplate, Object... params) throws IllegalArgumentException, IndexOutOfBoundsException {
  if (index < 0 || index >= size) {
    throw new IndexOutOfBoundsException(badIndexMsg(index, size, errorMsgTemplate, params));
  }
  return index;
}

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

/**
 * 检查给定字符串是否为空白(null、空串或只包含空白符),为空抛出 {@link IllegalArgumentException}
 * 
 * <pre class="code">
 * Assert.notBlank(name, "Name must not be blank");
 * </pre>
 * 
 * @param text 被检查字符串
 * @return 非空字符串
 * @see StrUtil#isNotBlank(CharSequence)
 * @throws IllegalArgumentException 被检查字符串为空白
 */
public static String notBlank(String text) throws IllegalArgumentException {
  return notBlank(text, "[Assertion failed] - this String argument must have text; it must not be null, empty, or blank");
}

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

/**
 * 图像类型转换:GIF=》JPG、GIF=》PNG、PNG=》JPG、PNG=》GIF(X)、BMP=》PNG
 * 
 * @param srcImageFile 源图像文件
 * @param destImageFile 目标图像文件
 */
public static void convert(File srcImageFile, File destImageFile) {
  Assert.notNull(srcImageFile);
  Assert.notNull(destImageFile);
  Assert.isFalse(srcImageFile.equals(destImageFile), "Src file is equals to dest file!");
  final String srcExtName = FileUtil.extName(srcImageFile);
  final String destExtName = FileUtil.extName(destImageFile);
  if (StrUtil.equalsIgnoreCase(srcExtName, destExtName)) {
    // 扩展名相同直接复制文件
    FileUtil.copy(srcImageFile, destImageFile, true);
  }
  ImageOutputStream imageOutputStream = null;
  try {
    imageOutputStream = getImageOutputStream(destImageFile);
    convert(read(srcImageFile), destExtName, imageOutputStream, StrUtil.equalsIgnoreCase(IMAGE_TYPE_PNG, srcExtName));
  } finally {
    IoUtil.close(imageOutputStream);
  }
}

代码示例来源: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

/**
 * 检查下标(数组、集合、字符串)是否符合要求,下标必须满足:
 * 
 * <pre>
 * 0 <= index < size
 * </pre>
 * 
 * @param index 下标
 * @param size 长度
 * @return 检查后的下标
 * @throws IllegalArgumentException 如果size < 0 抛出此异常
 * @throws IndexOutOfBoundsException 如果index < 0或者 index >= size 抛出此异常
 * @since 4.1.9
 */
public static int checkIndex(int index, int size) throws IllegalArgumentException, IndexOutOfBoundsException {
  return checkIndex(index, size, "[Assertion failed]");
}

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

/**
 * 检查下标(数组、集合、字符串)是否符合要求,下标必须满足:
 * 
 * <pre>
 * 0 <= index < size
 * </pre>
 * 
 * @param index 下标
 * @param size 长度
 * @param errorMsgTemplate 异常时的消息模板
 * @param params 参数列表
 * @return 检查后的下标
 * @throws IllegalArgumentException 如果size < 0 抛出此异常
 * @throws IndexOutOfBoundsException 如果index < 0或者 index >= size 抛出此异常
 * @since 4.1.9
 */
public static int checkIndex(int index, int size, String errorMsgTemplate, Object... params) throws IllegalArgumentException, IndexOutOfBoundsException {
  if (index < 0 || index >= size) {
    throw new IndexOutOfBoundsException(badIndexMsg(index, size, errorMsgTemplate, params));
  }
  return index;
}

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

/**
 * 指定类是否为Enum类
 * 
 * @param clazz 类
 * @return 是否为Enum类
 */
public static boolean isEnum(Class<?> clazz) {
  Assert.notNull(clazz);
  return clazz.isEnum();
}

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

/**
 * 写出图像为PNG格式
 * 
 * @param targetImageStream 写出到的目标流
 * @return 是否成功写出,如果返回false表示未找到合适的Writer
 * @throws IORuntimeException IO异常
 */
public boolean write(ImageOutputStream targetImageStream) throws IORuntimeException {
  Assert.notBlank(this.targetImageType, "Target image type is blank !");
  Assert.notNull(targetImageStream, "Target output stream is null !");
  final BufferedImage targetImage = (null == this.targetImage) ? this.srcImage : this.targetImage;
  Assert.notNull(targetImage, "Target image is null !");
  
  return ImageUtil.write(targetImage, this.targetImageType, targetImageStream, this.quality);
}

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

/**
   * 创建批量操作的{@link PreparedStatement}
   * 
   * @param conn 数据库连接
   * @param sql SQL语句,使用"?"做为占位符
   * @param paramsBatch "?"对应参数批次列表
   * @return {@link PreparedStatement}
   * @throws SQLException SQL异常
   * @since 4.1.13
   */
  public static PreparedStatement prepareStatementForBatch(Connection conn, String sql, Object[]... paramsBatch) throws SQLException {
    Assert.notBlank(sql, "Sql String must be not blank!");
    
    sql = sql.trim();
//        SqlLog.INSTASNCE.log(sql, paramsBatch);
    PreparedStatement ps = conn.prepareStatement(sql);
    for (Object[] params : paramsBatch) {
      StatementUtil.fillParams(ps, params);
      ps.addBatch();
    }
    return ps;
  }

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

/**
 * 检查是否未关闭状态
 */
private void checkNotClosed() {
  Assert.isFalse(this.isClosed, "ExcelReader has been closed!");
}
// ------------------------------------------------------------------------------------------------------- Private methods end

代码示例来源: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

/**
 * 获得一个类中所有字段列表,直接反射获取,无缓存
 * 
 * @param beanClass 类
 * @return 字段列表
 * @throws SecurityException 安全检查异常
 */
public static Constructor<?>[] getConstructorsDirectly(Class<?> beanClass) throws SecurityException {
  Assert.notNull(beanClass);
  return beanClass.getDeclaredConstructors();
}

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

/**
 * 通过JDK7+的 {@link Files#copy(Path, Path, CopyOption...)} 方法拷贝文件
 * 
 * @param src 源文件路径
 * @param dest 目标文件或目录路径,如果为目录使用与源文件相同的文件名
 * @param options {@link StandardCopyOption}
 * @return File
 * @throws IORuntimeException IO异常
 */
public static File copyFile(String src, String dest, StandardCopyOption... options) throws IORuntimeException {
  Assert.notBlank(src, "Source File path is blank !");
  Assert.notNull(src, "Destination File path is null !");
  return copyFile(Paths.get(src), Paths.get(dest), options).toFile();
}

相关文章