@@ -47,59 +47,64 @@ public void setFieldValue(int sectionId, String fieldName, Object value) {
4747 setFieldValue (Sections .SECTION_ID_NAME_MAP .get (sectionId ), fieldName , value );
4848 }
4949
50+ private EncodableSection getOrCreateSection (String sectionName ) {
51+ EncodableSection section = this .sections .get (sectionName );
52+ if (section == null ) {
53+ switch (sectionName ) {
54+ case TcfEuV2 .NAME :
55+ section = new TcfEuV2 ();
56+ break ;
57+ case TcfCaV1 .NAME :
58+ section = new TcfCaV1 ();
59+ break ;
60+ case UspV1 .NAME :
61+ section = new UspV1 ();
62+ break ;
63+ case UsNat .NAME :
64+ section = new UsNat ();
65+ break ;
66+ case UsCa .NAME :
67+ section = new UsCa ();
68+ break ;
69+ case UsVa .NAME :
70+ section = new UsVa ();
71+ break ;
72+ case UsCo .NAME :
73+ section = new UsCo ();
74+ break ;
75+ case UsUt .NAME :
76+ section = new UsUt ();
77+ break ;
78+ case UsCt .NAME :
79+ section = new UsCt ();
80+ break ;
81+ case UsFl .NAME :
82+ section = new UsFl ();
83+ break ;
84+ case UsMt .NAME :
85+ section = new UsMt ();
86+ break ;
87+ case UsOr .NAME :
88+ section = new UsOr ();
89+ break ;
90+ case UsTx .NAME :
91+ section = new UsTx ();
92+ break ;
93+ }
94+ if (section != null ) {
95+ this .sections .put (sectionName , section );
96+ }
97+ }
98+ return section ;
99+ }
100+
50101 public void setFieldValue (String sectionName , String fieldName , Object value ) {
51102 if (!this .decoded ) {
52103 this .sections = this .decodeModel (this .encodedString );
53104 this .dirty = false ;
54105 this .decoded = true ;
55106 }
56-
57- EncodableSection section = null ;
58- if (!this .sections .containsKey (sectionName )) {
59- if (sectionName .equals (TcfCaV1 .NAME )) {
60- section = new TcfCaV1 ();
61- this .sections .put (TcfCaV1 .NAME , section );
62- } else if (sectionName .equals (TcfEuV2 .NAME )) {
63- section = new TcfEuV2 ();
64- this .sections .put (TcfEuV2 .NAME , section );
65- } else if (sectionName .equals (UspV1 .NAME )) {
66- section = new UspV1 ();
67- this .sections .put (UspV1 .NAME , section );
68- } else if (sectionName .equals (UsNat .NAME )) {
69- section = new UsNat ();
70- this .sections .put (UsNat .NAME , section );
71- } else if (sectionName .equals (UsCa .NAME )) {
72- section = new UsCa ();
73- this .sections .put (UsCa .NAME , section );
74- } else if (sectionName .equals (UsVa .NAME )) {
75- section = new UsVa ();
76- this .sections .put (UsVa .NAME , section );
77- } else if (sectionName .equals (UsCo .NAME )) {
78- section = new UsCo ();
79- this .sections .put (UsCo .NAME , section );
80- } else if (sectionName .equals (UsUt .NAME )) {
81- section = new UsUt ();
82- this .sections .put (UsUt .NAME , section );
83- } else if (sectionName .equals (UsCt .NAME )) {
84- section = new UsCt ();
85- this .sections .put (UsCt .NAME , section );
86- } else if (sectionName .equals (UsFl .NAME )) {
87- section = new UsFl ();
88- this .sections .put (UsFl .NAME , section );
89- } else if (sectionName .equals (UsMt .NAME )) {
90- section = new UsMt ();
91- this .sections .put (UsMt .NAME , section );
92- } else if (sectionName .equals (UsOr .NAME )) {
93- section = new UsOr ();
94- this .sections .put (UsOr .NAME , section );
95- } else if (sectionName .equals (UsTx .NAME )) {
96- section = new UsTx ();
97- this .sections .put (UsTx .NAME , section );
98- }
99- } else {
100- section = this .sections .get (sectionName );
101- }
102-
107+ EncodableSection section = getOrCreateSection (sectionName );
103108 if (section != null ) {
104109 section .setFieldValue (fieldName , value );
105110 this .dirty = true ;
@@ -118,9 +123,9 @@ public Object getFieldValue(String sectionName, String fieldName) {
118123 this .dirty = false ;
119124 this .decoded = true ;
120125 }
121-
122- if (this . sections . containsKey ( sectionName ) ) {
123- return this . sections . get ( sectionName ) .getFieldValue (fieldName );
126+ EncodableSection field = this . sections . get ( sectionName );
127+ if (field != null ) {
128+ return field .getFieldValue (fieldName );
124129 } else {
125130 return null ;
126131 }
@@ -136,9 +141,9 @@ public boolean hasField(String sectionName, String fieldName) {
136141 this .dirty = false ;
137142 this .decoded = true ;
138143 }
139-
140- if (this . sections . containsKey ( sectionName ) ) {
141- return this . sections . get ( sectionName ) .hasField (fieldName );
144+ EncodableSection field = this . sections . get ( sectionName );
145+ if (field != null ) {
146+ return field .hasField (fieldName );
142147 } else {
143148 return false ;
144149 }
@@ -185,11 +190,7 @@ public EncodableSection getSection(String sectionName) {
185190 this .decoded = true ;
186191 }
187192
188- if (this .sections .containsKey (sectionName )) {
189- return this .sections .get (sectionName );
190- } else {
191- return null ;
192- }
193+ return this .sections .get (sectionName );
193194 }
194195
195196 public void deleteSection (int sectionId ) {
@@ -203,8 +204,7 @@ public void deleteSection(String sectionName) {
203204 this .decoded = true ;
204205 }
205206
206- if (this .sections .containsKey (sectionName )) {
207- this .sections .remove (sectionName );
207+ if (this .sections .remove (sectionName ) != null ) {
208208 this .dirty = true ;
209209 }
210210 }
@@ -274,25 +274,26 @@ public List<Integer> getSectionIds() {
274274 this .dirty = false ;
275275 this .decoded = true ;
276276 }
277-
278- List <Integer > sectionIds = new ArrayList <>();
279- for (int i = 0 ; i < Sections . SECTION_ORDER . size () ; i ++) {
277+ int length = Sections . SECTION_ORDER . size ();
278+ List <Integer > sectionIds = new ArrayList <>(length );
279+ for (int i = 0 ; i < length ; i ++) {
280280 String sectionName = Sections .SECTION_ORDER .get (i );
281- if ( this .sections .containsKey (sectionName )) {
282- EncodableSection section = this . sections . get ( sectionName );
281+ EncodableSection section = this .sections .get (sectionName );
282+ if ( section != null ) {
283283 sectionIds .add (section .getId ());
284284 }
285285 }
286286 return sectionIds ;
287287 }
288288
289289 protected String encodeModel (Map <String , EncodableSection > sections ) {
290- List <String > encodedSections = new ArrayList <>();
291- List <Integer > sectionIds = new ArrayList <>();
292- for (int i = 0 ; i < Sections .SECTION_ORDER .size (); i ++) {
290+ int length = Sections .SECTION_ORDER .size ();
291+ List <String > encodedSections = new ArrayList <>(length );
292+ List <Integer > sectionIds = new ArrayList <>(length );
293+ for (int i = 0 ; i < length ; i ++) {
293294 String sectionName = Sections .SECTION_ORDER .get (i );
294- if ( sections .containsKey (sectionName )) {
295- EncodableSection section = sections . get ( sectionName );
295+ EncodableSection section = sections .get (sectionName );
296+ if ( section != null ) {
296297 encodedSections .add (section .encode ());
297298 sectionIds .add (section .getId ());
298299 }
@@ -322,45 +323,47 @@ protected Map<String, EncodableSection> decodeModel(String str) {
322323 @ SuppressWarnings ("unchecked" )
323324 List <Integer > sectionIds = (List <Integer >) header .getFieldValue ("SectionIds" );
324325 for (int i = 0 ; i < sectionIds .size (); i ++) {
325- if (sectionIds .get (i ).equals (TcfEuV2 .ID )) {
326- TcfEuV2 section = new TcfEuV2 (encodedSections [i + 1 ]);
327- sections .put (TcfEuV2 .NAME , section );
328- } else if (sectionIds .get (i ).equals (TcfCaV1 .ID )) {
329- TcfCaV1 section = new TcfCaV1 (encodedSections [i + 1 ]);
330- sections .put (TcfCaV1 .NAME , section );
331- } else if (sectionIds .get (i ).equals (UspV1 .ID )) {
332- UspV1 section = new UspV1 (encodedSections [i + 1 ]);
333- sections .put (UspV1 .NAME , section );
334- } else if (sectionIds .get (i ).equals (UsCa .ID )) {
335- UsCa section = new UsCa (encodedSections [i + 1 ]);
336- sections .put (UsCa .NAME , section );
337- } else if (sectionIds .get (i ).equals (UsNat .ID )) {
338- UsNat section = new UsNat (encodedSections [i + 1 ]);
339- sections .put (UsNat .NAME , section );
340- } else if (sectionIds .get (i ).equals (UsVa .ID )) {
341- UsVa section = new UsVa (encodedSections [i + 1 ]);
342- sections .put (UsVa .NAME , section );
343- } else if (sectionIds .get (i ).equals (UsCo .ID )) {
344- UsCo section = new UsCo (encodedSections [i + 1 ]);
345- sections .put (UsCo .NAME , section );
346- } else if (sectionIds .get (i ).equals (UsUt .ID )) {
347- UsUt section = new UsUt (encodedSections [i + 1 ]);
348- sections .put (UsUt .NAME , section );
349- } else if (sectionIds .get (i ).equals (UsCt .ID )) {
350- UsCt section = new UsCt (encodedSections [i + 1 ]);
351- sections .put (UsCt .NAME , section );
352- } else if (sectionIds .get (i ).equals (UsFl .ID )) {
353- UsFl section = new UsFl (encodedSections [i + 1 ]);
354- sections .put (UsFl .NAME , section );
355- } else if (sectionIds .get (i ).equals (UsMt .ID )) {
356- UsMt section = new UsMt (encodedSections [i + 1 ]);
357- sections .put (UsMt .NAME , section );
358- } else if (sectionIds .get (i ).equals (UsOr .ID )) {
359- UsOr section = new UsOr (encodedSections [i + 1 ]);
360- sections .put (UsOr .NAME , section );
361- } else if (sectionIds .get (i ).equals (UsTx .ID )) {
362- UsTx section = new UsTx (encodedSections [i + 1 ]);
363- sections .put (UsTx .NAME , section );
326+ String section = encodedSections [i + 1 ];
327+ switch (sectionIds .get (i )) {
328+ case TcfEuV2 .ID :
329+ sections .put (TcfEuV2 .NAME , new TcfEuV2 (section ));
330+ break ;
331+ case TcfCaV1 .ID :
332+ sections .put (TcfCaV1 .NAME , new TcfCaV1 (section ));
333+ break ;
334+ case UspV1 .ID :
335+ sections .put (UspV1 .NAME , new UspV1 (section ));
336+ break ;
337+ case UsCa .ID :
338+ sections .put (UsCa .NAME , new UsCa (section ));
339+ break ;
340+ case UsNat .ID :
341+ sections .put (UsNat .NAME , new UsNat (section ));
342+ break ;
343+ case UsVa .ID :
344+ sections .put (UsVa .NAME , new UsVa (section ));
345+ break ;
346+ case UsCo .ID :
347+ sections .put (UsCo .NAME , new UsCo (section ));
348+ break ;
349+ case UsUt .ID :
350+ sections .put (UsUt .NAME , new UsUt (section ));
351+ break ;
352+ case UsCt .ID :
353+ sections .put (UsCt .NAME , new UsCt (section ));
354+ break ;
355+ case UsFl .ID :
356+ sections .put (UsFl .NAME , new UsFl (section ));
357+ break ;
358+ case UsMt .ID :
359+ sections .put (UsMt .NAME , new UsMt (section ));
360+ break ;
361+ case UsOr .ID :
362+ sections .put (UsOr .NAME , new UsOr (section ));
363+ break ;
364+ case UsTx .ID :
365+ sections .put (UsTx .NAME , new UsTx (section ));
366+ break ;
364367 }
365368 }
366369 }
@@ -393,9 +396,9 @@ public String encodeSection(String sectionName) {
393396 this .dirty = false ;
394397 this .decoded = true ;
395398 }
396-
397- if (this . sections . containsKey ( sectionName ) ) {
398- return this . sections . get ( sectionName ) .encode ();
399+ EncodableSection section = this . sections . get ( sectionName );
400+ if (section != null ) {
401+ return section .encode ();
399402 } else {
400403 return null ;
401404 }
@@ -406,52 +409,7 @@ public void decodeSection(int sectionId, String encodedString) {
406409 }
407410
408411 public void decodeSection (String sectionName , String encodedString ) {
409- EncodableSection section = null ;
410- if (!this .sections .containsKey (sectionName )) {
411- if (sectionName .equals (TcfEuV2 .NAME )) {
412- section = new TcfEuV2 ();
413- this .sections .put (TcfEuV2 .NAME , section );
414- } else if (sectionName .equals (TcfCaV1 .NAME )) {
415- section = new TcfCaV1 ();
416- this .sections .put (TcfCaV1 .NAME , section );
417- } else if (sectionName .equals (UspV1 .NAME )) {
418- section = new UspV1 ();
419- this .sections .put (UspV1 .NAME , section );
420- } else if (sectionName .equals (UsNat .NAME )) {
421- section = new UsNat ();
422- this .sections .put (UsNat .NAME , section );
423- } else if (sectionName .equals (UsCa .NAME )) {
424- section = new UsCa ();
425- this .sections .put (UsCa .NAME , section );
426- } else if (sectionName .equals (UsVa .NAME )) {
427- section = new UsVa ();
428- this .sections .put (UsVa .NAME , section );
429- } else if (sectionName .equals (UsCo .NAME )) {
430- section = new UsCo ();
431- this .sections .put (UsCo .NAME , section );
432- } else if (sectionName .equals (UsUt .NAME )) {
433- section = new UsUt ();
434- this .sections .put (UsUt .NAME , section );
435- } else if (sectionName .equals (UsCt .NAME )) {
436- section = new UsCt ();
437- this .sections .put (UsCt .NAME , section );
438- } else if (sectionName .equals (UsFl .NAME )) {
439- section = new UsFl ();
440- this .sections .put (UsFl .NAME , section );
441- } else if (sectionName .equals (UsMt .NAME )) {
442- section = new UsMt ();
443- this .sections .put (UsMt .NAME , section );
444- } else if (sectionName .equals (UsOr .NAME )) {
445- section = new UsOr ();
446- this .sections .put (UsOr .NAME , section );
447- } else if (sectionName .equals (UsTx .NAME )) {
448- section = new UsTx ();
449- this .sections .put (UsTx .NAME , section );
450- }
451- } else {
452- section = this .sections .get (sectionName );
453- }
454-
412+ EncodableSection section = getOrCreateSection (sectionName );
455413 if (section != null ) {
456414 section .decode (encodedString );
457415 }
0 commit comments