Simon Hunt
Committed by Gerrit Code Review

Simplified RegionCodec in places.

Change-Id: I309e9da9cfd714f1b19ee73806c3a8c8233730c7
...@@ -47,28 +47,22 @@ public class RegionCodec extends JsonCodec<Region> { ...@@ -47,28 +47,22 @@ public class RegionCodec extends JsonCodec<Region> {
47 private static final String TYPE = "type"; 47 private static final String TYPE = "type";
48 private static final String MASTERS = "masters"; 48 private static final String MASTERS = "masters";
49 private static final String NODE_ID = "nodeId"; 49 private static final String NODE_ID = "nodeId";
50 - private static final String REGION_NOT_NULL_MSG = "Region cannot be null"; 50 +
51 - private static final String MISSING_MEMBER_MESSAGE = " member is required in Region"; 51 + private static final String NULL_REGION_MSG = "Region cannot be null";
52 + private static final String MISSING_MEMBER_MSG = " member is required in Region";
52 53
53 private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create(); 54 private static final BiMap<String, Region.Type> REGION_TYPE_MAP = HashBiMap.create();
54 55
55 static { 56 static {
56 - // key is String representation of Region.Type 57 + // key is String representation of Region.Type; value is Region.Type
57 - // value is Region.Type 58 + for (Region.Type t : Region.Type.values()) {
58 - REGION_TYPE_MAP.put("CONTINENT", Region.Type.CONTINENT); 59 + REGION_TYPE_MAP.put(t.name(), t);
59 - REGION_TYPE_MAP.put("COUNTRY", Region.Type.COUNTRY); 60 + }
60 - REGION_TYPE_MAP.put("METRO", Region.Type.METRO);
61 - REGION_TYPE_MAP.put("CAMPUS", Region.Type.CAMPUS);
62 - REGION_TYPE_MAP.put("BUILDING", Region.Type.BUILDING);
63 - REGION_TYPE_MAP.put("FLOOR", Region.Type.FLOOR);
64 - REGION_TYPE_MAP.put("ROOM", Region.Type.ROOM);
65 - REGION_TYPE_MAP.put("RACK", Region.Type.RACK);
66 - REGION_TYPE_MAP.put("LOGICAL_GROUP", Region.Type.LOGICAL_GROUP);
67 } 61 }
68 62
69 @Override 63 @Override
70 public ObjectNode encode(Region region, CodecContext context) { 64 public ObjectNode encode(Region region, CodecContext context) {
71 - checkNotNull(region, REGION_NOT_NULL_MSG); 65 + checkNotNull(region, NULL_REGION_MSG);
72 66
73 ObjectNode result = context.mapper().createObjectNode() 67 ObjectNode result = context.mapper().createObjectNode()
74 .put(REGION_ID, region.id().toString()) 68 .put(REGION_ID, region.id().toString())
...@@ -97,39 +91,31 @@ public class RegionCodec extends JsonCodec<Region> { ...@@ -97,39 +91,31 @@ public class RegionCodec extends JsonCodec<Region> {
97 JsonNode mastersJson = json.get(MASTERS); 91 JsonNode mastersJson = json.get(MASTERS);
98 checkNotNull(mastersJson); 92 checkNotNull(mastersJson);
99 93
100 - if (mastersJson != null) { 94 + IntStream.range(0, mastersJson.size()).forEach(i -> {
101 - IntStream.range(0, mastersJson.size()).forEach(i -> { 95 + JsonNode setsJson = mastersJson.get(i);
102 - JsonNode setsJson = mastersJson.get(i); 96 + final Set<NodeId> nodeIds = Sets.newHashSet();
103 - final Set<NodeId> nodeIds = Sets.newHashSet(); 97 + if (setsJson != null && setsJson.isArray()) {
104 - if (setsJson != null && setsJson.isArray()) { 98 + Set<NodeId> localNodeIds = Sets.newHashSet();
105 - Set<NodeId> localNodeIds = Sets.newHashSet(); 99 + IntStream.range(0, setsJson.size()).forEach(j -> {
106 - IntStream.range(0, setsJson.size()).forEach(j -> { 100 + JsonNode nodeIdJson = setsJson.get(j);
107 - JsonNode nodeIdJson = setsJson.get(j); 101 + localNodeIds.add(decodeNodeId(nodeIdJson));
108 - localNodeIds.add(decodeNodeId(nodeIdJson)); 102 + });
109 - }); 103 + nodeIds.addAll(localNodeIds);
110 - nodeIds.addAll(localNodeIds); 104 + }
111 - } 105 + masters.add(nodeIds);
112 - masters.add(nodeIds); 106 + });
113 - });
114 - }
115 -
116 - // parse region id
117 - RegionId regionId = RegionId.regionId(nullIsIllegal(json.get(REGION_ID),
118 - REGION_ID + MISSING_MEMBER_MESSAGE).asText());
119 -
120 - // parse region name
121 - String name = nullIsIllegal(json.get(NAME), NAME +
122 - MISSING_MEMBER_MESSAGE).asText();
123 -
124 - // parse region type
125 - String typeText = nullIsIllegal(json.get(TYPE), TYPE +
126 - MISSING_MEMBER_MESSAGE).asText();
127 107
128 - Region.Type type = REGION_TYPE_MAP.get(typeText); 108 + RegionId regionId = RegionId.regionId(extractMember(REGION_ID, json));
109 + String name = extractMember(NAME, json);
110 + Region.Type type = REGION_TYPE_MAP.get(extractMember(TYPE, json));
129 111
130 return new DefaultRegion(regionId, name, type, masters); 112 return new DefaultRegion(regionId, name, type, masters);
131 } 113 }
132 114
115 + private String extractMember(String key, ObjectNode json) {
116 + return nullIsIllegal(json.get(key), key + MISSING_MEMBER_MSG).asText();
117 + }
118 +
133 /** 119 /**
134 * Decodes node id json to node id object. 120 * Decodes node id json to node id object.
135 * 121 *
...@@ -137,9 +123,7 @@ public class RegionCodec extends JsonCodec<Region> { ...@@ -137,9 +123,7 @@ public class RegionCodec extends JsonCodec<Region> {
137 * @return decoded node id object 123 * @return decoded node id object
138 */ 124 */
139 private NodeId decodeNodeId(JsonNode json) { 125 private NodeId decodeNodeId(JsonNode json) {
140 - NodeId nodeId = NodeId.nodeId(nullIsIllegal(json, NODE_ID + 126 + return NodeId.nodeId(nullIsIllegal(json, NODE_ID +
141 - MISSING_MEMBER_MESSAGE).asText()); 127 + MISSING_MEMBER_MSG).asText());
142 -
143 - return nodeId;
144 } 128 }
145 } 129 }
......