Example usage for java.util Formatter toString

List of usage examples for java.util Formatter toString

Introduction

In this page you can find the example usage for java.util Formatter toString.

Prototype

public String toString() 

Source Link

Document

Returns the result of invoking toString() on the destination for the output.

Usage

From source file:com.itemanalysis.psychometrics.irt.estimation.JointMaximumLikelihoodEstimation.java

/**
 * Joint maximum likelihood estimation is iterative. An iteration history is saved. The history includes
 * the largest change in logits and the log-likelihood at each iteration. This method provides a string
 * representation of the iteration history.
 *
 * @return a summary of the iteration history.
 *//*from  w  w w .  ja v  a  2 s. co  m*/
public String printIterationHistory() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    int index = 0;

    f.format("%10s", "Iteration");
    f.format("%5s", "");
    f.format("%17s", "Delta");
    f.format("%5s", "");
    f.format("%20s", "Log-likelihood");
    f.format("%n");
    f.format("%62s", "--------------------------------------------------------------");
    f.format("%n");
    for (IterationRecord ir : iterationHistory) {
        f.format("%10s", ++index);
        f.format("%5s", "");
        f.format("%42s", ir.toString());
        f.format("%n");
    }
    f.format("%62s", "--------------------------------------------------------------");
    f.format("%n");
    return f.toString();
}

From source file:org.xwiki.rest.resources.BaseSearchResult.java

/**
 * Search for keyword in the given scopes. Limit the search only to Pages. Search for keyword
 * /*from w  w w .j a va  2 s .co m*/
 * @param searchScopes
 * @param keywords
 * @param wikiName
 * @param space
 * @param hasProgrammingRights
 * @param number
 * @return
 * @throws QueryException
 * @throws IllegalArgumentException
 * @throws UriBuilderException
 * @throws XWikiException
 */
