Skip to content

Commit

Permalink
Релиз 1.5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
aNNiMON committed Oct 17, 2019
1 parent cb5cae1 commit 651bc3a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/main/java/com/annimon/ownlang/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Map<Value, Value>> mapSupplier) {
return (args) -> {
Arguments.checkOrOr(0, 1, args.length);
final Map<Value, Value> 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<SortedMap<Value, Value>> mapSupplier,
final java.util.function.Function<
Comparator<? super Value>,
SortedMap<Value, Value>> comparatorToMapFunction) {
return (args) -> {
Arguments.checkRange(0, 2, args.length);
final SortedMap<Value, Value> 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);
};
}
}

0 comments on commit 651bc3a

Please sign in to comment.