Example usage for org.apache.commons.collections.keyvalue MultiKey getKeys

List of usage examples for org.apache.commons.collections.keyvalue MultiKey getKeys

Introduction

In this page you can find the example usage for org.apache.commons.collections.keyvalue MultiKey getKeys.

Prototype

public Object[] getKeys() 

Source Link

Document

Gets a clone of the array of keys.

Usage

From source file:it.geosolutions.unredd.stats.impl.StatsRunner.java

protected void outputStats(Map<MultiKey, List<Result>> results) {

    boolean rangeAlreadyLogged = false;

    for (MultiKey classes : results.keySet()) {

        StringBuilder sb = new StringBuilder();

        //= append all classes
        for (Object o : classes.getKeys()) {
            //                LOGGER.info("Adding class " + o);
            sb.append(o).append(cfg.getOutput().getSeparator());
        }/* w w  w  .  j a  v  a  2  s.c om*/

        //= append stats in the requested order
        List<Result> resultList = results.get(classes);
        int statsnum = statsIndexes.size() + (countIndex != null ? 1 : 0);
        //            LOGGER.info(statsnum + " stats requested");
        // prefill outval
        List<Double> outval = new ArrayList<Double>(statsnum);
        for (int i = 0; i < statsnum; i++) {
            outval.add(Double.NaN);
        }

        // fill count stat
        if (countIndex != null) { // COUNT is not a real stat: set it by hand
            Long l = resultList.get(0).getNumAccepted();
            outval.set(countIndex, l.doubleValue());
        }

        // fill computed stats
        for (Result result : resultList) {
            //                LOGGER.info(result);
            Integer idx = statsIndexes.get(result.getStatistic());
            if (idx == null) {
                if (LOGGER.isDebugEnabled()) {
                    if (result.getStatistic() != Statistic.RANGE || !rangeAlreadyLogged) { // display range warning only once
                        LOGGER.debug("Encountered stat not requested (" + result.getStatistic().name() + "): "
                                + result);
                    }
                    rangeAlreadyLogged = result.getStatistic() == Statistic.RANGE;
                } // log
                continue;
            }
            //                 else
            //                    LOGGER.info("Adding " + result.getStatistic());
            outval.set(idx, result.getValue());
        }

        //= put stats in output line
        for (Iterator<Double> it = outval.iterator(); it.hasNext();) {
            Double val = it.next();
            if (!Double.isNaN(val))
                sb.append(val);
            else
                sb.append(cfg.getOutput().getNanValue());

            if (it.hasNext())
                sb.append(cfg.getOutput().getSeparator());
        }

        output(sb);
    }

    closeOutputFile();
    CSVConverter.convertFromCSV(cfg);
}

From source file:it.geosolutions.unredd.stats.impl.StatsRunner.java

protected void pivot(Map<MultiKey, List<Result>> results, int index, List<Double> pivotClasses) {

    // This map contains all results
    //   - MultiKeys here are built by all the Keys in the original MultiKey except the pivotted one
    //   - Key of the inner map is the class of the pivotted classification
    //   - Value of the inner map is the result of the single stat requested
    Map<MultiKey, Map<Double, Double>> pivotted = new HashMap<MultiKey, Map<Double, Double>>();

    // Regroup results by subkeys
    for (MultiKey classes : results.keySet()) {
        List<Result> result = results.get(classes);
        if (result.size() != 1)
            throw new IllegalStateException();
        Double value = result.get(0).getValue(); // FIXME: this won't work with COUNT statistic

        // Create subKey (all keys except the pivot one)
        int newkeys = classes.getKeys().length - 1;
        Object subkeys[] = new Object[newkeys];
        for (int i = 0; i < newkeys; i++) {
            subkeys[i] = classes.getKeys()[i + (i >= index ? 1 : 0)];
        }/*  w  ww  . j a va 2 s  .  c  om*/
        MultiKey subKey = new MultiKey(subkeys);
        Double pivotKey = ((Number) classes.getKeys()[index]).doubleValue();

        Map<Double, Double> submap;
        if (pivotted.containsKey(subKey)) {
            submap = pivotted.get(subKey);
        } else {
            submap = new HashMap<Double, Double>();
            pivotted.put(subKey, submap);
        }

        submap.put(pivotKey, value);
    }

    LOGGER.info("Pivotted " + pivotted.size() + " subkeys");

    // build the output string
    for (MultiKey subKey : pivotted.keySet()) {
        StringBuilder sb = new StringBuilder();

        // append classifications' classes
        for (Object object : subKey.getKeys()) {
            sb.append(object.toString()).append(cfg.getOutput().getSeparator());
        }
        // append pivotted data
        Map<Double, Double> pvalues = pivotted.get(subKey);
        // export data in the requested order
        for (Iterator<Double> it = pivotClasses.iterator(); it.hasNext();) {
            Double pRequestedClass = it.next();
            Double pval = pvalues.get(pRequestedClass);
            if (pval != null)
                sb.append(pval);
            else
                sb.append(cfg.getOutput().getMissingValue());
            if (it.hasNext())
                sb.append(cfg.getOutput().getSeparator());
        }

        output(sb);
    }

    closeOutputFile();
}

