Skip to content

Commit 8bc490c

Browse files
committed
Expanded and refined the utility classes
1 parent a698075 commit 8bc490c

4 files changed

Lines changed: 218 additions & 21 deletions

File tree

‎api/src/main/java/org/ocpsoft/common/util/Assert.java‎

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2+
* Copyright 2012 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,24 +16,56 @@
1616
package org.ocpsoft.common.util;
1717

1818
/**
19-
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
19+
* Utility methods for making precondition/postcondition assertions.
2020
*
21+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2122
*/
22-
public class Assert
23+
public final class Assert
2324
{
24-
public static void notNull(final Object object, final String message) throws IllegalStateException
25+
private Assert()
26+
{}
27+
28+
/**
29+
* Throw an exception if the given {@link Object} is not null.
30+
*/
31+
public static void isNull(final Object object, final String message) throws IllegalArgumentException
32+
{
33+
if (object != null)
34+
{
35+
throw new IllegalArgumentException(message);
36+
}
37+
}
38+
39+
/**
40+
* Throw an exception if the given {@link Object} is null.
41+
*/
42+
public static void notNull(final Object object, final String message) throws IllegalArgumentException
2543
{
2644
if (object == null)
2745
{
2846
throw new IllegalArgumentException(message);
2947
}
3048
}
3149

32-
public static void assertTrue(final boolean value, final String message)
50+
/**
51+
* Throw an exception if the given value is not true.
52+
*/
53+
public static void assertTrue(final boolean value, final String message) throws IllegalArgumentException
3354
{
3455
if (value != true)
3556
{
3657
throw new IllegalArgumentException(message);
3758
}
3859
}
60+
61+
/**
62+
* Throw an exception if the given value is not false.
63+
*/
64+
public static void assertFalse(final boolean value, final String message) throws IllegalArgumentException
65+
{
66+
if (value != false)
67+
{
68+
throw new IllegalArgumentException(message);
69+
}
70+
}
3971
}

‎api/src/main/java/org/ocpsoft/common/util/Iterators.java‎

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2+
* Copyright 2012 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,15 +16,24 @@
1616
package org.ocpsoft.common.util;
1717

1818
import java.util.ArrayList;
19+
import java.util.HashSet;
1920
import java.util.Iterator;
2021
import java.util.List;
22+
import java.util.Set;
2123

