-
Notifications
You must be signed in to change notification settings - Fork 852
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
53c8710
commit 1861880
Showing
3 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/com/googlecode/aviator/runtime/function/seq/SeqAddAllFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters