From 651bc3a3a3d256af011e8f5534c8b10debbc6a19 Mon Sep 17 00:00:00 2001 From: Victor Date: Thu, 17 Oct 2019 11:05:14 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=BB=D0=B8=D0=B7=201.5.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/com/annimon/ownlang/Main.java | 6 +- .../modules/collections/collections.java | 88 +++++++++++++++++++ 2 files changed, 91 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/annimon/ownlang/modules/collections/collections.java diff --git a/src/main/java/com/annimon/ownlang/Main.java b/src/main/java/com/annimon/ownlang/Main.java index 66b74d9d..470422da 100644 --- a/src/main/java/com/annimon/ownlang/Main.java +++ b/src/main/java/com/annimon/ownlang/Main.java @@ -22,9 +22,9 @@ */ public final class Main { - public static int VERSION_MAJOR = 1; - public static int VERSION_MINOR = 4; - public static int VERSION_PATCH = 0; + public static final int VERSION_MAJOR = 1; + public static final int VERSION_MINOR = 5; + public static final int VERSION_PATCH = 0; public static final String VERSION = VERSION_MAJOR + "." + VERSION_MINOR + "." + VERSION_PATCH + "_" + Gen.BUILD_DATE; diff --git a/src/main/java/com/annimon/ownlang/modules/collections/collections.java b/src/main/java/com/annimon/ownlang/modules/collections/collections.java new file mode 100644 index 00000000..3523b8d6 --- /dev/null +++ b/src/main/java/com/annimon/ownlang/modules/collections/collections.java @@ -0,0 +1,88 @@ +package com.annimon.ownlang.modules.collections; + +import com.annimon.ownlang.exceptions.TypeException; +import com.annimon.ownlang.lib.Arguments; +import com.annimon.ownlang.lib.Function; +import com.annimon.ownlang.lib.Functions; +import com.annimon.ownlang.lib.MapValue; +import com.annimon.ownlang.lib.Types; +import com.annimon.ownlang.lib.Value; +import com.annimon.ownlang.lib.ValueUtils; +import com.annimon.ownlang.modules.Module; +import java.util.Comparator; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.SortedMap; +import java.util.TreeMap; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentSkipListMap; +import java.util.function.Supplier; + +public class collections implements Module { + + public static void initConstants() { + } + + @Override + public void init() { + initConstants(); + Functions.set("hashMap", mapFunction(HashMap::new)); + Functions.set("linkedHashMap", mapFunction(LinkedHashMap::new)); + Functions.set("concurrentHashMap", mapFunction(ConcurrentHashMap::new)); + Functions.set("treeMap", sortedMapFunction(TreeMap::new, TreeMap::new)); + Functions.set("concurrentSkipListMap", sortedMapFunction(ConcurrentSkipListMap::new, ConcurrentSkipListMap::new)); + } + + private Function mapFunction(final Supplier> mapSupplier) { + return (args) -> { + Arguments.checkOrOr(0, 1, args.length); + final Map map = mapSupplier.get(); + if (args.length == 1) { + if (args[0].type() == Types.MAP) { + map.putAll(((MapValue) args[0]).getMap()); + } else { + throw new TypeException("Map expected in first argument"); + } + } + return new MapValue(map); + }; + } + + private Function sortedMapFunction(final Supplier> mapSupplier, + final java.util.function.Function< + Comparator, + SortedMap> comparatorToMapFunction) { + return (args) -> { + Arguments.checkRange(0, 2, args.length); + final SortedMap map; + switch (args.length) { + case 0: // treeMap() + map = mapSupplier.get(); + break; + case 1: // treeMap(map) || treeMap(comparator) + if (args[0].type() == Types.MAP) { + map = mapSupplier.get(); + map.putAll(((MapValue) args[0]).getMap()); + } else if (args[0].type() == Types.FUNCTION) { + final Function comparator = ValueUtils.consumeFunction(args[0], 0); + map = comparatorToMapFunction.apply((o1, o2) -> comparator.execute(o1, o2).asInt()); + } else { + throw new TypeException("Map or comparator function expected in first argument"); + } + break; + case 2: // treeMap(map, comparator) + if (args[0].type() != Types.MAP) { + throw new TypeException("Map expected in first argument"); + } + final Function comparator = ValueUtils.consumeFunction(args[1], 1); + map = comparatorToMapFunction.apply((o1, o2) -> comparator.execute(o1, o2).asInt()); + map.putAll(((MapValue) args[0]).getMap()); + break; + default: + throw new IllegalStateException(); + } + return new MapValue(map); + }; + } +}