Skip to content

Commit 17d829e

Browse files
committed
Number Utils
1 parent e89b8d8 commit 17d829e

File tree

3 files changed

+161
-1
lines changed

3 files changed

+161
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ Below topics/problems are covered as of now.
151151

152152
**17. Common Utils**
153153
- [X] [StringUtils](../master/src/com/deepak/data/structures/Utils/StringUtils.java)
154-
- [ ] NumberUtils
154+
- [X] [NumberUtils](../master/src/com/deepak/data/structures/Utils/NumberUtils.java)
155155
- [X] [ArrayUtils](../master/src/com/deepak/data/structures/Utils/ArrayUtils.java)
156156
- [ ] ListUtils
157157
- [X] [BooleanUtils](../master/src/com/deepak/data/structures/Utils/BooleanUtils.java)
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* NumberUtils.java
4+
*/
5+
package com.deepak.data.structures.Utils;
6+
7+
/**
8+
* Utilities for Numbers
9+
* Note : All of these methods are for integers only,
10+
* All others like, Double, float etc will have same implementation
11+
*
12+
* @author Deepak
13+
*/
14+
public class NumberUtils {
15+
16+
/**
17+
* Method to convert string to Integer
18+
*
19+
* @param str
20+
* @return {@link int}
21+
*/
22+
public static int toInt(final String str) {
23+
return toInt(str, 0);
24+
}
25+
26+
/**
27+
* Method to convert string to Integer when a default value is given
28+
*
29+
* @param str
30+
* @param defaultValue
31+
* @return {@link int}
32+
*/
33+
public static int toInt(final String str, int defaultValue) {
34+
if (str == null) {
35+
return defaultValue;
36+
}
37+
try {
38+
return Integer.parseInt(str);
39+
} catch (final NumberFormatException exception) {
40+
return defaultValue;
41+
}
42+
}
43+
44+
/**
45+
* Method to check if string has all zeros
46+
*
47+
* @param str
48+
* @return {@link boolean}
49+
*/
50+
public static boolean isAllZeros(final String str) {
51+
if (str == null) {
52+
return true;
53+
}
54+
for (int i = 0; i < str.length(); i++) {
55+
if (str.charAt(i) != '0') {
56+
return false;
57+
}
58+
}
59+
return str.length() > 0;
60+
}
61+
62+
/**
63+
* Find minimum out of Integer array
64+
*
65+
* @param array
66+
* @return {@link int}
67+
*/
68+
public static int min(final int... array) {
69+
if (array == null) {
70+
return 0;
71+
}
72+
int min = array[0];
73+
for (int i = 0; i < array.length; i++) {
74+
if (array[i] < min) {
75+
min = array[i];
76+
}
77+
}
78+
return min;
79+
}
80+
81+
/**
82+
* Find maximum out of Integer array
83+
*
84+
* @param array
85+
* @return {@link int}
86+
*/
87+
public static int max(final int... array) {
88+
if (array == null) {
89+
return 0;
90+
}
91+
int max = array[0];
92+
for (int i = 0; i < array.length; i++) {
93+
if (array[i] > max) {
94+
max = array[i];
95+
}
96+
}
97+
return max;
98+
}
99+
100+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* Data-Structures-In-Java
3+
* NumberUtilsTest.java
4+
*/
5+
package com.deepak.data.structures.Utils;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* Test cases for Number Utility Functions
12+
*
13+
* @author Deepak
14+
*/
15+
public class NumberUtilsTest {
16+
17+
/**
18+
* Test case to convert string to Integer
19+
*/
20+
@Test
21+
public void testToInt() {
22+
Assert.assertEquals(NumberUtils.toInt(null), 0);
23+
Assert.assertEquals(NumberUtils.toInt(""), 0);
24+
Assert.assertEquals(NumberUtils.toInt("1"), 1);
25+
Assert.assertEquals(NumberUtils.toInt(null, 1), 1);
26+
Assert.assertEquals(NumberUtils.toInt("", 1), 1);
27+
Assert.assertEquals(NumberUtils.toInt("1", 0), 1);
28+
}
29+
30+
/**
31+
* Method to test all zeros
32+
*/
33+
@Test
34+
public void testAllZeros() {
35+
Assert.assertEquals(NumberUtils.isAllZeros(null), true);
36+
Assert.assertEquals(NumberUtils.isAllZeros("000000"), true);
37+
Assert.assertEquals(NumberUtils.isAllZeros("000001"), false);
38+
}
39+
40+
/**
41+
* Method to test find minimum out of a given array
42+
*/
43+
@Test
44+
public void testFindMin() {
45+
int[] array = {1, 3, 6, 9, 10, 7};
46+
Assert.assertEquals(NumberUtils.min(array), 1);
47+
Assert.assertEquals(NumberUtils.min(null), 0);
48+
}
49+
50+
/**
51+
* Method to test find maximum out of a given array
52+
*/
53+
@Test
54+
public void testFindMax() {
55+
int[] array = {1, 3, 6, 9, 10, 7};
56+
Assert.assertEquals(NumberUtils.max(array), 10);
57+
Assert.assertEquals(NumberUtils.max(null), 0);
58+
}
59+
60+
}

0 commit comments

Comments
 (0)