I have to refactor this method: it seems to duplicate two code blocks with some slight variation. Any suggestions? It is a bit of a mess :)public EstimatedAttitude estimate(int numStars, Double2d starsBrf, Double2d starsMeq, Long1d starsIdsForRange,        double measerrRef, double probThresh) {    Double2d starsBrfTry = new Double2d(9, 3);    Double2d starsMeqTry = new Double2d(9, 3);    Int1d idBad = new Int1d(2);    int numBad = 0;    //Estimate the attitude using q-method    QMethodValues estimates = QMethod.compute(numStars, starsBrf, starsMeq, measerrRef);    //double loss = estimates.getLoss();    //Quaternion quat = estimates.getQuat();    //Calculate probability of getting such a large value of TASTE at random    double taste = estimates.getTaste();    double gammaP = new GammaP(0.5 * (2 * numStars - 3)).calc(0.5 * taste);    double prob = 1.0 - gammaP;    //If no error and a bad fit    if ((estimates.isValid()) && (prob < probThresh)) {        double bestTaste = taste;        //Try each star as a candidate bad star        for (int badCand = 0; badCand < numStars; badCand++) {            //Exclude candidate bad star            int loop = 0;            for (int star = 0; star < numStars; star++) {                if (star != badCand) {                    starsBrfTry.set(loop, starsBrf.get(star));                    starsMeqTry.set(loop, starsMeq.get(star));                    loop = loop + 1;                }            }            //Estimate attitude without candidate bad star            QMethodValues estimatesTry = QMethod.compute(numStars - 1, starsBrfTry, starsMeqTry, measerrRef);            double tasteTry = estimatesTry.getTaste();            //Calculate probability of getting such a large value of TASTE at random            double gammaPTry = new GammaP(0.5 * (2 * numStars - 5)).calc(0.5 * tasteTry);            double probTry = 1.0 - gammaPTry;            //If no error and we have found an acceptable value of taste that is better than            //the best so far                                 if ((estimatesTry.isValid()) && (probTry >= probThresh) && (tasteTry < bestTaste)) {                //Make a note of the details                numBad = 1;                idBad.set(0, ((Long ) starsIdsForRange.get(badCand)).intValue());                /*quat = estimatesTry.getQuat();                loss = estimatesTry.getLoss();                taste = tasteTry;*/                estimates = estimatesTry;                prob = probTry;            }        }    }    //If still no error and a bad fit    if ((estimates.isValid()) && (prob < probThresh)) {        //Initialize best value of taste        double bestTaste = taste;        //Try each pair of stars as a candidate bad star pair            //bad_cand_1 \in {0, 1,..., num_stars - 2}        for (int badCand_1 = 0; badCand_1 < numStars - 1; badCand_1++) {            //bad_cand_2 \in {bad_cand_1 + 1, bad_cand_1 + 2,..., num_stars - 1}            for (int loop_2 = 0; loop_2 < numStars - 1 - badCand_1; loop_2++) {                int bad_cand_2 = loop_2 + badCand_1 + 1;                //Exclude candidate bad star pair                int loop = 0;                for (int star = 0; star < numStars; star++) {                    if ((star != badCand_1) && (star != bad_cand_2)) {                        starsBrfTry.set(loop, starsBrf.get(star));                        starsMeqTry.set(loop, starsMeq.get(star));                        loop = loop + 1;                    }                }                //Estimate attitude without candidate bad star                QMethodValues estimatesTry = QMethod.compute(numStars - 2, starsBrfTry, starsMeqTry, measerrRef);                double tasteTry = estimatesTry.getTaste();                //Calculate probability of getting such a large value of TASTE at random                double gammaPTry = new GammaP(0.5 * (2 * numStars - 7)).calc(0.5 * tasteTry);                double probTry = 1.0 - gammaPTry;                //If no error and we have found an acceptable value of taste that is better than                //the best so far                                     if ((estimatesTry.isValid()) && (probTry >= probThresh) && (tasteTry < bestTaste)) {                    //Make a note of the details                    numBad = 2;                    idBad.set(0, ((Long ) starsIdsForRange.get(badCand_1)).intValue());                    idBad.set(1, ((Long ) starsIdsForRange.get(bad_cand_2)).intValue());                    /*quat = estimatesTry.getQuat();                    loss = estimatesTry.getLoss();                    taste = tasteTry;*/                    estimates = estimatesTry;                    prob = probTry;                }            }        }    }    //create a new qmethod value with correct fields    return new EstimatedAttitude(estimates, prob, numBad, idBad);}
Refactoring a method for estimation
java