Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* ************************************************************************
  #
  #  DivConq
  #
  #  http://divconq.com/
  #
  #  Copyright:
  #    Copyright 2014 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 java.util.List;
 
 
DivConq uses a specialized type system that provides type consistency across services (including web services), database fields and stored procedures, as well as scripting. All scalars (including primitives) and composites (collections) are wrapped by some subclass of Struct. List/array collections are expressed by this class. This class is analogous to an Array in JSON but may contain type information as well, similar to Yaml. TODO link to blog entries.

Author(s):
Andy
 
 public class ListStruct extends CompositeStruct implements IItemCollection {
 	protected List<Structitems = new CopyOnWriteArrayList<Struct>();		// TODO can we make a more efficient list (one that allows modifications but won't crash an iterator)
 
 	public DataType getType() {
 		if (this. != null)
 			return super.getType();
 
 		// implied only, not explicit
 		return ..getSchema().getType("AnyList");
 	}

Provide data type info (schema for fields) and a list of initial items

Parameters:
type field schema
items initial values
 
 	public ListStruct(DataType typeObject... items) {
 		super(type);
 		this.addItem(items);
 	}
 	
 	public ListStruct(DataType typeCollection<? extends Objectitems) {
 		super(type);
 		this.addCollection(items);
 	}

Optionally provide a list of initial items

Parameters:
items initial values
 
 	public ListStruct(Object... items) {
 		this.addItem(items);
 	}
 	
 	public ListStruct(Collection<? extends Objectitems) {
 		this.addCollection(items);
	}
	/* (non-Javadoc)
	 * @see divconq.struct.CompositeStruct#select(divconq.struct.PathPart[])
	 */
	public Struct select(PathPart... path) {
		if (path.length == 0)
			return this;
		PathPart part = path[0];
		OperationResult log = part.getLog();
		// not allowed
		if (log == null)
		String fld = part.getField();
		if ("Length".equals(fld))
			return new IntegerStruct(this..size());
		if (fld != null) {
			log.warnTr(501, this);
		}
		int idx = part.getIndex();
		if (idx >= this..size()) {
			log.warnTr(502, part.getIndex());
		}
		Struct o = this..get(idx);
		if (path.length == 1) 
			return o;			
		if (o instanceof CompositeStruct
			return ((CompositeStruct)o).select(Arrays.copyOfRange(path, 1, path.length));		
		log.warnTr(503, o);
	}
	public Stream<StructstructStream() {
		return this..stream();
	}
		return this..stream().map(p -> (RecordStruct)p);
	}
	public Stream<StringstringStream() {
		return this..stream().map(p -> Struct.objectToString(p));
	}
	public Stream<LongintegerStream() {
		return this..stream().map(p -> Struct.objectToInteger(p));
	}
	/* (non-Javadoc)
	 * @see divconq.struct.Struct#isBlank()
	 */
	public boolean isEmpty() {
		return (this..size() == 0);
	}
	/* (non-Javadoc)
	 * @see divconq.struct.builder.ICompositeOutput#toBuilder(divconq.struct.builder.ICompositeBuilder)
	 */
	public void toBuilder(ICompositeBuilder builderthrows BuilderStateException {
		builder.startList();
		for (Object o : this.
			builder.value(o);
		builder.endList();
	}

Attempt to add items to the list, but if there is a schema the items must match the schema.

Parameters:
items to add
Returns:
log of the result of the call (check hasErrors)
	public OperationResult addItem(Object... items) {
		for (Object o : items) {
			Object value = o;
			Struct svalue = null;
			if (value instanceof ICompositeBuilder)
				value = ((ICompositeBuilder)value).toLocal();
			if (this. != null) {
				Struct sv = this..wrapItem(valueor);
				if (sv != null)
					svalue = sv;
			}
			if (svalue == null
				svalue = Struct.objectToStruct(value); 
			this..add(svalue);
		}
		return or;
	}
	public OperationResult addCollection(Collection<? extends Objectcoll) {
		for (Object o : coll)
			or.copyMessages(this.addItem(o));		// extra slow, enhance TOTO
		return or;
	}
		for (Struct o : coll.getItems())
			or.copyMessages(this.addItem(o));		// extra slow, enhance TOTO
		return or;
	}

Returns:
collection of all the items the list holds
	public Iterable<StructgetItems() {
		return this.;
	}
		return new ClassicIterableAdapter<Struct>(this.);
	}

Parameters:
idx position in list of the item desired (0 based)
Returns:
the struct for that field
	public Struct getItem(int idx) {
		if ((idx >= this..size()) || (idx < 0))
			return null;
		return this..get(idx);
	}

Parameters:
idx position in list of the item desired (0 based)
Returns:
true if an item is at that position
	public boolean hasItem(int idx) {
		if (idx >= this..size())
			return false;
		return true;
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as Integer (DivConq thinks of integers as 64bit)
	public Long getItemAsInteger(int idx) {
		return Struct.objectToInteger(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as BigInteger
	public BigInteger getItemAsBigInteger(int idx) {
		return Struct.objectToBigInteger(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as BigDecimal
	public BigDecimal getItemAsDecimal(int idx) {
		return Struct.objectToDecimal(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as Boolean
	public Boolean getItemAsBoolean(int idx) {
		return Struct.objectToBoolean(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as Datetime
	public DateTime getItemAsDateTime(int idx) {
		return Struct.objectToDateTime(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as Datetime
	public BigDateTime getItemAsBigDateTime(int idx) {
		return Struct.objectToBigDateTime(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as Date
	public LocalDate getItemAsDate(int idx) {
		return Struct.objectToDate(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as Time
	public LocalTime getItemAsTime(int idx) {
		return Struct.objectToTime(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as String
	public String getItemAsString(int idx) {
		return Struct.objectToString(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as Binary
	public Memory getItemAsBinary(int idx) {
		return Struct.objectToBinary(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as CompositeStruct
	public CompositeStruct getItemAsComposite(int idx) {
		return Struct.objectToComposite(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as RecordStruct
	public RecordStruct getItemAsRecord(int idx) {
		return Struct.objectToRecord(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as ListStruct
	public ListStruct getItemAsList(int idx) {
		return Struct.objectToList(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as Struct
	public Struct getItemAsStruct(int idx) {
		return Struct.objectToStruct(this.getItem(idx));
	}

Unlike getItem, this returns the value (inner) rather than struct wrapping the value.

Parameters:
idx position in list of the item desired (0 based)
Returns:
field's "inner" value as Xml (will parse if value is string)
	public XElement getItemAsXml(int idx) {
		return Struct.objectToXml(this.getItem(idx));
	}

Parameters:
idx position in list of the item desired (0 based)
Returns:
true if item does not exist or if item is string and its value is empty
	public boolean isItemEmpty(int idx) {
		if (idx >= this..size())
			return true;
		Object o = this..get(idx);
		if (o == null)
			return true;
		if (o instanceof CharSequence)
			return o.toString().isEmpty();
		return false;
	}

Returns:
number of items in this list
	public int getSize() {
		return this..size();
	}

Parameters:
idx position in list of the item to remove from list
	public void removeItem(int idx) {		
		if (idx >= this..size())
			return;
		this..remove(idx);
	}
	/*
	 * @param idx position in list of the item to remove from list
	 */
	public void removeItem(Struct itm) {
		// TODO dispose
		//Struct old = this.items.get(itm);
		//if (old != null)
		//	old.dispose();
		this..remove(itm);
	}
    @Override
    protected void doCopy(Struct n) {
    	super.doCopy(n);
    	
    	ListStruct nn = (ListStruct)n;
    	
   		nn.addCollection(this.);
    }
    
	public Struct deepCopy() {
		ListStruct cp = new ListStruct();
		this.doCopy(cp);
		return cp;
	}

Returns:
schema for the primary/default data type of the list items
	public DataType getChildType() {
		if (this. != null
		return null;
	}
	/* (non-Javadoc)
	 * @see divconq.struct.CompositeStruct#clear()
	 */
	public void clear() {
		this..clear();
	}
	public void operation(StackEntry stackXElement code) {
		if ("Set".equals(code.getName())) {
			this.clear();
			String json = stack.resolveValueToString(code.getText());
			if (StringUtil.isNotEmpty(json)) {
				ListStruct pjson = (ListStruct) CompositeParser.parseJson(" [ " + json + " ] ").getResult();
				for (Struct s : pjson.getItems())
					this..add(s);
			}
			// TODO else check for Xml or Yaml
			stack.resume();
			return;
		}
		else if ("AddItem".equals(code.getName())) {
			Struct sref = stack.refFromElement(code"Value"); 
			this.addItem(sref);
			stack.resume();
			return;
		}
		else if ("RemoveItem".equals(code.getName())) {
			long idx = stack.intFromElement(code"Index", -1); 
			if (idx > -1)
				this.removeItem((intidx);
			stack.resume();
			return;
		}
		else if ("Clear".equals(code.getName())) {
			this.clear();
			stack.resume();
			return;
		}
		super.operation(stackcode);
	}
	public List<StringtoStringList() {
		List<Stringnlist = new ArrayList<>();
		for (Struct s : this.
			if (s != null)
				nlist.add(s.toString());
		return nlist;
	}
	public boolean contains(Struct v) {
		return this..contains(v);
	}
New to GrepCode? Check out our FAQ X