diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java index 52fc9a789..bd50aba77 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/lang/SystemNativeGenerator.java @@ -75,7 +75,7 @@ public class SystemNativeGenerator implements Generator, DependencyPlugin { String destPos = context.getParameterName(4); String length = context.getParameterName(5); writer.append("for (var i = 0; i < " + length + "; i = (i + 1) | 0) {").indent().softNewLine(); - writer.append(dest + ".data[" + srcPos + "++] = " + src + ".data[" + destPos + "++];").softNewLine(); + writer.append(dest + ".data[" + destPos + "++] = " + src + ".data[" + srcPos + "++];").softNewLine(); writer.outdent().append("}").softNewLine(); } diff --git a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeMap.java b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeMap.java index 5acb6deeb..e91a434db 100644 --- a/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeMap.java +++ b/teavm-classlib/src/main/java/org/teavm/classlib/java/util/TTreeMap.java @@ -34,15 +34,6 @@ package org.teavm.classlib.java.util; import org.teavm.classlib.java.io.TSerializable; import org.teavm.classlib.java.lang.*; - -/** - * TreeMap is an implementation of SortedMap. All optional operations (adding - * and removing) are supported. The values can be any objects. The keys can be - * any objects which are comparable to each other either using their natural - * order or a specified Comparator. - * - * @since 1.2 - */ public class TTreeMap extends TAbstractMap implements TSortedMap, TCloneable, TSerializable { transient int size; private TComparator comparator; @@ -79,8 +70,7 @@ public class TTreeMap extends TAbstractMap implements TSortedMap entry = (Entry) object; V value = getValue(); return (key == null ? entry.getKey() == null : key.equals(entry.getKey())) - && (value == null ? entry.getValue() == null : value - .equals(entry.getValue())); + && (value == null ? entry.getValue() == null : value.equals(entry.getValue())); } return false; } @@ -513,8 +503,7 @@ public class TTreeMap extends TAbstractMap implements TSortedMap object = backingMap.comparator == null ? - toComparable(startKey) : null; + TComparable object = backingMap.comparator == null ? toComparable(startKey) : null; K key = startKey; Node node = backingMap.root; Node foundNode = null; @@ -972,45 +961,17 @@ public class TTreeMap extends TAbstractMap implements TSortedMap comparator) { this.comparator = comparator; } - /** - * Constructs a new {@code TreeMap} instance containing the mappings from - * the specified map and using natural ordering. - * - * @param map - * the mappings to add. - * @throws ClassCastException - * if a key in the specified map does not implement the - * Comparable interface, or if the keys in the map cannot be - * compared. - */ public TTreeMap(TMap map) { putAll(map); } - /** - * Constructs a new {@code TreeMap} instance containing the mappings from - * the specified SortedMap and using the same comparator. - * - * @param map - * the mappings to add. - */ public TTreeMap(TSortedMap map) { this(map.comparator()); Node lastNode = null; @@ -1038,12 +999,6 @@ public class TTreeMap extends TAbstractMap implements TSortedMap extends TAbstractMap implements TSortedMap extends TAbstractMap implements TSortedMap comparator() { return comparator; } - /** - * Returns whether this map contains the specified key. - * - * @param key - * the key to search for. - * @return {@code true} if this map contains the specified key, - * {@code false} otherwise. - * @throws ClassCastException - * if the specified key cannot be compared with the keys in this - * map. - * @throws NullPointerException - * if the specified key is {@code null} and the comparator - * cannot handle {@code null} keys. - */ @Override public boolean containsKey(Object key) { @SuppressWarnings("unchecked") @@ -1163,14 +1092,6 @@ public class TTreeMap extends TAbstractMap implements TSortedMap extends TAbstractMap implements TSortedMap> entrySet() { if (entrySet == null) { @@ -1258,13 +1171,6 @@ public class TTreeMap extends TAbstractMap implements TSortedMap extends TAbstractMap implements TSortedMap extends TAbstractMap implements TSortedMap object, K key1, K key2) { - return object != null ? - object.compareTo(key2) : comparator.compare(key1, key2); + return object != null ? object.compareTo(key2) : comparator.compare(key1, key2); } - /** - * Returns a sorted map over a range of this sorted map with all keys that - * are less than the specified {@code endKey}. Changes to the returned - * sorted map are reflected in this sorted map and vice versa. - *

- * Note: The returned map will not allow an insertion of a key outside the - * specified range. - * - * @param endKey - * the high boundary of the range specified. - * @return a sorted map where the keys are less than {@code endKey}. - * @throws ClassCastException - * if the specified key cannot be compared with the keys in this - * map. - * @throws NullPointerException - * if the specified key is {@code null} and the comparator - * cannot handle {@code null} keys. - * @throws IllegalArgumentException - * if this map is itself a sorted map over a range of another - * map and the specified key is outside of its range. - */ @Override public TSortedMap headMap(K endKey) { // Check for errors @@ -1368,13 +1240,6 @@ public class TTreeMap extends TAbstractMap implements TSortedMap(this, endKey); } - /** - * Returns a set of the keys contained in this map. The set is backed by - * this map so changes to one are reflected by the other. The set does not - * support adding. - * - * @return a set of the keys. - */ @Override public TSet keySet() { if (cachedKeySet == null) { @@ -1412,13 +1277,6 @@ public class TTreeMap extends TAbstractMap implements TSortedMap extends TAbstractMap implements TSortedMap extends TAbstractMap implements TSortedMap map) { super.putAll(map); } - /** - * Removes the mapping with the specified key from this map. - * - * @param key - * the key of the mapping to remove. - * @return the value of the removed mapping or {@code null} if no mapping - * for the specified key was found. - * @throws ClassCastException - * if the specified key cannot be compared with the keys in this - * map. - * @throws NullPointerException - * if the specified key is {@code null} and the comparator - * cannot handle {@code null} keys. - */ @Override public V remove(Object key) { if (size == 0) { @@ -2275,42 +2088,11 @@ public class TTreeMap extends TAbstractMap implements TSortedMap - * Note: The returned map will not allow an insertion of a key outside the - * specified range. - * - * @param startKey - * the low boundary of the range (inclusive). - * @param endKey - * the high boundary of the range (exclusive), - * @return a sorted map with the key from the specified range. - * @throws ClassCastException - * if the start or end key cannot be compared with the keys in - * this map. - * @throws NullPointerException - * if the start or end key is {@code null} and the comparator - * cannot handle {@code null} keys. - * @throws IllegalArgumentException - * if the start key is greater than the end key, or if this map - * is itself a sorted map over a range of another sorted map and - * the specified range is outside of its range. - */ @Override public TSortedMap subMap(K startKey, K endKey) { if (comparator == null) { @@ -2325,28 +2107,6 @@ public class TTreeMap extends TAbstractMap implements TSortedMap - * Note: The returned map will not allow an insertion of a key outside the - * specified range. - * - * @param startKey - * the low boundary of the range specified. - * @return a sorted map where the keys are greater or equal to - * {@code startKey}. - * @throws ClassCastException - * if the specified key cannot be compared with the keys in this - * map. - * @throws NullPointerException - * if the specified key is {@code null} and the comparator - * cannot handle {@code null} keys. - * @throws IllegalArgumentException - * if this map itself a sorted map over a range of another map - * and the specified key is outside of its range. - */ @Override public TSortedMap tailMap(K startKey) { // Check for errors @@ -2358,25 +2118,6 @@ public class TTreeMap extends TAbstractMap implements TSortedMap(startKey, this); } - /** - * Returns a collection of the values contained in this map. The collection - * is backed by this map so changes to one are reflected by the other. The - * collection supports remove, removeAll, retainAll and clear operations, - * and it does not support add or addAll operations. - *

- * This method returns a collection which is the subclass of - * AbstractCollection. The iterator method of this subclass returns a - * "wrapper object" over the iterator of map's entrySet(). The {@code size} - * method wraps the map's size method and the {@code contains} method wraps - * the map's containsValue method. - *

- * The collection is created when this method is called for the first time - * and returned in response to all subsequent calls. This method may return - * different collections when multiple concurrent calls occur, since no - * synchronization is performed. - * - * @return a collection of the values contained in this map. - */ @Override public TCollection values() { if (cachedValues == null) { @@ -2405,4 +2146,3 @@ public class TTreeMap extends TAbstractMap implements TSortedMap { + @SuppressWarnings("unchecked") + @Override public int compare(Object o1, Object o2) { - return -(((Comparable) o1).compareTo(o2)); + return -(((Comparable) o1).compareTo(o2)); } + @SuppressWarnings("unchecked") public boolean equals(Object o1, Object o2) { - return (((Comparable) o1).compareTo(o2)) == 0; + return (((Comparable) o1).compareTo(o2)) == 0; } } // Regression for Harmony-1026 - public static class MockComparator> implements - Comparator, Serializable { - + public static class MockComparator> implements Comparator { + @Override public int compare(T o1, T o2) { if (o1 == o2) { return 0; @@ -77,7 +71,7 @@ public class TreeMapTest { // Regression for Harmony-1161 class MockComparatorNullTolerable implements Comparator { - + @Override public int compare(String o1, String o2) { if (o1 == o2) { return 0; @@ -92,12 +86,12 @@ public class TreeMapTest { } } - TreeMap tm; + TreeMap tm; Object objArray[] = new Object[1000]; public TreeMapTest() { - tm = new TreeMap(); + tm = new TreeMap<>(); for (int i = 0; i < objArray.length; i++) { Object x = objArray[i] = new Integer(i); tm.put(x.toString(), x); @@ -108,8 +102,8 @@ public class TreeMapTest { @Test public void test_ConstructorLjava_util_Comparator() { // Test for method java.util.TreeMap(java.util.Comparator) - Comparator comp = new ReversedComparator(); - TreeMap reversedTreeMap = new TreeMap(comp); + Comparator comp = new ReversedComparator(); + TreeMap reversedTreeMap = new TreeMap<>(comp); assertTrue("TreeMap answered incorrect comparator", reversedTreeMap .comparator() == comp); reversedTreeMap.put(new Integer(1).toString(), new Integer(1)); @@ -124,22 +118,21 @@ public class TreeMapTest { @Test public void test_ConstructorLjava_util_Map() { // Test for method java.util.TreeMap(java.util.Map) - TreeMap myTreeMap = new TreeMap(new HashMap(tm)); + TreeMap myTreeMap = new TreeMap<>(new HashMap<>(tm)); assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length); for (Object element : objArray) { - assertTrue("Map has incorrect mappings", myTreeMap.get( - element.toString()).equals(element)); + assertTrue("Map has incorrect mappings", myTreeMap.get(element.toString()).equals(element)); } } @Test public void test_ConstructorLjava_util_SortedMap() { // Test for method java.util.TreeMap(java.util.SortedMap) - Comparator comp = new ReversedComparator(); - TreeMap reversedTreeMap = new TreeMap(comp); + Comparator comp = new ReversedComparator(); + TreeMap reversedTreeMap = new TreeMap<>(comp); reversedTreeMap.put(new Integer(1).toString(), new Integer(1)); reversedTreeMap.put(new Integer(2).toString(), new Integer(2)); - TreeMap anotherTreeMap = new TreeMap(reversedTreeMap); + TreeMap anotherTreeMap = new TreeMap<>(reversedTreeMap); assertTrue("New tree map does not answer correct comparator", anotherTreeMap.comparator() == comp); assertTrue("TreeMap does not use comparator (firstKey was incorrect)", @@ -159,34 +152,34 @@ public class TreeMapTest { @Test public void test_clone() { // Test for method java.lang.Object java.util.TreeMap.clone() - TreeMap clonedMap = (TreeMap) tm.clone(); - assertTrue("Cloned map does not equal the original map", clonedMap - .equals(tm)); - assertTrue("Cloned map is the same reference as the original map", - clonedMap != tm); + @SuppressWarnings("unchecked") + TreeMap clonedMap = (TreeMap) tm.clone(); + assertTrue("Cloned map does not equal the original map", clonedMap.equals(tm)); + assertTrue("Cloned map is the same reference as the original map", clonedMap != tm); for (Object element : objArray) { assertTrue("Cloned map contains incorrect elements", clonedMap .get(element.toString()) == tm.get(element.toString())); } - TreeMap map = new TreeMap(); + TreeMap map = new TreeMap<>(); map.put("key", "value"); // get the keySet() and values() on the original Map - Set keys = map.keySet(); - Collection values = map.values(); + Set keys = map.keySet(); + Collection values = map.values(); assertEquals("values() does not work", "value", values.iterator() .next()); assertEquals("keySet() does not work", "key", keys.iterator().next()); - AbstractMap map2 = (AbstractMap) map.clone(); + @SuppressWarnings("unchecked") + Map map2 = (Map) map.clone(); map2.put("key", "value2"); - Collection values2 = map2.values(); + Collection values2 = map2.values(); assertTrue("values() is identical", values2 != values); // values() and keySet() on the cloned() map should be different assertEquals("values() was not cloned", "value2", values2.iterator() .next()); map2.clear(); map2.put("key2", "value3"); - Set key2 = map2.keySet(); + Set key2 = map2.keySet(); assertTrue("keySet() is identical", key2 != keys); assertEquals("keySet() was not cloned", "key2", key2.iterator().next()); } @@ -194,10 +187,9 @@ public class TreeMapTest { @Test public void test_comparator() { // Test for method java.util.Comparator java.util.TreeMap.comparator()\ - Comparator comp = new ReversedComparator(); - TreeMap reversedTreeMap = new TreeMap(comp); - assertTrue("TreeMap answered incorrect comparator", reversedTreeMap - .comparator() == comp); + Comparator comp = new ReversedComparator(); + TreeMap reversedTreeMap = new TreeMap<>(comp); + assertTrue("TreeMap answered incorrect comparator", reversedTreeMap.comparator() == comp); reversedTreeMap.put(new Integer(1).toString(), new Integer(1)); reversedTreeMap.put(new Integer(2).toString(), new Integer(2)); assertTrue("TreeMap does not use comparator (firstKey was incorrect)", @@ -227,15 +219,13 @@ public class TreeMapTest { @Test public void test_entrySet() { // Test for method java.util.Set java.util.TreeMap.entrySet() - Set anEntrySet = tm.entrySet(); - Iterator entrySetIterator = anEntrySet.iterator(); - assertTrue("EntrySet is incorrect size", - anEntrySet.size() == objArray.length); - Map.Entry entry; + Set> anEntrySet = tm.entrySet(); + Iterator> entrySetIterator = anEntrySet.iterator(); + assertTrue("EntrySet is incorrect size", anEntrySet.size() == objArray.length); + Map.Entry entry; while (entrySetIterator.hasNext()) { - entry = (Map.Entry) entrySetIterator.next(); - assertTrue("EntrySet does not contain correct mappings", tm - .get(entry.getKey()) == entry.getValue()); + entry = entrySetIterator.next(); + assertTrue("EntrySet does not contain correct mappings", tm.get(entry.getKey()) == entry.getValue()); } } @@ -259,15 +249,14 @@ public class TreeMapTest { public void test_headMapLjava_lang_Object() { // Test for method java.util.SortedMap // java.util.TreeMap.headMap(java.lang.Object) - Map head = tm.headMap("100"); + Map head = tm.headMap("100"); assertEquals("Returned map of incorrect size", 3, head.size()); assertTrue("Returned incorrect elements", head.containsKey("0") && head.containsValue(new Integer("1")) && head.containsKey("10")); // Regression for Harmony-1026 - TreeMap map = new TreeMap( - new MockComparator()); + TreeMap map = new TreeMap<>(new MockComparator()); map.put(1, 2.1); map.put(2, 3.1); map.put(3, 4.5); @@ -310,16 +299,16 @@ public class TreeMapTest { } }; - TreeMap treemap = new TreeMap(c); + TreeMap treemap = new TreeMap<>(c); assertEquals(0, treemap.headMap(null).size()); - treemap = new TreeMap(); - SortedMap headMap = treemap.headMap("100"); - headMap.headMap("100"); + treemap = new TreeMap<>(); + SortedMap headMap = treemap.headMap("100"); + headMap.headMap("100"); - SortedMap intMap,sub; + SortedMap intMap,sub; int size = 16; - intMap = new TreeMap(); + intMap = new TreeMap<>(); for(int i=0; i(); + intMap = new TreeMap<>(); for(int i=0; i ks = tm.keySet(); + assertTrue("Returned set of incorrect size",ks.size() == objArray.length); for (int i = 0; i < tm.size(); i++) { - assertTrue("Returned set is missing keys", ks.contains(new Integer( - i).toString())); + assertTrue("Returned set is missing keys", ks.contains(new Integer(i).toString())); } } @Test public void test_lastKey() { // Test for method java.lang.Object java.util.TreeMap.lastKey() - assertTrue("Returned incorrect last key", tm.lastKey().equals( - objArray[objArray.length - 1].toString())); + assertTrue("Returned incorrect last key", tm.lastKey().equals(objArray[objArray.length - 1].toString())); } @Test @@ -394,19 +380,18 @@ public class TreeMapTest { // expected } - tm = new TreeMap(); + tm = new TreeMap<>(); assertNull(tm.put(new Integer(1), new Object())); } @Test public void test_putAllLjava_util_Map() { // Test for method void java.util.TreeMap.putAll(java.util.Map) - TreeMap x = new TreeMap(); + TreeMap x = new TreeMap<>(); x.putAll(tm); assertTrue("Map incorrect size after put", x.size() == tm.size()); for (Object element : objArray) { - assertTrue("Failed to put all elements", x.get(element.toString()) - .equals(element)); + assertTrue("Failed to put all elements", x.get(element.toString()).equals(element)); } } @@ -429,8 +414,7 @@ public class TreeMapTest { public void test_subMapLjava_lang_ObjectLjava_lang_Object() { // Test for method java.util.SortedMap // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object) - SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109] - .toString()); + SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109].toString()); assertEquals("subMap is of incorrect size", 9, subMap.size()); for (int counter = 100; counter < 109; counter++) { assertTrue("SubMap contains incorrect elements", subMap.get( @@ -445,8 +429,7 @@ public class TreeMapTest { } // Regression for Harmony-1161 - TreeMap treeMapWithNull = new TreeMap( - new MockComparatorNullTolerable()); + TreeMap treeMapWithNull = new TreeMap<>(new MockComparatorNullTolerable()); treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$ treeMapWithNull.put(null, "value2"); //$NON-NLS-1$ SortedMap subMapWithNull = treeMapWithNull.subMap(null, @@ -454,7 +437,7 @@ public class TreeMapTest { assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$ // Regression test for typo in lastKey method - SortedMap map = new TreeMap(); + SortedMap map = new TreeMap<>(); map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$ map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$ map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$ @@ -466,7 +449,7 @@ public class TreeMapTest { @Test public void test_subMap_Iterator() { - TreeMap map = new TreeMap(); + TreeMap map = new TreeMap<>(); String[] keys = { "1", "2", "3" }; String[] values = { "one", "two", "three" }; @@ -476,15 +459,14 @@ public class TreeMapTest { assertEquals(3, map.size()); - Map subMap = map.subMap("", "test"); + Map subMap = map.subMap("", "test"); assertEquals(3, subMap.size()); - Set entrySet = subMap.entrySet(); - Iterator iter = entrySet.iterator(); + Set> entrySet = subMap.entrySet(); + Iterator> iter = entrySet.iterator(); int size = 0; while (iter.hasNext()) { - Map.Entry entry = (Map.Entry) iter - .next(); + Map.Entry entry = iter.next(); assertTrue(map.containsKey(entry.getKey())); assertTrue(map.containsValue(entry.getValue())); size++; @@ -492,10 +474,10 @@ public class TreeMapTest { assertEquals(map.size(), size); Set keySet = subMap.keySet(); - iter = keySet.iterator(); + Iterator keyIter = keySet.iterator(); size = 0; - while (iter.hasNext()) { - String key = (String) iter.next(); + while (keyIter.hasNext()) { + String key = keyIter.next(); assertTrue(map.containsKey(key)); size++; } @@ -507,12 +489,10 @@ public class TreeMapTest { public void test_tailMapLjava_lang_Object() { // Test for method java.util.SortedMap // java.util.TreeMap.tailMap(java.lang.Object) - Map tail = tm.tailMap(objArray[900].toString()); - assertTrue("Returned map of incorrect size : " + tail.size(), tail - .size() == (objArray.length - 900) + 9); + Map tail = tm.tailMap(objArray[900].toString()); + assertTrue("Returned map of incorrect size : " + tail.size(), tail.size() == (objArray.length - 900) + 9); for (int i = 900; i < objArray.length; i++) { - assertTrue("Map contains incorrect entries", tail - .containsValue(objArray[i])); + assertTrue("Map contains incorrect entries", tail.containsValue(objArray[i])); } // Regression for Harmony-1066 @@ -520,7 +500,7 @@ public class TreeMapTest { SortedMap intMap,sub; int size = 16; - intMap = new TreeMap(); + intMap = new TreeMap<>(); for(int i=0; i(); + intMap = new TreeMap<>(); for(int i=0; i vals = tm.values(); vals.iterator(); assertTrue("Returned collection of incorrect size", vals.size() == objArray.length); for (Object element : objArray) { - assertTrue("Collection contains incorrect elements", vals - .contains(element)); + assertTrue("Collection contains incorrect elements", vals.contains(element)); } - TreeMap myTreeMap = new TreeMap(); + TreeMap myTreeMap = new TreeMap<>(); for (int i = 0; i < 100; i++) { myTreeMap.put(objArray[i], objArray[i]); } - Collection values = myTreeMap.values(); + Collection values = myTreeMap.values(); values.remove(new Integer(0)); - assertTrue( - "Removing from the values collection should remove from the original map", + assertTrue("Removing from the values collection should remove from the original map", !myTreeMap.containsValue(new Integer(0))); } @Test public void test_equals() throws Exception { // comparing TreeMaps with different object types - Map m1 = new TreeMap(); - Map m2 = new TreeMap(); + Map m1 = new TreeMap<>(); + Map m2 = new TreeMap<>(); m1.put("key1", "val1"); m1.put("key2", "val2"); m2.put(new Integer(1), "val1"); @@ -597,8 +575,8 @@ public class TreeMapTest { assertFalse("Maps should not be equal 2", m2.equals(m1)); // comparing TreeMap with HashMap - m1 = new TreeMap(); - m2 = new HashMap(); + m1 = new TreeMap<>(); + m2 = new HashMap<>(); m1.put("key", "val"); m2.put(new Object(), "val"); assertFalse("Maps should not be equal 3", m1.equals(m2)); @@ -612,8 +590,8 @@ public class TreeMapTest { */ @Test public void test_entrySet_contains() throws Exception { - TreeMap master = new TreeMap(); - TreeMap test_map = new TreeMap(); + TreeMap master = new TreeMap<>(); + TreeMap test_map = new TreeMap<>(); master.put("null", null); Object[] entry = master.entrySet().toArray(); @@ -630,7 +608,7 @@ public class TreeMapTest { test_map.entrySet().containsAll(master.entrySet())); master.clear(); - master.put("null", '0'); + master.put("null", "0"); entry = master.entrySet().toArray(); assertFalse("Null-valued entry should not equal non-null-valued entry", test_map.entrySet().contains(entry[0]));