Skip to content

Commit d7c7ea1

Browse files
Added new functionalities
1 parent 93abb4e commit d7c7ea1

8 files changed

Lines changed: 67 additions & 28 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ and the dependency:
2323
<dependency>
2424
<groupId>com.mauriciotogneri</groupId>
2525
<artifactId>javautils</artifactId>
26-
<version>1.4.0</version>
26+
<version>1.5.0</version>
2727
</dependency>
2828
```
2929

@@ -32,6 +32,6 @@ or if you use Gradle:
3232
```groovy
3333
dependencies
3434
{
35-
compile 'com.mauriciotogneri:javautils:1.4.0'
35+
compile 'com.mauriciotogneri:javautils:1.5.0'
3636
}
3737
```

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<modelVersion>4.0.0</modelVersion>
88
<groupId>com.mauriciotogneri</groupId>
99
<artifactId>javautils</artifactId>
10-
<version>1.4.0</version>
10+
<version>1.5.0</version>
1111
<name>Java Utils</name>
1212
<packaging>jar</packaging>
1313
<url>https://github.com/mauriciotogneri/java-utils</url>

src/main/java/com/mauriciotogneri/javautils/FormattedNumber.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ private FormattedNumber(Locale locale, Integer minDecimals, Integer maxDecimals,
1818
this.withSign = withSign;
1919
}
2020

21-
public String format(Double value)
21+
public String format(double value)
2222
{
2323
NumberFormat numberFormat = NumberFormat.getNumberInstance(locale);
2424

src/main/java/com/mauriciotogneri/javautils/Json.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class Json
3535
{
3636
private static final Gson gson = new Gson();
3737

38-
public static Gson create(Boolean prettyPrint)
38+
public static Gson create(boolean prettyPrint)
3939
{
4040
return builder(prettyPrint).create();
4141
}
@@ -108,6 +108,11 @@ public static <A, B extends JsonToObject<A>> A[] array(B[] list, Class<A> clazz)
108108
return result;
109109
}
110110

111+
public static <T> T jsonToObject(JsonToObject<T> object)
112+
{
113+
return (object != null) ? object.object() : null;
114+
}
115+
111116
public interface JsonToObject<T>
112117
{
113118
T object();
@@ -241,7 +246,7 @@ public void write(JsonWriter out, T value) throws IOException
241246
}
242247
}
243248

244-
public static GsonBuilder builder(Boolean prettyPrint)
249+
public static GsonBuilder builder(boolean prettyPrint)
245250
{
246251
GsonBuilder builder = new GsonBuilder();
247252
builder.registerTypeAdapter(DateTime.class, new DateTimeTypeAdapter());

src/main/java/com/mauriciotogneri/javautils/Lists.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,16 @@ public static <T> T[] asArray(Collection<T> list, Class<T> clazz)
2626
return result;
2727
}
2828

29+
@SuppressWarnings("unchecked")
30+
private <T> T[] arrayAppend(T[] array, T value, Class<T> clazz)
31+
{
32+
T[] result = (T[]) Array.newInstance(clazz, array.length + 1);
33+
System.arraycopy(array, 0, result, 0, array.length);
34+
result[result.length - 1] = value;
35+
36+
return result;
37+
}
38+
2939
public static <T> List<T> split(String list, String separator, Factory<T> factory)
3040
{
3141
List<T> result = new ArrayList<>();

src/main/java/com/mauriciotogneri/javautils/Randomized.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,67 +11,67 @@ private Randomized()
1111
}
1212

1313
// 3 out of 5 => [ 0 1 2 ] 3 4
14-
public static Boolean chance(Integer valid, Integer outOf)
14+
public static boolean chance(int valid, int outOf)
1515
{
1616
return (nextInt(outOf) < valid);
1717
}
1818

19-
public static Boolean chance(Integer outOf)
19+
public static boolean chance(int outOf)
2020
{
2121
return (nextInt(outOf) == 0);
2222
}
2323

24-
public static Integer of(Integer min, Integer max)
24+
public static int of(int min, int max)
2525
{
2626
return nextInt((max - min) + 1) + min;
2727
}
2828

29-
public static Integer nextInt()
29+
public static int nextInt()
3030
{
3131
return random.nextInt();
3232
}
3333

34-
public static Integer nextInt(Integer max)
34+
public static int nextInt(int max)
3535
{
3636
return random.nextInt(max);
3737
}
3838

39-
public static Long nextLong()
39+
public static long nextLong()
4040
{
4141
return random.nextLong();
4242
}
4343

44-
public static Double nextDouble()
44+
public static double nextDouble()
4545
{
4646
return random.nextDouble();
4747
}
4848

49-
public static Double nextDouble(Integer places)
49+
public static double nextDouble(int places)
5050
{
5151
return nextDouble() * Math.pow(10, places);
5252
}
5353

54-
public static Double nextDouble(Double max)
54+
public static double nextDouble(double max)
5555
{
5656
return nextDouble() * max;
5757
}
5858

59-
public static Double nextSignedDouble()
59+
public static double nextSignedDouble()
6060
{
6161
return nextDouble() * sign();
6262
}
6363

64-
public static Double nextSignedDouble(Double max)
64+
public static double nextSignedDouble(double max)
6565
{
6666
return nextDouble(max) * sign();
6767
}
6868

69-
public static Integer sign()
69+
public static int sign()
7070
{
7171
return (nextBoolean() ? 1 : -1);
7272
}
7373

74-
public static Boolean nextBoolean()
74+
public static boolean nextBoolean()
7575
{
7676
return random.nextBoolean();
7777
}

src/main/java/com/mauriciotogneri/javautils/Resource.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,13 @@ public static String string(InputStream inputStream)
2323
public static byte[] bytes(InputStream inputStream) throws IOException
2424
{
2525
int read;
26-
int size = 1024;
27-
byte[] buf = new byte[size];
26+
byte[] buffer = new byte[1024];
2827

2928
ByteArrayOutputStream bos = new ByteArrayOutputStream();
3029

31-
while ((read = inputStream.read(buf, 0, size)) != -1)
30+
while ((read = inputStream.read(buffer, 0, buffer.length)) != -1)
3231
{
33-
bos.write(buf, 0, read);
32+
bos.write(buffer, 0, read);
3433
}
3534

3635
return bos.toByteArray();
@@ -68,9 +67,29 @@ public static void save(File file, String content) throws IOException
6867
}
6968
}
7069

71-
public static Boolean close(Closeable resource)
70+
public static boolean close(Closeable resource)
7271
{
73-
Boolean result = false;
72+
boolean result = false;
73+
74+
if (resource != null)
75+
{
76+
try
77+
{
78+
resource.close();
79+
result = true;
80+
}
81+
catch (Exception e)
82+
{
83+
// exception swallowed on purpose
84+
}
85+
}
86+
87+
return result;
88+
}
89+
90+
public static boolean close(AutoCloseable resource)
91+
{
92+
boolean result = false;
7493

7594
if (resource != null)
7695
{

src/main/java/com/mauriciotogneri/javautils/Strings.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,21 @@ private Strings()
88
{
99
}
1010

11-
public static Boolean isEmpty(String value)
11+
public static boolean isEmpty(String value)
1212
{
1313
return (value == null) || (value.isEmpty());
1414
}
1515

16-
public static Boolean isNotEmpty(String value)
16+
public static boolean isNotEmpty(String value)
1717
{
1818
return !isEmpty(value);
1919
}
2020

21+
public static String capitalize(String text)
22+
{
23+
return isEmpty(text) ? text : (text.substring(0, 1).toUpperCase() + text.substring(1));
24+
}
25+
2126
public static String trim(String value)
2227
{
2328
return (value != null) ? value.trim() : null;
@@ -45,7 +50,7 @@ public static String join(Object[] list, String separator)
4550
return join(Arrays.asList(list), separator);
4651
}
4752

48-
public static Boolean equals(String a, String b)
53+
public static boolean equals(String a, String b)
4954
{
5055
return (a != null) && (b != null) && (a.equals(b));
5156
}

0 commit comments

Comments
 (0)