/*
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 */

package vkc.clusterer;

/**
 * Datenstruktur zum Verwalten von Ähnlichkeit zwischen zwei Clustern re Ähnlichkeit A und B
 * Cluster A und ClusterB können auch Dokumente darstellen.
 *  Jeweils ein Object des Typs HeapEntry wird in den Heap gepackt.
 */
public class HeapEntry implements Comparable {
    //  Struktur eines Heap-Eintrages
    private double sim ;

    private Integer heapA ; //Heap-Id des Elements A
    private Integer heapB ;// Heap-ID des Elemntes B
    private Integer clusterA ; // unique-ID entpsricht der VKC-Category-ID
    private Integer clusterB ;

    HeapEntry(Integer heapA, Integer heapB, Integer clusterA, Integer clusterB, double sim) {
        super();
        this.heapA = heapA;
        this.heapB = heapB;
        this.clusterA = clusterA;
        this.clusterB = clusterB;
        this.sim = sim;
    }
    
    /* (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof HeapEntry)) {
            return false;
        }

        HeapEntry h = (HeapEntry)obj;
        
        // Betrachte HeapEntry(i,j,sim) == HeapEntry(j,i,sim) 
        return ((getClusterA().equals(h.getClusterA()) && getClusterB().equals(h.getClusterB())) || (getClusterA().equals(h.getClusterB()) && getClusterB().equals(h.getClusterA()))) ;
    }
    
    /**
     * @return Returns the clusterA.
     */
    public Integer getClusterA() {
        return clusterA;
    }
    
    /**
     * @param clusterA The clusterA to set.
     */
    public void setClusterA(Integer clusterA) {
        this.clusterA = clusterA;
    }
    
    /**
     * @return Returns the clusterB.
     */
    public Integer getClusterB() {
        return clusterB;
    }
    
    /**
     * @param clusterB The clusterB to set.
     */
    public void setClusterB(Integer clusterB) {
        this.clusterB = clusterB;
    }
    
    /**
     * @return Returns the sim.
     */
    public double getSim() {
        return sim;
    }
    
    /**
     * @param sim The sim to set.
     */
    public void setSim(double sim) {
        this.sim = sim;
    }

    public String toString() {
        return "CLUSTER_A = " + getClusterA() + " -- CLUSTER_B = " + getClusterB();
    }
    
    // Berechnet den Index des Eintrages für IndexedHeap
    public int getHeapIndex() {
        int c1 = getHeapA().intValue();
        int c2 = getHeapB().intValue();
        
        return c1 > c2 ? c1 * (c1 + 1) / 2 + c2 - c1 : c2 * (c2 + 1) / 2 + c1 - c2 ;        
    }

    public int compareTo(Object o) {
        if (o == null) {
            return 1;
        }
        
        if (!(o instanceof HeapEntry)) {
            throw new ClassCastException();
        }
        
        HeapEntry he = (HeapEntry)o;

        if (this.sim > he.sim) {
            return 1;
        }

        if (this.sim < he.sim) {
            return -1;
        }
        
        return 0;
    }
    
    /**
     * @return Returns the heapA.
     */
    public Integer getHeapA() {
        return heapA;
    }

    /**
     * @param heapA The heapA to set.
     */
    public void setHeapA(Integer heapA) {
        this.heapA = heapA;
    }
    
    /**
     * @return Returns the heapB.
     */
    public Integer getHeapB() {
        return heapB;
    }
    
    /**
     * @param heapB The heapB to set.
     */
    public void setHeapB(Integer heapB) {
        this.heapB = heapB;
    }
}