Skip to content

Commit

Permalink
feat: impl seq.add_all, #500
Browse files Browse the repository at this point in the history
  • Loading branch information
killme2008 committed Oct 29, 2022
1 parent 53c8710 commit 1861880
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import com.googlecode.aviator.runtime.function.math.MathSinFunction;
import com.googlecode.aviator.runtime.function.math.MathSqrtFunction;
import com.googlecode.aviator.runtime.function.math.MathTanFunction;
import com.googlecode.aviator.runtime.function.seq.SeqAddAllFunction;
import com.googlecode.aviator.runtime.function.seq.SeqAddFunction;
import com.googlecode.aviator.runtime.function.seq.SeqArrayFunction;
import com.googlecode.aviator.runtime.function.seq.SeqCollectorFunction;
Expand Down Expand Up @@ -947,6 +948,7 @@ private void loadSeqFunctions() {
addFunction(new SeqMapEntryFunction());
addFunction(new SeqIntoFunction());
addFunction(new SeqAddFunction());
addFunction(new SeqAddAllFunction());
addFunction(new SeqRemoveFunction());
addFunction(new SeqGetFunction());
addFunction(new SeqPutFunction());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.googlecode.aviator.runtime.function.seq;

import java.util.Collection;
import java.util.Map;
import com.googlecode.aviator.runtime.RuntimeFunctionDelegator;
import com.googlecode.aviator.runtime.RuntimeUtils;
import com.googlecode.aviator.runtime.function.AbstractFunction;
import com.googlecode.aviator.runtime.type.AviatorObject;
import com.googlecode.aviator.runtime.type.Collector;
import com.googlecode.aviator.utils.ArrayUtils;

/**
* seq.add function to add all elements in other sequence into this one.
*
* @author dennis
* @since 5.3.3
*
*/
public class SeqAddAllFunction extends AbstractFunction {

private static final long serialVersionUID = -4406740199823615336L;

@Override
public String getName() {
return "seq.add_all";
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Override
public AviatorObject call(final Map<String, Object> env, final AviatorObject arg1,
final AviatorObject arg2) {

Object coll1 = arg1.getValue(env);
Object coll2 = arg2.getValue(env);
if (coll1 == null) {
throw new NullPointerException("null seq");
}
if (coll2 == null) {
return arg1;
}
Class<?> clazz = coll1.getClass();

for (Object element : RuntimeUtils.seq(coll2, env)) {
if (Collection.class.isAssignableFrom(clazz)) {
((Collection) coll1).add(element);
} else if (Map.class.isAssignableFrom(clazz) && element instanceof Map.Entry) {
Map.Entry entry = (Map.Entry) element;
((Map) coll1).put(entry.getKey(), entry.getValue());
} else if (Collector.class.isAssignableFrom(clazz)) {
((Collector) coll1).add(element);
} else {
throw new IllegalArgumentException(arg1.desc(env) + " is not a collection or map.");
}
}
return arg1;
}
}
21 changes: 20 additions & 1 deletion src/test/resources/lib/test_aviator.av
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,24 @@ fn test_partial() {
j.assertEquals(21, t(4, 5, 6));
}

fn test_seq_add_all() {
tests = tests + 1;

let s1 = seq.set(1, 2, 3, 4, 5);
let s2 = seq.list(6, 7, 7, 8, 9);

seq.add_all(s1, s2);
j.assertEquals(9, count(s1));
j.assertEquals(s1, seq.set(1, 2, 3, 4, 5, 6, 7, 8, 9));

let m1 = seq.map('a', 1, 'b', 2);
let m2 = seq.map('b', 3, 'c', 4, 'd', 5);
seq.add_all(m1, m2);

j.assertEquals(4, count(m1));
j.assertEquals(m1, seq.map('a', 1, 'b', 3, 'c', 4, 'd', 5));
}

test_take_while();
test_drop_while();
test_concat();
Expand All @@ -378,5 +396,6 @@ test_static_methods();
test_is_distinct();
test_meta();
test_partial();
test_seq_add_all();

j.assertEquals(20, tests);
j.assertEquals(21, tests);

0 comments on commit 1861880

Please sign in to comment.