Search in sources :

Example 91 with FieldPosition

use of java.text.FieldPosition in project sonar-java by SonarSource.

the class UsingLambda method foo.

void foo(Calendar c) {
    Object myObject;
    double value;
    String.format("The value of my integer is %d", "Hello World");
    // Noncompliant  {{Looks like there is a confusion with the use of java.text.MessageFormat, parameters will be simply ignored here}}
    String.format("First {0} and then {1}", "foo", "bar");
    String.format("Duke's Birthday year is %tX", 12l);
    // Noncompliant {{2nd argument is not used.}}
    String.format("Display %3$d and then %d", 1, 2, 3);
    // Noncompliant {{3rd argument is not used.}}
    String.format("Too many arguments %d and %d", 1, 2, 3);
    String.format("Not enough arguments %d and %d", 1);
    // Noncompliant {{%n should be used in place of \n to produce the platform-specific line separator.}}
    String.format("First Line\n %d", 1);
    // Noncompliant {{String contains no format specifiers.}}
    String.format("First Line");
    // Noncompliant {{String contains no format specifiers.}}
    String.format("First Line%%");
    // Compliant
    String.format("First Line%n");
    String.format("%< is equals to %d", 2);
    // Noncompliant {{Directly inject the boolean value.}}
    String.format("Is myObject null ? %b", myObject);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    String.format("value is " + value);
    // Noncompliant {{String contains no format specifiers.}}
    String.format("string without arguments");
    PrintWriter pr;
    PrintStream ps;
    Formatter formatter;
    Locale loc;
    pr.format("The value of my integer is %d", "Hello World");
    pr.printf("The value of my integer is %d", "Hello World");
    ps.format("The value of my integer is %d", "Hello World");
    ps.printf(loc, "The value of my integer is %d", "Hello World");
    formatter.format("The value of my integer is %d", "Hello World");
    pr.format("%s:\tintCompact %d\tintVal %d\tscale %d\tprecision %d%n", "", 1, 1, 1, 1);
    pr.format("%s:\tintCompact %n%n%n%d\tintVal %d\tscale %d\tprecision %d%n", "", 1, 1, 1, 1);
    pr.format("%TH", 1l);
    pr.format("%d", new Long(12));
    pr.format("%d", new java.math.BigInteger("12"));
    // Noncompliant {{4th argument is not used.}}
    String.format("Too many arguments %d and %d and %d", 1, 2, 3, 4);
    // Compliant
    String.format("normal %d%% ", 1);
    String.format("Duke's Birthday year is %t", 12l);
    // Compliant
    String.format("Duke's Birthday year is %tH", 12l);
    // Compliant
    String.format("Duke's Birthday year is %tH", Long.valueOf(12L));
    String.format("Duke's Birthday year is %tH", loc);
    String.format("%08d%n", 1);
    GregorianCalendar gc;
    String.format("Duke's Birthday year is %tH", gc);
    String.format("Duke's Birthday year is %t", loc);
    // Compliant
    String.format("Accessed before %tF%n", java.time.LocalDate.now());
    // Compliant
    System.out.printf("%1$ty_%1$tm_%1$td_%1$tH_%1$tM_%1$tS", java.time.LocalDateTime.now());
    // Noncompliant  {{String contains no format specifiers.}}
    pr.format("string without arguments");
    // Noncompliant  {{String contains no format specifiers.}}
    pr.format(loc, "string without arguments");
    // Noncompliant  {{String contains no format specifiers.}}
    pr.printf("string without arguments");
    // Noncompliant  {{String contains no format specifiers.}}
    pr.printf(loc, "string without arguments");
    // Noncompliant  {{String contains no format specifiers.}}
    ps.format("string without arguments");
    // Noncompliant  {{String contains no format specifiers.}}
    ps.format(loc, "string without arguments");
    // Noncompliant  {{String contains no format specifiers.}}
    ps.printf("string without arguments");
    // Noncompliant  {{String contains no format specifiers.}}
    ps.printf(loc, "string without arguments");
    // Noncompliant  {{String contains no format specifiers.}}
    formatter.format("string without arguments");
    // Noncompliant  {{String contains no format specifiers.}}
    formatter.format(loc, "string without arguments");
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    pr.format("value is " + value);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    pr.format(loc, "value is " + value);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    pr.printf("value is " + value);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    pr.printf(loc, "value is " + value);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    ps.format("value is " + value);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    ps.format(loc, "value is " + value);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    ps.printf("value is " + value);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    ps.printf(loc, "value is " + value);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    formatter.format("value is " + value);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    formatter.format(loc, "value is " + value);
    // Noncompliant {{Format specifiers should be used instead of string concatenation.}}
    pr.format("value is " + "asd");
    pr.format("value is " + // Compliant operand not on the same line.
    "asd");
    // Compliant
    String.format("value is %d", value);
    String.format("%0$s", "tmp");
    // Compliant
    String.format("Dude's Birthday: %1$tm %<te,%<tY", c);
    // Compliant
    String.format("Dude's Birthday: %1$tm %1$te,%1$tY", c);
    String.format("log/protocol_%tY_%<tm_%<td_%<tH_%<tM_%<tS.zip", new java.util.Date());
    MessageFormat messageFormat = new MessageFormat("{0}");
    // Compliant - Not considered
    messageFormat.format(new Object(), new StringBuffer(), new FieldPosition(0));
    // Compliant - Not considered
    messageFormat.format(new Object());
    // Compliant - Not considered
    messageFormat.format("");
    Object[] objs;
    // Compliant
    MessageFormat.format("{0,number,$'#',##}", value);
    // Compliant
    MessageFormat.format("Result ''{0}''.", 14);
    // Noncompliant {{String contains no format specifiers.}}
    MessageFormat.format("Result '{0}'", 14);
    MessageFormat.format("Result ' {0}", 14);
    MessageFormat.format("Result {{{0}}.", 14);
    // Noncompliant {{No need to call toString "method()" as formatting and string conversion is done by the Formatter.}}
    MessageFormat.format("Result {0}!", myObject.toString());
    // Compliant
    MessageFormat.format("Result {0}!", myObject.hashCode());
    // Noncompliant {{String contains no format specifiers.}}
    MessageFormat.format("Result yeah!", 14);
    MessageFormat.format("Result {1}!", 14);
    MessageFormat.format("Result {0} and {1}!", 14);
    // Noncompliant {{2nd argument is not used.}}
    MessageFormat.format("Result {0} and {0}!", 14, 42);
    // compliant
    MessageFormat.format("Result {0, number, integer} and {1, number, integer}!", 14, 42);
    // Noncompliant {{3rd argument is not used.}}
    MessageFormat.format("Result {0} and {1}!", 14, 42, 128);
    // Compliant
    MessageFormat.format("{0,number,#.#}{1}", new Object[] { 0.07, "$" });
    MessageFormat.format("{0,number,#.#}{1}", new Object[] { 0.07 });
    // Compliant - skipped as the array is not initialized in the method invocation
    MessageFormat.format("{0,number,#.#}{1}", objs);
    // Compliant - Not considered
    MessageFormat.format("{0,number,#.#}{1}", new Object[42]);
    MessageFormat.format("value=\"'{'{0}'}'{1}\"", new Object[] { "value 1", "value 2" });
    MessageFormat.format("value=\"{0}'{'{1}'}'\"", new Object[] { "value 1", "value 2" });
    java.util.logging.Logger logger;
    // Compliant
    logger.log(java.util.logging.Level.SEVERE, "{0,number,$'#',##}", value);
    // Compliant
    logger.log(java.util.logging.Level.SEVERE, "Result ''{0}''.", 14);
    // Noncompliant {{String contains no format specifiers.}}
    logger.log(java.util.logging.Level.SEVERE, "Result '{0}'", 14);
    logger.log(java.util.logging.Level.SEVERE, "Result ' {0}", 14);
    logger.log(java.util.logging.Level.SEVERE, "Result {{{0}}.", 14);
    // Noncompliant {{No need to call toString "method()" as formatting and string conversion is done by the Formatter.}}
    logger.log(java.util.logging.Level.SEVERE, "Result {0}!", myObject.toString());
    // Compliant
    logger.log(java.util.logging.Level.SEVERE, "Result {0}!", myObject.hashCode());
    // Noncompliant {{String contains no format specifiers.}}
    logger.log(java.util.logging.Level.SEVERE, "Result yeah!", 14);
    // compliant, throwable parameter
    logger.log(java.util.logging.Level.SEVERE, "Result yeah!", new Exception());
    logger.log(java.util.logging.Level.SEVERE, "Result {1}!", 14);
    logger.log(java.util.logging.Level.SEVERE, "Result {0} and {1}!", 14);
    // Compliant
    logger.log(java.util.logging.Level.SEVERE, "{0,number,#.#}{1}", new Object[] { 0.07, "$" });
    logger.log(java.util.logging.Level.SEVERE, "{0,number,#.#}{1}", new Object[] { 0.07 });
    // Compliant - skipped as the array is not initialized in the method invocation
    logger.log(java.util.logging.Level.SEVERE, "{0,number,#.#}{1}", objs);
    // Compliant - Not considered
    logger.log(java.util.logging.Level.SEVERE, "{0,number,#.#}{1}", new Object[42]);
    logger.log(java.util.logging.Level.SEVERE, "value=\"'{'{0}'}'{1}\"", new Object[] { "value 1", "value 2" });
    logger.log(java.util.logging.Level.SEVERE, "value=\"{0}'{'{1}'}'\"", new Object[] { "value 1", "value 2" });
    org.slf4j.Logger slf4jLog;
    org.slf4j.Marker marker;
    slf4jLog.debug(marker, "message {}");
    // Noncompliant {{String contains no format specifiers.}}
    slf4jLog.debug(marker, "message ", 1);
    slf4jLog.debug(marker, "message {}", 1);
    slf4jLog.debug(marker, "message {} - {}", 1, 2);
    // Noncompliant {{2nd argument is not used.}}
    slf4jLog.debug(marker, "message {}", 1, 2);
    slf4jLog.debug(marker, "message {} {} {}", 1, 2, 3);
    // Noncompliant
    slf4jLog.debug(marker, "message {} {}", 1, 2, 3);
    // Noncompliant
    slf4jLog.debug(marker, "message {} {}", new Object[] { 1, 2, 3 });
    // compliant
    slf4jLog.debug(marker, "message {} {} {}", new Object[] { 1, 2, 3 });
    slf4jLog.debug(marker, "message ", new Exception());
    slf4jLog.debug(marker, "message {}", new Exception());
    slf4jLog.debug("message {}");
    // Noncompliant {{String contains no format specifiers.}}
    slf4jLog.debug("message ", 1);
    slf4jLog.debug("message {}", 1);
    slf4jLog.debug("message {} - {}", 1, 2);
    // Noncompliant {{2nd argument is not used.}}
    slf4jLog.debug("message {}", 1, 2);
    slf4jLog.debug("message {} {} {}", 1, 2, 3);
    // Noncompliant
    slf4jLog.debug("message {} {}", 1, 2, 3);
    // Noncompliant
    slf4jLog.debug("message {} {}", new Object[] { 1, 2, 3 });
    // compliant
    slf4jLog.debug("message {} {} {}", new Object[] { 1, 2, 3 });
    slf4jLog.debug("message ", new Exception());
    slf4jLog.debug("message {}", new Exception());
    slf4jLog.error("message {}");
    // Noncompliant {{String contains no format specifiers.}}
    slf4jLog.error("message ", 1);
    slf4jLog.error("message {}", 1);
    slf4jLog.info("message {} - {}", 1, 2);
    // Noncompliant {{2nd argument is not used.}}
    slf4jLog.info("message {}", 1, 2);
    slf4jLog.info("message {} {} {}", 1, 2, 3);
    // Noncompliant
    slf4jLog.trace("message {} {}", 1, 2, 3);
    // Noncompliant
    slf4jLog.trace("message {} {}", new Object[] { 1, 2, 3 });
    // compliant
    slf4jLog.trace("message {} {} {}", new Object[] { 1, 2, 3 });
    slf4jLog.trace("message ", new Exception());
    slf4jLog.trace("message {}", new Exception());
    slf4jLog.warn("message {}");
    // Noncompliant {{String contains no format specifiers.}}
    slf4jLog.warn("message ", 1);
    slf4jLog.warn("message {}", 1);
    slf4jLog.warn("message");
}
Also used : Locale(java.util.Locale) PrintStream(java.io.PrintStream) MessageFormat(java.text.MessageFormat) Formatter(java.util.Formatter) GregorianCalendar(java.util.GregorianCalendar) FieldPosition(java.text.FieldPosition) PrintWriter(java.io.PrintWriter)

