add methods

This commit is contained in:
Looly 2022-05-05 11:52:51 +08:00
parent fa9d095e4e
commit 78a5c53d98
10 changed files with 111 additions and 26 deletions

View File

@ -1497,12 +1497,12 @@ public class CollUtil {
if (t instanceof Map) {
final Map<?, ?> map = (Map<?, ?>) t;
final Object value = map.get(fieldName);
return ObjUtil.equal(value, fieldValue);
return ObjUtil.equals(value, fieldValue);
}
// 普通Bean
final Object value = FieldUtil.getFieldValue(t, fieldName);
return ObjUtil.equal(value, fieldValue);
return ObjUtil.equals(value, fieldValue);
});
}

View File

@ -50,7 +50,7 @@ public class VersionComparator implements Comparator<String>, Serializable {
*/
@Override
public int compare(final String version1, final String version2) {
if(ObjUtil.equal(version1, version2)) {
if(ObjUtil.equals(version1, version2)) {
return 0;
}
if (version1 == null && version2 == null) {

View File

@ -206,7 +206,7 @@ public class Validator {
* @return 当两值都为null或相等返回true
*/
public static boolean equal(final Object t1, final Object t2) {
return ObjUtil.equal(t1, t2);
return ObjUtil.equals(t1, t2);
}
/**

View File

@ -117,7 +117,7 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
public List<V> getValues(final K key) {
return CollUtil.getAny(
this.values,
ListUtil.indexOfAll(this.keys, (ele) -> ObjUtil.equal(ele, key))
ListUtil.indexOfAll(this.keys, (ele) -> ObjUtil.equals(ele, key))
);
}
@ -131,7 +131,7 @@ public class TableMap<K, V> implements Map<K, V>, Iterable<Map.Entry<K, V>>, Ser
public List<K> getKeys(final V value) {
return CollUtil.getAny(
this.keys,
ListUtil.indexOfAll(this.values, (ele) -> ObjUtil.equal(ele, value))
ListUtil.indexOfAll(this.values, (ele) -> ObjUtil.equals(ele, value))
);
}

View File

@ -216,9 +216,9 @@ public abstract class AbsTable<R, C, V> implements Table<R, C, V> {
}
if (obj instanceof Cell) {
final Cell<?, ?, ?> other = (Cell<?, ?, ?>) obj;
return ObjUtil.equal(rowKey, other.getRowKey())
&& ObjUtil.equal(columnKey, other.getColumnKey())
&& ObjUtil.equal(value, other.getValue());
return ObjUtil.equals(rowKey, other.getRowKey())
&& ObjUtil.equals(columnKey, other.getColumnKey())
&& ObjUtil.equals(value, other.getValue());
}
return false;
}

View File

@ -172,7 +172,7 @@ public class TreeUtil {
* @since 5.2.4
*/
public static <T> Tree<T> getNode(final Tree<T> node, final T id) {
if (ObjUtil.equal(id, node.getId())) {
if (ObjUtil.equals(id, node.getId())) {
return node;
}

View File

@ -797,7 +797,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 3.0.7
*/
public static <T> int indexOf(final T[] array, final Object value, final int beginIndexInclude) {
return matchIndex((obj) -> ObjUtil.equal(value, obj), beginIndexInclude, array);
return matchIndex((obj) -> ObjUtil.equals(value, obj), beginIndexInclude, array);
}
/**
@ -810,7 +810,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
* @since 3.0.7
*/
public static <T> int indexOf(final T[] array, final Object value) {
return matchIndex((obj) -> ObjUtil.equal(value, obj), array);
return matchIndex((obj) -> ObjUtil.equals(value, obj), array);
}
/**
@ -861,7 +861,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
public static <T> int lastIndexOf(final T[] array, final Object value, final int endInclude) {
if (isNotEmpty(array)) {
for (int i = endInclude; i >= 0; i--) {
if (ObjUtil.equal(value, array[i])) {
if (ObjUtil.equals(value, array[i])) {
return i;
}
}
@ -1816,7 +1816,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
}
for (int i = 0; i < subArray.length; i++) {
if (false == ObjUtil.equal(array[i + firstIndex], subArray[i])) {
if (false == ObjUtil.equals(array[i + firstIndex], subArray[i])) {
return indexOfSub(array, firstIndex + 1, subArray);
}
}
@ -1861,7 +1861,7 @@ public class ArrayUtil extends PrimitiveArrayUtil {
}
for (int i = 0; i < subArray.length; i++) {
if (false == ObjUtil.equal(array[i + firstIndex], subArray[i])) {
if (false == ObjUtil.equals(array[i + firstIndex], subArray[i])) {
return lastIndexOfSub(array, firstIndex - 1, subArray);
}
}

View File

@ -144,7 +144,7 @@ public class EnumUtil {
continue;
}
for (final Enum<?> enumObj : enums) {
if (ObjUtil.equal(value, FieldUtil.getFieldValue(enumObj, field))) {
if (ObjUtil.equals(value, FieldUtil.getFieldValue(enumObj, field))) {
return (E) enumObj;
}
}

View File

@ -1,5 +1,6 @@
package cn.hutool.core.lang;
import cn.hutool.core.text.StrUtil;
import org.junit.Test;
public class AssertTest {
@ -35,4 +36,27 @@ public class AssertTest {
//noinspection ConstantConditions
Assert.isTrue(i > 0, ()-> new IndexOutOfBoundsException("relation message to return"));
}
@Test
public void equalsTest() {
//String a="ab";
//final String b = new String("abc");
final String a = null;
final String b = null;
Assert.equals(a, b);
Assert.equals(a, b, "{}不等于{}", a, b);
Assert.equals(a, b, () -> new RuntimeException(StrUtil.format("{}和{}不相等", a, b)));
}
@Test
public void notEqualsTest() {
//String c="19";
//final String d = new String("19");
final String c = null;
final String d = "null";
//Assert.notEquals(c,d);
//Assert.notEquals(c,d,"{}等于{}",c,d);
Assert.notEquals(c, d, () -> new RuntimeException(StrUtil.format("{}和{}相等", c, d)));
}
}

View File

@ -277,6 +277,56 @@ public class CellUtil {
return false;
}
/**
* 获取合并单元格{@link CellRangeAddress}如果不是返回null
*
* @param sheet {@link Sheet}
* @param locationRef 单元格地址标识符例如A11B5
* @return {@link CellRangeAddress}
* @since 5.8.0
*/
public static CellRangeAddress getCellRangeAddress(final Sheet sheet, final String locationRef) {
final CellLocation cellLocation = ExcelUtil.toLocation(locationRef);
return getCellRangeAddress(sheet, cellLocation.getX(), cellLocation.getY());
}
/**
* 获取合并单元格{@link CellRangeAddress}如果不是返回null
*
* @param cell {@link Cell}
* @return {@link CellRangeAddress}
* @since 5.8.0
*/
public static CellRangeAddress getCellRangeAddress(final Cell cell) {
return getCellRangeAddress(cell.getSheet(), cell.getColumnIndex(), cell.getRowIndex());
}
/**
* 获取合并单元格{@link CellRangeAddress}如果不是返回null
*
* @param sheet {@link Sheet}
* @param x 列号从0开始
* @param y 行号从0开始
* @return {@link CellRangeAddress}
* @since 5.8.0
*/
public static CellRangeAddress getCellRangeAddress(final Sheet sheet, final int x, final int y) {
if (sheet != null) {
final int sheetMergeCount = sheet.getNumMergedRegions();
CellRangeAddress ca;
for (int i = 0; i < sheetMergeCount; i++) {
ca = sheet.getMergedRegion(i);
if (y >= ca.getFirstRow() && y <= ca.getLastRow()
&& x >= ca.getFirstColumn() && x <= ca.getLastColumn()) {
return ca;
}
}
}
return null;
}
/**
* 合并单元格可以根据设置的值来合并行和列
*
@ -310,16 +360,7 @@ public class CellUtil {
lastColumn // last column (0-based)
);
if (null != cellStyle) {
RegionUtil.setBorderTop(cellStyle.getBorderTop(), cellRangeAddress, sheet);
RegionUtil.setBorderRight(cellStyle.getBorderRight(), cellRangeAddress, sheet);
RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cellRangeAddress, sheet);
RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cellRangeAddress, sheet);
RegionUtil.setTopBorderColor(cellStyle.getTopBorderColor(),cellRangeAddress,sheet);
RegionUtil.setRightBorderColor(cellStyle.getRightBorderColor(),cellRangeAddress,sheet);
RegionUtil.setLeftBorderColor(cellStyle.getLeftBorderColor(),cellRangeAddress,sheet);
RegionUtil.setBottomBorderColor(cellStyle.getBottomBorderColor(),cellRangeAddress,sheet);
}
setMergeCellStyle(cellStyle, cellRangeAddress, sheet);
return sheet.addMergedRegion(cellRangeAddress);
}
@ -435,5 +476,25 @@ public class CellUtil {
}
return null;
}
/**
* 根据{@link CellStyle}设置合并单元格边框样式
*
* @param cellStyle {@link CellStyle}
* @param cellRangeAddress {@link CellRangeAddress}
* @param sheet {@link Sheet}
*/
private static void setMergeCellStyle(final CellStyle cellStyle, final CellRangeAddress cellRangeAddress, final Sheet sheet) {
if (null != cellStyle) {
RegionUtil.setBorderTop(cellStyle.getBorderTop(), cellRangeAddress, sheet);
RegionUtil.setBorderRight(cellStyle.getBorderRight(), cellRangeAddress, sheet);
RegionUtil.setBorderBottom(cellStyle.getBorderBottom(), cellRangeAddress, sheet);
RegionUtil.setBorderLeft(cellStyle.getBorderLeft(), cellRangeAddress, sheet);
RegionUtil.setTopBorderColor(cellStyle.getTopBorderColor(), cellRangeAddress, sheet);
RegionUtil.setRightBorderColor(cellStyle.getRightBorderColor(), cellRangeAddress, sheet);
RegionUtil.setLeftBorderColor(cellStyle.getLeftBorderColor(), cellRangeAddress, sheet);
RegionUtil.setBottomBorderColor(cellStyle.getBottomBorderColor(), cellRangeAddress, sheet);
}
}
// -------------------------------------------------------------------------------------------------------------- Private method end
}