public Binder getOrCreateBinder(String key) { checkNotFrozen(); HashMap<String, Object> x = (HashMap<String, Object>) get(key); Binder b = null; if (x == null) { b = new Binder(); put(key, b); } else b = (x instanceof Binder) ? (Binder) x : new Binder(x); return b; }
public static JSApiScriptParameters fromBinder(Binder binder) { JSApiScriptParameters scriptParameters = new JSApiScriptParameters(); JSApiCompressionEnum compression = JSApiCompressionEnum.valueOf(binder.getStringOrThrow("compression")); scriptParameters.isCompressed = (compression != JSApiCompressionEnum.RAW); scriptParameters.timeLimitMillis = binder.getIntOrThrow("time_limit"); scriptParameters.permissions = binder.getBinderOrThrow("permissions"); scriptParameters.domainMasks = binder.getListOrThrow("domainMasks"); scriptParameters.ipMasks = binder.getListOrThrow("ipMasks"); return scriptParameters; }
/** * Convert some map to the binder. Do nothing if it is already a binder. * * @param x source map */ static public Binder of(Object x) { return from(x); }
private Binder onPing(Binder params) { Binder result = Binder.fromKeysValues("ping", "pong"); result.putAll(params); return result; }
@Test public void isFrozen() throws Exception { Binder b = new Binder(); b.put("hello", "world"); Binder x = b.getOrCreateBinder("inner"); x.put("foo", "bar"); assertFalse(b.isFrozen()); assertFalse(b.of("inner").isFrozen()); b.freeze(); assertTrue(b.isFrozen()); assertTrue(b.of("inner").isFrozen()); assertEquals("bar", b.of("inner").get("foo")); Binder e = Binder.EMPTY; assertTrue(e.isFrozen()); x = new Binder(); assertEquals(e, x); assertFalse(b.equals(e)); }
/** * Saves client's javascript in contract's state. It can be executed with {@link Contract#execJS(JSApiExecOptions, byte[], String...)} */ public void setJS(byte[] jsFileContent, String jsFileName, JSApiScriptParameters scriptParameters, boolean putContentIntoContract) { String fileNameKey = JSApiHelpers.fileName2fileKey(jsFileName); Binder scriptBinder = JSApiHelpers.createScriptBinder(jsFileContent, jsFileName, scriptParameters, putContentIntoContract); Binder scripts = getData().getBinder(JSAPI_SCRIPT_FIELD, new Binder()); scripts.set(fileNameKey, scriptBinder); getData().put(JSAPI_SCRIPT_FIELD, scripts); }
public void deserializeWith(Binder data, BiDeserializer d) { registerRole(d.deserialize(data.getBinderOrThrow("issuer"))); createdAt = data.getZonedDateTimeOrThrow("created_at"); expiresAt = data.getZonedDateTime("expires_at", null); extendedType = data.getString("extended_type", null); this.data = d.deserialize(data.getBinder("data", Binder.EMPTY)); references = d.deserialize(data.getList("references", null)); Map<String, Permission> perms = d.deserialize(data.getOrThrow("permissions")); perms.forEach((id, perm) -> { perm.setId(id); addPermission(perm); }); }
@Test public void cachingNestedMaps() throws Exception { Binder a = Binder.fromKeysValues("foo", "bar"); Binder b = Binder.fromKeysValues("foo", "bar"); b.put("bar", "buzz"); Binder root = Binder.fromKeysValues("a", b, "b", b, "c", b); Binder res = Boss.unpack(Boss.pack(root)); assertEquals("buzz", res.getBinderOrThrow("c").getStringOrThrow("bar")); }
@NonNull private Binder unpackPayload(byte[] packedCoffer, boolean allowPartiallySigned) throws EncryptionError { signed = false; Binder outer = Boss.unpack(packedCoffer); Collection<Binder> signatures = outer.getBinders("signatures"); final byte[] source = outer.getBinary("content"); Binder payload = Boss.unpack(source); if (!payload.get("type").equals("capsule")) throw new FormatException("not capsule/unknown type"); checkSignatures(source, signatures, payload, allowPartiallySigned); publicData = payload.getBinder("public"); return payload; }
public Binder toBinder() { Binder binder = new Binder(); binder.set("compression", (isCompressed ? JSApiCompressionEnum.ZIP : JSApiCompressionEnum.RAW).toString()); binder.put("time_limit", timeLimitMillis); binder.put("permissions", permissions); binder.put("domainMasks", domainMasks); binder.put("ipMasks", ipMasks); return binder; }
@Override public void deserialize(Binder data, BiDeserializer deserializer) { publicKey = deserializer.deserialize(data.getBinderOrThrow("publicKey")); publicHost = data.getStringOrThrow("publicHost"); String host = data.getStringOrThrow("host"); nodeAddress = new InetSocketAddress(host, data.getIntOrThrow("udpPort")); clientAddress = new InetSocketAddress(host, data.getIntOrThrow("clientPort")); serverAddress = new InetSocketAddress(host, data.getIntOrThrow("serverPort")); nodeName = data.getStringOrThrow("name"); number = data.getIntOrThrow("number"); }
@Override public Binder serialize(Object object, BiSerializer serializer) { Object s = serializer.serialize(((KeyAddress) object).getPacked());; Binder serialized = (Binder) s; if(serialized.containsKey("base64")) { serialized.put("base58", Safe58.encode(Base64.decodeLines((String) serialized.remove("base64")))); } return Binder.of("uaddress", serialized); }
/** * Get the boolean value, return false if not found, same as if found 'false'. We recommend to use either {@link * #getBoolean(String, Boolean)} } or {@link #getBinaryOrThrow(String)} to resolve this ambigity. * * @param key * @return */ @Deprecated public boolean getBoolean(String key) { Boolean res = (Boolean) get(key); return res == null ? false : (boolean) res; }
@Test public void getInt() throws Exception { Binder b = Binder.fromKeysValues( "i1", 100, "i2", "101", "l1", "1505774997427", "l2", 1505774997427L ); assertEquals(100, (int) b.getInt("i1",222)); assertEquals(101, (int) b.getInt("i2",222)); assertEquals(100, b.getIntOrThrow("i1")); assertEquals(101, b.getIntOrThrow("i2")); assertEquals(1505774997427L, b.getLongOrThrow("l1")); assertEquals(1505774997427L, b.getLongOrThrow("l2")); }
protected void initFromParams() { fieldName = params.getStringOrThrow("field_name"); minValue = new Decimal(params.getString("min_value", "0")); minUnit = new Decimal(params.getString("min_unit", "1e-9")); mergeFields = params.getList("join_match_fields", asList("state.origin")); }
@Test public void toBinder() throws Exception { ItemResult r1 = new ItemResult(REVOKED, false, ZonedDateTime.now(), inFuture(300)); r1.extraDataBinder.set("someString", "90314tughiouqehmtcy34ntcn8341tgfesgfwerh"); r1.extraDataBinder.set("someInteger", 42); ItemResult r2 = new ItemResult(r1.toBinder()); assertEquals(r1.state, r2.state); assertEquals(r1.haveCopy, r2.haveCopy); assertEquals(r1.createdAt, r2.createdAt); assertEquals(r1.expiresAt, r2.expiresAt); assertEquals(r1.extraDataBinder.getStringOrThrow("someString"), r2.extraDataBinder.getStringOrThrow("someString")); assertEquals(r1.extraDataBinder.getIntOrThrow("someInteger"), r2.extraDataBinder.getIntOrThrow("someInteger")); assertEquals(r1, r2); }