| 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.schedulers; |
| 15 |
|
| 16 |
import java.util.Objects; |
| 17 |
import java.util.concurrent.TimeUnit; |
| 18 |
|
| 19 |
import io.reactivex.rxjava3.annotations.NonNull; |
| 20 |
|
| 21 |
/** |
| 22 |
* Holds onto a value along with time information. |
| 23 |
* |
| 24 |
* @param <T> the value type |
| 25 |
*/ |
| 26 |
public final class Timed<T> { |
| 27 |
final T value; |
| 28 |
final long time; |
| 29 |
final TimeUnit unit; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructs a {@code Timed} instance with the given value and time information. |
| 33 |
* @param value the value to hold |
| 34 |
* @param time the time to hold |
| 35 |
* @param unit the time unit, not null |
| 36 |
* @throws NullPointerException if {@code value} or {@code unit} is {@code null} |
| 37 |
*/ |
| 38 |
public Timed(@NonNull T value, long time, @NonNull TimeUnit unit) { |
| 39 |
this.value = Objects.requireNonNull(value, "value is null"); |
| 40 |
this.time = time; |
| 41 |
this.unit = Objects.requireNonNull(unit, "unit is null"); |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Returns the contained value. |
| 46 |
* @return the contained value |
| 47 |
*/ |
| 48 |
@NonNull |
| 49 |
public T value() { |
| 50 |
return value; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Returns the time unit of the contained time. |
| 55 |
* @return the time unit of the contained time |
| 56 |
*/ |
| 57 |
@NonNull |
| 58 |
public TimeUnit unit() { |
| 59 |
return unit; |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the time value. |
| 64 |
* @return the time value |
| 65 |
*/ |
| 66 |
public long time() { |
| 67 |
return time; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns the contained time value in the time unit specified. |
| 72 |
* @param unit the time unit |
| 73 |
* @return the converted time |
| 74 |
*/ |
| 75 |
public long time(@NonNull TimeUnit unit) { |
| 76 |
return unit.convert(time, this.unit); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public boolean equals(Object other) { |
| 81 |
if (other instanceof Timed) { |
| 82 |
Timed<?> o = (Timed<?>) other; |
| 83 |
return Objects.equals(value, o.value) |
| 84 |
&& time == o.time |
| 85 |
&& Objects.equals(unit, o.unit); |
| 86 |
} |
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
@Override |
| 91 |
public int hashCode() { |
| 92 |
int h = value.hashCode(); |
| 93 |
h = h * 31 + (int)((time >>> 31) ^ time); |
| 94 |
h = h * 31 + unit.hashCode(); |
| 95 |
return h; |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public String toString() { |
| 100 |
return "Timed[time=" + time + ", unit=" + unit + ", value=" + value + "]"; |
| 101 |
} |
| 102 |
} |