de.cubeisland.engine.configuration.node.ListNode Maven / Gradle / Ivy
/**
* The MIT License (MIT)
*
* Copyright (c) 2013 Anselm Brehme, Phillip Schichtel
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package de.cubeisland.engine.configuration.node;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static de.cubeisland.engine.configuration.node.ConfigPath.LIST;
public class ListNode extends ParentNode
{
private ArrayList listedNodes = new ArrayList();
public ListNode(Iterable list)
{
if (list != null)
{
for (Object object : list)
{
Node node = wrapIntoNode(object);
node.setParentNode(this);
listedNodes.add(node);
}
}
}
public ListNode(Object[] array)
{
if (array != null)
{
for (Object object : array)
{
Node node = wrapIntoNode(object);
node.setParentNode(this);
listedNodes.add(node);
}
}
}
private ListNode()
{
}
public ArrayList getListedNodes()
{
return listedNodes;
}
@Override
public List getValue()
{
return this.getListedNodes();
}
public static ListNode emptyList()
{
return new ListNode();
}
public void addNode(Node node)
{
this.listedNodes.add(node);
node.setParentNode(this);
}
@Override
protected Node setExactNode(String key, Node node)
{
if (key.startsWith(LIST))
{
try
{
int pos = Integer.valueOf(key.substring(1));
node.setParentNode(this);
return this.listedNodes.set(pos, node);
}
catch (NumberFormatException ex)
{
throw new IllegalArgumentException("Cannot set Node! Could not parse ListPath", ex);
}
catch (IndexOutOfBoundsException ex)
{
throw new IllegalArgumentException("Cannot set Node! Out of Range!", ex);
}
}
else
{
throw new IllegalArgumentException("Cannot set Node! ListPath has to start with [!");
}
}
@Override
public Node getExactNode(String key)
{
if (key.startsWith(LIST))
{
try
{
int pos = Integer.valueOf(key.substring(1));
return this.listedNodes.get(pos);
}
catch (NumberFormatException ex)
{
throw new IllegalArgumentException("Cannot get Node! Could not parse ListPath", ex);
}
catch (IndexOutOfBoundsException ex)
{
throw new IllegalArgumentException("Cannot get Node! Out of Range!", ex);
}
}
else
{
throw new IllegalArgumentException("Cannot get Node! ListPath has to start with [! | " + key);
}
}
@Override
protected Node removeExactNode(String key)
{
if (key.startsWith(LIST))
{
try
{
int pos = Integer.valueOf(key.substring(1));
return this.listedNodes.remove(pos);
}
catch (NumberFormatException ex)
{
throw new IllegalArgumentException("Cannot remove Node! Could not parse ListPath!", ex);
}
catch (IndexOutOfBoundsException ex)
{
throw new IllegalArgumentException("Cannot remove Node! Out of Range!", ex);
}
}
else
{
throw new IllegalArgumentException("Cannot remove Node! ListPath has to start with [!");
}
}
public Node setNode(IntNode keyNode, Node node)
{
return this.setExactNode(LIST + keyNode.getValue(), node);
}
@Override
public boolean isEmpty()
{
return this.listedNodes.isEmpty();
}
@Override
public boolean removeNode(Node node)
{
return this.listedNodes.remove(node);
}
@Override
public void cleanUpEmptyNodes()
{
Set nodesToRemove = new HashSet();
for (Node node : this.getListedNodes())
{
if (node instanceof ParentNode)
{
((ParentNode)node).cleanUpEmptyNodes();
if (((ParentNode)node).isEmpty())
{
nodesToRemove.add(node);
}
}
}
this.listedNodes.removeAll(nodesToRemove);
}
@Override
protected ConfigPath getPathOfSubNode(Node node, ConfigPath path)
{
int pos = this.listedNodes.indexOf(node);
if (pos == -1)
{
throw new IllegalArgumentException("Parented Node not in list!");
}
if (path == null)
{
path = ConfigPath.forName(LIST + pos);
}
else
{
path = path.asSubPath(LIST + pos);
}
if (this.getParentNode() != null)
{
return this.getParentNode().getPathOfSubNode(this, path);
}
return path;
}
@Override
public ConfigPath getPathOfSubNode(Node node)
{
return this.getPathOfSubNode(node, null);
}
@Override
public String toString()
{
StringBuilder sb = new StringBuilder("ListNode=[");
for (Node listedNode : this.listedNodes)
{
sb.append("\n- ").append(listedNode.toString());
}
sb.append("]ListEnd");
return sb.toString();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy