AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / rosetta / src / main / java / org / onap / aaf / misc / rosetta / Marshal.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;
23
24 import java.util.Iterator;
25
26 import org.onap.aaf.misc.env.Env;
27 import org.onap.aaf.misc.env.TimeTaken;
28
29 public abstract class Marshal<T> implements Parse<T, Marshal.State> {
30
31         /* (non-Javadoc)
32          * @see org.onap.aaf.misc.rosetta.Parse#newParsed()
33          */
34         @Override
35         public Parsed<State> newParsed() throws ParseException {
36                 return new Parsed<State>(new State());
37         }
38
39         @Override
40         public TimeTaken start(Env env) {
41                 //TODO is a way to mark not-JSON?
42                 return env.start("Rosetta Marshal", Env.JSON);
43         };
44
45         public static class State {
46                 // Note:  Need a STATEFUL stack... one that will remain stateful until marked as finished
47                 // "finished" is know by Iterators with no more to do/null
48                 // Thus the concept of "Ladder", which one ascends and decends
49                 public Ladder<Iterator<?>> ladder = new Ladder<Iterator<?>>();
50                 public boolean smallest = true;
51         }
52
53         public static final Iterator<Void> DONE_ITERATOR = new Iterator<Void>() {
54                 @Override
55                 public boolean hasNext() {
56                         return false;
57                 }
58
59                 @Override
60                 public Void next() {
61                         return null;
62                 }
63
64                 @Override
65                 public void remove() {
66                 }
67         };
68
69         /**
70          * Typical definition of Done is when Iterator in Ladder is "DONE_ITERATOR"
71          * 
72          * It is important, however, that the "Ladder Rung" is set to the right level.
73          * 
74          * @param state
75          * @return
76          */
77         public boolean amFinished(State state) {
78                 return DONE_ITERATOR.equals(state.ladder.peek());
79         }
80
81 }