org.dtools.ini
Class AdvancedIniSection

java.lang.Object
  extended by org.dtools.ini.IniSection
      extended by org.dtools.ini.AdvancedIniSection
All Implemented Interfaces:
Cloneable, Iterable<IniItem>, Commentable

public class AdvancedIniSection
extends IniSection

This implementation of the IniSection interface offers faster performance compared with the BasicIniSection implementation, but at the expense of greater memory usage.

Since:
0.2.00
Version:
1.1.0
Author:
David Lewis

Field Summary
 
Fields inherited from class org.dtools.ini.IniSection
validator
 
Fields inherited from interface org.dtools.ini.Commentable
COMMENT_SYMBOL
 
Constructor Summary
AdvancedIniSection(String name)
          Default constructor which creates a new instance of this IniSection and sets the IniSection to have a default IniValidator and to be case insensitive.
AdvancedIniSection(String name, boolean caseSensitive)
          Default constructor which creates a new instance of this IniSection and sets the IniSection to have a default IniValidator and to be case insensitive.
AdvancedIniSection(String name, IniValidator validator)
          Default constructor which creates a new instance of this IniSection and sets the IniSection to have a default IniValidator and to be case insensitive.
AdvancedIniSection(String name, IniValidator validator, boolean caseSensitive)
          Default constructor which creates a new instance of this IniSection and sets the IniSection to have a default IniValidator and to be case insensitive.
 
Method Summary
 boolean addItem(IniItem item)
          Adds an IniItem to this section.
 boolean addItem(IniItem item, int index)
          Adds an IniItem to this section at the given index.
 IniItem addItem(String name)
          Adds a new IniItem to this section with the given name.
 IniItem addItem(String itemName, int index)
          Adds an IniItem to this section at the given index.
 void addItems(String... itemNames)
          Add multiple new items to this section.
 Object clone()
          This class implements the Cloneable interface, but leaves implementation to subclasses which override this class.
protected  IniItem createItem(String name)
          This method creates and returns a new instance of an IniItem with the same IniValidator and case sensitivity as this object.
 IniItem getItem(int index)
          Get the item which is at the given index.
 IniItem getItem(String name)
          Returns the item with the given name, or null if no item exists with the given name.
 Collection<String> getItemNames()
          This method returns a collection of all the names of all the items within this section.
 Collection<IniItem> getItems()
          Get a collection of all the items within this section.
 int getNumberOfItems()
          Return the number of IniItems that this array has.
 boolean hasItem(IniItem item)
          Predicate that returns true if an item is in this section.
 boolean hasItem(String name)
          Predicate that returns true if an item already exists.
 int indexOf(IniItem item)
          Returns the index of item that is in this section.
 int indexOf(String name)
          Returns the index of item that is in this section.
 boolean isEmpty()
          Predicate that returns true if the section has no items, false if it does have at least one item.
 Iterator<IniItem> iterator()
           
 boolean merge(IniSection otherSection)
          This method merges another IniSection to this section.
 void moveItem(int fromIndex, int toIndex)
          This method moves an item at index fromIndex to the index toIndex.
 boolean removeItem(IniItem item)
          Removes the item this section.
 
Methods inherited from class org.dtools.ini.IniSection
equals, getEndLineComment, getName, getPostComment, getPreComment, getValidator, hashCode, isCaseSensitive, moveItem, removeAll, removeEndLineComment, removeItem, removeItem, removeItems, removePostComment, removePreComment, setEndLineComment, setPostComment, setPreComment, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AdvancedIniSection

public AdvancedIniSection(String name)

Default constructor which creates a new instance of this IniSection and sets the IniSection to have a default IniValidator and to be case insensitive.

Parameters:
name - The name of this IniSection.
Throws:
InvalidNameException - if the name of the IniSection given is not considered valid by this object's IniValidator.

AdvancedIniSection

public AdvancedIniSection(String name,
                          IniValidator validator)