protected List<SearchResult> searchPages(List<SearchScope> searchScopes, String keywords, String wikiName,
        String space, boolean hasProgrammingRights, int number)
        throws QueryException, IllegalArgumentException, UriBuilderException, XWikiException {
    String database = Utils.getXWikiContext(componentManager).getDatabase();

    /* This try is just needed for executing the finally clause. */
    try {
        List<SearchResult> result = new ArrayList<SearchResult>();

        if (keywords == null) {
            return result;
        }

        Formatter f = new Formatter();

        if (space != null) {
            f.format(
                    "select distinct doc.fullName, doc.space, doc.name, doc.language from XWikiDocument as doc where doc.space = :space and ( ");
        } else {
            f.format(
                    "select distinct doc.fullName, doc.space, doc.name, doc.language from XWikiDocument as doc where ( ");
        }

        /* Look for scopes related to pages */
        int acceptedScopes = 0;
        for (int i = 0; i < searchScopes.size(); i++) {
            SearchScope scope = searchScopes.get(i);

            switch (scope) {
            case CONTENT:
                f.format("upper(doc.content) like :keywords ");
                acceptedScopes++;
                break;
            case NAME:
                f.format("upper(doc.fullName) like :keywords ");
                acceptedScopes++;
                break;
            case TITLE:
                f.format("upper(doc.title) like :keywords ");
                acceptedScopes++;
                break;
            }

            if (i != searchScopes.size() - 1) {
                f.format(" or ");
            }
        }

        /* If we don't find any scope related to pages then return empty results */
        if (acceptedScopes == 0) {
            return result;
        }

        if (hasProgrammingRights) {
            f.format(") order by doc.fullName asc");
        } else {
            f.format(
                    ") and doc.space<>'XWiki' and doc.space<>'Admin' and doc.space<>'Panels' and doc.name<>'WebPreferences' order by doc.fullName asc");
        }

        String query = f.toString();

        List<Object> queryResult = null;

        /* This is needed because if the :space placeholder is not in the query, setting it would cause an exception */
        if (space != null) {
            queryResult = queryManager.createQuery(query, Query.XWQL)
                    .bindValue("keywords", String.format("%%%s%%", keywords.toUpperCase()))
                    .bindValue("space", space).setLimit(number).execute();
        } else {
            queryResult = queryManager.createQuery(query, Query.XWQL)
                    .bindValue("keywords", String.format("%%%s%%", keywords.toUpperCase())).setLimit(number)
                    .execute();
        }

        for (Object object : queryResult) {
            Object[] fields = (Object[]) object;

            String spaceName = (String) fields[1];
            String pageName = (String) fields[2];
            String language = (String) fields[3];

            String pageId = Utils.getPageId(wikiName, spaceName, pageName);

            if (Utils.getXWikiApi(componentManager).hasAccessLevel("view", pageId)) {
                SearchResult searchResult = objectFactory.createSearchResult();
                searchResult.setType("page");
                searchResult.setId(pageId);
                searchResult.setPageFullName(Utils.getPageFullName(wikiName, spaceName, pageName));
                searchResult.setWiki(wikiName);
                searchResult.setSpace(spaceName);
                searchResult.setPageName(pageName);

                String pageUri = null;
                try {
                    if (StringUtils.isBlank(language)) {
                        pageUri = UriBuilder.fromUri(this.uriInfo.getBaseUri()).path(PageResource.class)
                                .buildFromEncoded(URLEncoder.encode(wikiName, "UTF-8"),
                                        URLEncoder.encode(spaceName, "UTF-8"),
                                        URLEncoder.encode(pageName, "UTF-8"))
                                .toString();
                    } else {
                        searchResult.setLanguage(language);
                        pageUri = UriBuilder.fromUri(this.uriInfo.getBaseUri())
                                .path(PageTranslationResource.class)
                                .buildFromEncoded(URLEncoder.encode(wikiName, "UTF-8"),
                                        URLEncoder.encode(spaceName, "UTF-8"),
                                        URLEncoder.encode(pageName, "UTF-8"), language)
                                .toString();
                    }
                } catch (UnsupportedEncodingException ex) {
                    // This should never happen, UTF-8 is always valid.
                }

                Link pageLink = new Link();
                pageLink.setHref(pageUri);
                pageLink.setRel(Relations.PAGE);
                searchResult.getLinks().add(pageLink);

                result.add(searchResult);
            }
        }

        return result;
    } finally {
        Utils.getXWikiContext(componentManager).setDatabase(database);
    }
}

From source file:com.itemanalysis.psychometrics.histogram.Histogram.java

/**
 * A string representation of the histogram. It lists the bin intervals, midpoints, and value values.
 *
 * @return histogram values for display as plain text.
 *//* w w w.jav  a2  s.  c o  m*/
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    String li = "", ui = "";

    f.format("%n");
    f.format("%64s", "                        HISTOGRAM VALUES                        ");
    f.format("%n");
    f.format("%64s", "================================================================");
    f.format("%n");
    f.format("%16s", "Lower Bound");
    f.format("%18s", "Upper Bound");
    f.format("%15s", "MidPoint");

    if (histogramType == HistogramType.FREQUENCY) {
        f.format("%15s", "Frequency");
    } else if (histogramType == HistogramType.RELATIVE_FREQUENCY) {
        f.format("%15s", "Rel. Freq.");
    } else if (histogramType == HistogramType.NORMALIZED_RELATIVE_FREQUENCY) {
        f.format("%15s", "Rel. Freq.");
    } else {
        f.format("%15s", "Density");
    }

    f.format("%n");
    f.format("%64s", "----------------------------------------------------------------");
    f.format("%n");

    Bin b = null;
    for (int i = bins.size() - 1; i > -1; i--) {
        b = bins.get(i);
        //      for(Bin b : bins){
        if (b.lowerInclusive()) {
            li = "[";
            ui = ")";
        } else {
            li = "(";
            ui = "]";
        }

        f.format("%1s", li);
        f.format("% 15.5f, ", b.getLowerBound());
        f.format("% 15.5f", b.getUpperBound());
        f.format("%1s", ui);
        f.format("% 15.5f", points[i]);
        f.format("% 15.5f", value[i]);
        f.format("%n");
    }
    f.format("%64s", "================================================================");
    f.format("%n");
    f.format("%11s", "Binwidth = ");
    f.format("% .4f", binWidth);
    f.format("%n");
    return f.toString();
}

