@NotNull private static String toString(@NotNull OtpErlangObject quoted) { String string; if (quoted instanceof OtpErlangBoolean || quoted instanceof OtpErlangAtom || quoted instanceof OtpErlangByte || quoted instanceof OtpErlangChar || quoted instanceof OtpErlangFloat || quoted instanceof OtpErlangDouble || quoted instanceof OtpErlangExternalFun || quoted instanceof OtpErlangFun || quoted instanceof OtpErlangInt || quoted instanceof OtpErlangLong || quoted instanceof OtpErlangMap || quoted instanceof OtpErlangPid || quoted instanceof OtpErlangString) { string = quoted.toString(); } else if (quoted instanceof OtpErlangBitstr) { string = toString((OtpErlangBitstr) quoted); } else if (quoted instanceof OtpErlangList) { string = toString((OtpErlangList) quoted); } else if (quoted instanceof OtpErlangTuple) { string = toString((OtpErlangTuple) quoted); } else { throw new IllegalArgumentException("Don't know how to convert " + quoted.getClass() + " to string"); } return string; }
/** * Receives a list of tuples and returns the first tuple where the item at {@code position} in the tuple matches the * given {@code item}. * * @param tupleList List of tuples. Tuples don't all have to be the same size or have position as a valid offset. * @param key key to search for at * @param position position of the * @param defaultValue * @return tuple with equal {@code key} or {@code defaultValue} if {@code key} is not found. */ public static OtpErlangObject keyfind(OtpErlangList tupleList, OtpErlangObject key, int position, OtpErlangObject defaultValue) { OtpErlangObject matchTuple = defaultValue; for (OtpErlangObject element : tupleList) { OtpErlangTuple tuple = (OtpErlangTuple) element; if (tuple.elementAt(position).equals(key)) { matchTuple = tuple; break; } } return matchTuple; } }
@Override public Object clone() { final OtpErlangBitstr that = (OtpErlangBitstr) super.clone(); that.bin = bin.clone(); that.pad_bits = pad_bits; return that; } }
@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; }
@Override protected int doHashCode() { final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(4); final int a = arity(); if (a == 0) { return (int) 3468870702L; } for (int i = 0; i < a; i++) { hash.combine(elementAt(i).hashCode()); } final OtpErlangObject t = getLastTail(); if (t != null) { final int h = t.hashCode(); hash.combine(h, h); } return hash.valueOf(); }
/** * Write an arbitrary Erlang term to the stream. * * @param o * the Erlang term to write. */ public void write_any(final OtpErlangObject o) { // calls one of the above functions, depending on o o.encode(this); }
@Override public int hashCode() { if (hashCodeValue == 0) { hashCodeValue = doHashCode(); } return hashCodeValue; }
@Override @SuppressWarnings("unchecked") public Object clone() { final OtpErlangMap newMap = (OtpErlangMap) super.clone(); newMap.map = (OtpMap) map.clone(); return newMap; } }
@Override public <T> OtpErlangObject bind(final T binds) throws OtpErlangException { final OtpErlangTuple tuple = (OtpErlangTuple) this.clone(); final int a = tuple.elems.length; for (int i = 0; i < a; i++) { final OtpErlangObject e = tuple.elems[i]; tuple.elems[i] = e.bind(binds); } return tuple; }
@Override protected int doHashCode() { final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(9); final int a = arity(); hash.combine(a); for (int i = 0; i < a; i++) { hash.combine(elems[i].hashCode()); } return hash.valueOf(); }
/** * Create an OtpErlangExit exception with the given reason. * * @param reason * the reason this exit signal has been sent. */ public OtpErlangExit(final OtpErlangObject reason) { super(reason.toString()); this.reason = reason; }
@Nullable public static OtpErlangPid whereis(OtpMbox localMbox, OtpNode localNode, String remoteName, String remoteNode, int timeout) throws OtpErlangExit, OtpErlangDecodeException { OtpErlangObject response = RPC.INSTANCE.unmonitoredCall( remoteNode, localMbox, localNode, timeout, new OtpErlangAtom("Elixir.Process"), new OtpErlangAtom("whereis"), new OtpErlangAtom(remoteName) ); OtpErlangPid pid; if (response instanceof OtpErlangAtom) { if (response.equals(NIL)) { pid = null; } else { throw new NotImplementedException("Expected atoms to be nil, but got" + response); } } else { pid = (OtpErlangPid) response; } return pid; } }
@Override public Object clone() { final OtpErlangRef newRef = (OtpErlangRef) super.clone(); newRef.ids = ids.clone(); return newRef; } }
@Override public <T> OtpErlangObject bind(final T binds) throws OtpErlangException { final OtpErlangMap ret = new OtpErlangMap(); OtpErlangObject key, val; for (final Map.Entry<OtpErlangObject, OtpErlangObject> e : entrySet()) { key = e.getKey(); val = e.getValue(); ret.put(key, val.bind(binds)); } return ret; }
@Override protected int doHashCode() { final OtpErlangObject.Hash hash = new OtpErlangObject.Hash(1); hash.combine(pid.hashCode(), module.hashCode()); hash.combine(arity); if (md5 != null) { hash.combine(md5); } hash.combine(index); hash.combine(uniq); if (freeVars != null) { for (final OtpErlangObject o : freeVars) { hash.combine(o.hashCode(), 1); } } return hash.valueOf(); }
/** * Create an OtpErlangExit exception with the given reason and sender pid. * * @param reason * the reason this exit signal has been sent. * * @param pid * the pid that sent this exit. */ public OtpErlangExit(final OtpErlangObject reason, final OtpErlangPid pid) { super(reason.toString()); this.reason = reason; this.pid = pid; }
private static void assertQuotedCorrectly(@NotNull OtpErlangObject expectedQuoted, @NotNull OtpErlangObject actualQuoted) { if (!expectedQuoted.equals(actualQuoted)) { throw new ComparisonFailure(null, toString(expectedQuoted), toString(actualQuoted)); } }
@Override public Object clone() { final OtpErlangTuple newTuple = (OtpErlangTuple) super.clone(); newTuple.elems = elems.clone(); return newTuple; } }
protected String toString(final int start) { final StringBuffer s = new StringBuffer(); s.append("["); for (int i = start; i < arity(); i++) { if (i > start) { s.append(","); } s.append(elems[i].toString()); } if (lastTail != null) { s.append("|").append(lastTail.toString()); } s.append("]"); return s.toString(); }