Example usage for org.apache.commons.lang StringUtils leftPad

List of usage examples for org.apache.commons.lang StringUtils leftPad

Introduction

In this page you can find the example usage for org.apache.commons.lang StringUtils leftPad.

Prototype

public static String leftPad(String str, int size) 

Source Link

Document

Left pad a String with spaces (' ').

Usage

From source file:xc.mst.repo.RepositoryDAO.java

private double getMemUsage() {
    Runtime r = Runtime.getRuntime();
    long maxMem = r.maxMemory() / 1048576;
    long totalMem = r.totalMemory() / 1048576;

    long freeBytes = r.freeMemory();
    long freeMem = freeBytes / 1048576;

    long usedMem = totalMem - freeMem;
    double percentageUsed = ((double) usedMem) / maxMem;

    LOG.debug("");
    LOG.debug("Free  memory: " + StringUtils.leftPad(freeMem + "", 7) + " MB.");
    LOG.debug("Used  memory: " + StringUtils.leftPad(usedMem + "", 7) + " MB.");
    LOG.debug("Total memory: " + StringUtils.leftPad(totalMem + "", 7) + " MB.");
    LOG.debug("Max'm memory: " + StringUtils.leftPad(maxMem + "", 7) + " MB.");
    LOG.debug("memory percentageUsed: " + StringUtils.leftPad(Double.toString(percentageUsed), 7));

    return percentageUsed;
}

From source file:xc.mst.repo.RepositoryDAO.java

public String getRecordStatsByType(String name) {
    StringBuilder sb = new StringBuilder();

    List<Map<String, Object>> otherRows = new ArrayList<Map<String, Object>>();
    List<Map<String, Object>> totalsRows = this.jdbcTemplate.queryForList("select status, count(*) c from "
            + getTableName(name, RECORDS_TABLE) + " group by status order by status");
    List<Map<String, Object>> rowsByType = null;

    // the below returns something like:
    //+------+--------+---------+
    //| type | status | c       |
    //+------+--------+---------+
    //| e    | A      | 2808321 |
    //| h    | A      | 2791443 |
    //| h    | H      |   10931 |
    //| m    | A      | 2630614 |
    //| w    | A      | 2808321 |
    //+------+--------+---------+
    ////from  w w  w.  j  av a2s. c  o  m
    // those results will be used in the record counts summary displayed in a service's log file:
    //     e-active:   2,808,321                      h-active:   2,791,443                        h-held:      10,931
    //     m-active:   2,630,614                      w-active:   2,808,321
    // total-active:  11,038,699                    total-held:      10,931
    //
    // (as contrasted with grabbing the data from the outgoing_record_counts table)
    //
    if (isServiceRepo(name)) {
        rowsByType = this.jdbcTemplate.queryForList("select type, status, count(*) c from "
                + getTableName(name, RECORDS_TABLE) + " group by type, status order by type, status");
    } else {
        rowsByType = this.jdbcTemplate.queryForList("select rs.set_id type, r.status status, count(*) c "
                + "from " + getTableName(name, RECORDS_SETS_TABLE) + " rs, " + getTableName(name, RECORDS_TABLE)
                + " r " + " where r.record_id = rs.record_id group by rs.set_id, r.status");

        for (Map<String, Object> row : totalsRows) {
            Map<String, Object> m = new HashMap<String, Object>();
            m.put("status", row.get("status"));
            m.put("c", row.get("c"));
            m.put("type", "unknown");
            otherRows.add(m);
        }
        List<Map<String, Object>> rows2remove = new ArrayList<Map<String, Object>>();
        for (Map<String, Object> row : rowsByType) {
            LOG.debug("row.get(type): " + row.get("type"));
            if (row.get("type") != null)
                LOG.debug("row.get(type).getClass(): " + row.get("type").getClass());
            if (row.get("type") instanceof Integer) {
                try {
                    String type = getSetDAO().getById((Integer) row.get("type")).getSetSpec();
                    row.put("type", type);
                    if (!type.contains(":")) {
                        rows2remove.add(row);
                        continue;
                    }
                } catch (Throwable t) {
                    getUtil().throwIt(t);
                }
            }
            String status = (String) row.get("status");
            Map<String, Object> m = null;
            for (Map<String, Object> otherRow : otherRows) {
                if (status.equals(otherRow.get("status"))) {
                    m = otherRow;
                    break;
                }
            }
            m.put("c", ((Long) m.get("c")) - ((Long) row.get("c")));
        }
        rowsByType.removeAll(rows2remove);
    }
    for (List<Map<String, Object>> rows : new List[] { rowsByType, otherRows, totalsRows }) {
        int col = 0;
        for (Map<String, Object> row : rows) {
            String type = "total";
            if (row.containsKey("type")) {
                if (StringUtils.isEmpty((String) row.get("type"))) {
                    type = RecordCounts.OTHER;
                } else {
                    type = (String) row.get("type");
                }
            }
            if (col == 0)
                sb.append("\n");
            sb.append(StringUtils
                    .leftPad(type + "-" + Record.statusNames.get(((String) row.get("status")).charAt(0)), 30));
            sb.append(":");
            DecimalFormat myFormatter = new DecimalFormat("###,###,###");
            sb.append(StringUtils.leftPad(myFormatter.format(getUtil().getLongPrim(row.get("c"))), 12));
            if (++col == 3) {
                col = 0;
            }
        }
    }
    return sb.toString();
}

