@@ -111,6 +111,26 @@ public static boolean isLong(@Nullable String string) {
111111 }
112112 }
113113
114+ /**
115+ * Convert a string to int object or null if this string is null or not a number.
116+ *
117+ * @param string the string to convert.
118+ * @return the int object or null.
119+ * @since 9.4.0
120+ */
121+ public static @ Nullable Integer safeToInt (@ Nullable String string ) {
122+
123+ if (string == null ) {
124+ return null ;
125+ } else {
126+ try {
127+ return Integer .valueOf (string );
128+ } catch (NumberFormatException e ) {
129+ return null ;
130+ }
131+ }
132+ }
133+
114134 /**
115135 * Convert a string to long object or null if this string is null or not a number.
116136 *
@@ -151,6 +171,98 @@ public static boolean isLong(@Nullable String string) {
151171 }
152172 }
153173
174+ /**
175+ * Set a bit in a number by a pos to 1.
176+ *
177+ * @param value the byte.
178+ * @param pos the bit position.
179+ * @return the update number.
180+ */
181+ public static int setBit (int value , int pos ) {
182+ return value | (1 << pos );
183+ }
184+
185+ /**
186+ * Set a bit in a number by a pos to 0.
187+ *
188+ * @param value the byte.
189+ * @param pos the bit position.
190+ * @return the update number.
191+ */
192+ public static int unsetBit (int value , int pos ) {
193+ return value & ~(1 << pos );
194+ }
195+
196+ /**
197+ * Return true if bit by pos in a byte is 1.
198+ *
199+ * @param value the byte.
200+ * @return true if the bit is 1.
201+ */
202+ public static boolean isSetBit (int value , int pos ) {
203+ return (value & (1L << pos )) != 0 ;
204+ }
205+
206+ /**
207+ * Return true if bit by pos in a byte is 0.
208+ *
209+ * @param value the byte.
210+ * @return true if the bit is 0.
211+ */
212+ public static boolean isNotSetBit (int value , int pos ) {
213+ return (value & (1L << pos )) == 0 ;
214+ }
215+
216+ /**
217+ * Set last high 4 bits to a byte.
218+ *
219+ * @param value the byte value.
220+ * @return the result value with updating last high 4 bits.
221+ */
222+ public static int setHighByteBits (int value , int highBits ) {
223+ return value | highBits << 4 ;
224+ }
225+
226+ /**
227+ * Get last high 4 bits from a byte.
228+ *
229+ * @param value the byte value.
230+ * @return the value of last 4 high bits.
231+ */
232+ public static byte getHighByteBits (int value ) {
233+ return (byte ) (value >> 4 );
234+ }
235+
236+ /**
237+ * Set first low 4 bits to a byte.
238+ *
239+ * @param value the byte value.
240+ * @return the result value with updating first low 4 bits.
241+ */
242+ public static int setLowByteBits (int value , int lowBits ) {
243+ return value | lowBits & 0x0F ;
244+ }
245+
246+ /**
247+ * Get first low 4 bits from a byte.
248+ *
249+ * @param value the byte value.
250+ * @return the value of last 4 low bits.
251+ */
252+ public static byte getLowByteBits (int value ) {
253+ return (byte ) (value & 0x0F );
254+ }
255+
256+ /**
257+ * Covert a byte to unsigned byte.
258+ *
259+ * @param value the byte.
260+ * @return the unsigned byte.
261+ */
262+ public static int toUnsignedByte (byte value ) {
263+ return value & 0xFF ;
264+ }
265+
154266 private NumberUtils () {
155267 throw new IllegalArgumentException ();
156268 }
0 commit comments