/*
 * Copyright (c) 2012 Mateusz Parzonka, Eric Bodden
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Mateusz Parzonka - initial API and implementation
 */
package prm4j.indexing.staticdata;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

import prm4j.api.Parameter;
import prm4j.indexing.realtime.DefaultNode;
import prm4j.indexing.realtime.Node;
import prm4j.indexing.realtime.NodeMap;

/**
 * Every {@link Node} is equipped with a MetaNode, containing factory methods and providing statically computed
 * algorithm logic.
 *
 */
public class MetaNode {

    private MetaNode[] successors;
    private ChainData[] chainData;
    private Set<Parameter<?>> parameterSet;
    private boolean isConfigured = false;
    private int monitorSetCount;

    public MetaNode(Set<Parameter<?>> parameterSet) {
	super();
	this.parameterSet = parameterSet;
    }

    public ChainData[] getChainData() {
	return chainData;
    }

    public Node createNode() {
	return new DefaultNode(this);
    }

    public Node createNode(int parameterId) {
	return successors[parameterId].createNode();
    }

    public NodeMap createNodeMap() {
	// TODO Auto-generated method stub
	return null;
    }

    void setChainData(ChainData[] chainData) {
	this.chainData = chainData;
    }

    public MetaNode getMetaNode(Parameter<?> parameter) {
	MetaNode node = successors[parameter.getIndex()];
	if (node == null) {
	    Set<Parameter<?>> parameterSet = new HashSet<Parameter<?>>();
	    parameterSet.addAll(this.parameterSet);
	    assert !parameterSet.contains(parameter) : "Parameter set could not have had contained new parameter.";
	    parameterSet.add(parameter);
	    node = new MetaNode(parameterSet);
	    successors[parameter.getIndex()] = node;
	}
	return node;
    }

    public Set<Parameter<?>> getParameterSet() {
        return parameterSet;
    }

    boolean isConfigured() {
	return isConfigured;
    }

    void setConfigured(boolean isConfigured) {
	this.isConfigured = isConfigured;
    }

    public int getMonitorSetCount() {
	return monitorSetCount;
    }

    public void setMonitorSetCount(int monitorSetCount) {
	this.monitorSetCount = monitorSetCount;
    }

    @Override
    public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result + Arrays.hashCode(chainData);
	result = prime * result + (isConfigured ? 1231 : 1237);
	result = prime * result + monitorSetCount;
	result = prime * result + ((parameterSet == null) ? 0 : parameterSet.hashCode());
	result = prime * result + Arrays.hashCode(successors);
	return result;
    }

    @Override
    public boolean equals(Object obj) {
	if (this == obj)
	    return true;
	if (obj == null)
	    return false;
	if (getClass() != obj.getClass())
	    return false;
	MetaNode other = (MetaNode) obj;
	if (!Arrays.equals(chainData, other.chainData))
	    return false;
	if (isConfigured != other.isConfigured)
	    return false;
	if (monitorSetCount != other.monitorSetCount)
	    return false;
	if (parameterSet == null) {
	    if (other.parameterSet != null)
		return false;
	} else if (!parameterSet.equals(other.parameterSet))
	    return false;
	if (!Arrays.equals(successors, other.successors))
	    return false;
	return true;
    }

    @Override
    public String toString() {
	return "MetaNode [successors=" + Arrays.toString(successors) + ", chainData=" + Arrays.toString(chainData)
		+ ", parameterSet=" + parameterSet + ", isConfigured=" + isConfigured + ", monitorSetCount="
		+ monitorSetCount + "]";
    }

}

@@ -811,16 +811,22 @@
 private 
+final 
 MetaNode
@@ -897,33 +897,37 @@
 t%3CParameter%3C?%3E%3E 
-p
+fullP
 arameterSet;%0A   
@@ -1029,33 +1029,37 @@
 t%3CParameter%3C?%3E%3E 
-p
+fullP
 arameterSet) %7B%0A%09
@@ -1069,25 +1069,29 @@
 er();%0A%09this.
-p
+fullP
 arameterSet 
@@ -1092,17 +1092,66 @@
 erSet = 
-p
+fullParameterSet;%0A%09successors = new MetaNode%5BfullP
 arameter
@@ -1145,32 +1145,40 @@
 fullParameterSet
+.size()%5D
 ;%0A    %7D%0A%0A    pub
@@ -1807,14 +1807,13 @@
 All(
-this.p
+fullP
 aram
@@ -2128,24 +2128,21 @@
 ) %7B%0A
-        
+%09
 return 
-p
+fullP
 aram
@@ -2747,17 +2747,21 @@
 ult + ((
-p
+fullP
 arameter
@@ -2779,17 +2779,21 @@
 ) ? 0 : 
-p
+fullP
 arameter
@@ -3302,17 +3302,21 @@
 e;%0A%09if (
-p
+fullP
 arameter
@@ -3341,25 +3341,29 @@
   if (other.
-p
+fullP
 arameterSet 
@@ -3400,17 +3400,21 @@
 se if (!
-p
+fullP
 arameter
@@ -3430,17 +3430,21 @@
 s(other.
-p
+fullP
 arameter
@@ -3736,17 +3736,21 @@
 Set=%22 + 
-p
+fullP
 arameter

Rename field