From source file:com.itemanalysis.psychometrics.irt.estimation.JointMaximumLikelihoodEstimation.java

/**
 * This method is for displaying person parameter estimates, standard errors, and other information.
 * It will display statistic for every examinee.
 * @return/*from w  ww.j  a v a  2 s  .com*/
 */
public String printPersonStats() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    f.format("%12s", "Sum Score");
    f.format("%12s", "Theta");
    f.format("%12s", "Std. Error");
    f.format("%12s", "Extreme");
    f.format("%n");
    f.format("%50s", "--------------------------------------------------");
    f.format("%n");
    for (int i = 0; i < nPeople; i++) {
        f.format("%12.2f", sumScore[i]);
        f.format("%12.2f", theta[i]);
        f.format("%12.2f", thetaStdError[i]);
        if (extremePerson[i] == -1) {
            f.format("%12s", "MINIMUM");
        } else if (extremePerson[i] == 1) {
            f.format("%12s", "MAXIMUM");
        } else {
            f.format("%12s", "");
        }
        f.format("%n");
    }
    f.format("%50s", "--------------------------------------------------");
    f.format("%n");
    return f.toString();
}

From source file:com.itemanalysis.jmetrik.stats.irt.linking.CommonItemSummaryStatistics.java

public String printItemSummary() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    f.format("%n");
    f.format("%64s", "                    ITEM SUMMARY STATISTICS                     ");
    f.format("%n");
    f.format("%64s", "================================================================");
    f.format("%n");
    f.format("%-15s", " Form");
    f.format("%-17s", "Parameter");
    f.format("%-17s", " Mean");
    f.format("%-15s", "S.D.");
    f.format("%n");
    f.format("%64s", "----------------------------------------------------------------");
    f.format("%n");
    f.format("%-14s", " X (From)");
    f.format("%-17s", " Discrimination");
    f.format("%10.6f", xAMean.getResult());
    f.format("%6s", " ");
    f.format("%10.6f", xASd.getResult());
    f.format("%n");
    f.format("%-14s", "  ");
    f.format("%-17s", " Difficulty");
    f.format("%10.6f", xBMean.getResult());
    f.format("%6s", " ");
    f.format("%10.6f", xBSd.getResult());
    f.format("%n");
    f.format("%-14s", "  ");
    f.format("%-17s", " Guessing");
    f.format("%10.6f", xCMean.getResult());
    f.format("%6s", " ");
    f.format("%10.6f", xCSd.getResult());
    f.format("%n");
    f.format("%64s", " ");
    f.format("%n");
    f.format("%-14s", " Y (To)");
    f.format("%-17s", " Discrimination");
    f.format("%10.6f", yAMean.getResult());
    f.format("%6s", " ");
    f.format("%10.6f", yASd.getResult());
    f.format("%n");
    f.format("%-14s", "  ");
    f.format("%-17s", " Difficulty");
    f.format("%10.6f", yBMean.getResult());
    f.format("%6s", " ");
    f.format("%10.6f", yBSd.getResult());
    f.format("%n");
    f.format("%-14s", "  ");
    f.format("%-17s", " Guessing");
    f.format("%10.6f", yCMean.getResult());
    f.format("%6s", " ");
    f.format("%10.6f", yCSd.getResult());
    f.format("%n");
    f.format("%64s", "================================================================");
    f.format("%n");
    f.format("%n");
    return f.toString();
}

From source file:com.itemanalysis.psychometrics.irt.equating.IrtScaleLinking.java

