/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira
 */
package net.ranides.assira.xml;

import java.util.Map;
import net.ranides.assira.enums.EnumInt;
import net.ranides.assira.enums.EnumUtils;
import org.w3c.dom.Node;

/**
 *
 * @author ranides
 */
public enum NodeType implements EnumInt {
    
    /** The node is an <code>Element</code>. */
    ELEMENT(1),
    
    /** The node is an <code>Attr</code>. */
    ATTRIBUTE(2),
    
    /** The node is a <code>Text</code> node. */
    TEXT(3),
    
    /** The node is a <code>CDATASection</code>. */
    CDATA_SECTION(4),
    
    /** The node is an <code>EntityReference</code>. */
    ENTITY_REFERENCE(5),
    
    /** The node is an <code>Entity</code>. */
    ENTITY(6),
    
    /** The node is a <code>ProcessingInstruction</code>.*/
    PROCESSING_INSTRUCTION(7),
    
    /** The node is a <code>Comment</code>.*/
    COMMENT(8),
    
    /** The node is a <code>Document</code>.*/
    DOCUMENT(9),
    
    /** The node is a <code>DocumentType</code>.*/
    DOCUMENT_TYPE(10),
    
    /** The node is a <code>DocumentFragment</code>.*/
    DOCUMENT_FRAGMENT(11),
    
    /** The node is a <code>Notation</code>.*/
    NOTATION(12)
    
    ;
    
    private static final Map<Integer, NodeType> MAP = EnumUtils.mapInt(NodeType.class);
    
    private final int value;

    private NodeType(int value) {
        this.value = value;
    }

    @Override
    public int toEnumKey() {
        return value;
    }
    
    public boolean match(Node node) {
        return match(node.getNodeType());
    }
    
    public boolean match(int value) {
        return this.value == value;
    }
    
    public static NodeType valueOf(Node node) {
        return valueOf(node.getNodeType());
    }
    
    public static NodeType valueOf(int value) {
        NodeType item = MAP.get(value);
        if(null == item) {
            throw new IllegalArgumentException("Unknown value: " + value);
        }
        return item;
    }
}
