Sonar Fixes, Formatting
[aaf/authz.git] / misc / rosetta / src / main / java / org / onap / aaf / misc / rosetta / marshal / FieldArray.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
4  * ===========================================================================
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
6  * ===========================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.rosetta.marshal;
23
24 import java.util.Iterator;
25 import java.util.List;
26
27 import org.onap.aaf.misc.rosetta.Ladder;
28 import org.onap.aaf.misc.rosetta.Marshal;
29 import org.onap.aaf.misc.rosetta.ParseException;
30 import org.onap.aaf.misc.rosetta.Parsed;
31
32
33 public abstract class FieldArray<T,S> extends Marshal<T> {
34     private DataWriter<S> dataWriter;
35     private String name;
36
37     public FieldArray(String name, DataWriter<S> dw) {
38         this.name = name;
39         dataWriter = dw;
40     }
41
42     @SuppressWarnings("unchecked")
43     @Override
44     public Parsed<State> parse(T t, Parsed<State> parsed) throws ParseException {
45         Ladder<Iterator<?>> ladder = parsed.state.ladder;
46         Iterator<?> iter = ladder.peek();
47         if (iter==null) {
48             List<S> list = data(t);
49             if (list.isEmpty() && parsed.state.smallest) {
50                 ladder.push(DONE_ITERATOR);
51             } else {
52                 ladder.push(new ListIterator<S>(list));
53                 parsed.event = START_ARRAY;
54                 parsed.name = name;
55             }
56         } else if (DONE_ITERATOR.equals(iter)) {
57         } else {
58             ladder.ascend(); // look at field info
59                 Iterator<?> memIter = ladder.peek();
60                 ListIterator<S> mems = (ListIterator<S>)iter;
61                 S mem;
62                 if (memIter==null) {
63                     mem=mems.next();
64                 } else if (!DONE_ITERATOR.equals(memIter)) {
65                     mem=mems.peek();
66                 } else if (iter.hasNext()) {
67                     mem=null;
68                     ladder.push(null);
69                 } else {
70                     mem=null;
71                 }
72
73                 if (mem!=null) {
74                     parsed.isString=dataWriter.write(mem, parsed.sb);
75                     parsed.event = NEXT;
76                 }
77             ladder.descend();
78             if (mem==null) {
79                 if (iter.hasNext()) {
80                     parsed.event = NEXT;
81                 } else {
82                     parsed.event = END_ARRAY;
83                     ladder.push(DONE_ITERATOR);
84                 }
85             }
86         }
87         return parsed; // if unchanged, then it will end process
88     }
89
90     protected abstract List<S> data(T t);
91
92 }