11package org .maproulette .client .model ;
22
3+ import java .io .IOException ;
4+ import java .io .Serializable ;
5+ import java .util .ArrayList ;
6+ import java .util .List ;
7+
8+ import org .maproulette .client .exception .MapRouletteRuntimeException ;
9+ import org .maproulette .client .utilities .ObjectMapperSingleton ;
10+
311import com .fasterxml .jackson .annotation .JsonValue ;
412import com .fasterxml .jackson .core .JsonGenerator ;
513import com .fasterxml .jackson .core .JsonParser ;
1119import com .fasterxml .jackson .databind .annotation .JsonSerialize ;
1220import com .fasterxml .jackson .databind .deser .std .StdDeserializer ;
1321import com .fasterxml .jackson .databind .ser .std .StdSerializer ;
22+
1423import lombok .AllArgsConstructor ;
1524import lombok .Builder ;
1625import lombok .Data ;
1726import lombok .NoArgsConstructor ;
18- import org .maproulette .client .exception .MapRouletteRuntimeException ;
19- import org .maproulette .client .utilities .ObjectMapperSingleton ;
20-
21- import java .io .IOException ;
22- import java .io .Serializable ;
23- import java .util .ArrayList ;
24- import java .util .List ;
2527
2628/**
2729 * @author mcuthbert
@@ -53,44 +55,48 @@ public String toJson()
5355 try
5456 {
5557 return ObjectMapperSingleton .getMapper ().writeValueAsString (this );
56- } catch (final JsonProcessingException e )
58+ }
59+ catch (final JsonProcessingException e )
5760 {
5861 throw new MapRouletteRuntimeException (e );
5962 }
6063 }
6164
65+ /**
66+ * Serialize a {@code RuleList}
67+ */
6268 public static class RuleListSerializer extends StdSerializer <RuleList >
6369 {
64-
6570 public RuleListSerializer ()
6671 {
6772 this (null );
6873 }
6974
70- public RuleListSerializer (Class <RuleList > t )
75+ public RuleListSerializer (final Class <RuleList > clazzRuleList )
7176 {
72- super (t );
77+ super (clazzRuleList );
7378 }
7479
75- private static void serializeRuleListHelper (List <RuleList > ruleList , JsonGenerator gen ) throws IOException
80+ private static void serializeRuleListHelper (final List <RuleList > ruleListList ,
81+ final JsonGenerator gen ) throws IOException
7682 {
77- for (RuleList r : ruleList )
83+ for (final RuleList ruleList : ruleListList )
7884 {
7985 gen .writeStartObject ();
80- gen .writeStringField ("condition" , r .getCondition ());
86+ gen .writeStringField ("condition" , ruleList .getCondition ());
8187 gen .writeArrayFieldStart ("rules" );
82- if (r .getRuleList () != null )
88+ if (ruleList .getRuleList () != null )
8389 {
84- for (RuleList it : r .getRuleList ())
90+ for (final RuleList nestedRuleList : ruleList .getRuleList ())
8591 {
86- serializeRuleListHelper (it .getRuleList (), gen );
92+ serializeRuleListHelper (nestedRuleList .getRuleList (), gen );
8793 }
8894 }
89- if (r .getRules () != null )
95+ if (ruleList .getRules () != null )
9096 {
91- for (PriorityRule p : r .getRules ())
97+ for (final PriorityRule priorityRule : ruleList .getRules ())
9298 {
93- gen .writeObject (p );
99+ gen .writeObject (priorityRule );
94100 }
95101 }
96102 gen .writeEndArray ();
@@ -99,7 +105,8 @@ private static void serializeRuleListHelper(List<RuleList> ruleList, JsonGenerat
99105 }
100106
101107 @ Override
102- public void serialize (RuleList value , JsonGenerator gen , SerializerProvider serializers ) throws IOException
108+ public void serialize (final RuleList value , final JsonGenerator gen ,
109+ final SerializerProvider serializers ) throws IOException
103110 {
104111 gen .writeStartObject ();
105112 gen .writeStringField ("condition" , value .getCondition ());
@@ -111,55 +118,56 @@ public void serialize(RuleList value, JsonGenerator gen, SerializerProvider seri
111118
112119 if (value .getRules () != null )
113120 {
114- for (PriorityRule p : value .getRules ())
121+ for (final PriorityRule priorityRule : value .getRules ())
115122 {
116- gen .writeObject (p );
123+ gen .writeObject (priorityRule );
117124 }
118125 }
119126 gen .writeEndArray ();
120127 gen .writeEndObject ();
121128 }
122129 }
123130
131+ /**
132+ * Deserialize a {@code RuleList}
133+ */
124134 public static class RuleListDeserializer extends StdDeserializer <RuleList >
125135 {
126-
127136 public RuleListDeserializer ()
128137 {
129138 this (null );
130139 }
131140
132- public RuleListDeserializer (final Class <?> vc )
141+ public RuleListDeserializer (final Class <?> valueClass )
133142 {
134- super (vc );
143+ super (valueClass );
135144 }
136145
137- private static RuleList buildRuleListHelper (JsonNode node , DeserializationContext ctxt )
146+ private static RuleList buildRuleListHelper (final JsonNode node ,
147+ final DeserializationContext ctxt )
138148 {
139149 if (node .get ("condition" ) == null )
140150 {
141151 return null ;
142152 }
143- final RuleList ret = RuleList .builder ()
144- .condition (node .get ("condition" ).asText ())
145- .ruleList (new ArrayList <>())
146- .rules (new ArrayList <>())
147- .build ();
153+ final RuleList ret = RuleList .builder ().condition (node .get ("condition" ).asText ())
154+ .ruleList (new ArrayList <>()).rules (new ArrayList <>()).build ();
148155
149- for (JsonNode it : node .withArray ("rules" ))
156+ for (final JsonNode jsonNode : node .withArray ("rules" ))
150157 {
151- if (it .get ("condition" ) != null )
158+ if (jsonNode .get ("condition" ) != null )
152159 {
153- // If the child is a PriorityRule, do a recursive call to build the rule and add it to the list.
154- RuleList child = buildRuleListHelper (it , ctxt );
160+ // If the child is a PriorityRule, do a recursive call to build the rule and add
161+ // it to the list.
162+ final RuleList child = buildRuleListHelper (jsonNode , ctxt );
155163 ret .getRuleList ().add (child );
156- } else
164+ }
165+ else
157166 {
158- PriorityRule priorityRule = PriorityRule .builder ()
159- .type (it .get ("type" ).asText ())
160- .operator (it .get ("operator" ).asText ())
161- .value (it .get ("value" ).asText ())
162- .build ();
167+ final PriorityRule priorityRule = PriorityRule .builder ()
168+ .type (jsonNode .get ("type" ).asText ())
169+ .operator (jsonNode .get ("operator" ).asText ())
170+ .value (jsonNode .get ("value" ).asText ()).build ();
163171 ret .getRules ().add (priorityRule );
164172 }
165173 }
@@ -168,9 +176,10 @@ private static RuleList buildRuleListHelper(JsonNode node, DeserializationContex
168176 }
169177
170178 @ Override
171- public RuleList deserialize (JsonParser p , DeserializationContext ctxt ) throws IOException
179+ public RuleList deserialize (final JsonParser jsonParser , final DeserializationContext ctxt )
180+ throws IOException
172181 {
173- JsonNode node = p .getCodec ().readTree (p );
182+ final JsonNode node = jsonParser .getCodec ().readTree (jsonParser );
174183 return buildRuleListHelper (node , ctxt );
175184 }
176185 }
0 commit comments