Example 92 with FieldPosition

use of java.text.FieldPosition in project cu-kfs by CU-CommunityApps.

the class DisbursementVoucherInputFileType method getFileName.

/**
 * @see org.kuali.kfs.sys.batch.BatchInputFileType#getFileName(java.lang.String, java.lang.Object, java.lang.String)
 */
public String getFileName(String principalId, Object parsedFileContents, String fileUserIdentifer) {
    Timestamp currentTimestamp = dateTimeService.getCurrentTimestamp();
    StringBuffer buf = new StringBuffer();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss");
    formatter.setLenient(false);
    formatter.format(currentTimestamp, buf, new FieldPosition(0));
    String fileName = FPConstants.DV_FILE_UPLOAD_FILE_PREFIX + principalId;
    if (StringUtils.isNotBlank(fileUserIdentifer)) {
        fileName += "_" + StringUtils.remove(fileUserIdentifer, " ");
    }
    fileName += "_" + buf.toString();
    return fileName;
}
Also used : FieldPosition(java.text.FieldPosition) Timestamp(java.sql.Timestamp) SimpleDateFormat(java.text.SimpleDateFormat)

Example 93 with FieldPosition

use of java.text.FieldPosition in project cu-kfs by CU-CommunityApps.

the class IWantDocumentInputFileType method getFileName.

