Skip to content

Commit

Permalink
[lang][core] Add function for enabling casting from sequence of chara…
Browse files Browse the repository at this point in the history
…cters to object number types.

Signed-off-by: Stéphane Galland <galland@arakhne.org>
  • Loading branch information
gallandarakhneorg committed Dec 13, 2020
1 parent f669a90 commit fbc4001
Show file tree
Hide file tree
Showing 3 changed files with 565 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,30 @@ public static byte byteValue(CharSequence value) {
return 0;
}

/** Decodes a {@code CharSequence} into a {@code Byte}.
*
* <p>In opposite to the functions of {@link Byte}, this function is
* null-safe and does not generate a {@link NumberFormatException}.
* If the given string cannot by parsed, {@code 0} is replied.
*
* <p>See {@link Byte#decode(String)} for details on the accepted formats
* for the input string of characters.
*
* @param value a value of {@code CharSequence} type.
* @return the equivalent value to {@code value} of {@code Byte} type.
* @since 0.12
* @see Byte#decode(String)
*/
@Pure
public static byte toByte(CharSequence value) {
try {
return Byte.decode(value.toString());
} catch (Throwable exception) {
// Silent exception.
}
return 0;
}

/** Decodes a {@code CharSequence} into a {@code short}.
*
* <p>In opposite to the functions of {@link Short}, this function is
Expand All @@ -145,7 +169,31 @@ public static short shortValue(CharSequence value) {
return 0;
}

/** Decodes a {@code CharSequence} into a {@code int}.
/** Decodes a {@code CharSequence} into a {@code Short}.
*
* <p>In opposite to the functions of {@link Short}, this function is
* null-safe and does not generate a {@link NumberFormatException}.
* If the given string cannot by parsed, {@code 0} is replied.
*
* <p>See {@link Short#decode(String)} for details on the accepted formats
* for the input string of characters.
*
* @param value a value of {@code CharSequence} type.
* @return the equivalent value to {@code value} of {@code Short} type.
* @since 0.12
* @see Short#decode(String)
*/
@Pure
public static Short toShort(CharSequence value) {
try {
return Short.decode(value.toString());
} catch (Throwable exception) {
// Silent exception.
}
return 0;
}

/** Decodes a {@code CharSequence} into an {@code int}.
*
* <p>In opposite to the functions of {@link Integer}, this function is
* null-safe and does not generate a {@link NumberFormatException}.
Expand All @@ -169,16 +217,60 @@ public static int intValue(CharSequence value) {
return 0;
}

/** Decodes a {@code CharSequence} into an {@code Integer}.
*
* <p>In opposite to the functions of {@link Integer}, this function is
* null-safe and does not generate a {@link NumberFormatException}.
* If the given string cannot by parsed, {@code 0} is replied.
*
* <p>See {@link Integer#decode(String)} for details on the accepted formats
* for the input string of characters.
*
* @param value a value of {@code CharSequence} type.
* @return the equivalent value to {@code value} of {@code Integer} type.
* @since 0.12
* @see Integer#decode(String)
*/
@Pure
public static Integer toInteger(CharSequence value) {
try {
return Integer.decode(value.toString());
} catch (Throwable exception) {
// Silent exception.
}
return 0;
}

/** Decodes a {@code CharSequence} into a {@code char}.
*
* @param value a value of {@code CharSequence} type.
* @return the equivalent value to {@code value} of {@code char} type.
* @since 0.12
*/
@Pure
@Inline("($1).charAt(0)")
public static char charValue(CharSequence value) {
return value.charAt(0);
try {
return value.charAt(0);
} catch (Throwable exception) {
//
}
return '\0';
}

/** Decodes a {@code CharSequence} into a {@code Character}.
*
* @param value a value of {@code CharSequence} type.
* @return the equivalent value to {@code value} of {@code Character} type.
* @since 0.12
*/
@Pure
public static Character toCharacter(CharSequence value) {
try {
return value.charAt(0);
} catch (Throwable exception) {
//
}
return '\0';
}

/** Decodes a {@code CharSequence} into a {@code long}.
Expand All @@ -205,6 +297,30 @@ public static long longValue(CharSequence value) {
return 0;
}

/** Decodes a {@code CharSequence} into a {@code Long}.
*
* <p>In opposite to the functions of {@link Long}, this function is
* null-safe and does not generate a {@link NumberFormatException}.
* If the given string cannot by parsed, {@code 0} is replied.
*
* <p>See {@link Long#decode(String)} for details on the accepted formats
* for the input string of characters.
*
* @param value a value of {@code CharSequence} type.
* @return the equivalent value to {@code value} of {@code Long} type.
* @since 0.9
* @see Long#decode(String)
*/
@Pure
public static Long toLong(CharSequence value) {
try {
return Long.decode(value.toString());
} catch (Throwable exception) {
// Silent exception.
}
return 0L;
}

