|
| 1 | +package com.annimon.ownlang.modules.collections; |
| 2 | + |
| 3 | +import com.annimon.ownlang.exceptions.TypeException; |
| 4 | +import com.annimon.ownlang.lib.Arguments; |
| 5 | +import com.annimon.ownlang.lib.Function; |
| 6 | +import com.annimon.ownlang.lib.Functions; |
| 7 | +import com.annimon.ownlang.lib.MapValue; |
| 8 | +import com.annimon.ownlang.lib.Types; |
| 9 | +import com.annimon.ownlang.lib.Value; |
| 10 | +import com.annimon.ownlang.lib.ValueUtils; |
| 11 | +import com.annimon.ownlang.modules.Module; |
| 12 | +import java.util.Comparator; |
| 13 | +import java.util.HashMap; |
| 14 | +import java.util.LinkedHashMap; |
| 15 | +import java.util.Map; |
| 16 | +import java.util.SortedMap; |
| 17 | +import java.util.TreeMap; |
| 18 | +import java.util.concurrent.ConcurrentHashMap; |
| 19 | +import java.util.concurrent.ConcurrentSkipListMap; |
| 20 | +import java.util.function.Supplier; |
| 21 | + |
| 22 | +public class collections implements Module { |
| 23 | + |
| 24 | + public static void initConstants() { |
| 25 | + } |
| 26 | + |
| 27 | + @Override |
| 28 | + public void init() { |
| 29 | + initConstants(); |
| 30 | + Functions.set("hashMap", mapFunction(HashMap::new)); |
| 31 | + Functions.set("linkedHashMap", mapFunction(LinkedHashMap::new)); |
| 32 | + Functions.set("concurrentHashMap", mapFunction(ConcurrentHashMap::new)); |
| 33 | + Functions.set("treeMap", sortedMapFunction(TreeMap::new, TreeMap::new)); |
| 34 | + Functions.set("concurrentSkipListMap", sortedMapFunction(ConcurrentSkipListMap::new, ConcurrentSkipListMap::new)); |
| 35 | + } |
| 36 | + |
| 37 | + private Function mapFunction(final Supplier<Map<Value, Value>> mapSupplier) { |
| 38 | + return (args) -> { |
| 39 | + Arguments.checkOrOr(0, 1, args.length); |
| 40 | + final Map<Value, Value> map = mapSupplier.get(); |
| 41 | + if (args.length == 1) { |
| 42 | + if (args[0].type() == Types.MAP) { |
| 43 | + map.putAll(((MapValue) args[0]).getMap()); |
| 44 | + } else { |
| 45 | + throw new TypeException("Map expected in first argument"); |
| 46 | + } |
| 47 | + } |
| 48 | + return new MapValue(map); |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + private Function sortedMapFunction(final Supplier<SortedMap<Value, Value>> mapSupplier, |
| 53 | + final java.util.function.Function< |
| 54 | + Comparator<? super Value>, |
| 55 | + SortedMap<Value, Value>> comparatorToMapFunction) { |
| 56 | + return (args) -> { |
| 57 | + Arguments.checkRange(0, 2, args.length); |
| 58 | + final SortedMap<Value, Value> map; |
| 59 | + switch (args.length) { |
| 60 | + case 0: // treeMap() |
| 61 | + map = mapSupplier.get(); |
| 62 | + break; |
| 63 | + case 1: // treeMap(map) || treeMap(comparator) |
| 64 | + if (args[0].type() == Types.MAP) { |
| 65 | + map = mapSupplier.get(); |
| 66 | + map.putAll(((MapValue) args[0]).getMap()); |
| 67 | + } else if (args[0].type() == Types.FUNCTION) { |
| 68 | + final Function comparator = ValueUtils.consumeFunction(args[0], 0); |
| 69 | + map = comparatorToMapFunction.apply((o1, o2) -> comparator.execute(o1, o2).asInt()); |
| 70 | + } else { |
| 71 | + throw new TypeException("Map or comparator function expected in first argument"); |
| 72 | + } |
| 73 | + break; |
| 74 | + case 2: // treeMap(map, comparator) |
| 75 | + if (args[0].type() != Types.MAP) { |
| 76 | + throw new TypeException("Map expected in first argument"); |
| 77 | + } |
| 78 | + final Function comparator = ValueUtils.consumeFunction(args[1], 1); |
| 79 | + map = comparatorToMapFunction.apply((o1, o2) -> comparator.execute(o1, o2).asInt()); |
| 80 | + map.putAll(((MapValue) args[0]).getMap()); |
| 81 | + break; |
| 82 | + default: |
| 83 | + throw new IllegalStateException(); |
| 84 | + } |
| 85 | + return new MapValue(map); |
| 86 | + }; |
| 87 | + } |
| 88 | +} |
0 commit comments