Skip to content

Commit 88b6823

Browse files
committed
port SimpleImmutableEntry for Android2.2 or below
1 parent 8c507e6 commit 88b6823

2 files changed

Lines changed: 106 additions & 2 deletions

File tree

src/main/java/org/msgpack/type/SequentialMapValueImpl.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.NoSuchElementException;
2828
import java.io.IOException;
2929
import org.msgpack.packer.Packer;
30+
import org.msgpack.util.PortedImmutableEntry;
3031

3132
class SequentialMapValueImpl extends AbstractMapValue {
3233
private static SequentialMapValueImpl emptyInstance = new SequentialMapValueImpl(new Value[0], true);
@@ -89,6 +90,17 @@ private static class EntrySetIterator implements
8990
Iterator<Map.Entry<Value, Value>> {
9091
private Value[] array;
9192
private int pos;
93+
private static final boolean hasDefaultImmutableEntry;
94+
static {
95+
boolean hasIt = true;
96+
try {
97+
Class.forName("java.util.AbstractMap.SimpleImmutableEntry");
98+
} catch (ClassNotFoundException e) {
99+
hasIt = false;
100+
} finally {
101+
hasDefaultImmutableEntry = hasIt;
102+
}
103+
}
92104

93105
EntrySetIterator(Value[] array) {
94106
this.array = array;
@@ -105,8 +117,13 @@ public Map.Entry<Value, Value> next() {
105117
if (pos >= array.length) {
106118
throw new NoSuchElementException(); // TODO message
107119
}
108-
Map.Entry<Value, Value> pair =
109-
new AbstractMap.SimpleImmutableEntry<Value, Value>(array[pos], array[pos + 1]);
120+
121+
Value key = array[pos];
122+
Value value = array[pos + 1];
123+
Map.Entry<Value, Value> pair = hasDefaultImmutableEntry ?
124+
new AbstractMap.SimpleImmutableEntry<Value, Value>(key, value) :
125+
new PortedImmutableEntry<Value, Value>(key, value);
126+
110127
pos += 2;
111128
return pair;
112129
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)