java.util.IllegalFormatConversionException Java Examples

The following examples show how to use java.util.IllegalFormatConversionException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: QuantityType.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String format(@Nullable String pattern) {
    String formatPattern = pattern;

    if (formatPattern != null && formatPattern.contains(UnitUtils.UNIT_PLACEHOLDER)) {
        String unitSymbol = getUnit().equals(SmartHomeUnits.PERCENT) ? "%%" : getUnit().toString();
        formatPattern = formatPattern.replace(UnitUtils.UNIT_PLACEHOLDER, unitSymbol);
    }

    // The value could be an integer value. Try to convert to BigInteger in
    // order to have access to more conversion formats.
    try {
        return String.format(formatPattern, toBigDecimal().toBigIntegerExact());
    } catch (ArithmeticException ae) {
        // Could not convert to integer value without loss of
        // information. Fall through to default behavior.
    } catch (IllegalFormatConversionException ifce) {
        // The conversion is not valid for the type BigInteger. This
        // happens, if the format is like "%.1f" but the value is an
        // integer. Fall through to default behavior.
    }

    return String.format(formatPattern, toBigDecimal());
}
 
Example #2
Source File: DecimalType.java    From openhab-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String format(String pattern) {
    // The value could be an integer value. Try to convert to BigInteger in
    // order to have access to more conversion formats.
    try {
        return String.format(pattern, value.toBigIntegerExact());
    } catch (ArithmeticException ae) {
        // Could not convert to integer value without loss of
        // information. Fall through to default behavior.
    } catch (IllegalFormatConversionException ifce) {
        // The conversion is not valid for the type BigInteger. This
        // happens, if the format is like "%.1f" but the value is an
        // integer. Fall through to default behavior.
    }

    return String.format(pattern, value);
}
 
Example #3
Source File: DecimalType.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public String format(String pattern) {
    // The value could be an integer value. Try to convert to BigInteger in
    // order to have access to more conversion formats.
    try {
        return String.format(pattern, value.toBigIntegerExact());
    } catch (ArithmeticException ae) {
        // Could not convert to integer value without loss of
        // information. Fall through to default behavior.
    } catch (IllegalFormatConversionException ifce) {
        // The conversion is not valid for the type BigInteger. This
        // happens, if the format is like "%.1f" but the value is an
        // integer. Fall through to default behavior.
    }

    return String.format(pattern, value);
}
 
Example #4
Source File: CardBuilder.java    From org.openhab.ui.habot with Eclipse Public License 1.0 5 votes vote down vote up
private String formatState(Item item, State state) throws TransformationException {
    if (item.getStateDescription() != null) {
        try {
            StateDescription stateDescription = item.getStateDescription();
            if (stateDescription != null && stateDescription.getPattern() != null) {
                String transformedState = TransformationHelper.transform(
                        FrameworkUtil.getBundle(CardBuilder.class).getBundleContext(),
                        stateDescription.getPattern(), state.toString());
                if (transformedState == null) {
                    return state.toString();
                }
                if (transformedState.equals(state.toString())) {
                    return state.format(stateDescription.getPattern());
                } else {
                    return transformedState;
                }

            } else {
                return state.toString();
            }
        } catch (IllegalFormatConversionException e) {
            return state.toString();
        }
    } else {
        return state.toString();
    }
}
 
Example #5
Source File: L4MLogger.java    From symja_android_library with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Make a formated info log
 *
 * @param fmt
 *            format specification
 * @param args
 *            objects to log
 */
public void info(final String fmt, final Object... args) {
	String msg = null;
	if (null != fmt) {
		try {
			msg = String.format(fmt, args);
			super.log(Level.INFO, msg);
		} catch (IllegalFormatConversionException e) {
			msg = "wrong formated logging: " + e.getMessage();
			super.log(Level.WARNING, msg);
		}
	} else {
		error("no format string specified for: " + Arrays.toString(args));
	}
}
 
Example #6
Source File: IllegalFormatConversionExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * serialization/deserialization compatibility with RI.
 */
public void testSerializationCompatibility() throws Exception {

    SerializationTest.verifyGolden(this,
            new IllegalFormatConversionException('*', String.class),
            exComparator);
}
 
Example #7
Source File: IllegalFormatConversionExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public void assertDeserialized(Serializable initial,
        Serializable deserialized) {

    SerializationTest.THROWABLE_COMPARATOR.assertDeserialized(initial,
            deserialized);

    IllegalFormatConversionException initEx = (IllegalFormatConversionException) initial;
    IllegalFormatConversionException desrEx = (IllegalFormatConversionException) deserialized;

    assertEquals("ArgumentClass", initEx.getArgumentClass(), desrEx
            .getArgumentClass());
    assertEquals("Conversion", initEx.getConversion(), desrEx
            .getConversion());
}
 
Example #8
Source File: IllegalFormatConversionExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.IllegalFormatConversionException#getMessage()
 */
