diff options
author | Dmitriy Kargapolov <[email protected]> | 2015-02-03 16:39:39 -0500 |
---|---|---|
committer | Dmitriy Kargapolov <[email protected]> | 2015-02-03 16:39:39 -0500 |
commit | 2fdc3d313485a76b6acf12154b8f3bd3e1ceb2ca (patch) | |
tree | 39e42440dcaebb29368fd0290391d328f216524b /lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java | |
parent | ab1cfe19a0af082a2c207fb5c07c7a6ad5782c14 (diff) | |
download | otp-2fdc3d313485a76b6acf12154b8f3bd3e1ceb2ca.tar.gz otp-2fdc3d313485a76b6acf12154b8f3bd3e1ceb2ca.tar.bz2 otp-2fdc3d313485a76b6acf12154b8f3bd3e1ceb2ca.zip |
jinterface: match/bind added to OtpErlangObject
Adding these two methods to the OtpErlangObject abstract class makes possible
implementing of pattern matching and variable binding for all types of Erlang
terms. Particular implementations may vary and include additional classes to
define Variable placeholder objects and Bindings objects. Also Parser class
may be added to facilitate creating complex patterns and terms.
The purpose of this commit is to provide low level interface base methods
sufficient for variety of higher level pattern matching/variable binding
implementations.
OtpErlangMap class is re-designed for efficiency (it is based on HashMap now).
All changes are backward-compatible. Detailed test cases implemented.
Diffstat (limited to 'lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java')
-rw-r--r-- | lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java index 990e50ddcd..268261ec10 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpErlangList.java @@ -297,6 +297,54 @@ public class OtpErlangList extends OtpErlangObject implements return getLastTail().equals(l.getLastTail()); } + @Override + public <T> boolean match(final OtpErlangObject term, final T bindings) { + if (!(term instanceof OtpErlangList)) { + return false; + } + final OtpErlangList that = (OtpErlangList) term; + + final int thisArity = this.arity(); + final int thatArity = that.arity(); + final OtpErlangObject thisTail = this.getLastTail(); + final OtpErlangObject thatTail = that.getLastTail(); + + if (thisTail == null) { + if (thisArity != thatArity || thatTail != null) { + return false; + } + } else { + if (thisArity > thatArity) { + return false; + } + } + for (int i = 0; i < thisArity; i++) { + if (!elementAt(i).match(that.elementAt(i), bindings)) { + return false; + } + } + if (thisTail == null) { + return true; + } + return thisTail.match(that.getNthTail(thisArity), bindings); + } + + @Override + public <T> OtpErlangObject bind(final T binds) throws OtpErlangException { + final OtpErlangList list = (OtpErlangList) this.clone(); + + final int a = list.elems.length; + for (int i = 0; i < a; i++) { + list.elems[i] = list.elems[i].bind(binds); + } + + if (list.lastTail != null) { + list.lastTail = list.lastTail.bind(binds); + } + + return list; + } + public OtpErlangObject getLastTail() { return lastTail; } |