/** Decodes a {@code CharSequence} into a {@code float}.
*
* <p>In opposite to the functions of {@link Float}, this function is
Expand All @@ -229,6 +345,30 @@ public static float floatValue(CharSequence value) {
return 0;
}

/** Decodes a {@code CharSequence} into a {@code Float}.
*
* <p>In opposite to the functions of {@link Float}, this function is
* null-safe and does not generate a {@link NumberFormatException}.
* If the given string cannot by parsed, {@code 0} is replied.
*
* <p>See {@link Float#valueOf(String)} for details on the accepted formats
* for the input string of characters.
*
* @param value a value of {@code CharSequence} type.
* @return the equivalent value to {@code value} of {@code Float} type.
* @since 0.9
* @see Float#valueOf(String)
*/
@Pure
public static Float toFloat(CharSequence value) {
try {
return Float.parseFloat(value.toString());
} catch (Throwable exception) {
// Silent exception.
}
return 0f;
}

/** Decodes a {@code CharSequence} into a {@code double}.
*
* <p>In opposite to the functions of {@link Double}, this function is
Expand All @@ -253,6 +393,30 @@ public static double doubleValue(CharSequence value) {
return 0;
}

/** Decodes a {@code CharSequence} into a {@code Double}.
*
* <p>In opposite to the functions of {@link Double}, this function is
* null-safe and does not generate a {@link NumberFormatException}.
* If the given string cannot by parsed, {@code 0} is replied.
*
* <p>See {@link Double#valueOf(String)} for details on the accepted formats
* for the input string of characters.
*
* @param value a value of {@code CharSequence} type.
* @return the equivalent value to {@code value} of {@code Double} type.
* @since 0.12
* @see Double#valueOf(String)
*/
@Pure
public static Double toDouble(CharSequence value) {
try {
return Double.parseDouble(value.toString());
} catch (Throwable exception) {
// Silent exception.
}
return 0.0;
}

