Loop

public class Loop implements Iterable<Segment>

The Loop class is the representation of an Loop in a ANSI X12 transaction. The building block of an X12 transaction is an element. Some elements may be made of sub elements. Elements combine to form segments. Segments are grouped as loops. And a set of loops form an X12 transaction.

Author:Prasad Balan

Constructors

Loop

public Loop(Context c, String name)

The constructor takes a context object.

Parameters:

Methods

addChild

public Loop addChild(String name)

Creates an empty instance of Loop and adds the loop as a child to the current Loop. The returned instance can be used to add segments to the child loop.

Parameters:
  • name – name of the loop
Returns:

a new child Loop object

addChild

public void addChild(int index, Loop loop)

Inserts Loop as a child loop at the specified position.

Parameters:
  • index – position at which to add the loop.
  • loop – a Loop object.

addChild

public Loop addChild(int index, String name)

Creates an empty instance of Loop and inserts the loop as a child loop at the specified position. The returned instance can be used to add segments to the child loop.

Parameters:
  • index – position at which to add the loop
  • name – name of the loop
Returns:

a new child Loop object

addSegment

public Segment addSegment()

Creates an empty instance of Segment and adds the segment to current Loop. The returned instance can be used to add elements to the segment.

Returns:a new Segment object

addSegment

public Segment addSegment(String segment)

Takes a java.lang.String representation of segment, creates a Segment object and adds the segment to the current Loop.

Parameters:
Returns:

a new Segment object

addSegment

public void addSegment(Segment segment)

Adds Segment at the end of the current Loop

Parameters:

addSegment

public Segment addSegment(int index)

Creates an empty instance of Segment and adds the segment at the specified position in the current Loop. The returned instance can be used to add elements to the segment.

Parameters:
  • index – position at which to add the segment.
Returns:

a new Segment object

addSegment

public Segment addSegment(int index, String segment)

Takes a java.lang.String representation of segment, creates a Segment object and adds the segment at the specified position in the current Loop.

Parameters:
  • index – position to add the segment.
  • segmentjava.lang.String representation of the segment.
Returns:

a new Segment object

addSegment

public void addSegment(int index, Segment segment)

Adds Segment at the specified position in current Loop.

Parameters:
  • index – position to add the segment.
  • segmentSegment representation of the segment.

childList

public List<Loop> childList()

Returns java.util.List of child Loops

Returns:java.util.List<Loop>

findLoop

public List<Loop> findLoop(String name)

Get the loop in the X12 transaction It will check the complete child hierarchy.

Parameters:
  • name – name of a loop
Returns:

java.util.List<Loop>

findSegment

public List<Segment> findSegment(String name)

Get the segment in the X12 transaction It will check the current loop and the complete child hierarchy.

Parameters:
  • name – name of a segment
Returns:

java.util.List<Segment>

getContext

public Context getContext()

Returns the context of the X12 transaction.

Returns:Context object

getDepth

public int getDepth()

Generally not used. Mostly for debugging.

Returns:depth

getLoop

public Loop getLoop(int index)

Returns the Loop at the specified position.

Parameters:
  • index – the index from which to return the loop.
Returns:

Loop at the specified index

getLoops

public List<Loop> getLoops()

Returns the loops

Returns:java.util.List<Loop<

getName

public String getName()

Returns the name of the current Loop.

Returns:java.lang.String

getParent

public Loop getParent()

Getter for the field Loop.

Returns:Parent Loop

getSegment

public Segment getSegment()

Returns the Segment at the default position.

Returns:Segment at the default index

getSegment

public Segment getSegment(int index)

Returns the Segment at the specified position.

Parameters:
  • index – the index from which to get the segment.
Returns:

Segment at the specified index

getSegments

public List<Segment> getSegments()

Returns the segments in the current loop.

Returns:java.util.List

hasLoop

public boolean hasLoop(String name)

Checks if the Loop contains the specified child Loop. It will check the complete child hierarchy.

Parameters:
  • name – name of a child loop
Returns:

boolean

iterator

public Iterator<Segment> iterator()

Returns and java.util.Iteratorto the segments in the loop.

Returns:java.util.Iterator<Segment>

removeLoop

public Loop removeLoop(int index)

Removes the loop at the specified position in this list.

Parameters:
  • index – the index of the loop to remove.
Returns:

the loop that was removed.

removeSegment

public Segment removeSegment(int index)

Removes the segment at the specified position in this list.

Parameters:
  • index – the index of the segment to remove.
Returns:

the segment that was removed.

setChild

public Loop setChild(int index, String name)

Creates a new Loop and replaces the child loop at the specified position. The returned instance can be used to add segments to the child loop.

Parameters:
  • name – name of the loop
  • index – position at which to add the loop.
Returns:

a new child Loop object

setChild

public void setChild(int index, Loop loop)

Replaces child Loop at the specified position.

Parameters:
  • index – position at which to add the loop.
  • loop – Loop to add

setContext

public void setContext(Context context)

Sets the context of the current transaction.

Parameters:
  • context – the context to set for the current transaction.

setName

public void setName(String name)

Sets the name of the current Loop

Parameters:

setParent

public void setParent(Loop parent)

Sets the parent to the specified Loop.

Parameters:
  • parent – the loop to use as the parent of this loop.

setSegment

public Segment setSegment(int index)

Creates an empty instance of Segment and replaces the segment at specified position in the X12 transaction. The returned instance can be used to add elements to the segment.

Parameters:
  • index – position at which to add the segment.
Returns:

a new Segment object

setSegment

public Segment setSegment(int index, String segment)

Takes a java.lang.String representation of segment, creates a java.lang.String object and replaces the segment at the specified position in the X12 transaction.

Parameters:
  • index – position of the segment to be replaced.
  • segmentjava.lang.String representation of the Segment.
Returns:

a new Segment object

setSegment

public void setSegment(int index, Segment segment)

Replaces Segment at the specified position in X12 transaction.

Parameters:
  • index – position of the segment to be replaced.
  • segmentSegment

size

public int size()

Returns number of segments in Loop and child loops

Returns:size

toString

public String toString()

Returns the Loop in X12 java.lang.String format. This method is used to convert the X12 object into a X12 transaction.

Returns:String

toString

public String toString(boolean bRemoveTrailingEmptyElements)

Returns the Loop in X12 java.lang.String format. This method is used to convert the X12 object into a X12 transaction.

Parameters:
  • bRemoveTrailingEmptyElements – a flag for whether or not empty trailing elements should be removed.
Returns:

String representation of the loop.

toXML

public String toXML()

Returns the Loop in XML java.lang.String format. This method is used to convert the X12 object into a XML string.

Returns:XML String

toXML

public String toXML(boolean bRemoveTrailingEmptyElements)

Returns the Loop in XML java.lang.String format. This method is used to convert the X12 object into a XML string.

Parameters:
  • bRemoveTrailingEmptyElements – a flag for whether or not empty trailing elements should be removed.
Returns:

String the loop in XML string format.