From source file:xc.mst.utils.TimingStats.java

public void log(String name, String message, boolean suppressOutput) {
    if (TimingStats.LOG.isDebugEnabled() && !manualShutOff) {
        long tnow = System.currentTimeMillis();
        if (name == null) {
            name = DEFAULT;// w w w . j a v  a  2s .c o  m
        }

        Long lastTime = namedLastTimes.get(name);
        long incrTime = 0;
        if (lastTime == null) {
            lastTime = 0l;
        } else {
            incrTime = tnow - lastTime;
        }

        Long beginTime = namedBeginTimes.get(name);
        if (beginTime == null) {
            beginTime = tnow;
            namedBeginTimes.put(name, beginTime);
        }

        if (!suppressOutput) {
            String thisTime = StringUtils.leftPad((incrTime) + "", 6);
            String totalTime = StringUtils.leftPad((tnow - beginTime) + "", 7);

            StringBuilder sb = getStringBuilder();
            sb.append("TimingLogger! " + thisTime + " " + totalTime + " : " + message);
            LOG.debug(sb.toString());
        }
        if (name != DEFAULT) {
            Timer timer = (Timer) namedTimers.get(name);
            if (timer == null) {
                timer = new Timer();
                namedTimers.put(name, timer);
            }
            timer.totalTime.addAndGet(incrTime);
            if (timer.longestTime < incrTime) {
                timer.longestTime = incrTime;
            }
            timer.numTimes.incrementAndGet();
        }
        namedLastTimes.put(name, tnow);
    }
}

From source file:xc.mst.utils.TimingStats.java

private void outputMemory(boolean runGC) {
    if (runGC) {/* ww  w  . ja  v  a  2 s . c o m*/
        System.gc();
    }
    Runtime r = Runtime.getRuntime();
    long maxMem = r.maxMemory() / 1048576;
    long totalMem = r.totalMemory() / 1048576;

    long freeBytes = r.freeMemory();
    long freeMem = freeBytes / 1048576;

    long usedMem = totalMem - freeMem;
    long memIncrease = usedMem - memUsedAtLastReset;
    memUsedAtLastReset = usedMem;

    LOG.debug("");
    LOG.debug("Free  memory: " + StringUtils.leftPad(freeMem + "", 7) + " MB.");
    LOG.debug("Used  memory: " + StringUtils.leftPad(usedMem + "", 7) + " MB.");
    LOG.debug("Increased by: " + StringUtils.leftPad(memIncrease + "", 7) + " MB.");
    LOG.debug("Total memory: " + StringUtils.leftPad(totalMem + "", 7) + " MB.");
    LOG.debug("Max'm memory: " + StringUtils.leftPad(maxMem + "", 7) + " MB.");

    //double percentageUsed = ((double) usedMem) / maxMem;
    //System.out.println("percentageUsed = " + percentageUsed);
    //MemoryWarningSystem.setPercentageUsageThreshold(0.8);  // 80%
}

From source file:xc.mst.utils.TimingStats.java

protected void reset(String name) {
    displayPerformanceData = false;/* w w w.j  a  v a2 s  .c  o  m*/
    batchSize = 0l;

    StringBuilder sb = getStringBuilder();
    Timer timer = (Timer) namedTimers.get(name);
    if (timer != null) {
        String totalTime = StringUtils.leftPad(timer.totalTime.get() + "", 10);
        if (indentation.containsKey(name)) {
            // LOG.debug("indentation.get("+name+"):"+ indentation.get(name));
            StringBuilder sb2 = new StringBuilder();
            for (int i = 0; i < indentation.get(name); i++) {
                sb2.append("  ");
            }
            name = sb2.append(name).toString();
        }
        String avgTime = "";
        if (timer.numTimes.get() != 0) {
            double avg = (0. + timer.totalTime.get()) / timer.numTimes.get();
            avgTime = String.format("%.2f", avg);
        } else {
            avgTime = "n/a";
        }
        avgTime = StringUtils.leftPad(avgTime, 12);
        String longestTime = StringUtils.leftPad(timer.longestTime + "", 9);
        String num = StringUtils.leftPad(timer.numTimes + "", 7);
        sb.append("TimingLogger! total: " + totalTime + "    avg:" + avgTime + "    longest:" + longestTime
                + "    num:" + num + "   " + name);
        LOG.debug(sb);
    }
}