List of usage examples for org.apache.commons.lang StringUtils rightPad
public static String rightPad(String str, int size, String padStr)
Right pad a String with a specified String.
From source file:com.ancientprogramming.fixedformat4j.format.impl.AbstractDecimalFormatter.java
public String asString(T obj, FormatInstructions instructions) { BigDecimal roundedValue = null; int decimals = instructions.getFixedFormatDecimalData().getDecimals(); if (obj != null) { BigDecimal value = obj instanceof BigDecimal ? (BigDecimal) obj : BigDecimal.valueOf(obj.doubleValue()); RoundingMode roundingMode = instructions.getFixedFormatDecimalData().getRoundingMode(); roundedValue = value.setScale(decimals, roundingMode); if (LOG.isDebugEnabled()) { LOG.debug("Value before rounding = '" + value + "', value after rounding = '" + roundedValue + "', decimals = " + decimals + ", rounding mode = " + roundingMode); }/* w w w. ja va 2s . c o m*/ } DecimalFormat formatter = new DecimalFormat(); formatter.setDecimalSeparatorAlwaysShown(true); formatter.setMaximumFractionDigits(decimals); char decimalSeparator = formatter.getDecimalFormatSymbols().getDecimalSeparator(); char groupingSeparator = formatter.getDecimalFormatSymbols().getGroupingSeparator(); String zeroString = "0" + decimalSeparator + "0"; String rawString = roundedValue != null ? formatter.format(roundedValue) : zeroString; if (LOG.isDebugEnabled()) { LOG.debug("rawString: " + rawString + " - G[" + groupingSeparator + "] D[" + decimalSeparator + "]"); } rawString = rawString.replaceAll("\\" + groupingSeparator, ""); boolean useDecimalDelimiter = instructions.getFixedFormatDecimalData().isUseDecimalDelimiter(); String beforeDelimiter = rawString.substring(0, rawString.indexOf(decimalSeparator)); String afterDelimiter = rawString.substring(rawString.indexOf(decimalSeparator) + 1, rawString.length()); if (LOG.isDebugEnabled()) { LOG.debug("beforeDelimiter[" + beforeDelimiter + "], afterDelimiter[" + afterDelimiter + "]"); } //trim decimals afterDelimiter = StringUtils.substring(afterDelimiter, 0, decimals); afterDelimiter = StringUtils.rightPad(afterDelimiter, decimals, '0'); String delimiter = useDecimalDelimiter ? "" + instructions.getFixedFormatDecimalData().getDecimalDelimiter() : ""; String result = beforeDelimiter + delimiter + afterDelimiter; if (LOG.isDebugEnabled()) { LOG.debug("result[" + result + "]"); } return result; }
From source file:com.pureinfo.srm.project.action.CardIdGenerateAction.java
private void getCardId() throws PureException { String sCampus = null;/*w w w .j av a 2 s.c o m*/ String sOrgCode = null; String sPrjSort = null; String sAS = null; try { sCampus = request.getRequiredParameter("campus", ""); sAS = request.getRequiredParameter("administerSection", ""); int nOrgId = request.getRequiredInt("department", ""); try { IOrganizationMgr mgr = (IOrganizationMgr) ArkContentHelper.getContentMgrOf(Organization.class); Organization org = (Organization) mgr.lookupById(nOrgId); if (org == null) { m_sCardId = "ERROR"; m_sMessage = ""; return; } sOrgCode = org.getCode(); sOrgCode = StringUtils.rightPad(sOrgCode, 5, '0'); } catch (PureException ex2) { // TODO Auto-generated catch block ex2.printStackTrace(System.err); } sPrjSort = request.getRequiredParameter("prjSortId", ""); } catch (RuntimeException ex) { m_sCardId = "ERROR"; m_sMessage = ""; return; } CardIdGenerator cg = CardIdGenerator.instance(sCampus, sOrgCode, sPrjSort, sAS); try { m_sCardId = cg.getNextId(); if ("true".equals(request.getParameter("short")) && SRMConstants.ADMIN_SECTION_DIQI.equals(sAS)) { m_sCardId = m_sCardId.substring(0, m_sCardId.length() - 2); } int nRound = cg.getRound(); m_sMessage = (nRound == 0 ? "" : "" + (nRound + 1) + ""); } catch (PureException ex1) { if (ex1.getErrNo() == SRMExceptionTypes.PRJ_CARD_ID_FULL) { m_sCardId = "OVERFLOW"; m_sMessage = ex1.getMessage(); } else { m_sCardId = "ERROR"; m_sMessage = ""; } } }
From source file:net.big_oh.common.flatfile.FlatFileRecordBuilder.java
/** * Builds a single record (i.e. line) in the flat file using the collection * of {@link FlatFileRecordField} objects. * //from w w w .j av a 2 s . c o m * @param fieldsForRecord * The FlatFileRecordField objects used to construct a single * record for the flat file. * @param fieldDelimiter * An optional character that can be placed between fields in the * flat file record. * @param interFieldPadChar * The character used to fill any spaces that do not correspond * to a field in the flat file record. * @return A single record (i.e. line) to be included in a flat file. * @throws IllegalArgumentException * Thrown if the Collection of FlatFileRecordField objects * overlap. */ public static String buildFlatFileRecord(Collection<FlatFileRecordField> fieldsForRecord, String fieldDelimiter, char interFieldPadChar) throws IllegalArgumentException { StringBuffer recordContent = new StringBuffer(); List<FlatFileRecordField> sortedFieldsForRecord = new ArrayList<FlatFileRecordField>(fieldsForRecord); Collections.sort(sortedFieldsForRecord, new Comparator<FlatFileRecordField>() { public int compare(FlatFileRecordField o1, FlatFileRecordField o2) { return new Integer(o1.getFieldFormat().getFieldStartPosition()) .compareTo(new Integer(o2.getFieldFormat().getFieldStartPosition())); } }); int lastPositionPopulated = 0; for (FlatFileRecordField recordField : sortedFieldsForRecord) { FlatFileFieldFormat fieldFormat = recordField.getFieldFormat(); // add the field delimiter before all fields but the first if (fieldDelimiter != null && lastPositionPopulated > 0) { recordContent.append(fieldDelimiter); } // check to see whether we've encountered two overlapping fields if (lastPositionPopulated >= fieldFormat.getFieldStartPosition()) { throw new IllegalArgumentException( "Field " + fieldFormat.toString() + " overlaps with another field. This is not allowed."); } // add whitespace as needed to fill gap between consecutive fields while (lastPositionPopulated < fieldFormat.getFieldStartPosition() - 1) { recordContent.append(interFieldPadChar); lastPositionPopulated++; } int fieldLength = fieldFormat.getFieldLength(); String fieldValue = recordField.getFieldValue(); // abbreviate values that are too long if (fieldValue.length() > fieldLength) { fieldValue = fieldValue.substring(0, fieldLength); } // pad values that are too short if (fieldFormat.getPaddingType().equals(FlatFileFieldPaddingType.PADDING_ON_LEFT)) { fieldValue = StringUtils.leftPad(fieldValue, fieldLength, fieldFormat.getIntraFieldPaddingChar()); } else if (fieldFormat.getPaddingType().equals(FlatFileFieldPaddingType.PADDING_ON_RIGHT)) { fieldValue = StringUtils.rightPad(fieldValue, fieldLength, fieldFormat.getIntraFieldPaddingChar()); } // add the field value now that it is the proper size for the field recordContent.append(fieldValue); lastPositionPopulated += fieldValue.length(); } return recordContent.toString(); }
From source file:jext2.DirectoryEntry.java
/** * Create a new directory entry. Note that the name is mandatory because * it dictates the record length on disk *///from www.ja va 2s. com // TODO make visibility package public static DirectoryEntry create(String name) throws FileNameTooLong { int nameLen = Ext2fsDataTypes.getStringByteLength(name); if (nameLen > MAX_NAME_LEN) { throw new FileNameTooLong(); } DirectoryEntry dir = new DirectoryEntry(); /* * The directory entry must be divisible by 4, so the name * gets zero padded */ short padNameLen = (short) (nameLen + (DIR_PAD - (nameLen % DIR_PAD))); String namePadded = StringUtils.rightPad(name, padNameLen, (char) (0x00)); dir.recLen = (short) (8 + padNameLen); dir.nameLen = (short) nameLen; dir.name = namePadded; if (dir.recLen > MAX_REC_LEN) throw new RuntimeException("MAX_REC_LEN"); return dir; }
From source file:net.sourceforge.fenixedu.util.FenixStringTools.java
public static String multipleLineRightPadWithSuffix(String field, int LINE_LENGTH, char fillPaddingWith, String suffix) {//from w w w .j av a 2s.c o m if (!StringUtils.isEmpty(field) && !field.endsWith(" ")) { field += " "; } if (StringUtils.isEmpty(suffix)) { return multipleLineRightPad(field, LINE_LENGTH, fillPaddingWith); } else if (!suffix.startsWith(" ")) { suffix = " " + suffix; } int LINE_LENGTH_WITH_SUFFIX = LINE_LENGTH - suffix.length(); if (field.length() < LINE_LENGTH_WITH_SUFFIX) { return FenixStringTools.rightPad(field, LINE_LENGTH_WITH_SUFFIX, fillPaddingWith) + suffix; } else { final List<String> words = Arrays.asList(field.split(" ")); int currentLineLength = 0; String result = ""; for (final String word : words) { final String toAdd = word + " "; if (currentLineLength + toAdd.length() > LINE_LENGTH) { result = StringUtils.rightPad(result, LINE_LENGTH, ' ') + '\n'; currentLineLength = toAdd.length(); } else { currentLineLength += toAdd.length(); } result += toAdd; } if (currentLineLength < LINE_LENGTH_WITH_SUFFIX) { return FenixStringTools.rightPad(result, result.length() + (LINE_LENGTH_WITH_SUFFIX - currentLineLength), fillPaddingWith) + suffix; } else { return FenixStringTools.rightPad(result, result.length() + (LINE_LENGTH - currentLineLength), fillPaddingWith) + "\n" + StringUtils.leftPad(suffix, LINE_LENGTH, fillPaddingWith); } } }
From source file:com.devnexus.ting.common.SystemInformationUtils.java
/** * * @return//from w w w . j a v a 2s . c o m */ public static String getAllSystemProperties() { final StringBuilder systemProperties = new StringBuilder(); for (SystemPropertyInformation property : SystemPropertyInformation.getSystemPropertyValuesAsList()) { final String label = StringUtils.rightPad(property.getPropertyKey(), CONSOLE_SPACER_WIDTH, CONSOLE_SPACER_CHARACTER); systemProperties.append(label + ": " + property.getPropertyValue()).append("\n"); } return systemProperties.toString(); }
From source file:com.sds.acube.ndisc.xadmin.XNDiscAdminFile.java
/** * ? Console? //from w w w. j a v a 2 s . com * * @param fileList NFile */ private void showFileInfo(ArrayList<NFile> fileList) { int size = (fileList == null) ? 0 : fileList.size(); NFile file = null; StringBuilder files = new StringBuilder(LINE_SEPERATOR); files.append("").append(StringUtils.center("", 32, "-")); files.append(""); files.append(StringUtils.center("", 65, "-")); files.append(""); files.append(StringUtils.center("", 10, "-")); files.append(""); files.append(StringUtils.center("", 14, "-")); files.append(""); files.append(StringUtils.center("", 14, "-")); files.append(""); files.append(StringUtils.center("", 10, "-")); files.append(""); files.append(StringUtils.center("", 5, "-")); files.append("?").append(LINE_SEPERATOR); files.append("").append(StringUtils.center("File ID", 32, " ")); files.append(""); files.append(StringUtils.center("File Name", 65, " ")); files.append(""); files.append(StringUtils.center("Size", 10, " ")); files.append(""); files.append(StringUtils.center("Create Date", 14, " ")); files.append(""); files.append(StringUtils.center("Modify Date", 14, " ")); files.append(""); files.append(StringUtils.center("Status", 10, " ")); files.append(""); files.append(StringUtils.center("Media", 5, " ")); files.append("").append(LINE_SEPERATOR); if (size == 0) { int colidx = 0; files.append("").append(StringUtils.center("", 32, "-")); files.append(""); colidx++; files.append(StringUtils.center("", 65, "-")); files.append(""); colidx++; files.append(StringUtils.center("", 10, "-")); files.append(""); colidx++; files.append(StringUtils.center("", 14, "-")); files.append(""); colidx++; files.append(StringUtils.center("", 14, "-")); files.append(""); colidx++; files.append(StringUtils.center("", 10, "-")); files.append(""); colidx++; files.append(StringUtils.center("", 5, "-")); files.append("").append(LINE_SEPERATOR); files.append("").append(StringUtils.center(" No Data Found.", PRINT_COLUMN_SIZE + colidx, " ")) .append("").append(LINE_SEPERATOR); } else { files.append("").append(StringUtils.center("", 32, "-")); files.append(""); files.append(StringUtils.center("", 65, "-")); files.append(""); files.append(StringUtils.center("", 10, "-")); files.append(""); files.append(StringUtils.center("", 14, "-")); files.append(""); files.append(StringUtils.center("", 14, "-")); files.append(""); files.append(StringUtils.center("", 10, "-")); files.append(""); files.append(StringUtils.center("", 5, "-")); files.append("").append(LINE_SEPERATOR); } for (int i = 0; i < size; i++) { file = (NFile) fileList.get(i); files.append("").append(StringUtils.center(file.getId(), 32, " ")); files.append(""); files.append(StringUtils.rightPad(getName(file.getName(), 65), 65, " ")); files.append(""); files.append(StringUtils.center(Integer.toString(file.getSize()), 10, " ")); files.append(""); files.append(StringUtils .center((StringUtils.isEmpty(file.getCreatedDate()) ? "N/A" : file.getCreatedDate()), 14, " ")); files.append(""); files.append(StringUtils.center( (StringUtils.isEmpty(file.getModifiedDate()) ? "N/A" : file.getModifiedDate()), 14, " ")); files.append(""); files.append(StringUtils.center(file.getStatType(), 10, " ")); files.append(""); files.append(StringUtils.center(Integer.toString(file.getMediaId()), 5, " ")); files.append("").append(LINE_SEPERATOR); if ((i < size - 1) && (i <= MAX_LIST_SIZE)) { files.append("").append(StringUtils.center("", 32, "-")); files.append(""); files.append(StringUtils.center("", 65, "-")); files.append(""); files.append(StringUtils.center("", 10, "-")); files.append(""); files.append(StringUtils.center("", 14, "-")); files.append(""); files.append(StringUtils.center("", 14, "-")); files.append(""); files.append(StringUtils.center("", 10, "-")); files.append(""); files.append(StringUtils.center("", 5, "-")); files.append("").append(LINE_SEPERATOR); } if (i > MAX_LIST_SIZE) { break; } } if (size == 0) { files.append("").append(StringUtils.center("", 32, "-")); files.append("-"); files.append(StringUtils.center("", 65, "-")); files.append("-"); files.append(StringUtils.center("", 10, "-")); files.append("-"); files.append(StringUtils.center("", 14, "-")); files.append("-"); files.append(StringUtils.center("", 14, "-")); files.append("-"); files.append(StringUtils.center("", 10, "-")); files.append("-"); files.append(StringUtils.center("", 5, "-")); files.append("").append(LINE_SEPERATOR); } else { files.append("").append(StringUtils.center("", 32, "-")); files.append(""); files.append(StringUtils.center("", 65, "-")); files.append(""); files.append(StringUtils.center("", 10, "-")); files.append(""); files.append(StringUtils.center("", 14, "-")); files.append(""); files.append(StringUtils.center("", 14, "-")); files.append(""); files.append(StringUtils.center("", 10, "-")); files.append(""); files.append(StringUtils.center("", 5, "-")); files.append("").append(LINE_SEPERATOR); } files.append(size + " row selected.").append(LINE_SEPERATOR).append(LINE_SEPERATOR); if (printlog) { log.info(files.toString()); } else { out.print(files.toString()); } }
From source file:com.syaku.commons.DateUtils.java
public static String date(String format, String date) { String string = null;//from ww w .ja va2 s .co m try { if (StringUtils.isEmpty(format)) format = "yyyy-MM-dd HH:mm:ss"; if (StringUtils.isEmpty(date)) return date; date = date.replaceAll("[^0-9]+", ""); date = StringUtils.rightPad(date, 14, '0'); date = date.replaceAll("(^[0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})", "$1-$2-$3 $4:$5:$6"); formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date redate = formatter.parse(date); formatter = new SimpleDateFormat(format); string = formatter.format(redate); } catch (Exception e) { log.error("[#MEI DateUtils.date] " + e.toString()); } return string; }
From source file:me.taylorkelly.mywarp.bukkit.util.FormattingUtils.java
/** * Pads the given string with the given character on the right until the string has the given width. */*from ww w . ja va 2s. c om*/ * @param str the string * @param pad the padding char * @param paddedWidth the width of the padded string * @return the padded string */ public static String paddingRight(String str, char pad, int paddedWidth) { paddedWidth -= getWidth(str); return StringUtils.rightPad(str, paddedWidth / getWidth(pad), pad); }
From source file:net.erdfelt.android.sdkfido.configer.ConfigCmdLineParser.java
private String toSectionHeader(String message) { return StringUtils.rightPad(String.format(".\\ %s \\.", message), 78, '_'); }