Skip to content

Commit

Permalink
Objectutil 新增 equalsAny 方法 fix #350
Browse files Browse the repository at this point in the history
  • Loading branch information
venusdrogon committed Jun 9, 2022
1 parent 127c2e7 commit 6ffe14e
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 2 deletions.
52 changes: 50 additions & 2 deletions feilong-core/src/main/java/com/feilong/core/lang/ObjectUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
package com.feilong.core.lang;

import static com.feilong.core.Validator.isNotNullOrEmpty;
import static com.feilong.core.Validator.isNullOrEmpty;

import java.util.Objects;

import com.feilong.core.Validate;

Expand Down Expand Up @@ -145,7 +148,7 @@ public static <T> T defaultIfNullOrEmpty(final T object,final T defaultValue){
}

/**
* Returns a default value if the object passed is {@code null}.
* 如果 <code>object</code> 是null,返回默认值 <code>defaultValue</code>.
*
* <pre>
* ObjectUtil.defaultIfNull(null, null) = null
Expand All @@ -161,7 +164,7 @@ public static <T> T defaultIfNullOrEmpty(final T object,final T defaultValue){
* the {@code Object} to test, may be {@code null}
* @param defaultValue
* the default value to return, may be {@code null}
* @return {@code object} if it is not {@code null}, defaultValue otherwise
* @return 如果 <code>object</code> 是null,返回 <code>defaultValue</code>,否则返回 <code>object</code>
* @since 3.0.0
*/
public static <T> T defaultIfNull(final T object,final T defaultValue){
Expand Down Expand Up @@ -286,4 +289,49 @@ public static boolean isPrimitiveArray(Object object){
Validate.notNull(object, "object can't be null!");
return isArray(object) && object.getClass().getComponentType().isPrimitive();//原始型的
}

//---------------------------------------------------------------
/**
* <p>
* 对比 given {@code t} to a vararg of {@code searchTargets},
* 如果 {@code true} if the {@code t} is equal to any of the {@code searchTargets}.
* </p>
*
* <p>
* 比 apache commons-lang3 StringUtils#equalsAny 适用面更广
* </p>
*
* <pre>
* ObjectUtil.equalsAny(null, (CharSequence[]) null) = false
* ObjectUtil.equalsAny(null, null, null) = true
* ObjectUtil.equalsAny(null, "abc", "def") = false
* ObjectUtil.equalsAny("abc", null, "def") = false
* ObjectUtil.equalsAny("abc", "abc", "def") = true
* ObjectUtil.equalsAny("abc", "ABC", "DEF") = false
* ObjectUtil.equalsAny(5, 5, 6) = true
* </pre>
*
* @param t
* to compare, may be {@code null}.
* @param searchTargets
* a vararg of t, may be {@code null}.
* @return {@code true} 如果 t is equal to any other element of {@code searchTargets};
* {@code false} if {@code searchTargets} is null or contains no matches.
* @see com.feilong.lib.lang3.StringUtils#equalsAny(CharSequence, CharSequence...)
* @since 3.1.0
*/
@SafeVarargs
public static <T> boolean equalsAny(T t,T...searchTargets){
if (isNullOrEmpty(searchTargets)){
return false;
}
//---------------------------------------------------------------
for (T target : searchTargets){
if (Objects.equals(t, target)){
return true;
}
}
return false;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (C) 2008 feilong
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.feilong.core.lang.objectutil;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

import com.feilong.core.lang.ObjectUtil;

public class EqualsAnyTest{

@Test
public void testNumber(){
assertEquals(false, ObjectUtil.equalsAny(5L, 6, 5));
assertEquals(true, ObjectUtil.equalsAny(5L, 5L, 6L));
assertEquals(false, ObjectUtil.equalsAny(5L, 7L, 6L));
assertEquals(true, ObjectUtil.equalsAny(5, 6, 5));
assertEquals(true, ObjectUtil.equalsAny(5, null, 5));
assertEquals(false, ObjectUtil.equalsAny(5, 6, 8));
assertEquals(false, ObjectUtil.equalsAny(5, 6, null));
assertEquals(false, ObjectUtil.equalsAny(5, 6, null, 9));
assertEquals(false, ObjectUtil.equalsAny(5, 6, null, 9, 19, 20));
}

@Test
public void testString(){
assertEquals(false, ObjectUtil.equalsAny("abc", "ABC", "DEF"));
assertEquals(true, ObjectUtil.equalsAny("abc", "abc", "def"));

assertEquals(false, ObjectUtil.equalsAny("abc", null, "def"));
assertEquals(false, ObjectUtil.equalsAny(null, "abc", "def"));

}

@Test
public void testNull(){
assertEquals(true, ObjectUtil.equalsAny(null, 1, null));
assertEquals(true, ObjectUtil.equalsAny(null, null, null));
assertEquals(false, ObjectUtil.equalsAny(null, null));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
DefaultIfNullOrEmptyParameterizedTest.class,
DefaultIfNullParameterizedTest.class,

EqualsAnyTest.class,

ObjectUtilTest.class, })
public class ObjectUtilSuiteTests{

Expand Down

0 comments on commit 6ffe14e

Please sign in to comment.