From source file:com.nextep.designer.sqlgen.mysql.impl.MySqlCapturer.java

/**
 * Builds the map of unique keys for the specified table. Note that the
 * fetched unique keys will be added to the table
 * //from   ww w.  ja  v a2 s .c o  m
 * @param md
 *            {@link DatabaseMetaData} of the underlying database connection
 * @param monitor
 *            a {@link IProgressMonitor} to report progress to
 * @param table
 *            the {@link IBasicTable} to fetch unique keys for
 * @param columnsMap
 *            the map of {@link IBasicColumn} hashed by their unique name
 * @return a map of {@link IKeyConstraint} hashed by their unique name
 * @throws SQLException
 */
private Map<String, IKeyConstraint> buildUniqueKeyMap(DatabaseMetaData md, IProgressMonitor monitor,
        IBasicTable table, Map<String, IBasicColumn> columnsMap) throws SQLException {
    final Map<String, IKeyConstraint> keysMap = new HashMap<String, IKeyConstraint>();
    final String tabName = table.getName();
    ResultSet rset = null;
    try {
        // Creating primary keys for this table
        rset = md.getPrimaryKeys(null, null, tabName);
        IKeyConstraint uk = null;
        List<MultiKey> pkCols = new ArrayList<MultiKey>();
        // Because JDBC may not give us a sorted list, we first fill
        // a list with all pk columns, we sort it by KEY_SEQ, and we
        // fill our neXtep PK.
        while (rset.next()) {
            monitor.worked(1);
            final String pkName = rset.getString("PK_NAME"); //$NON-NLS-1$
            final String colName = rset.getString("COLUMN_NAME"); //$NON-NLS-1$
            final int colIndex = rset.getInt("KEY_SEQ") - 1; //$NON-NLS-1$
            pkCols.add(new MultiKey(pkName, colIndex, colName));
        }
        Collections.sort(pkCols, new Comparator<MultiKey>() {

            @Override
            public int compare(MultiKey o1, MultiKey o2) {
                if ((Integer) o1.getKeys()[1] > (Integer) o2.getKeys()[1]) {
                    return 1;
                }
                return -1;
            }
        });
        for (MultiKey pkCol : pkCols) {
            final String pkName = (String) pkCol.getKey(0);
            final String colName = (String) pkCol.getKey(2);

            monitor.worked(1);
            if (uk == null) {
                uk = new UniqueKeyConstraint(pkName, "", table); //$NON-NLS-1$
                uk.setConstraintType(ConstraintType.PRIMARY);
                table.addConstraint(uk);
                keysMap.put(tabName.toUpperCase(), uk);
            }
            // Retrieving UK column and adding it to UK
            final String columnKey = CaptureHelper.getUniqueObjectName(tabName, colName);
            final IBasicColumn ukColumn = columnsMap.get(columnKey);
            if (ukColumn != null) {
                uk.addColumn(ukColumn);
            } else {
                LOGGER.warn(MessageFormat.format(MySQLMessages.getString("capturer.mysql.uniqueKeyNotFound"), //$NON-NLS-1$
                        columnKey));
            }

        }
    } finally {
        CaptureHelper.safeClose(rset, null);
    }
    return keysMap;
}