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