/**
 * @see org.kuali.kfs.sys.batch.BatchInputFileType#getFileName(java.lang.String, java.lang.Object, java.lang.String)
 */
public String getFileName(String principalId, Object parsedFileContents, String fileUserIdentifer) {
    Timestamp currentTimestamp = dateTimeService.getCurrentTimestamp();
    StringBuffer buf = new StringBuffer();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HHmmss");
    formatter.setLenient(false);
    formatter.format(currentTimestamp, buf, new FieldPosition(0));
    String fileName = CUPurapConstants.I_WANT_DOC_FEED_FILE_PREFIX + principalId;
    if (StringUtils.isNotBlank(fileUserIdentifer)) {
        fileName += "_" + StringUtils.remove(fileUserIdentifer, " ");
    }
    fileName += "_" + buf.toString();
    return fileName;
}
Also used : FieldPosition(java.text.FieldPosition) Timestamp(java.sql.Timestamp) SimpleDateFormat(java.text.SimpleDateFormat)

Example 94 with FieldPosition

use of java.text.FieldPosition in project com.revolsys.open by revolsys.

the class Matrix method toString.

static String toString(final Matrix matrix) {
    final int numRow = matrix.getRowCount();
    final int numCol = matrix.getColumnCount();
    StringBuffer buffer = new StringBuffer();
    final String lineSeparator = "\n";
    final FieldPosition dummy = new FieldPosition(0);
    final NumberFormat format = NumberFormat.getNumberInstance();
    format.setGroupingUsed(false);
    format.setMinimumFractionDigits(6);
    format.setMaximumFractionDigits(6);
    for (int j = 0; j < numRow; ++j) {
        for (int i = 0; i < numCol; ++i) {
            final int position = buffer.length();
            buffer = format.format(matrix.get(j, i), buffer, dummy);
            buffer.insert(position, " ");
        }
        buffer.append(lineSeparator);
    }
    return buffer.toString();
}
Also used : FieldPosition(java.text.FieldPosition) NumberFormat(java.text.NumberFormat)