public String toString() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    String gapFormat1 = "%-" + Math.max(13, precision + 4 + 5) + "s";
    String gapFormat2 = "%-" + Math.max(9, precision + 4 + 5) + "s";
    String intFormat = "%" + Math.max(13, (precision + 4)) + "." + precision + "f";
    String sclFormat = "%" + Math.max(9, (precision + 4)) + "." + precision + "f";
    f.format("%n");
    f.format("%60s", "                TRANSFORMATION COEFFICIENTS                   ");
    f.format("%n");
    f.format("%60s", "            Form X (New Form) to Form Y (Old Form)            ");
    f.format("%n");
    f.format("%63s", "===============================================================");
    f.format("%n");
    f.format("%-18s", " Method");
    f.format(gapFormat2, "Slope (A)");
    f.format("%5s", " ");
    f.format(gapFormat1, "Intercept (B)");
    f.format("%5s", " ");
    f.format(gapFormat1, "fmin");
    f.format("%n");
    f.format("%63s", "---------------------------------------------------------------");
    f.format("%n");
    f.format("%-17s", " Mean/Mean");
    f.format(sclFormat, mm.getScale());//from  www . ja  v  a2 s. c om
    f.format("%5s", " ");
    f.format(intFormat, mm.getIntercept());
    f.format("%5s", " ");
    f.format("%n");
    f.format("%-17s", " Mean/Sigma");
    f.format(sclFormat, ms.getScale());
    f.format("%5s", " ");
    f.format(intFormat, ms.getIntercept());
    f.format("%5s", " ");
    f.format("%n");
    f.format("%-17s", " Haebara");
    f.format(sclFormat, hb.getScale());
    f.format("%5s", " ");
    f.format(intFormat, hb.getIntercept());
    f.format("%5s", " ");
    f.format("%13.6f", fHB);
    f.format("%5s", " ");
    f.format("%n");
    f.format("%-17s", " Stocking-Lord");
    f.format(sclFormat, sl.getScale());
    f.format("%5s", " ");
    f.format(intFormat, sl.getIntercept());
    f.format("%5s", " ");
    f.format("%13.6f", fSL);
    f.format("%5s", " ");
    f.format("%n");
    f.format("%63s", "===============================================================");
    f.format("%n");
    return f.toString();
}

From source file:com.itemanalysis.psychometrics.irt.model.Irm3PL.java

/**
 * A string representation of the item parameters. Mainly used for printing and debugging.
 *
 * @return a string of item parameters./*from   w  w  w. java 2 s .  co m*/
 */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    String name = getName().toString();
    if (getName() != null) {
        name = getName().toString().substring(0, Math.min(18, name.length()));
    } else {
        name = "";
    }
    f.format("%-18s", name);
    f.format("%2s", "");

    String m = "";
    if (numberOfParameters == 3) {
        m = "L3";
    } else if (numberOfParameters == 2) {
        m = "L2";
    } else {
        m = "L1";
    }
    f.format("%-3s", m);
    f.format("%4s", "");

    f.format("% 4.2f", getDiscrimination());
    f.format("%1s", "");
    f.format("(%4.2f)", getDiscriminationStdError());
    f.format("%4s", "");

    f.format("% 4.2f", getDifficulty());
    f.format("%1s", "");
    f.format("(%4.2f)", getDifficultyStdError());
    f.format("%4s", "");

    if ((numberOfParameters < 3 && getGuessing() > 0) || numberOfParameters == 3) {
        f.format("% 4.2f", getGuessing());
        f.format("%1s", "");
        f.format("(%4.2f)", getGuessingStdError());
        f.format("%4s", "");
    } else {
        f.format("%13s", "");
    }

    if (getSlipping() < 1) {
        f.format("% 4.2f", getSlipping());
        f.format("%1s", "");
        f.format("(%4.2f)", getSlippingStdError());
        f.format("%4s", "");
    }
    return f.toString();

    //        //OLD==================================================================
    //        String name = "";
    //        if(getName()!=null){
    //            name = getName().toString();
    //        }
    //
    //        f.format("%10s", name);f.format("%2s", ": ");
    //        f.format("%1s", "[");
    //        f.format("% .6f", getDiscrimination()); f.format("%2s", ", ");
    //        f.format("% .6f", getDifficulty()); f.format("%2s", ", ");
    //        f.format("% .6f", getGuessing());
    //
    //        if(getSlipping()<1) {
    //            f.format("%2s", ", ");
    //            f.format("% .6f", getSlipping());
    //        }
    //        f.format("%1s", "]");
    //        f.format("%n");
    //        f.format("%10s", "");f.format("%2s", "");
    //        f.format("%1s", "(");
    //        f.format("% .6f", getDiscriminationStdError()); f.format("%2s", ", ");
    //        f.format("% .6f", getDifficultyStdError()); f.format("%2s", ", ");
    //        f.format("% .6f", getGuessingStdError());
    //        if(getSlipping()<1){
    //            f.format("%2s", ", ");
    //            f.format("% .6f", getSlippingStdError());
    //        }
    //        f.format("%1s", ")");
    //
    //        return f.toString();
}

