Skip to content

Commit 3c98414

Browse files
authored
Merge pull request #3 from playmoweb/new-helper-methods
New method added on ListUtils and StringUtils. Also added some tests
2 parents 8bf9853 + c09dcaf commit 3c98414

File tree

10 files changed

+144
-50
lines changed

10 files changed

+144
-50
lines changed

.travis.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
language: android
2+
android:
3+
components:
4+
- tools
5+
- platform-tools
6+
- tools
7+
8+
# The BuildTools version used by your project
9+
- build-tools-27.0.3
10+
11+
# The SDK version used to compile your project
12+
- android-27
13+
licenses:
14+
- 'android-sdk-license-.+'
15+
16+
script:
17+
- ./gradlew build connectedCheck

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# library-android-utils
1+
# library-android-utils [![Build Status](https://travis-ci.org/playmoweb/library-android-utils.svg?branch=master)](https://travis-ci.org/playmoweb/library-android-utils)
22

33
## Usage
44

@@ -15,7 +15,7 @@ allprojects {
1515
Step 2. Add the dependency
1616
```
1717
dependencies {
18-
compile "com.github.playmoweb:library-android-utils:1.0.0"
18+
compile "com.github.playmoweb:library-android-utils:1.0.2"
1919
}
2020
```
2121

android-utils/build.gradle

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
apply plugin: 'com.android.library'
22

33
android {
4+
buildToolsVersion '27.0.3'
45
compileSdkVersion 27
56
defaultConfig {
67
minSdkVersion 16
78
targetSdkVersion 27
89
versionCode 1
9-
versionName "1.0.0"
10+
versionName "1.0.2"
1011
}
1112
}
1213

1314
dependencies {
14-
implementation 'com.android.support:support-annotations:27.1.0'
15+
implementation "com.android.support:support-annotations:27.1.1"
16+
testImplementation "junit:junit:4.12"
1517
}

android-utils/src/main/java/com/playmoweb/android/utils/ListUtils.java

+10
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.playmoweb.android.utils;
22

3+
import java.lang.reflect.Array;
34
import java.util.ArrayList;
5+
import java.util.Collection;
46
import java.util.Collections;
57
import java.util.List;
68

@@ -80,4 +82,12 @@ public static List<Integer> range(final int from, final int to) {
8082
}
8183
return output;
8284
}
85+
86+
public static <T> T[] toArray(Collection<? extends T> itr, Class<T> type) {
87+
return itr.toArray(newArray(type, itr.size()));
88+
}
89+
90+
public static <T> T[] newArray(Class<T> type, int length) {
91+
return (T[]) Array.newInstance(type, length);
92+
}
8393
}

android-utils/src/main/java/com/playmoweb/android/utils/StringUtils.java

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package com.playmoweb.android.utils;
22

3+
import android.support.annotation.NonNull;
4+
import android.support.annotation.Nullable;
5+
6+
import java.util.Collection;
37
import java.util.regex.Matcher;
48
import java.util.regex.Pattern;
59

