boolean symbolKeys = hasSymbolKeys(map); for (final Map.Entry<OtpErlangObject, OtpErlangObject> e : map.entrySet()) { OtpErlangObject key = e.getKey(); if (!(isStruct(map) && key instanceof OtpErlangAtom && ((OtpErlangAtom) key).atomValue().equals("__struct__"))) {
@Nullable private static String structType(OtpErlangMap map) { OtpErlangObject structValue = map.get(new OtpErlangAtom("__struct__")); if (structValue instanceof OtpErlangAtom) { return ElixirModulesUtil.INSTANCE.erlangModuleNameToElixir(((OtpErlangAtom) structValue).atomValue()); } else { return null; } }
public static boolean hasSymbolKeys(OtpErlangMap map) { for (OtpErlangObject key : map.keys()) { if (!(key instanceof OtpErlangAtom) || ((OtpErlangAtom) key).atomValue().startsWith("Elixir.")) return false; } return true; }
@Override public <T> boolean match(final OtpErlangObject term, final T binds) { if (!(term instanceof OtpErlangMap)) { return false; } final OtpErlangMap t = (OtpErlangMap) term; final int a = arity(); if (a > t.arity()) { return false; } if (a == 0) { return true; } OtpErlangObject key, val; for (final Map.Entry<OtpErlangObject, OtpErlangObject> e : entrySet()) { key = e.getKey(); val = e.getValue(); final OtpErlangObject v = t.get(key); if (v == null || !val.match(v, binds)) { return false; } } return true; }
@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; }
/** * Convert this map to the equivalent Erlang external representation. * * @param buf * an output stream to which the encoded map should be written. */ @Override public void encode(final OtpOutputStream buf) { final int arity = arity(); buf.write_map_head(arity); for (final Map.Entry<OtpErlangObject, OtpErlangObject> e : entrySet()) { buf.write_any(e.getKey()); buf.write_any(e.getValue()); } }
/** * Get all the keys from the map as an array. * * @return an array containing all of the map's keys. */ public OtpErlangObject[] keys() { return map.keySet().toArray(new OtpErlangObject[arity()]); }
+ ")"); put(key, val);
return new OtpErlangMap(this);
/** * Get all the values from the map as an array. * * @return an array containing all of the map's values. */ public OtpErlangObject[] values() { return map.values().toArray(new OtpErlangObject[arity()]); }
/** * Create a map from a stream containing a map encoded in Erlang external * format. * * @param buf * the stream containing the encoded map. * * @exception OtpErlangDecodeException * if the buffer does not contain a valid external * representation of an Erlang map. */ public OtpErlangMap(final OtpInputStream buf) throws OtpErlangDecodeException { final int arity = buf.read_map_head(); if (arity > 0) { map = new OtpMap(); for (int i = 0; i < arity; i++) { OtpErlangObject key, val; key = buf.read_any(); val = buf.read_any(); put(key, val); } } else { map = new OtpMap(); } }
/** * Get the string representation of the map. * * @return the string representation of the map. */ @Override public String toString() { final StringBuffer s = new StringBuffer(); s.append("#{"); boolean first = true; for (final Map.Entry<OtpErlangObject, OtpErlangObject> e : entrySet()) { if (first) { first = false; } else { s.append(","); } s.append(e.getKey().toString()); s.append(" => "); s.append(e.getValue().toString()); } s.append("}"); return s.toString(); }