public void test_getMessage() {
    char c = '*';
    Class<String> argClass = String.class;
    IllegalFormatConversionException illegalFormatConversionException = new IllegalFormatConversionException(
            c, argClass);
    assertTrue(null != illegalFormatConversionException.getMessage());

}
 
Example #9
Source File: IllegalFormatConversionExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.IllegalFormatConversionException#getConversion()
 */
public void test_getConversion() {
    char c = '*';
    Class<String> argClass = String.class;
    IllegalFormatConversionException illegalFormatConversionException = new IllegalFormatConversionException(
            c, argClass);
    assertEquals(c, illegalFormatConversionException.getConversion());

}
 
Example #10
Source File: IllegalFormatConversionExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.IllegalFormatConversionException#getArgumentClass()
 */
public void test_getArgumentClass() {
    char c = '*';
    Class<String> argClass = String.class;
    IllegalFormatConversionException illegalFormatConversionException = new IllegalFormatConversionException(
            c, argClass);
    assertEquals(argClass, illegalFormatConversionException
            .getArgumentClass());

}
 
Example #11
Source File: IllegalFormatConversionExceptionTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * java.util.IllegalFormatConversionException#IllegalFormatConversionException(char,
 *Class)
 */
public void test_illegalFormatConversionException() {
    try {
        new IllegalFormatConversionException(' ', null);
        fail("should throw NullPointerExcetpion.");
    } catch (NullPointerException e) {
        // desired
    }
}
 
Example #12
Source File: SuntimesUtils.java    From SuntimesWidget with GNU General Public License v3.0 5 votes vote down vote up
public static String formatAsHeight(Context context, double value, WidgetSettings.LengthUnit units, boolean convert, int places)
{
    int stringID;
    switch (units)
    {
        case USC:
        case IMPERIAL:
            if (convert) {
                value = WidgetSettings.LengthUnit.metersToFeet(value);
            }
            stringID = R.plurals.units_feet_long;
            break;

        case METRIC:
        default:
            stringID = R.plurals.units_meters_long;
            break;
    }
    int h = ((value > 1) ? (int)Math.ceil(value)   // TODO: better use of plurals w/ fractional values..
           : (value < 1) ? 2 : 1);   // this is a hack; there must be a better way to treat fractions as plural

    NumberFormat formatter = NumberFormat.getInstance();
    formatter.setMinimumFractionDigits(0);
    formatter.setMaximumFractionDigits(places);

    try {
        return context.getResources().getQuantityString(stringID, h, formatter.format(value));

    } catch (IllegalFormatConversionException e) {
        Log.e("formatAsHeight", "ignoring invalid format in stringID: " + stringID);
        return String.valueOf(value);
    }
}
 
Example #13
Source File: DateTimePickerDialog.java    From 365browser with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public String getString(int id, Object... formatArgs) throws NotFoundException {
    try {
        return super.getString(id, formatArgs);
    } catch (IllegalFormatConversionException conversationException) {
        String template = super.getString(id);
        char conversion = conversationException.getConversion();
        // Trying to replace either all digit patterns (%d) or first one (%1$d).
        template = template.replaceAll(Pattern.quote("%" + conversion), "%s")
                           .replaceAll(Pattern.quote("%1$" + conversion), "%s");

        return String.format(getLocale(), template, formatArgs);
    }
}
 
Example #14
Source File: FormattedTextTest.java    From cactoos with MIT License 5 votes vote down vote up
@Test(expected = IllegalFormatConversionException.class)
public void ensuresThatFormatterFails() throws Exception {
    new FormattedText(
        new TextOf("Local time: %d"),
        Locale.ROOT,
        Calendar.getInstance()
    ).asString();
}
 
Example #15
Source File: IllegalFormatConversionExceptionTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
/**
 * serialization/deserialization.
 */
public void testSerializationSelf() throws Exception {

    SerializationTest.verifySelf(new IllegalFormatConversionException('*',
            String.class), exComparator);
}
 
Example #16
Source File: TestUtilsTest.java    From hazelcast-simulator with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalFormatConversionException.class)
public void testAssertEqualsStringFormatIllegalFormatConversionException() {
    assertEqualsStringFormat("%d %d", "42", "42");
}
 
Example #17
Source File: MessageTest.java    From api-layer with Eclipse Public License 2.0 4 votes vote down vote up
@Test(expected = IllegalFormatConversionException.class)
public void testCheckConvertValidation_whenThereIsWrongFormatThanRequested() {
    Message.of("org.zowe.apiml.common.serviceTimeout",
        createMessageTemplate("No response received within the allowed time: %d"),
        new Object[]{"3000"});
}
 
Example #18
Source File: SymbolFormatterTest.java    From crate with Apache License 2.0 4 votes vote down vote up
@Test
public void testFormatInvalidEscape() throws Exception {
    expectedException.expect(IllegalFormatConversionException.class);
    expectedException.expectMessage("d != java.lang.String");
    assertThat(Symbols.format("%d", Literal.of(42L)), is(""));
}