/**
 * 
 */
package normalization;

import java.util.ArrayList;
import java.util.List;

import datastructures.AttributeJoint;
import datastructures.DFJoint;
import datastructures.KeyJoint;
import datastructures.Relation;
import dependency.ADependency;
import dependency.FunctionalDependency;

/**
 * @author Pavel Nichita
 *
 */
public final class Normalization {
    
    private Normalization() {
        // Private constructor to hide the implicit public one.
    }
    
    /**
     * Calculates all attributes that are being implied by {@code attrJoint} in
     * {@code dfJoint}.
     * 
     * The algorithm used is Ullman.
     * 
     * @param attrJoint Attribute joint to who'm calculate ullman.
     * @param dfJoint Where to calculate.
     * @return an attribute joint with all the attributes implied.
     */
    public static AttributeJoint simpleUllman(AttributeJoint attrJoint, DFJoint dfJoint){
        AttributeJoint result = new AttributeJoint(attrJoint);
        boolean isChanged;
        
        do {
            isChanged = false;
            for (ADependency df:dfJoint) {
                if (df.getClass() == new FunctionalDependency().getClass() &&
                    df.getAntecedent().isContained(result) &&
                    !(df.getConsequent().isContained(result))) {
                        result.addAttributes(df.getConsequent());
                        isChanged = true;
                    }
                }
        } while (isChanged);
        
        return result;
    }

    /**
     * Returns a list of relations that comply with Boyce-Codd normal form.
     * 
     * It takes the input {@code relation} and decompose it into smaller 
     * relations that comply with Boyce-Codd criteria. If {@code auto} is 
     * {@code true} that it decompose based on first non Boyce-Codd dependency,
     * if {@code false} it gives a choice to the user.
     * 
     * @param relation The relation that must be decomposed to smaller Boyce-Codd relations.
     * @param auto Decompose based on first non Boyce-Codd dependency if true, gives user to
     * choose between posibilities.
     * @return a list of relations that comply Boyce-Codd criteria. 
     */
    public static List<Relation> normalizeBCNF(Relation relation, boolean auto) {
        List<Relation> normalizedRelation = new ArrayList<>();
        normalizedRelation.add(relation);
        List<Relation> newRelations = null;
        int option = 0;
        for (int i = 0; i < normalizedRelation.size(); i++) {
            Relation newRelation = normalizedRelation.get(i);
            List<ADependency> nonNF_DFs;
            if (!newRelation.isBCNF()) {
                nonNF_DFs = newRelation.getNonBCNFDFs();
                if (!auto) {
                    /*
                     * option = get.stdin()
                     */
                }
                newRelations = newRelation.split(nonNF_DFs.get(option));
                normalizedRelation.remove(newRelation);
                normalizedRelation.addAll(i, newRelations);
                i--;
            }
        }                       
        return normalizedRelation;
    }
    
    public static List<Relation> normalize3NF(Relation relation, boolean auto) {
        List<Relation> normalizedRelation = new ArrayList<>();
        normalizedRelation.add(relation);
        List<Relation> newRelations = null;
        KeyJoint keyJoint = relation.calculateKeyJoint();
        int option = 0;
        for (int i = 0; i < normalizedRelation.size(); i++) {
            Relation newRelation = normalizedRelation.get(i);
            List<ADependency> nonNF_DFs;
            if (!newRelation.is3NF()) {
                nonNF_DFs = newRelation.getNon3NFDFs();
                if (!auto) {
                    /*
                     * option = get.stdin()
                     */
                }
                newRelations = newRelation.split(nonNF_DFs.get(option));
                normalizedRelation.remove(newRelation);
                normalizedRelation.addAll(i, newRelations);
                i--;
            }
        }
        boolean projectionOnKey = false;
        for (Relation r : normalizedRelation) {
            if (r.getAttrJoint().containsJoinsFrom(keyJoint))
                projectionOnKey = true;
        }
        if (!projectionOnKey) {
            AttributeJoint key = keyJoint.getKey(0);
            normalizedRelation.add(relation.splitByKey(key));
        }
        return normalizedRelation;
    }
}

@@ -2387,52 +2387,8 @@
 n);%0A
-        List%3CRelation%3E newRelations = null;%0A
     
@@ -2558,33 +2558,32 @@
 ependency%3E nonNF
-_
 DFs;%0A           
@@ -2625,33 +2625,32 @@
            nonNF
-_
 DFs = newRelatio
@@ -2810,81 +2810,8 @@
   %7D%0A
-                newRelations = newRelation.split(nonNF_DFs.get(option));%0A
     
@@ -2910,33 +2910,60 @@
 l(i, newRelation
-s
+.split(nonNFDFs.get(option))
 );%0A             
@@ -3253,52 +3253,8 @@
 n);%0A
-        List%3CRelation%3E newRelations = null;%0A
     
@@ -3490,17 +3490,16 @@
 y%3E nonNF
-_
 DFs;%0A   
@@ -3556,17 +3556,16 @@
    nonNF
-_
 DFs = ne
@@ -3732,81 +3732,8 @@
   %7D%0A
-                newRelations = newRelation.split(nonNF_DFs.get(option));%0A
     
@@ -3840,17 +3840,44 @@
 Relation
-s
+.split(nonNFDFs.get(option))
 );%0A     

Fix SonarQube issues