@@ -10,7 +14,16 @@
1014
*/
1115
public class StringUtils {
1216

13-
public static String decodeUnicode(final String encoded) {
17+
/**
18+
* Returns true if the string is null or 0-length.
19+
* @param str the string to be examined
20+
* @return true if str is null or zero length
21+
*/
22+
public static boolean isEmpty(@Nullable CharSequence str) {
23+
return str == null || str.length() == 0;
24+
}
25+
26+
public static String decodeUnicode(@NonNull final String encoded) {
1427
final Pattern p = Pattern.compile("%u([0-9a-f]{4})", Pattern.CASE_INSENSITIVE);
1528

1629
final Matcher m = p.matcher(encoded);
@@ -24,22 +37,26 @@ public static String decodeUnicode(final String encoded) {
2437
return decoded.toString();
2538
}
2639

27-
public static String capitalizeFirstLetter(final String original) {
40+
public static String capitalizeFirstLetter(@Nullable final String original) {
2841
if (original == null || original.length() == 0) {
2942
return original;
3043
}
3144
return original.substring(0, 1).toUpperCase() + original.substring(1);
3245
}
3346

47+
public static String implode(final String separator, final Collection<String> data) {
48+
return implode(separator, ListUtils.toArray(data, String.class));
49+
}
50+
3451
public static String implode(final String separator, final String... data) {
3552
final StringBuilder sb = new StringBuilder();
3653
for (int i = 0; i < data.length - 1; i++) {
37-
//data.length - 1 => to not add separator at the end
38-
if (!data[i].matches(" *")) {//empty string are ""; " "; " "; and so on
54+
if (!isEmpty(data[i]) && !data[i].matches(" *")) {//empty string are ""; " "; " "; and so on
3955
sb.append(data[i]);
4056
sb.append(separator);
4157
}
4258
}
59+
//data.length - 1 => to not add separator at the end
4360
sb.append(data[data.length - 1].trim());
4461
return sb.toString();
4562
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.playmoweb.android.utils;
2+
3+
import org.junit.Test;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
/**
11+
* StringUtilsTest
12+
*
13+
* @author Playmoweb
14+
*/
15+
public class StringUtilsTest {
16+
17+
@Test
18+
public void testImplodeOne() {
19+
String result = StringUtils.implode(", ", "test1");
20+
assertEquals("test1", result);
21+
}
22+
23+
@Test
24+
public void testImplodeTwo() {
25+
String result = StringUtils.implode(", ", "test1", "test2");
26+
assertEquals("test1, test2", result);
27+
}
28+
29+
@Test
30+
public void testImplodeList() {
31+
List<String> strings = new ArrayList<>(2);
32+
strings.add("test1");
33+
strings.add("test2");
34+
String result = StringUtils.implode(", ", strings);
35+
assertEquals("test1, test2", result);
36+
}
37+
38+
@Test
39+
public void testImplodeWithNull() {
40+
String result = StringUtils.implode(", ", "test1", null, "test2");
41+
assertEquals("test1, test2", result);
42+
}
43+
}

gradle/wrapper/gradle-wrapper.jar

1.05 KB
Binary file not shown.
+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
#Fri Mar 23 10:13:38 CET 2018
21
distributionBase=GRADLE_USER_HOME
32
distributionPath=wrapper/dists
43
zipStoreBase=GRADLE_USER_HOME
54
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
5+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.7-all.zip

gradlew

+42-30
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,43 @@
1-
#!/usr/bin/env bash
1+
#!/usr/bin/env sh
22

33
##############################################################################
44
##
55
## Gradle start up script for UN*X
66
##
77
##############################################################################
88

9-
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10-
DEFAULT_JVM_OPTS=""
9+
# Attempt to set APP_HOME
10+
# Resolve links: $0 may be a link
11+
PRG="$0"
12+
# Need this for relative symlinks.
13+
while [ -h "$PRG" ] ; do
14+
ls=`ls -ld "$PRG"`
15+
link=`expr "$ls" : '.*-> \(.*\)$'`
16+
if expr "$link" : '/.*' > /dev/null; then
17+
PRG="$link"
18+
else
19+
PRG=`dirname "$PRG"`"/$link"
20+
fi
21+
done
22+
SAVED="`pwd`"
23+
cd "`dirname \"$PRG\"`/" >/dev/null
24+
APP_HOME="`pwd -P`"
25+
cd "$SAVED" >/dev/null
1126

1227
APP_NAME="Gradle"
1328
APP_BASE_NAME=`basename "$0"`
1429

30+
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31+
DEFAULT_JVM_OPTS=""
32+
1533
# Use the maximum available, or set MAX_FD != -1 to use that value.
1634
MAX_FD="maximum"
1735

18-
warn ( ) {
36+
warn () {
1937
echo "$*"
2038
}
2139

22-
die ( ) {
40+
die () {
2341
echo
2442
echo "$*"
2543
echo
@@ -30,6 +48,7 @@ die ( ) {
3048
cygwin=false
3149
msys=false
3250
darwin=false
51+
nonstop=false
3352
case "`uname`" in
3453
CYGWIN* )
3554
cygwin=true
@@ -40,26 +59,11 @@ case "`uname`" in
4059
MINGW* )
4160
msys=true
4261
;;
62+
NONSTOP* )
63+
nonstop=true
64+
;;
4365
esac
4466

45-
# Attempt to set APP_HOME
46-
# Resolve links: $0 may be a link
47-
PRG="$0"
48-
# Need this for relative symlinks.
49-
while [ -h "$PRG" ] ; do
50-
ls=`ls -ld "$PRG"`
51-
link=`expr "$ls" : '.*-> \(.*\)$'`
52-
if expr "$link" : '/.*' > /dev/null; then
53-
PRG="$link"
54-
else
55-
PRG=`dirname "$PRG"`"/$link"
56-
fi
57-
done
58-
SAVED="`pwd`"
59-
cd "`dirname \"$PRG\"`/" >/dev/null
60-
APP_HOME="`pwd -P`"
61-
cd "$SAVED" >/dev/null
62-
6367
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
6468

6569
# Determine the Java command to use to start the JVM.
@@ -85,7 +89,7 @@ location of your Java installation."
8589
fi
8690

8791
# Increase the maximum file descriptors if we can.
88-
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
92+
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
8993
MAX_FD_LIMIT=`ulimit -H -n`
9094
if [ $? -eq 0 ] ; then
9195
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
@@ -150,11 +154,19 @@ if $cygwin ; then
150154
esac
151155
fi
152156

153-
# Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154-
function splitJvmOpts() {
155-
JVM_OPTS=("$@")
157+
# Escape application args
158+
save () {
159+
for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160+
echo " "
156161
}
157-
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158-
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
162+
APP_ARGS=$(save "$@")
163+
164+
# Collect all arguments for the java command, following the shell quoting and substitution rules
165+
eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166+
167+
# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168+
if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169+
cd "$(dirname "$0")"
170+
fi
159171

160-
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
172+
exec "$JAVACMD" "$@"

gradlew.bat

+4-10
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
@rem Set local scope for the variables with windows NT shell
99
if "%OS%"=="Windows_NT" setlocal
1010

11-
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12-
set DEFAULT_JVM_OPTS=
13-
1411
set DIRNAME=%~dp0
1512
if "%DIRNAME%" == "" set DIRNAME=.
1613
set APP_BASE_NAME=%~n0
1714
set APP_HOME=%DIRNAME%
1815

16+
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17+
set DEFAULT_JVM_OPTS=
18+
1919
@rem Find java.exe
2020
if defined JAVA_HOME goto findJavaFromJavaHome
2121

@@ -46,10 +46,9 @@ echo location of your Java installation.
4646
goto fail
4747

4848
:init
49-
@rem Get command-line arguments, handling Windowz variants
49+
@rem Get command-line arguments, handling Windows variants
5050

5151
if not "%OS%" == "Windows_NT" goto win9xME_args
52-
if "%@eval[2+2]" == "4" goto 4NT_args
5352

5453
:win9xME_args
5554
@rem Slurp the command line arguments.
@@ -60,11 +59,6 @@ set _SKIP=2
6059
if "x%~1" == "x" goto execute
6160

6261
set CMD_LINE_ARGS=%*
63-
goto execute
64-
65-
:4NT_args
66-
@rem Get arguments from the 4NT Shell from JP Software
67-
set CMD_LINE_ARGS=%$
6862

6963
:execute
7064
@rem Setup the command line

0 commit comments

Comments
 (0)