From source file:com.itemanalysis.psychometrics.cmh.CochranMantelHaenszel.java

@Override
public String toString() {
    StringBuilder buffer = new StringBuilder();
    Formatter f = new Formatter(buffer);
    ChiSquaredDistribution chiSquare = new ChiSquaredDistribution(1.0);

    double cmh = cochranMantelHaenszel();
    Double pvalue = 1.0;/*from   w w w . ja  v a 2  s  .  co  m*/
    pvalue = 1.0 - chiSquare.cumulativeProbability(cmh);

    double commonOddsRatio = 0.0;
    double[] tempConfInt = { 0.0, 0.0 };
    double[] confInt = { 0.0, 0.0 };
    double smd = 0.0;
    double effectSize = 0.0;
    String etsClass = "";

    if (itemVariable.getType().getItemType() == ItemType.BINARY_ITEM) {
        commonOddsRatio = commonOddsRatio();
        tempConfInt = commonOddsRatioConfidenceInterval(commonOddsRatio);
        if (etsDelta) {
            effectSize = etsDelta(commonOddsRatio);
            confInt[0] = etsDelta(tempConfInt[0]);
            confInt[1] = etsDelta(tempConfInt[1]);
        } else {
            effectSize = commonOddsRatio;
            confInt[0] = tempConfInt[0];
            confInt[1] = tempConfInt[1];
        }
        etsClass = etsBinayClassification(cmh, pvalue, commonOddsRatio);
    } else if (itemVariable.getType().getItemType() == ItemType.POLYTOMOUS_ITEM) {
        smd = pF();
        tempConfInt = smdConfidenceInterval(smd);
        confInt[0] = tempConfInt[0];
        confInt[1] = tempConfInt[1];
        effectSize = pF();
        //            etsClass = etsPolytomousClassification(cmh, pvalue, effectSize);
        etsClass = smdDifClass();
    }

    f.format("%-10s", itemVariable.getName().toString());
    f.format("%2s", " ");
    //      f.format("%10s", focalCode + "/" + referenceCode);f.format("%2s", " ");
    f.format("%10.2f", cmh);
    f.format("%2s", " ");
    f.format("%7.2f", pvalue);
    f.format("%2s", " ");
    f.format("%7.0f", validSampleSize);
    f.format("%2s", " ");
    f.format("%8.2f", effectSize);
    f.format(" (%8.2f", confInt[0]);
    f.format(",%8.2f)", confInt[1]);
    f.format("%2s", " ");
    f.format("%5s", etsClass);
    f.format("%2s", " ");
    return f.toString();
}

From source file:com.itemanalysis.psychometrics.rasch.JMLE.java

/**
 * Publishes global iteration information in the jmetrik log.
 */// w w  w  . ja  v  a  2 s .  c  om
