|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.msgpack.util; |
| 19 | + |
| 20 | +import java.io.Serializable; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +/** |
| 24 | + * An immutable key-value mapping. Despite the name, this class is non-final |
| 25 | + * and its subclasses may be mutable. |
| 26 | + * |
| 27 | + * This class is ported from java.util.AbstractMap$SimpleImmutableEntry of |
| 28 | + * https://github.com/OESF/OHA-Android-4.0.3_r1.0 (Apache License). |
| 29 | + */ |
| 30 | +public class PortedImmutableEntry<K, V> implements Map.Entry<K, V>, Serializable { |
| 31 | + private static final long serialVersionUID = -4564047655287765373L; |
| 32 | + |
| 33 | + private final K key; |
| 34 | + private final V value; |
| 35 | + |
| 36 | + public PortedImmutableEntry(K theKey, V theValue) { |
| 37 | + key = theKey; |
| 38 | + value = theValue; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Constructs an instance with the key and value of {@code copyFrom}. |
| 43 | + */ |
| 44 | + public PortedImmutableEntry(Map.Entry<? extends K, ? extends V> copyFrom) { |
| 45 | + key = copyFrom.getKey(); |
| 46 | + value = copyFrom.getValue(); |
| 47 | + } |
| 48 | + |
| 49 | + public K getKey() { |
| 50 | + return key; |
| 51 | + } |
| 52 | + |
| 53 | + public V getValue() { |
| 54 | + return value; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * This base implementation throws {@code UnsupportedOperationException} |
| 59 | + * always. |
| 60 | + */ |
| 61 | + public V setValue(V object) { |
| 62 | + throw new UnsupportedOperationException(); |
| 63 | + } |
| 64 | + |
| 65 | + @Override public boolean equals(Object object) { |
| 66 | + if (this == object) { |
| 67 | + return true; |
| 68 | + } |
| 69 | + if (object instanceof Map.Entry) { |
| 70 | + Map.Entry<?, ?> entry = (Map.Entry<?, ?>) object; |
| 71 | + return (key == null ? entry.getKey() == null : key.equals(entry |
| 72 | + .getKey())) |
| 73 | + && (value == null ? entry.getValue() == null : value |
| 74 | + .equals(entry.getValue())); |
| 75 | + } |
| 76 | + return false; |
| 77 | + } |
| 78 | + |
| 79 | + @Override public int hashCode() { |
| 80 | + return (key == null ? 0 : key.hashCode()) |
| 81 | + ^ (value == null ? 0 : value.hashCode()); |
| 82 | + } |
| 83 | + |
| 84 | + @Override public String toString() { |
| 85 | + return key + "=" + value; |
| 86 | + } |
| 87 | +} |
0 commit comments