@Test public void testRegionEncode() { NodeId nodeId1 = NodeId.nodeId("1"); NodeId nodeId2 = NodeId.nodeId("2"); NodeId nodeId3 = NodeId.nodeId("3"); NodeId nodeId4 = NodeId.nodeId("4"); Set<NodeId> set1 = ImmutableSet.of(nodeId1); Set<NodeId> set2 = ImmutableSet.of(nodeId1, nodeId2); Set<NodeId> set3 = ImmutableSet.of(nodeId1, nodeId2, nodeId3); Set<NodeId> set4 = ImmutableSet.of(nodeId1, nodeId2, nodeId3, nodeId4); List<Set<NodeId>> masters = ImmutableList.of(set1, set2, set3, set4); RegionId regionId = RegionId.regionId("1"); String name = "foo"; Region.Type type = Region.Type.ROOM; Region region = new DefaultRegion(regionId, name, type, masters); ObjectNode regionJson = regionCodec.encode(region, context); assertThat(regionJson, matchesRegion(region)); }
@Override public ObjectNode encode(Region region, CodecContext context) { checkNotNull(region, NULL_REGION_MSG); ObjectNode result = context.mapper().createObjectNode() .put(REGION_ID, region.id().toString()) .put(NAME, region.name()) .put(TYPE, region.type().toString()); ArrayNode masters = context.mapper().createArrayNode(); region.masters().forEach(sets -> { ArrayNode setsJson = context.mapper().createArrayNode(); sets.forEach(nodeId -> setsJson.add(nodeId.toString())); masters.add(setsJson); }); result.set(MASTERS, masters); return result; }
RegionCodec extends JsonCodec<Region> { @Override public ObjectNode encode(Region region, CodecContext context) { checkNotNull(region, NULL_REGION_MSG); ObjectNode result = context.mapper().createObjectNode() .put(REGION_ID, region.id().toString()) .put(NAME, region.name()) .put(TYPE, region.type().toString()); ArrayNode masters = context.mapper().createArrayNode(); region.masters().forEach(sets -> { ArrayNode setsJson = context.mapper().createArrayNode(); sets.forEach(nodeId -> setsJson.add(nodeId.toString())); masters.add(setsJson); }); result.set(MASTERS, masters); return result; } }
RegionCodec extends JsonCodec<Region> { @Override public ObjectNode encode(Region region, CodecContext context) { checkNotNull(region, NULL_REGION_MSG); ObjectNode result = context.mapper().createObjectNode() .put(REGION_ID, region.id().toString()) .put(NAME, region.name()) .put(TYPE, region.type().toString()); ArrayNode masters = context.mapper().createArrayNode(); region.masters().forEach(sets -> { ArrayNode setsJson = context.mapper().createArrayNode(); sets.forEach(nodeId -> setsJson.add(nodeId.toString())); masters.add(setsJson); }); result.set(MASTERS, masters); return result; }  }
RegionCodec extends JsonCodec<Region> { @Override public ObjectNode encode(Region region, CodecContext context) { checkNotNull(region, NULL_REGION_MSG); ObjectNode result = context.mapper().createObjectNode() .put(REGION_ID, region.id().toString()) .put(NAME, region.name()) .put(TYPE, region.type().toString()); ArrayNode masters = context.mapper().createArrayNode(); region.masters().forEach(sets -> { ArrayNode setsJson = context.mapper().createArrayNode(); sets.forEach(nodeId -> setsJson.add(nodeId.toString())); masters.add(setsJson); }); result.set(MASTERS, masters); return result; }  @Override ObjectNode encode(Region region, CodecContext context); @Override Region decode(ObjectNode json, CodecContext context); }
RegionCodec extends JsonCodec<Region> { @Override public ObjectNode encode(Region region, CodecContext context) { checkNotNull(region, NULL_REGION_MSG); ObjectNode result = context.mapper().createObjectNode() .put(REGION_ID, region.id().toString()) .put(NAME, region.name()) .put(TYPE, region.type().toString()); ArrayNode masters = context.mapper().createArrayNode(); region.masters().forEach(sets -> { ArrayNode setsJson = context.mapper().createArrayNode(); sets.forEach(nodeId -> setsJson.add(nodeId.toString())); masters.add(setsJson); }); result.set(MASTERS, masters); return result; }  @Override ObjectNode encode(Region region, CodecContext context); @Override Region decode(ObjectNode json, CodecContext context);  }