package LinkedHashMap //import NoWurst import HashMap import LinkedList import Wurstunit public class LinkedHashMap extends HashMap private LinkedList keys private LHMEntry dummy construct() super() keys = new LinkedList() dummy = new LHMEntry(null, null) /** Saves the given value under the given key. */ override function put(K key, V value) print("putting") if not has(key) print("adding") keys.add(key) super.put(key, value) /** Removes the value saved under the given key. */ override function remove(K key) super.remove(key) keys. remove(key) /** Clears the HashMap of all entries */ override function flush() super.flush() destroy keys keys = new LinkedList() /** Gets an iterator for this LinkedHashMap. */ function iterator() returns LHMIterator return new LHMIterator(this, keys) ondestroy destroy keys destroy dummy public class LHMEntry K key V value construct(K key, V value) this.key = key this.value = value public class LHMIterator LHMEntry dummy LinkedList keys LLIterator keysIterator LinkedHashMap parentMap construct(LinkedHashMap parentMap, LinkedList keys) this.dummy = new LHMEntry(null, null) this.keys = keys this.keysIterator = keys.iterator() this.parentMap = parentMap /** Remove the current element from the LinkedHashMap. */ function remove() if not dummy.key == null parentMap.remove(dummy.key) keysIterator.remove() function hasNext() returns bool return keysIterator.hasNext() function next() returns LHMEntry dummy.key = keysIterator.next() dummy.value = parentMap.get(dummy.key) return dummy function close() keysIterator.close() destroy this @test function checkPutFetch() let map = new LinkedHashMap() map.put(5, 6) let six = map.get(5) six.assertEquals(6) // destroy map @test function checkCanIterate() let map = new LinkedHashMap() map.put(5, 6) print("hello world") map.put(6, 9) print("hello world") var sum = 0 print(".") let iter = map.iterator() print(".") while iter.hasNext() print(".") let entry = iter.next() print(".") sum += entry.value print(".") print(".") iter.close() print(".") sum.assertEquals(15) // destroy map @test function canHaveMultipleMaps() let map = new LinkedHashMap() let map2 = new LinkedHashMap() map. put(1, 2) map2.put(3, 4) map. get(1).assertEquals(2) map2.get(3).assertEquals(4) destroy map destroy map2