@@ -43,47 +43,53 @@ public void setFieldValue(int sectionId, String fieldName, Object value) {
4343 setFieldValue (Sections .SECTION_ID_NAME_MAP .get (sectionId ), fieldName , value );
4444 }
4545
46+ private EncodableSection getOrCreateSection (String sectionName ) {
47+ EncodableSection section = this .sections .get (sectionName );
48+ if (section == null ) {
49+ switch (sectionName ) {
50+ case TcfEuV2 .NAME :
51+ section = new TcfEuV2 ();
52+ break ;
53+ case TcfCaV1 .NAME :
54+ section = new TcfCaV1 ();
55+ break ;
56+ case UspV1 .NAME :
57+ section = new UspV1 ();
58+ break ;
59+ case UsNatV1 .NAME :
60+ section = new UsNatV1 ();
61+ break ;
62+ case UsCaV1 .NAME :
63+ section = new UsCaV1 ();
64+ break ;
65+ case UsVaV1 .NAME :
66+ section = new UsVaV1 ();
67+ break ;
68+ case UsCoV1 .NAME :
69+ section = new UsCoV1 ();
70+ break ;
71+ case UsUtV1 .NAME :
72+ section = new UsUtV1 ();
73+ break ;
74+ case UsCtV1 .NAME :
75+ section = new UsCtV1 ();
76+ break ;
77+ }
78+ if (section != null ) {
79+ this .sections .put (sectionName , section );
80+ }
81+ }
82+ return section ;
83+ }
84+
4685 public void setFieldValue (String sectionName , String fieldName , Object value ) {
4786 if (!this .decoded ) {
4887 this .sections = this .decodeModel (this .encodedString );
4988 this .dirty = false ;
5089 this .decoded = true ;
5190 }
5291
53- EncodableSection section = null ;
54- if (!this .sections .containsKey (sectionName )) {
55- if (sectionName .equals (TcfCaV1 .NAME )) {
56- section = new TcfCaV1 ();
57- this .sections .put (TcfCaV1 .NAME , section );
58- } else if (sectionName .equals (TcfEuV2 .NAME )) {
59- section = new TcfEuV2 ();
60- this .sections .put (TcfEuV2 .NAME , section );
61- } else if (sectionName .equals (UspV1 .NAME )) {
62- section = new UspV1 ();
63- this .sections .put (UspV1 .NAME , section );
64- } else if (sectionName .equals (UsNatV1 .NAME )) {
65- section = new UsNatV1 ();
66- this .sections .put (UsNatV1 .NAME , section );
67- } else if (sectionName .equals (UsCaV1 .NAME )) {
68- section = new UsCaV1 ();
69- this .sections .put (UsCaV1 .NAME , section );
70- } else if (sectionName .equals (UsVaV1 .NAME )) {
71- section = new UsVaV1 ();
72- this .sections .put (UsVaV1 .NAME , section );
73- } else if (sectionName .equals (UsCoV1 .NAME )) {
74- section = new UsCoV1 ();
75- this .sections .put (UsCoV1 .NAME , section );
76- } else if (sectionName .equals (UsUtV1 .NAME )) {
77- section = new UsUtV1 ();
78- this .sections .put (UsUtV1 .NAME , section );
79- } else if (sectionName .equals (UsCtV1 .NAME )) {
80- section = new UsCtV1 ();
81- this .sections .put (UsCtV1 .NAME , section );
82- }
83- } else {
84- section = this .sections .get (sectionName );
85- }
86-
92+ EncodableSection section = getOrCreateSection (sectionName );
8793 if (section != null ) {
8894 section .setFieldValue (fieldName , value );
8995 this .dirty = true ;
@@ -102,9 +108,9 @@ public Object getFieldValue(String sectionName, String fieldName) {
102108 this .dirty = false ;
103109 this .decoded = true ;
104110 }
105-
106- if (this . sections . containsKey ( sectionName ) ) {
107- return this . sections . get ( sectionName ) .getFieldValue (fieldName );
111+ EncodableSection field = this . sections . get ( sectionName );
112+ if (field != null ) {
113+ return field .getFieldValue (fieldName );
108114 } else {
109115 return null ;
110116 }
@@ -120,9 +126,9 @@ public boolean hasField(String sectionName, String fieldName) {
120126 this .dirty = false ;
121127 this .decoded = true ;
122128 }
123-
124- if (this . sections . containsKey ( sectionName ) ) {
125- return this . sections . get ( sectionName ) .hasField (fieldName );
129+ EncodableSection field = this . sections . get ( sectionName );
130+ if (field != null ) {
131+ return field .hasField (fieldName );
126132 } else {
127133 return false ;
128134 }
@@ -169,11 +175,7 @@ public EncodableSection getSection(String sectionName) {
169175 this .decoded = true ;
170176 }
171177
172- if (this .sections .containsKey (sectionName )) {
173- return this .sections .get (sectionName );
174- } else {
175- return null ;
176- }
178+ return this .sections .get (sectionName );
177179 }
178180
179181 public void deleteSection (int sectionId ) {
@@ -187,8 +189,7 @@ public void deleteSection(String sectionName) {
187189 this .decoded = true ;
188190 }
189191
190- if (this .sections .containsKey (sectionName )) {
191- this .sections .remove (sectionName );
192+ if (this .sections .remove (sectionName ) != null ) {
192193 this .dirty = true ;
193194 }
194195 }
@@ -242,25 +243,26 @@ public List<Integer> getSectionIds() {
242243 this .dirty = false ;
243244 this .decoded = true ;
244245 }
245-
246- List <Integer > sectionIds = new ArrayList <>();
247- for (int i = 0 ; i < Sections . SECTION_ORDER . size () ; i ++) {
246+ int length = Sections . SECTION_ORDER . size ();
247+ List <Integer > sectionIds = new ArrayList <>(length );
248+ for (int i = 0 ; i < length ; i ++) {
248249 String sectionName = Sections .SECTION_ORDER .get (i );
249- if ( this .sections .containsKey (sectionName )) {
250- EncodableSection section = this . sections . get ( sectionName );
250+ EncodableSection section = this .sections .get (sectionName );
251+ if ( section != null ) {
251252 sectionIds .add (section .getId ());
252253 }
253254 }
254255 return sectionIds ;
255256 }
256257
257258 protected String encodeModel (Map <String , EncodableSection > sections ) {
258- List <String > encodedSections = new ArrayList <>();
259- List <Integer > sectionIds = new ArrayList <>();
260- for (int i = 0 ; i < Sections .SECTION_ORDER .size (); i ++) {
259+ int length = Sections .SECTION_ORDER .size ();
260+ List <String > encodedSections = new ArrayList <>(length );
261+ List <Integer > sectionIds = new ArrayList <>(length );
262+ for (int i = 0 ; i < length ; i ++) {
261263 String sectionName = Sections .SECTION_ORDER .get (i );
262- if ( sections .containsKey (sectionName )) {
263- EncodableSection section = sections . get ( sectionName );
264+ EncodableSection section = sections .get (sectionName );
265+ if ( section != null ) {
264266 encodedSections .add (section .encode ());
265267 sectionIds .add (section .getId ());
266268 }
@@ -290,33 +292,35 @@ protected Map<String, EncodableSection> decodeModel(String str) {
290292 @ SuppressWarnings ("unchecked" )
291293 List <Integer > sectionIds = (List <Integer >) header .getFieldValue ("SectionIds" );
292294 for (int i = 0 ; i < sectionIds .size (); i ++) {
293- if (sectionIds .get (i ).equals (TcfEuV2 .ID )) {
294- TcfEuV2 section = new TcfEuV2 (encodedSections [i + 1 ]);
295- sections .put (TcfEuV2 .NAME , section );
296- } else if (sectionIds .get (i ).equals (TcfCaV1 .ID )) {
297- TcfCaV1 section = new TcfCaV1 (encodedSections [i + 1 ]);
298- sections .put (TcfCaV1 .NAME , section );
299- } else if (sectionIds .get (i ).equals (UspV1 .ID )) {
300- UspV1 section = new UspV1 (encodedSections [i + 1 ]);
301- sections .put (UspV1 .NAME , section );
302- } else if (sectionIds .get (i ).equals (UsCaV1 .ID )) {
303- UsCaV1 section = new UsCaV1 (encodedSections [i + 1 ]);
304- sections .put (UsCaV1 .NAME , section );
305- } else if (sectionIds .get (i ).equals (UsNatV1 .ID )) {
306- UsNatV1 section = new UsNatV1 (encodedSections [i + 1 ]);
307- sections .put (UsNatV1 .NAME , section );
308- } else if (sectionIds .get (i ).equals (UsVaV1 .ID )) {
309- UsVaV1 section = new UsVaV1 (encodedSections [i + 1 ]);
310- sections .put (UsVaV1 .NAME , section );
311- } else if (sectionIds .get (i ).equals (UsCoV1 .ID )) {
312- UsCoV1 section = new UsCoV1 (encodedSections [i + 1 ]);
313- sections .put (UsCoV1 .NAME , section );
314- } else if (sectionIds .get (i ).equals (UsUtV1 .ID )) {
315- UsUtV1 section = new UsUtV1 (encodedSections [i + 1 ]);
316- sections .put (UsUtV1 .NAME , section );
317- } else if (sectionIds .get (i ).equals (UsCtV1 .ID )) {
318- UsCtV1 section = new UsCtV1 (encodedSections [i + 1 ]);
319- sections .put (UsCtV1 .NAME , section );
295+ String section = encodedSections [i + 1 ];
296+ switch (sectionIds .get (i )) {
297+ case TcfEuV2 .ID :
298+ sections .put (TcfEuV2 .NAME , new TcfEuV2 (section ));
299+ break ;
300+ case TcfCaV1 .ID :
301+ sections .put (TcfCaV1 .NAME , new TcfCaV1 (section ));
302+ break ;
303+ case UspV1 .ID :
304+ sections .put (UspV1 .NAME , new UspV1 (section ));
305+ break ;
306+ case UsCaV1 .ID :
307+ sections .put (UsCaV1 .NAME , new UsCaV1 (section ));
308+ break ;
309+ case UsNatV1 .ID :
310+ sections .put (UsNatV1 .NAME , new UsNatV1 (section ));
311+ break ;
312+ case UsVaV1 .ID :
313+ sections .put (UsVaV1 .NAME , new UsVaV1 (section ));
314+ break ;
315+ case UsCoV1 .ID :
316+ sections .put (UsCoV1 .NAME , new UsCoV1 (section ));
317+ break ;
318+ case UsUtV1 .ID :
319+ sections .put (UsUtV1 .NAME , new UsUtV1 (section ));
320+ break ;
321+ case UsCtV1 .ID :
322+ sections .put (UsCtV1 .NAME , new UsCtV1 (section ));
323+ break ;
320324 }
321325 }
322326 }
@@ -349,9 +353,9 @@ public String encodeSection(String sectionName) {
349353 this .dirty = false ;
350354 this .decoded = true ;
351355 }
352-
353- if (this . sections . containsKey ( sectionName ) ) {
354- return this . sections . get ( sectionName ) .encode ();
356+ EncodableSection section = this . sections . get ( sectionName );
357+ if (section != null ) {
358+ return section .encode ();
355359 } else {
356360 return null ;
357361 }
@@ -362,40 +366,7 @@ public void decodeSection(int sectionId, String encodedString) {
362366 }
363367
364368 public void decodeSection (String sectionName , String encodedString ) {
365- EncodableSection section = null ;
366- if (!this .sections .containsKey (sectionName )) {
367- if (sectionName .equals (TcfEuV2 .NAME )) {
368- section = new TcfEuV2 ();
369- this .sections .put (TcfEuV2 .NAME , section );
370- } else if (sectionName .equals (TcfCaV1 .NAME )) {
371- section = new TcfCaV1 ();
372- this .sections .put (TcfCaV1 .NAME , section );
373- } else if (sectionName .equals (UspV1 .NAME )) {
374- section = new UspV1 ();
375- this .sections .put (UspV1 .NAME , section );
376- } else if (sectionName .equals (UsNatV1 .NAME )) {
377- section = new UsNatV1 ();
378- this .sections .put (UsNatV1 .NAME , section );
379- } else if (sectionName .equals (UsCaV1 .NAME )) {
380- section = new UsCaV1 ();
381- this .sections .put (UsCaV1 .NAME , section );
382- } else if (sectionName .equals (UsVaV1 .NAME )) {
383- section = new UsVaV1 ();
384- this .sections .put (UsVaV1 .NAME , section );
385- } else if (sectionName .equals (UsCoV1 .NAME )) {
386- section = new UsCoV1 ();
387- this .sections .put (UsCoV1 .NAME , section );
388- } else if (sectionName .equals (UsUtV1 .NAME )) {
389- section = new UsUtV1 ();
390- this .sections .put (UsUtV1 .NAME , section );
391- } else if (sectionName .equals (UsCtV1 .NAME )) {
392- section = new UsCtV1 ();
393- this .sections .put (UsCtV1 .NAME , section );
394- }
395- } else {
396- section = this .sections .get (sectionName );
397- }
398-
369+ EncodableSection section = getOrCreateSection (sectionName );
399370 if (section != null ) {
400371 section .decode (encodedString );
401372 }
0 commit comments