/* ************************************************************************
#
#  DivConq
#
#  http://divconq.com/
#
#  Copyright:
#    Copyright 2012 eTimeline, LLC. All rights reserved.
#
#  License:
#    See the license.txt file in the project's top-level directory for details.
#
#  Authors:
#    * Andy White
#
************************************************************************ */
package divconq.struct;

import divconq.struct.builder.BuilderStateException;
import divconq.struct.builder.ICompositeBuilder;
import divconq.struct.builder.ICompositeOutput;

public class FieldStruct implements ICompositeOutput {
	protected String name = null;
	protected Object orgvalue = null;
	protected boolean prepped = false;
	protected Struct value = null;
	
	public String getName() {
		return this.name;
	}
	
	public Struct getValue() {
		return this.value;
	}

	public void setValue(Struct v) {
		this.value = v;
		this.orgvalue = null;	// replaced with real value
	}
	
	public FieldStruct(String name, Object value) {
		this.name = name;
		this.orgvalue = value;
	}
	
	@Override
	public void toBuilder(ICompositeBuilder builder) throws BuilderStateException {
		if (this.value instanceof ICompositeOutput) {
			builder.field(this.name);
			((ICompositeOutput)this.value).toBuilder(builder);
		}
		else
			builder.field(this.name, this.value);
	}

	public boolean isEmpty() {
		if (this.value == null)
			return true;
		
		if (this.value instanceof CharSequence)
			return this.value.toString().isEmpty();
		
		return false;
	}

	public void dispose() {
		//if (this.value != null)
		//	this.value.dispose();
		
		this.value = null;
	}
}