public String printIterationSummary() {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);
    int iter = 1;

    f.format("%-35s", "RASCH ANALYSIS ITERATION SUMMARY");
    f.format("%n");
    f.format("%10s", "Iteration");
    f.format("%5s", "");
    f.format("%10s", "Max Change");
    f.format("%n");
    f.format("%-35s", "===================================");
    f.format("%n");

    for (Double d : iterationDelta) {
        f.format("%10d", iter);
        f.format("%5s", "");
        f.format("%10.6f", d);
        f.format("%n");
        iter++;
    }

    if (extremeIterationDelta.size() > 0) {
        iter = 0;

        f.format("%n");
        f.format("%n");
        f.format("%-35s", "EXTREME ITEM/PERSON ITERATION SUMMARY");
        f.format("%n");
        f.format("%10s", "Iteration");
        f.format("%5s", "");
        f.format("%10s", "Max Change");
        f.format("%n");
        f.format("%-35s", "===================================");
        f.format("%n");

        for (Double d : extremeIterationDelta) {
            f.format("%10d", iter);
            f.format("%5s", "");
            f.format("%10.6f", d);
            f.format("%n");
            iter++;
        }
    }

    return f.toString();
}

From source file:com.itemanalysis.psychometrics.factoranalysis.ExploratoryFactorAnalysis.java

/**
 * Formatted output of the results. It includes the factor loadings, communalities, and unqiuenesses.
 *
 * @param precision number of decimal places to report
 * @return string of result//from   www  .ja v  a 2s.com
 */
public String printOutput(int precision) {
    StringBuilder sb = new StringBuilder();
    Formatter f = new Formatter(sb);

    int size = 24;
    String line1 = "=========================";
    String line2 = "-------------------------";
    for (int j = 0; j < nFactors; j++) {
        size = size + 10;
        line1 += "==========";
        line2 += "----------";
    }
    size = size + 10;
    line1 += "==========";
    line2 += "----------";
    size = size + 6;
    line1 += "======";
    line2 += "------";

    f.format("%36s", title);
    f.format("%n");
    f.format("%" + size + "s", line1);
    f.format("%n");
    f.format("%20s", "Name");
    f.format("%4s", "");
    for (int j = 0; j < nFactors; j++) {
        f.format("%6s", "F" + (j + 1));
        f.format("%4s", "");
    }
    f.format("%6s", "H2");
    f.format("%4s", "");
    f.format("%6s", "U2");
    f.format("%n");
    f.format("%" + size + "s", line2);
    f.format("%n");

    for (int i = 0; i < nVariables; i++) {
        f.format("%20s", "V" + (i + 1));
        f.format("%5s", "");
        for (int j = 0; j < nFactors; j++) {
            f.format("%6." + precision + "f", factorMethod.getFactorLoadingAt(i, j));
            f.format("%4s", "");
        }
        f.format("%6." + precision + "f", factorMethod.getCommunalityAt(i));
        f.format("%4s", "");
        f.format("%6." + precision + "f", factorMethod.getUniquenessAt(i));
        f.format("%n");
    }

    f.format("%" + size + "s", line1);
    f.format("%n");
    f.format("%30s", "Value of the objective function = ");
    f.format("%-8.4f", fmin);
    f.format("%n");
    f.format("%n");

    f.format("%20s", "");
    for (int j = 0; j < nFactors; j++) {
        f.format("%6s", "F" + (j + 1));
        f.format("%2s", "");
    }
    f.format("%n");

    f.format("%20s", "SS loadings");
    for (int j = 0; j < nFactors; j++) {
        f.format("%6." + precision + "f", factorMethod.getSumsOfSquaresAt(j));
        f.format("%2s", "");
    }
    f.format("%n");

    f.format("%20s", "Proportion Var");
    for (int j = 0; j < nFactors; j++) {
        f.format("%6." + precision + "f", factorMethod.getProportionOfVarianceAt(j));
        f.format("%2s", "");
    }
    f.format("%n");

    f.format("%20s", "Proportion Explained");
    for (int j = 0; j < nFactors; j++) {
        f.format("%6." + precision + "f", factorMethod.getProportionOfExplainedVarianceAt(j));
        f.format("%2s", "");
    }
    f.format("%n");

    return f.toString();
}