All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.att.rosetta.marshal.ObjArray Maven / Gradle / Ivy

The newest version!
/*******************************************************************************
 * Copyright (c) 2016 AT&T Intellectual Property. All rights reserved.
 *******************************************************************************/
package com.att.rosetta.marshal;

import java.util.Iterator;
import java.util.List;

import com.att.rosetta.Ladder;
import com.att.rosetta.Marshal;
import com.att.rosetta.ParseException;
import com.att.rosetta.Parsed;


public abstract class ObjArray extends Marshal {
	private String name;
	private Marshal subMarshaller;

	public ObjArray(String name, Marshal subMarshaller) {
		this.name = name;
		this.subMarshaller = subMarshaller;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Parsed parse(T t, Parsed parsed) throws ParseException {
		Ladder> ladder = parsed.state.ladder;
		Iterator iter = ladder.peek();
		if(iter==null) {
			List list = data(t);
			if(list.isEmpty() && parsed.state.smallest) {
				ladder.push(DONE_ITERATOR);
			} else {
				ladder.push(new ListIterator(list));
				parsed.event = START_ARRAY;
				parsed.name = name;
			}
		} else if (DONE_ITERATOR.equals(iter)) {
		} else {
			ladder.ascend(); // look at field info
				Iterator memIter = ladder.peek();
				ListIterator mems = (ListIterator)iter;
				S mem;
				if(memIter==null) {
					mem=mems.next();
				} else if(!DONE_ITERATOR.equals(memIter)) {
					mem=mems.peek();
				} else if(iter.hasNext()) {
					mem=null;
					ladder.push(null);
				} else {
					mem=null;
				}
				
				if(mem!=null)
					parsed = subMarshaller.parse(mem, parsed);
			ladder.descend();
			if(mem==null) {
				if(iter.hasNext()) {
					parsed.event = NEXT;
				} else {
					parsed.event = END_ARRAY;
					ladder.push(DONE_ITERATOR);
				}
			}
		}
		return parsed; // if unchanged, then it will end process
	}

	protected abstract List data(T t);

}