Default constructor which creates a new instance of this IniSection and sets the IniSection to have a default IniValidator and to be case insensitive.

Parameters:
name - The name of this IniSection.
validator - The validator of this IniSection.
Throws:
InvalidNameException - if the name of the IniSection given is not considered valid by this object's IniValidator.

AdvancedIniSection

public AdvancedIniSection(String name,
                          boolean caseSensitive)

Default constructor which creates a new instance of this IniSection and sets the IniSection to have a default IniValidator and to be case insensitive.

Parameters:
name - The name of this IniSection.
caseSensitive - Sets whether this IniSection is case sensitive.
Throws:
InvalidNameException - if the name of the IniSection given is not considered valid by this object's IniValidator.

AdvancedIniSection

public AdvancedIniSection(String name,
                          IniValidator validator,
                          boolean caseSensitive)

Default constructor which creates a new instance of this IniSection and sets the IniSection to have a default IniValidator and to be case insensitive.

Parameters:
name - The name of this IniSection.
validator - The validator of this IniSection.
caseSensitive - Sets whether this IniSection is case sensitive.
Throws:
InvalidNameException - if the name of the IniSection given is not considered valid by this object's IniValidator.
Method Detail

addItem

public boolean addItem(IniItem item)
Description copied from class: IniSection
Adds an IniItem to this section. If an item already in the section has the same name as the one being added, then the method will not overwrite the item and will instead return false. The index of the item, if it added successfully, is unknown and depends on the implementation of this interface. Programmers should not make any assumptions on where new items are added.

Overrides:
addItem in class IniSection
Parameters:
item - the item to add
Returns:
true if the item was added successfully, false if the item already existed or could not be added

addItem

public boolean addItem(IniItem item,
                       int index)
Description copied from class: IniSection
Adds an IniItem to this section at the given index. If an item already in the section has the same name as the one being added, then the method will not overwrite the item and will instead return false.

Specified by:
addItem in class IniSection
Parameters:
item - the item to add
index - the index where to add the item, where 0 is the index of the first item. Any items that already exists at this index will be moved to index + 1. If the value is greater than the number of items within this section, then the item is appended to the end of the section.
Returns:
true if the item was added successfully, false if the item already existed or could not be added

addItem

public IniItem addItem(String name)
Description copied from class: IniSection
Adds a new IniItem to this section with the given name. If an item already in the section has an the same name, then the method returns false. The index of the item, if it added successfully, is unknown and depends on the implementation of this interface. Programmers should not make any assumptions on where new items are added.

Overrides:
addItem in class IniSection
Parameters:
name - The name of the item to add.
Returns:
A reference to the Item added to this section, or null if the item could not be created.

addItem

public IniItem addItem(String itemName,
                       int index)
Description copied from class: IniSection
Adds an IniItem to this section at the given index. If an item already in the section has an the same name, then the method returns false.

Overrides:
addItem in class IniSection
Parameters:
itemName - The name of the item to add.
index - The index where to add the item, where 0 is the index of the first item. Any items that already exists at this index will be moved to index + 1. If the value is greater than the number of items within this section, then the item is appended to the end of the section.
Returns:
A reference to the Item added to this section, or null if the item could not be created.

createItem

protected IniItem createItem(String name)
Description copied from class: IniSection

This method creates and returns a new instance of an IniItem with the same IniValidator and case sensitivity as this object.

Specified by:
createItem in class IniSection
Parameters:
name - The name of the IniItem to create.
Returns:
A new instance of an IniItem with the same IniValidator and case sensitivity as this object.

addItems

public void addItems(String... itemNames)
Description copied from class: IniSection
Add multiple new items to this section. The values of the new items will be null. If any of the given names are duplicates, or if a name is identical to an item that already exist. The index of the item, if it added successfully, is unknown and depends on the implementation of this interface. Programmers should not make any assumptions on where new items are added.

Overrides:
addItems in class IniSection
Parameters:
itemNames - the names of the items to add.

getItem

