/** * 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; } }
@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(); } }
private static void renderTuple(OtpErlangTuple tuple, XValueTextRenderer renderer) { int i; renderer.renderSpecialSymbol("{"); for (i = 0; i < tuple.arity(); i++) { if (i > 0) { renderer.renderSpecialSymbol(", "); } renderObject(tuple.elementAt(i), renderer); } renderer.renderSpecialSymbol("}"); }
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); } }
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); } }
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()); } } } } }
tag = (int) ((OtpErlangLong) ((OtpErlangTuple) h).elementAt(0)) .longValue();
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; }
final OtpErlangTuple t = (OtpErlangTuple) msg; if (t.arity() == 2) { return t.elementAt(1); // obs: second element
final OtpErlangAtom a = (OtpErlangAtom) t.elementAt(1); return "yes".equals(a.atomValue()); } catch (final Exception e) {
if (!(head.elementAt(0) instanceof OtpErlangLong)) { break receive_loop; tag = (int) ((OtpErlangLong) head.elementAt(0)).longValue(); if (!(head.elementAt(1) instanceof OtpErlangAtom)) { break receive_loop; cookie = (OtpErlangAtom) head.elementAt(1); if (sendCookie) { if (!cookie.atomValue().equals(localNode.cookie())) { to = (OtpErlangPid) head.elementAt(2); if (!(head.elementAt(2) instanceof OtpErlangAtom)) { break receive_loop; cookie = (OtpErlangAtom) head.elementAt(2); if (sendCookie) { if (!cookie.atomValue().equals(localNode.cookie())) { from = (OtpErlangPid) head.elementAt(1); toName = (OtpErlangAtom) head.elementAt(3); if (head.elementAt(3) == null) { break receive_loop; from = (OtpErlangPid) head.elementAt(1); to = (OtpErlangPid) head.elementAt(2);