|
| 1 | +package edu.hm.hafner.util; |
| 2 | + |
| 3 | +import java.io.Serial; |
| 4 | +import java.io.Serializable; |
| 5 | +import java.util.Objects; |
| 6 | + |
| 7 | +import com.google.errorprone.annotations.Immutable; |
| 8 | + |
| 9 | +/** |
| 10 | + * An option is a key value pair. |
| 11 | + * |
| 12 | + * @author Ullrich Hafner |
| 13 | + */ |
| 14 | +@Immutable |
| 15 | +public final class Option implements Serializable, Comparable<Option> { |
| 16 | + @Serial |
| 17 | + private static final long serialVersionUID = -1L; |
| 18 | + |
| 19 | + private final String key; |
| 20 | + private final String value; |
| 21 | + private final int wert = 0; |
| 22 | + |
| 23 | + /** |
| 24 | + * Creates an entry representing a mapping from the specified key to the specified value. |
| 25 | + * |
| 26 | + * @param key |
| 27 | + * the key represented by this entry |
| 28 | + * @param value |
| 29 | + * the value represented by this entry |
| 30 | + */ |
| 31 | + public Option(final String key, final String value) { |
| 32 | + this.key = key; |
| 33 | + this.value = value; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Returns the key corresponding to this entry. |
| 38 | + * |
| 39 | + * @return the key corresponding to this entry |
| 40 | + */ |
| 41 | + public String getKey() { |
| 42 | + return key; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Returns the value corresponding to this entry. |
| 47 | + * |
| 48 | + * @return the value corresponding to this entry |
| 49 | + */ |
| 50 | + public String getValue() { |
| 51 | + return value; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean equals(final Object o) { |
| 56 | + if (this == o) { |
| 57 | + return true; |
| 58 | + } |
| 59 | + if (o == null || getClass() != o.getClass()) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + Option option = (Option) o; |
| 63 | + return Objects.equals(key, option.key) && Objects.equals(value, option.value); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public int hashCode() { |
| 68 | + return Objects.hash(key, value); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public String toString() { |
| 73 | + return "Option{key='" + key + '\'' + ", value='" + value + '\'' + '}'; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Compares this object with the specified object for order. Returns a negative integer, zero, or a positive |
| 78 | + * integer as this object is less than, equal to, or greater than the specified object. |
| 79 | + * |
| 80 | + * <p>The implementor must ensure {@link Integer#signum |
| 81 | + * signum}{@code (x.compareTo(y)) == -signum(y.compareTo(x))} for all {@code x} and {@code y}. (This implies that |
| 82 | + * {@code x.compareTo(y)} must throw an exception if and only if {@code y.compareTo(x)} throws an exception.) |
| 83 | + * |
| 84 | + * <p>The implementor must also ensure that the relation is transitive: |
| 85 | + * {@code (x.compareTo(y) > 0 && y.compareTo(z) > 0)} implies {@code x.compareTo(z) > 0}. |
| 86 | + * |
| 87 | + * <p>Finally, the implementor must ensure that {@code |
| 88 | + * x.compareTo(y)==0} implies that {@code signum(x.compareTo(z)) == signum(y.compareTo(z))}, for all {@code z}. |
| 89 | + * |
| 90 | + * @param other |
| 91 | + * the object to be compared. |
| 92 | + * |
| 93 | + * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than |
| 94 | + * the specified object. |
| 95 | + * @throws NullPointerException |
| 96 | + * if the specified object is null |
| 97 | + * @throws ClassCastException |
| 98 | + * if the specified object's type prevents it from being compared to this object. |
| 99 | + */ |
| 100 | + @Override |
| 101 | + public int compareTo(final Option other) { |
| 102 | + if (other == null) { |
| 103 | + throw new IllegalArgumentException("Cannot compare to null"); |
| 104 | + } |
| 105 | + return getKey().compareTo(other.getKey()); |
| 106 | + } |
| 107 | +} |
0 commit comments