2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.misc.rosetta;
24 import java.util.Iterator;
25 import java.util.NoSuchElementException;
27 import org.onap.aaf.misc.env.Env;
28 import org.onap.aaf.misc.env.TimeTaken;
30 public abstract class Marshal<T> implements Parse<T, Marshal.State> {
33 * @see org.onap.aaf.misc.rosetta.Parse#newParsed()
36 public Parsed<State> newParsed() throws ParseException {
37 return new Parsed<State>(new State());
41 public TimeTaken start(Env env) {
42 //TODO is a way to mark not-JSON?
43 return env.start("Rosetta Marshal", Env.JSON);
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;
54 public static final Iterator<Void> DONE_ITERATOR = new Iterator<Void>() {
56 public boolean hasNext() {
63 throw new NoSuchElementException();
69 public void remove() {
74 * Typical definition of Done is when Iterator in Ladder is "DONE_ITERATOR"
76 * It is important, however, that the "Ladder Rung" is set to the right level.
81 public boolean amFinished(State state) {
82 return DONE_ITERATOR.equals(state.ladder.peek());