|
| 1 | +package org.schabi.newpipe.extractor.utils; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.util.Objects; |
| 5 | + |
| 6 | +/** |
| 7 | + * Serializable class to create a pair of objects. |
| 8 | + * |
| 9 | + * <p> |
| 10 | + * The two objects of the pair must be {@link Serializable serializable} and can be of the same |
| 11 | + * type. |
| 12 | + * </p> |
| 13 | + * |
| 14 | + * <p> |
| 15 | + * Note that this class is not intended to be used as a general-purpose pair and should only be |
| 16 | + * used when interfacing with the extractor. |
| 17 | + * </p> |
| 18 | + * |
| 19 | + * @param <F> the type of the first object, which must be {@link Serializable} |
| 20 | + * @param <S> the type of the second object, which must be {@link Serializable} |
| 21 | + */ |
| 22 | +public class Pair<F extends Serializable, S extends Serializable> implements Serializable { |
| 23 | + |
| 24 | + /** |
| 25 | + * The first object of the pair. |
| 26 | + */ |
| 27 | + private F firstObject; |
| 28 | + |
| 29 | + /** |
| 30 | + * The second object of the pair. |
| 31 | + */ |
| 32 | + private S secondObject; |
| 33 | + |
| 34 | + /** |
| 35 | + * Creates a new {@link Pair} object. |
| 36 | + * |
| 37 | + * @param first the first object of the pair |
| 38 | + * @param second the second object of the pair |
| 39 | + */ |
| 40 | + public Pair(final F first, final S second) { |
| 41 | + firstObject = first; |
| 42 | + secondObject = second; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Sets the first object, which must be of the {@link F} type. |
| 47 | + * |
| 48 | + * @param first the new first object of the pair |
| 49 | + */ |
| 50 | + public void setFirst(final F first) { |
| 51 | + firstObject = first; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Sets the first object, which must be of the {@link S} type. |
| 56 | + * |
| 57 | + * @param second the new first object of the pair |
| 58 | + */ |
| 59 | + public void setSecond(final S second) { |
| 60 | + secondObject = second; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Gets the first object of the pair. |
| 65 | + * |
| 66 | + * @return the first object of the pair |
| 67 | + */ |
| 68 | + public F getFirst() { |
| 69 | + return firstObject; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * Gets the second object of the pair. |
| 74 | + * |
| 75 | + * @return the second object of the pair |
| 76 | + */ |
| 77 | + public S getSecond() { |
| 78 | + return this.secondObject; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Returns a string representation of the current {@code Pair}. |
| 83 | + * |
| 84 | + * <p> |
| 85 | + * The string representation will look like this: |
| 86 | + * <code> |
| 87 | + * {<i>firstObject.toString()</i>, <i>secondObject.toString()</i>} |
| 88 | + * </code> |
| 89 | + * </p> |
| 90 | + * |
| 91 | + * @return a string representation of the current {@code Pair} |
| 92 | + */ |
| 93 | + @Override |
| 94 | + public String toString() { |
| 95 | + return "{" + firstObject + ", " + secondObject + "}"; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Reveals whether an object is equal to this {@code Pair} instance. |
| 100 | + * |
| 101 | + * @param obj the object to compare with this {@code Pair} instance |
| 102 | + * @return whether an object is equal to this {@code Pair} instance |
| 103 | + */ |
| 104 | + @Override |
| 105 | + public boolean equals(final Object obj) { |
| 106 | + if (this == obj) { |
| 107 | + return true; |
| 108 | + } |
| 109 | + |
| 110 | + if (obj == null || getClass() != obj.getClass()) { |
| 111 | + return false; |
| 112 | + } |
| 113 | + |
| 114 | + final Pair<?, ?> pair = (Pair<?, ?>) obj; |
| 115 | + return Objects.equals(firstObject, pair.firstObject) && Objects.equals(secondObject, |
| 116 | + pair.secondObject); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Returns a hash code of the current {@code Pair} by using the first and second object. |
| 121 | + * |
| 122 | + * @return a hash code of the current {@code Pair} |
| 123 | + */ |
| 124 | + @Override |
| 125 | + public int hashCode() { |
| 126 | + return Objects.hash(firstObject, secondObject); |
| 127 | + } |
| 128 | +} |
0 commit comments