Increase code coverage auth fs
[aaf/authz.git] / misc / rosetta / src / main / java / org / onap / aaf / misc / rosetta / marshal / ObjMarshal.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.NoSuchElementException;
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  * Object Marshal
34  * Assumes has Fields and other Objects
35  * s
36  * @author Jonathan
37  *
38  * @param <T>
39  */
40 public abstract class ObjMarshal<T> extends Marshal<T> {
41         // Note: Not Using List or ArrayList, because there is no "Peek" concept in their iterator.
42         private Marshal<T>[] pml;
43         private int end=0;
44         
45         /**
46          * @param pm
47          */
48         @SuppressWarnings("unchecked")
49         protected void add(Marshal<T> pm) {
50                 if(pml==null) {
51                         pml = new Marshal[Ladder.DEFAULT_INIT_SIZE]; 
52                 } else if(end>pml.length) {
53                         Object temp[] = pml; 
54                         pml = new Marshal[pml.length+Ladder.DEFAULT_INIT_SIZE];
55                         System.arraycopy(temp, 0, pml, 0, pml.length);
56                 }
57                 pml[end]=pm;
58                 ++end;
59         }
60         
61         /* (non-Javadoc)
62          * @see org.onap.aaf.misc.rosetta.Parse#parse(java.lang.Object, org.onap.aaf.misc.rosetta.Parsed)
63          */
64         @SuppressWarnings("unchecked")
65         @Override
66         public Parsed<State> parse(T in, Parsed<State> parsed) throws ParseException {
67                 Ladder<Iterator<?>> ladder = parsed.state.ladder;
68                 Iterator<Marshal<T>> iter = (Iterator<Marshal<T>>)ladder.peek();
69                 if(iter==null) {
70                         if(pml.length>0) {
71                                 ladder.push(new FieldsIterator());
72                                 parsed.event = START_OBJ;
73                         } else {
74                                 ladder.push(DONE_ITERATOR);
75                         }
76                 } else if (DONE_ITERATOR.equals(iter)) {
77                 } else {
78                         FieldsIterator fields = (FieldsIterator)iter;
79                         ladder.ascend(); // look at field info
80                                 Iterator<?> currFieldIter = ladder.peek();
81                                 Marshal<T> marshal;
82                                 if(currFieldIter==null) {
83                                         marshal=fields.next();
84                                 } else if(!DONE_ITERATOR.equals(currFieldIter)) {
85                                         marshal=fields.peek();
86                                         if(marshal==null && fields.hasNext())marshal=fields.next();
87                                 } else if(fields.hasNext()) {
88                                         marshal=fields.next();
89                                         ladder.push(null);
90                                 } else {
91                                         marshal=null;
92                                 }
93                                 
94                                 if(marshal!=null)
95                                         parsed = marshal.parse(in, parsed);
96                         ladder.descend();
97                         if(marshal==null || parsed.event==NONE) {
98                                 parsed.event = END_OBJ;
99                                 ladder.push(DONE_ITERATOR);
100                         }
101                 }
102                 return parsed; // if unchanged, then it will end process
103         }
104
105         private class FieldsIterator implements Iterator<Marshal<T>> {
106                 private int idx = -1;
107
108                 @Override
109                 public boolean hasNext() {
110                         return idx<end;
111                 }
112
113                 @Override
114                 public Marshal<T> next() {
115                         if(!hasNext()) {
116                                 throw new NoSuchElementException();
117                         }
118                         return pml[++idx];
119                 }
120
121                 public Marshal<T> peek() {
122                         return idx<0?null:pml[idx];
123                 }
124                 
125                 @Override
126                 public void remove() {
127                         pml[idx]=null;
128                 }
129                 
130         }
131
132 }