All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.greenpepper.repository.DocumentNode Maven / Gradle / Ivy

The newest version!
package com.greenpepper.repository;

import com.greenpepper.shaded.com.google.common.collect.TreeTraverser;

import java.util.*;


/**
 * 

DocumentNode class.

* * @author oaouattara * @version $Id: $Id */ public class DocumentNode implements Comparable, Marshalizable { /** * Allows retrieval of an Iterator using DocumentNode hierarchy. * * @see TreeTraverser#preOrderTraversal(Object) * @see TreeTraverser#postOrderTraversal(Object) */ public static final TreeTraverser traverser = new TreeTraverser() { @Override public Iterable children(DocumentNode root) { return root.getChildren(); } }; /** Constant NODE_TITLE_INDEX=0 */ final static int NODE_TITLE_INDEX = 0; /** Constant NODE_EXECUTABLE_INDEX=1 */ final static int NODE_EXECUTABLE_INDEX = 1; /** Constant NODE_CAN_BE_IMPLEMENTED_INDEX=2 */ final static int NODE_CAN_BE_IMPLEMENTED_INDEX = 2; /** Constant NODE_CHILDREN_INDEX=3 */ private final static int NODE_CHILDREN_INDEX = 3; /** * Note: this position is used for something else in a referenceNode */ private static final int NODE_URL_INDEX = 4; private String title; private boolean executable; private boolean canBeImplemented; private List children = new ArrayList(); private String URL; /** *

Constructor for DocumentNode.

* * @param title a {@link java.lang.String} object. */ public DocumentNode(String title) { this.title = title; } /** *

Getter for the field children.

* * @return a {@link java.util.List} object. */ public List getChildren() { return children; } /** *

Getter for the field title.

* * @return a {@link java.lang.String} object. */ public String getTitle() { return title; } /** *

isExecutable.

* * @return a boolean. */ public boolean isExecutable() { return executable; } /** *

setIsExecutable.

* * @param executable a boolean. */ public void setIsExecutable(boolean executable) { this.executable = executable; } /** *

canBeImplemented.

* * @return a boolean. */ public boolean canBeImplemented() { return canBeImplemented; } /** *

Setter for the field canBeImplemented.

* * @param canBeImplemented a boolean. */ public void setCanBeImplemented(boolean canBeImplemented) { this.canBeImplemented = canBeImplemented; } public void setURL(String URL) { this.URL = URL; } public String getURL() { return URL; } /** *

addChildren.

* * @param child a {@link DocumentNode} object. */ public void addChildren(DocumentNode child) { children.add(child); } /** *

hasChildren.

* * @return a boolean. */ public boolean hasChildren() { return children.size() > 0; } /** *

marshallize.

* * @return a {@link java.util.Vector} object. */ public Vector marshallize() { Vector vector = new Vector(); vector.add(NODE_TITLE_INDEX, title); vector.add(NODE_EXECUTABLE_INDEX, executable); vector.add(NODE_CAN_BE_IMPLEMENTED_INDEX, canBeImplemented); Hashtable hashtable = new Hashtable(); for (DocumentNode node : children) hashtable.put(node.getTitle(), node.marshallize()); vector.add(NODE_CHILDREN_INDEX, hashtable); return vector; } /** * Rebuilds a DocumentNode based on the given vector. *

* * @param documentNodeParams a {@link Vector} object. * @return a DocumentNode based on the given vector. */ public static DocumentNode toDocumentNode(List documentNodeParams) { DocumentNode node = new DocumentNode((String) documentNodeParams.get(NODE_TITLE_INDEX)); node.setIsExecutable((Boolean) documentNodeParams.get(NODE_EXECUTABLE_INDEX)); node.setCanBeImplemented((Boolean) documentNodeParams.get(NODE_CAN_BE_IMPLEMENTED_INDEX)); if (documentNodeParams.size() == 5) { node.setURL((String) documentNodeParams.get(NODE_URL_INDEX)); } Hashtable children = (Hashtable) documentNodeParams.get(NODE_CHILDREN_INDEX); @SuppressWarnings("unchecked") Collection> values = children.values(); for (Vector nodeParams : values) { if(nodeParams.size() > 5 ) { node.addChildren(ReferenceNode.toReferenceNode(nodeParams)); } else { node.addChildren(toDocumentNode(nodeParams)); } } return node; } /** {@inheritDoc} */ public int compareTo(Object node) { return title.compareTo(((DocumentNode)node).getTitle()); } /** {@inheritDoc} */ public boolean equals(Object o) { if(o == null || !(o instanceof DocumentNode)) { return false; } DocumentNode nodeCompared = (DocumentNode)o; return getTitle().equals(nodeCompared.getTitle()); } /** *

hashCode.

* * @return a int. */ public int hashCode() { return getTitle().hashCode(); } }