public IniItem getItem(int index)
Description copied from class: IniSection
Get the item which is at the given index.

Specified by:
getItem in class IniSection
Parameters:
index - the index of the item to retrieve.
Returns:
The item that is at the given index.

getItem

public IniItem getItem(String name)
Description copied from class: IniSection
Returns the item with the given name, or null if no item exists with the given name.

Overrides:
getItem in class IniSection
Parameters:
name - The name of the item.
Returns:
The IniItem with the given name, or null if the item doesn't exists.

getItemNames

public Collection<String> getItemNames()
Description copied from class: IniSection
This method returns a collection of all the names of all the items within this section.

Overrides:
getItemNames in class IniSection
Returns:
A collection of all the names.

getItems

public Collection<IniItem> getItems()
Description copied from class: IniSection
Get a collection of all the items within this section.

Specified by:
getItems in class IniSection
Returns:
A collection of all the items.

getNumberOfItems

public int getNumberOfItems()
Description copied from class: IniSection
Return the number of IniItems that this array has.

Overrides:
getNumberOfItems in class IniSection
Returns:
the number of items that this array has

hasItem

public boolean hasItem(IniItem item)
Description copied from class: IniSection
Predicate that returns true if an item is in this section. More specifically this method returns true if the class contains a reference to the given item, and not if this class has a similar item with the same name and value.

Overrides:
hasItem in class IniSection
Parameters:
item - The item to test.
Returns:
True if an item that matches that name exists, false otherwise.

hasItem

public boolean hasItem(String name)
Description copied from class: IniSection
Predicate that returns true if an item already exists.

Overrides:
hasItem in class IniSection
Parameters:
name - the name of the item to test
Returns:
true if an item that matches that name exists, false otherwise.

indexOf

public int indexOf(IniItem item)
Description copied from class: IniSection
Returns the index of item that is in this section. If no such item exists, then the value -1 is returned.

Specified by:
indexOf in class IniSection
Parameters:
item - The IniItem whose index will be returned.
Returns:
The index of the given item.

indexOf

public int indexOf(String name)
Description copied from class: IniSection
Returns the index of item that is in this section. If no such item exists, then the value -1 is returned.

Overrides:
indexOf in class IniSection
Parameters:
name - The name of the item whose index will be returned.
Returns:
The index of the item.

isEmpty

public boolean isEmpty()
Description copied from class: IniSection
Predicate that returns true if the section has no items, false if it does have at least one item.

Overrides:
isEmpty in class IniSection
Returns:
true or false

merge

public boolean merge(IniSection otherSection)
Description copied from class: IniSection
This method merges another IniSection to this section. All the items that the other section has will be copied over to this section providing that no item has the same name in both section.

Overrides:
merge in class IniSection
Parameters:
otherSection - the other section to merge to this section
Returns:
true if the merge was successful, false if the merge failed due to an item that had the same name in both sections.

moveItem

public void moveItem(int fromIndex,
                     int toIndex)
Description copied from class: IniSection
This method moves an item at index fromIndex to the index toIndex.

Overrides:
moveItem in class IniSection
Parameters:
fromIndex - The index of the item to move
toIndex - The index where to place the item, any items already at that index will be moved to index+1.

removeItem

public boolean removeItem(IniItem item)
Description copied from class: IniSection
Removes the item this section. If the given item does not exists in the section, then the method does nothing.

Specified by:
removeItem in class IniSection
Parameters:
item - The item to remove.
Returns:
boolean, true if the IniItem was removed, false it the given IniItem did not exists in the first place.

clone

public Object clone()
Description copied from class: IniSection

This class implements the Cloneable interface, but leaves implementation to subclasses which override this class.

Specified by:
clone in class IniSection
Returns:
An object which is an instance of class, has the same values as this object, would return true for thisObject.equals( thisObject.clone() );, but returns false for thisObject == thisObject.clone();
Since:
1.0.00

iterator

public Iterator<IniItem> iterator()