/** * 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; } }
private boolean netKernel(final OtpMsg m) { OtpMbox mbox = null; try { final OtpErlangTuple t = (OtpErlangTuple) m.getMsg(); final OtpErlangTuple req = (OtpErlangTuple) t.elementAt(1); // actual // request final OtpErlangPid pid = (OtpErlangPid) req.elementAt(0); // originating // pid final OtpErlangObject[] pong = new OtpErlangObject[2]; pong[0] = req.elementAt(1); // his #Ref pong[1] = new OtpErlangAtom("yes"); mbox = createMbox(); mbox.send(pid, new OtpErlangTuple(pong)); return true; } catch (final Exception e) { } finally { closeMbox(mbox); } return false; }
/** * Get the specified element from the tuple. * * @param i * the index of the requested element. Tuple elements are * numbered as array elements, starting at 0. * * @return the requested element, of null if i is not a valid element index. */ public OtpErlangObject elementAt(final int i) { if (i >= arity() || i < 0) { return null; } return elems[i]; }
private OtpErlangTuple getPingTuple(final OtpMbox mbox) { final OtpErlangObject[] ping = new OtpErlangObject[3]; final OtpErlangObject[] pid = new OtpErlangObject[2]; final OtpErlangObject[] anode = new OtpErlangObject[2]; pid[0] = mbox.self(); pid[1] = createRef(); anode[0] = new OtpErlangAtom("is_auth"); anode[1] = new OtpErlangAtom(node()); ping[0] = new OtpErlangAtom("$gen_call"); ping[1] = new OtpErlangTuple(pid); ping[2] = new OtpErlangTuple(anode); return new OtpErlangTuple(ping); }
public ErlangErrorRpcException(OtpErlangTuple tuple) { super(tuple.toString()); this.reasonTuple = tuple; }
@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; }
/** * Get all the elements from the tuple as an array. * * @return an array containing all of the tuple's elements. */ public OtpErlangObject[] elements() { final OtpErlangObject[] res = new OtpErlangObject[arity()]; System.arraycopy(elems, 0, res, 0, res.length); return res; }
rpc[1] = new OtpErlangTuple(call); send("rex", new OtpErlangTuple(rpc));
public ErlangBadRpcException(OtpErlangTuple tuple) { super(tuple.toString()); this.reasonTuple = tuple; }
public static void assertQuotedCorrectly(PsiFile file) { final String text = file.getText(); try { OtpErlangTuple quotedMessage = Quoter.quote(text); Quoter.assertMessageReceived(quotedMessage); OtpErlangAtom status = (OtpErlangAtom) quotedMessage.elementAt(0); String statusString = status.atomValue(); OtpErlangObject expectedQuoted = quotedMessage.elementAt(1); if (statusString.equals("ok")) { OtpErlangObject actualQuoted = ElixirPsiImplUtil.quote(file); assertQuotedCorrectly(expectedQuoted, actualQuoted); } else if (statusString.equals("error")) { OtpErlangTuple error = (OtpErlangTuple) expectedQuoted; OtpErlangLong line = (OtpErlangLong) error.elementAt(0); OtpErlangBinary messageBinary = (OtpErlangBinary) error.elementAt(1); String message = ElixirPsiImplUtil.javaString(messageBinary); OtpErlangBinary tokenBinary = (OtpErlangBinary) error.elementAt(2); String token = ElixirPsiImplUtil.javaString(tokenBinary); throw new AssertionError( "intellij_elixir returned \"" + message + "\" on line " + line + " due to " + token + ", use assertQuotesAroundError if error is expect in Elixir natively, " + "but not in intellij-elixir plugin" ); } } catch (IOException | OtpErlangDecodeException | OtpErlangExit e) { throw new RuntimeException(e); } }
@NotNull private static String toString(@NotNull OtpErlangTuple quoted) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("{"); for (int i = 0; i < quoted.arity(); i++) { if (i > 0) { stringBuilder.append(","); } stringBuilder.append(toString(quoted.elementAt(i))); } stringBuilder.append("}"); return stringBuilder.toString(); } }
/** * Determine if two tuples are equal. Tuples are equal if they have the same * arity and all of the elements are equal. * * @param o * the tuple to compare to. * * @return true if the tuples have the same arity and all the elements are * equal. */ @Override public boolean equals(final Object o) { if (!(o instanceof OtpErlangTuple)) { return false; } final OtpErlangTuple t = (OtpErlangTuple) o; final int a = arity(); if (a != t.arity()) { return false; } for (int i = 0; i < a; i++) { if (!elems[i].equals(t.elems[i])) { return false; // early exit } } return true; }
msg[1] = new OtpErlangTuple(msgbody); new OtpErlangTuple(msg));
public static void assertError(PsiFile file) { final String text = file.getText(); try { OtpErlangTuple quotedMessage = Quoter.quote(text); Quoter.assertMessageReceived(quotedMessage); OtpErlangAtom status = (OtpErlangAtom) quotedMessage.elementAt(0); String statusString = status.atomValue(); assertEquals(statusString, "error"); } catch (IOException | OtpErlangDecodeException | OtpErlangExit e) { throw new RuntimeException(e); } }
@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(); }
return new OtpErlangTuple(this);
private BigCouchUpdateSequence(final String encodedVector, final String packedSeqs) { this.since = encodedVector; final byte[] bytes = new Base64(true).decode(packedSeqs); final OtpInputStream stream = new OtpInputStream(bytes); try { final OtpErlangList list = (OtpErlangList) stream.read_any(); for (int i = 0, arity = list.arity(); i < arity; i++) { final OtpErlangTuple tuple = (OtpErlangTuple) list.elementAt(i); final OtpErlangObject node = tuple.elementAt(0); final OtpErlangObject range = tuple.elementAt(1); final OtpErlangObject seq_obj = tuple.elementAt(2); final OtpErlangLong node_seq; if (seq_obj instanceof OtpErlangLong) { node_seq = (OtpErlangLong) seq_obj; } else if (seq_obj instanceof OtpErlangTuple) { node_seq = (OtpErlangLong) ((OtpErlangTuple)seq_obj).elementAt(0); } else { throw new IllegalArgumentException("could not decode seq"); } vector.put(node + "-" + range, node_seq.longValue()); } } catch (final OtpErlangDecodeException e) { throw new IllegalArgumentException(encodedVector + " not valid."); } }
public void handleResponseError(String module, String function, OtpErlangObject result) { //{badrpc,{'EXIT',{undef,[{rabbit_access_control,list_users,[[]]},{rpc,'-handle_call/3-fun-0-',5}]}}} if (result instanceof OtpErlangTuple) { OtpErlangTuple msg = (OtpErlangTuple)result; if (msg.elementAt(0) instanceof OtpErlangAtom) { OtpErlangAtom responseAtom = (OtpErlangAtom)msg.elementAt(0); //TODO consider error handler strategy. if (responseAtom.atomValue().equals("badrpc")) { if (msg.elementAt(1) instanceof OtpErlangTuple) { throw new ErlangBadRpcException( (OtpErlangTuple)msg.elementAt(1)); } else { throw new ErlangBadRpcException( msg.elementAt(1).toString()); } } else if (responseAtom.atomValue().equals("error")) { if (msg.elementAt(1) instanceof OtpErlangTuple) { throw new ErlangErrorRpcException( (OtpErlangTuple)msg.elementAt(1)); } else { throw new ErlangErrorRpcException( msg.elementAt(1).toString()); } } } } }