| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
package io.reactivex.rxjava3.internal.functions; |
| 15 |
|
| 16 |
import io.reactivex.rxjava3.functions.BiPredicate; |
| 17 |
import java.util.Objects; |
| 18 |
|
| 19 |
/** |
| 20 |
* Utility methods containing the backport of Java 7's Objects utility class. |
| 21 |
* <p>Named as such to avoid clash with java.util.Objects. |
| 22 |
*/ |
| 23 |
public final class ObjectHelper { |
| 24 |
|
| 25 |
/** Utility class. */ |
| 26 |
private ObjectHelper() { |
| 27 |
throw new IllegalStateException("No instances!"); |
| 28 |
} |
| 29 |
|
| 30 |
static final BiPredicate<Object, Object> EQUALS = new BiObjectPredicate(); |
| 31 |
|
| 32 |
/** |
| 33 |
* Returns a BiPredicate that compares its parameters via Objects.equals(). |
| 34 |
* @param <T> the value type |
| 35 |
* @return the bi-predicate instance |
| 36 |
*/ |
| 37 |
@SuppressWarnings("unchecked") |
| 38 |
public static <T> BiPredicate<T, T> equalsPredicate() { |
| 39 |
return (BiPredicate<T, T>)EQUALS; |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Validate that the given value is positive or report an IllegalArgumentException with |
| 44 |
* the parameter name. |
| 45 |
* @param value the value to validate |
| 46 |
* @param paramName the parameter name of the value |
| 47 |
* @return value |
| 48 |
* @throws IllegalArgumentException if bufferSize <= 0 |
| 49 |
*/ |
| 50 |
public static int verifyPositive(int value, String paramName) { |
| 51 |
if (value <= 0) { |
| 52 |
throw new IllegalArgumentException(paramName + " > 0 required but it was " + value); |
| 53 |
} |
| 54 |
return value; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Validate that the given value is positive or report an IllegalArgumentException with |
| 59 |
* the parameter name. |
| 60 |
* @param value the value to validate |
| 61 |
* @param paramName the parameter name of the value |
| 62 |
* @return value |
| 63 |
* @throws IllegalArgumentException if bufferSize <= 0 |
| 64 |
*/ |
| 65 |
public static long verifyPositive(long value, String paramName) { |
| 66 |
if (value <= 0L) { |
| 67 |
throw new IllegalArgumentException(paramName + " > 0 required but it was " + value); |
| 68 |
} |
| 69 |
return value; |
| 70 |
} |
| 71 |
|
| 72 |
static final class BiObjectPredicate implements BiPredicate<Object, Object> { |
| 73 |
@Override |
| 74 |
public boolean test(Object o1, Object o2) { |
| 75 |
return Objects.equals(o1, o2); |
| 76 |
} |
| 77 |
} |
| 78 |
} |