2224
/**
23-
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
25+
* Utility methods for working with {@link Iterator} and {@link Iterable} instances.
2426
*
27+
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2528
*/
26-
public class Iterators
29+
public final class Iterators
2730
{
31+
public Iterators()
32+
{}
33+
34+
/**
35+
* Return the elements of the given {@link Iterable} as a {@link List}.
36+
*/
2837
public static <T> List<T> asList(final Iterable<T> iterable)
2938
{
3039
List<T> result = new ArrayList<T>();
@@ -35,6 +44,9 @@ public static <T> List<T> asList(final Iterable<T> iterable)
3544
return result;
3645
}
3746

47+
/**
48+
* Return the elements of the given {@link Iterator} as a {@link List}.
49+
*/
3850
public static <T> List<T> asList(final Iterator<T> iterator)
3951
{
4052
List<T> result = new ArrayList<T>();
@@ -46,18 +58,29 @@ public static <T> List<T> asList(final Iterator<T> iterator)
4658
return result;
4759
}
4860

49-
public static <T> List<T> asUniqueList(final Iterable<T> iterable)
61+
/**
62+
* Return the elements of the given {@link Iterable} as a {@link List}.
63+
*/
64+
public static <T> Set<T> asSet(final Iterable<T> iterable)
5065
{
51-
List<T> result = new ArrayList<T>();
66+
Set<T> result = new HashSet<T>();
5267
for (T t : iterable)
5368
{
54-
boolean found = false;
55-
for (T r : result) {
56-
if ((r != null) && r.getClass().isInstance(t))
57-
found = true;
58-
}
59-
if (!found)
60-
result.add(t);
69+
result.add(t);
70+
}
71+
return result;
72+
}
73+
74+
/**
75+
* Return the elements of the given {@link Iterator} as a {@link List}.
76+
*/
77+
public static <T> Set<T> asSet(final Iterator<T> iterator)
78+
{
79+
Set<T> result = new HashSet<T>();
80+
while (iterator.hasNext())
81+
{
82+
T t = iterator.next();
83+
result.add(t);
6184
}
6285
return result;
6386
}

‎api/src/main/java/org/ocpsoft/common/util/Streams.java‎

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2+
* Copyright 2012 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,15 +15,25 @@
1515
*/
1616
package org.ocpsoft.common.util;
1717

18+
import java.io.ByteArrayInputStream;
19+
import java.io.Closeable;
1820
import java.io.IOException;
1921
import java.io.InputStream;
22+
import java.io.InputStreamReader;
2023
import java.io.OutputStream;
24+
import java.io.Reader;
25+
import java.io.UnsupportedEncodingException;
2126

2227
/**
28+
* Utility methods for working with {@link InputStream} and {@link OutputStream} instances.
29+
*
2330
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2431
*/
2532
public final class Streams
2633
{
34+
public Streams()
35+
{}
36+
2737
private static final int BUFFER_SIZE = 2048;
2838

2939
/**
@@ -56,4 +66,62 @@ public static boolean copy(InputStream input, OutputStream output, int bufferSiz
5666
}
5767
return true;
5868
}
69+
70+
/**
71+
* Return a {@link String} containing the contents of the given {@link InputStream}
72+
*/
73+
public static String toString(final InputStream stream)
74+
{
75+
StringBuilder out = new StringBuilder();
76+
try
77+
{
78+
final char[] buffer = new char[0x10000];
79+
Reader in = new InputStreamReader(stream, "UTF-8");
80+
int read;
81+
do
82+
{
83+
read = in.read(buffer, 0, buffer.length);
84+
if (read > 0)
85+
{
86+
out.append(buffer, 0, read);
87+
}
88+
}
89+
while (read >= 0);
90+
}
91+
catch (UnsupportedEncodingException e)
92+
{
93+
throw new RuntimeException(e);
94+
}
95+
catch (IOException e)
96+
{
97+
throw new RuntimeException(e);
98+
}
99+
return out.toString();
100+
}
101+
102+
/**
103+
* Create an {@link InputStream} from the given {@link String}.
104+
*/
105+
public static InputStream fromString(final String data)
106+
{
107+
return new ByteArrayInputStream(data.getBytes());
108+
}
109+
110+
/**
111+
* Close the given {@link Closeable} without throwing exceptions on failure.
112+
*/
113+
public static void closeQuietly(final Closeable source)
114+
{
115+
if (source != null)
116+
{
117+
try
118+
{
119+
source.close();
120+
}
121+
catch (IOException ignore)
122+
{
123+
124+
}
125+
}
126+
}
59127
}

‎api/src/main/java/org/ocpsoft/common/util/Strings.java‎

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2+
* Copyright 2012 <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,12 +19,74 @@
1919
import java.util.Iterator;
2020

2121
/**
22+
* Utility methods for manipulating {@link String} instances
23+
*
2224
* @author <a href="mailto:lincolnbaxter@gmail.com">Lincoln Baxter, III</a>
2325
*/
24-
public abstract class Strings
26+
public final class Strings
2527
{
28+
public Strings()
29+
{}
30+
31+
/**
32+
* Return true if the given {@link String} instances are equal, or if both {@link String} instances are null.
33+
*/
34+
public static boolean areEqual(final String left, final String right)
35+
{
36+
if ((left == null) && (right == null))
37+
{
38+
return true;
39+
}
40+
else if ((left == null) || (right == null))
41+
{
42+
return false;
43+
}
44+
return left.equals(right);
45+
}
46+
47+
/**
48+
* Return true if the given {@link String} instances are equal when outer whitespace is removed, or if both
49+
* {@link String} instances are null. (E.g.: " hello world " is equal to "hello world ")
50+
*/
51+
public static boolean areEqualTrimmed(final String left, final String right)
52+
{
53+
if ((left != null) && (right != null))
54+
{
55+
return left.trim().equals(right.trim());
56+
}
57+
return areEqual(left, right);
58+
}
59+
60+
/**
61+
* Capitalize the given {@link String}: "input" -> "Input"
62+
*/
63+
public static String capitalize(final String input)
64+
{
65+
if ((input == null) || (input.length() == 0))
66+
{
67+
return input;
68+
}
69+
return input.substring(0, 1).toUpperCase() + input.substring(1);
70+
}
71+
2672
/**
27-
* Join a collection of strings using the given delimeter.
73+
* Return true if the given {@link String} is null or is empty.
74+
*/
75+
public static boolean isNullOrEmpty(final String string)
76+
{
77+
return (string == null) || string.isEmpty();
78+
}
79+
80+
/**
81+
* Return true if the given {@link String} equals "true", case insensitive; otherwise return false.
82+
*/
83+
public static boolean isTrue(final String value)
84+
{
85+
return value == null ? false : "true".equalsIgnoreCase(value.trim());
86+
}
87+
88+
/**
89+
* Join a {@link Collection} of {@link String} instances using the given delimiter.
2890
*/
2991
public static String join(final Collection<?> collection, final String delimiter)
3092
{
@@ -40,4 +102,16 @@ public static String join(final Collection<?> collection, final String delimiter
40102
}
41103
return buffer.toString();
42104
}
105+
106+
/**
107+
* Uncapitalize the given {@link String}. (E.g.: "CamelCase" -> "camelCase")
108+
*/
109+
public static String uncapitalize(final String input)
110+
{
111+
if ((input == null) || (input.length() == 0))
112+
{
113+
return input;
114+
}
115+
return input.substring(0, 1).toLowerCase() + input.substring(1);
116+
}
43117
}

0 commit comments

Comments
 (0)