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,7 +91,6 @@ public class RegionCodec extends JsonCodec<Region> { ...@@ -97,7 +91,6 @@ 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) {
101 IntStream.range(0, mastersJson.size()).forEach(i -> { 94 IntStream.range(0, mastersJson.size()).forEach(i -> {
102 JsonNode setsJson = mastersJson.get(i); 95 JsonNode setsJson = mastersJson.get(i);
103 final Set<NodeId> nodeIds = Sets.newHashSet(); 96 final Set<NodeId> nodeIds = Sets.newHashSet();
...@@ -111,25 +104,18 @@ public class RegionCodec extends JsonCodec<Region> { ...@@ -111,25 +104,18 @@ public class RegionCodec extends JsonCodec<Region> {
111 } 104 }
112 masters.add(nodeIds); 105 masters.add(nodeIds);
113 }); 106 });
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 107
124 - // parse region type 108 + RegionId regionId = RegionId.regionId(extractMember(REGION_ID, json));
125 - String typeText = nullIsIllegal(json.get(TYPE), TYPE + 109 + String name = extractMember(NAME, json);
126 - MISSING_MEMBER_MESSAGE).asText(); 110 + Region.Type type = REGION_TYPE_MAP.get(extractMember(TYPE, json));
127 -
128 - Region.Type type = REGION_TYPE_MAP.get(typeText);
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 }
......