Example 95 with FieldPosition

use of java.text.FieldPosition in project tesb-rt-se by Talend.

the class TalendDate method getDate.

/**
 * getDate : return the current datetime with the given display format format : (optional) string representing the
 * wished format of the date. This string contains fixed strings and variables related to the date. By default, the
 * format string is DD/MM/CCYY. Here is the list of date variables:
 *
 * {talendTypes} String
 *
 * {Category} TalendDate
 *
 * {param} string("CCYY-MM-DD hh:mm:ss") pattern : date pattern + CC for century + YY for year + MM for month + DD
 * for day + hh for hour + mm for minute + ss for second
 *
 * {example} getDate("CCYY-MM-DD hh:mm:ss") #
 */
public static String getDate(String pattern) {
    if (pattern == null) {
        pattern = "yyyy-MM-dd HH:mm:ss";
    }
    StringBuffer result = new StringBuffer();
    // $NON-NLS-1$ //$NON-NLS-2$
    pattern = pattern.replace("CC", "yy");
    // $NON-NLS-1$ //$NON-NLS-2$
    pattern = pattern.replace("YY", "yy");
    // $NON-NLS-1$ //$NON-NLS-2$
    pattern = pattern.replace("MM", "MM");
    // $NON-NLS-1$ //$NON-NLS-2$
    pattern = pattern.replace("DD", "dd");
    // $NON-NLS-1$ //$NON-NLS-2$
    pattern = pattern.replace("hh", "HH");
    // not needed
    // pattern.replace("mm", "mm");
    // pattern.replace("ss", "ss");
    SimpleDateFormat sdf = new SimpleDateFormat(pattern);
    sdf.format(Calendar.getInstance().getTime(), result, new FieldPosition(0));
    return result.toString();
}
Also used : FieldPosition(java.text.FieldPosition) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

FieldPosition (java.text.FieldPosition)100 Date (java.util.Date)31 SimpleDateFormat (java.text.SimpleDateFormat)28 DecimalFormat (java.text.DecimalFormat)12 NumberFormat (java.text.NumberFormat)12 ParsePosition (java.text.ParsePosition)12 IsoDateFormat (alma.acs.util.IsoDateFormat)9 Test (org.junit.Test)9 ILogEntry (com.cosylab.logging.engine.log.ILogEntry)8 Format (java.text.Format)8 MessageFormat (java.text.MessageFormat)8 DateFormat (java.text.DateFormat)6 ACSLogParser (alma.acs.logging.engine.parser.ACSLogParser)5 IOException (java.io.IOException)3 ParseException (java.text.ParseException)3 Calendar (java.util.Calendar)3 GregorianCalendar (java.util.GregorianCalendar)3 Vector (java.util.Vector)3 ExtendedDateFormat (org.jkiss.utils.time.ExtendedDateFormat)3 LogTypeHelper (com.cosylab.logging.engine.log.LogTypeHelper)2