Skip to content

Commit 5b11020

Browse files
committed
Add more Mappers
- `toFloat(Object)` - `toShort(Object)` - `toByte(Object)` - `toBigInteger(Object)` - `toBigDecimal(Object)`
1 parent 9aafb0e commit 5b11020

File tree

1 file changed

+60
-0
lines changed
  • src/main/java/xyz/srnyx/javautilities/manipulation

1 file changed

+60
-0
lines changed

src/main/java/xyz/srnyx/javautilities/manipulation/Mapper.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,66 @@ public static Optional<Long> toLong(@Nullable Object object) {
7070
return object == null ? Optional.empty() : MiscUtility.handleException(() -> Long.parseLong(object.toString()), NumberFormatException.class);
7171
}
7272

73+
/**
74+
* Converts an {@link Object} to a {@link Float}
75+
*
76+
* @param object the {@link Object} to convert
77+
*
78+
* @return the {@link Float} or {@link Optional#empty()}
79+
*/
80+
@NotNull
81+
public static Optional<Float> toFloat(@Nullable Object object) {
82+
return object == null ? Optional.empty() : MiscUtility.handleException(() -> Float.parseFloat(object.toString()), NumberFormatException.class);
83+
}
84+
85+
/**
86+
* Converts an {@link Object} to a {@link Short}
87+
*
88+
* @param object the {@link Object} to convert
89+
*
90+
* @return the {@link Short} or {@link Optional#empty()}
91+
*/
92+
@NotNull
93+
public static Optional<Short> toShort(@Nullable Object object) {
94+
return object == null ? Optional.empty() : MiscUtility.handleException(() -> Short.parseShort(object.toString()), NumberFormatException.class);
95+
}
96+
97+
/**
98+
* Converts an {@link Object} to a {@link Byte}
99+
*
100+
* @param object the {@link Object} to convert
101+
*
102+
* @return the {@link Byte} or {@link Optional#empty()}
103+
*/
104+
@NotNull
105+
public static Optional<Byte> toByte(@Nullable Object object) {
106+
return object == null ? Optional.empty() : MiscUtility.handleException(() -> Byte.parseByte(object.toString()), NumberFormatException.class);
107+
}
108+
109+
/**
110+
* Converts an {@link Object} to a {@link BigInteger}
111+
*
112+
* @param object the {@link Object} to convert
113+
*
114+
* @return the {@link BigInteger} or {@link Optional#empty()}
115+
*/
116+
@NotNull
117+
public static Optional<BigInteger> toBigInteger(@Nullable Object object) {
118+
return object == null ? Optional.empty() : MiscUtility.handleException(() -> new BigInteger(object.toString()), NumberFormatException.class);
119+
}
120+
121+
/**
122+
* Converts an {@link Object} to a {@link BigDecimal}
123+
*
124+
* @param object the {@link Object} to convert
125+
*
126+
* @return the {@link BigDecimal} or {@link Optional#empty()}
127+
*/
128+
@NotNull
129+
public static Optional<BigDecimal> toBigDecimal(@Nullable Object object) {
130+
return object == null ? Optional.empty() : MiscUtility.handleException(() -> new BigDecimal(object.toString()), NumberFormatException.class);
131+
}
132+
73133
/**
74134
* Converts an {@link Object} to a {@link UUID}
75135
*

0 commit comments

Comments
 (0)