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