/** Decodes a {@code CharSequence} into a {@code AtomicBoolean}.
*
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ public void toString_char() {
assertEquals("a", PrimitiveCastExtensions.toString('a'));
}

@Test
public void charValue_CharSequence() {
assertEquals('4', PrimitiveCastExtensions.charValue("4"));
assertEquals('0', PrimitiveCastExtensions.charValue("0xa"));
assertEquals('-', PrimitiveCastExtensions.charValue("-4"));
assertEquals('-', PrimitiveCastExtensions.charValue("-0xa"));
assertEquals('\0', PrimitiveCastExtensions.charValue(""));
assertEquals('z', PrimitiveCastExtensions.charValue("z"));
assertEquals('\0', PrimitiveCastExtensions.charValue(null));
}

@Test
public void toCharacter_CharSequence() {
assertEquals('4', PrimitiveCastExtensions.toCharacter("4"));
assertEquals('0', PrimitiveCastExtensions.toCharacter("0xa"));
assertEquals('-', PrimitiveCastExtensions.toCharacter("-4"));
assertEquals('-', PrimitiveCastExtensions.toCharacter("-0xa"));
assertEquals('\0', PrimitiveCastExtensions.toCharacter(""));
assertEquals('z', PrimitiveCastExtensions.toCharacter("z"));
assertEquals('\0', PrimitiveCastExtensions.toCharacter(null));
}

@Test
public void byteValue_CharSequence() {
assertEquals(4, PrimitiveCastExtensions.byteValue("4"));
Expand All @@ -96,6 +118,17 @@ public void byteValue_CharSequence() {
assertEquals(0, PrimitiveCastExtensions.byteValue(null));
}

@Test
public void toByte_CharSequence() {
assertEquals(4, PrimitiveCastExtensions.toByte("4"));
assertEquals(10, PrimitiveCastExtensions.toByte("0xa"));
assertEquals(-4, PrimitiveCastExtensions.toByte("-4"));
assertEquals(-10, PrimitiveCastExtensions.toByte("-0xa"));
assertEquals(0, PrimitiveCastExtensions.toByte(""));
assertEquals(0, PrimitiveCastExtensions.toByte("z"));
assertEquals(0, PrimitiveCastExtensions.toByte(null));
}

@Test
public void shortValue_CharSequence() {
assertEquals(4, PrimitiveCastExtensions.shortValue("4"));
Expand All @@ -107,6 +140,17 @@ public void shortValue_CharSequence() {
assertEquals(0, PrimitiveCastExtensions.shortValue(null));
}

@Test
public void toShort_CharSequence() {
assertEquals((short) 4, PrimitiveCastExtensions.toShort("4"));
assertEquals((short) 10, PrimitiveCastExtensions.toShort("0xa"));
assertEquals((short) -4, PrimitiveCastExtensions.toShort("-4"));
assertEquals((short) -10, PrimitiveCastExtensions.toShort("-0xa"));
assertEquals((short) 0, PrimitiveCastExtensions.toShort(""));
assertEquals((short) 0, PrimitiveCastExtensions.toShort("z"));
assertEquals((short) 0, PrimitiveCastExtensions.toShort(null));
}

@Test
public void intValue_CharSequence() {
assertEquals(4, PrimitiveCastExtensions.intValue("4"));
Expand All @@ -118,6 +162,17 @@ public void intValue_CharSequence() {
assertEquals(0, PrimitiveCastExtensions.intValue(null));
}

@Test
public void toInteger_CharSequence() {
assertEquals(4, PrimitiveCastExtensions.toInteger("4"));
assertEquals(10, PrimitiveCastExtensions.toInteger("0xa"));
assertEquals(-4, PrimitiveCastExtensions.toInteger("-4"));
assertEquals(-10, PrimitiveCastExtensions.toInteger("-0xa"));
assertEquals(0, PrimitiveCastExtensions.toInteger(""));
assertEquals(0, PrimitiveCastExtensions.toInteger("z"));
assertEquals(0, PrimitiveCastExtensions.toInteger(null));
}

@Test
public void longValue_CharSequence() {
assertEquals(4l, PrimitiveCastExtensions.longValue("4"));
Expand All @@ -129,6 +184,17 @@ public void longValue_CharSequence() {
assertEquals(0l, PrimitiveCastExtensions.longValue(null));
}

@Test
public void toLong_CharSequence() {
assertEquals(4l, PrimitiveCastExtensions.toLong("4"));
assertEquals(10l, PrimitiveCastExtensions.toLong("0xa"));
assertEquals(-4l, PrimitiveCastExtensions.toLong("-4"));
assertEquals(-10l, PrimitiveCastExtensions.toLong("-0xa"));
assertEquals(0l, PrimitiveCastExtensions.toLong(""));
assertEquals(0l, PrimitiveCastExtensions.toLong("z"));
assertEquals(0l, PrimitiveCastExtensions.toLong(null));
}

@Test
public void floatValue_CharSequence() {
assertEpsilonEquals(4f, PrimitiveCastExtensions.floatValue("4"));
Expand All @@ -140,6 +206,17 @@ public void floatValue_CharSequence() {
assertEpsilonEquals(0f, PrimitiveCastExtensions.floatValue(null));
}

@Test
public void toFloat_CharSequence() {
assertEpsilonEquals(4f, PrimitiveCastExtensions.toFloat("4"));
assertEpsilonEquals(0f, PrimitiveCastExtensions.toFloat("0xa"));
assertEpsilonEquals(-4f, PrimitiveCastExtensions.toFloat("-4"));
assertEpsilonEquals(0f, PrimitiveCastExtensions.toFloat("-0xa"));
assertEpsilonEquals(0f, PrimitiveCastExtensions.toFloat(""));
assertEpsilonEquals(0f, PrimitiveCastExtensions.toFloat("z"));
assertEpsilonEquals(0f, PrimitiveCastExtensions.toFloat(null));
}

@Test
public void doubleValue_CharSequence() {
assertEpsilonEquals(4., PrimitiveCastExtensions.doubleValue("4"));
Expand All @@ -151,6 +228,17 @@ public void doubleValue_CharSequence() {
assertEpsilonEquals(0., PrimitiveCastExtensions.doubleValue(null));
}

@Test
public void toDoubleCharSequence() {
assertEpsilonEquals(4., PrimitiveCastExtensions.toDouble("4"));
assertEpsilonEquals(0., PrimitiveCastExtensions.toDouble("0xa"));
assertEpsilonEquals(-4., PrimitiveCastExtensions.toDouble("-4"));
assertEpsilonEquals(0., PrimitiveCastExtensions.toDouble("-0xa"));
assertEpsilonEquals(0., PrimitiveCastExtensions.toDouble(""));
assertEpsilonEquals(0., PrimitiveCastExtensions.toDouble("z"));
assertEpsilonEquals(0., PrimitiveCastExtensions.toDouble(null));
}

@Test
public void toAtomicBoolean_CharSequence() {
assertFalse(PrimitiveCastExtensions.toAtomicBoolean("4").get());
Expand Down

0 comments on commit fbc4001

Please sign in to comment.