155 |
156 |
157 |
158 |
--------------------------------------------------------------------------------
/doc/javadoc/package-list:
--------------------------------------------------------------------------------
1 | de.mrapp.android.validation
2 | de.mrapp.android.validation.adapter
3 | de.mrapp.android.validation.constraints
4 | de.mrapp.android.validation.constraints.text
5 | de.mrapp.android.validation.validators
6 | de.mrapp.android.validation.validators.misc
7 | de.mrapp.android.validation.validators.text
8 |
--------------------------------------------------------------------------------
/doc/javadoc/script.js:
--------------------------------------------------------------------------------
1 | function show(type)
2 | {
3 | count = 0;
4 | for (var key in methods) {
5 | var row = document.getElementById(key);
6 | if ((methods[key] & type) != 0) {
7 | row.style.display = '';
8 | row.className = (count++ % 2) ? rowColor : altColor;
9 | }
10 | else
11 | row.style.display = 'none';
12 | }
13 | updateTabs(type);
14 | }
15 |
16 | function updateTabs(type)
17 | {
18 | for (var value in tabs) {
19 | var sNode = document.getElementById(tabs[value][0]);
20 | var spanNode = sNode.firstChild;
21 | if (value == type) {
22 | sNode.className = activeTableTab;
23 | spanNode.innerHTML = tabs[value][1];
24 | }
25 | else {
26 | sNode.className = tableTab;
27 | spanNode.innerHTML = "" + tabs[value][1] + "";
28 | }
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/example/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/example/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion project.TARGET_SDK_VERSION.toInteger()
5 | buildToolsVersion project.BUILD_TOOLS_VERSION
6 |
7 | defaultConfig {
8 | minSdkVersion project.MIN_SDK_VERSION.toInteger()
9 | targetSdkVersion project.TARGET_SDK_VERSION.toInteger()
10 | versionName project.VERSION_NAME
11 | versionCode project.VERSION_CODE.toInteger()
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | }
17 | }
18 | lintOptions {
19 | disable 'MissingTranslation'
20 | }
21 | }
22 |
23 | dependencies {
24 | api project(':library')
25 | }
--------------------------------------------------------------------------------
/example/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
19 |
20 |
25 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/example/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
21 |
22 |
30 |
31 |
37 |
38 |
47 |
48 |
57 |
58 |
67 |
68 |
74 |
75 |
82 |
83 |
91 |
92 |
100 |
101 |
109 |
110 |
117 |
118 |
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/michael-rapp/AndroidMaterialValidation/ac8693baeac7abddf2e38a47da4c1849d4661a8b/example/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/michael-rapp/AndroidMaterialValidation/ac8693baeac7abddf2e38a47da4c1849d4661a8b/example/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/michael-rapp/AndroidMaterialValidation/ac8693baeac7abddf2e38a47da4c1849d4661a8b/example/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/michael-rapp/AndroidMaterialValidation/ac8693baeac7abddf2e38a47da4c1849d4661a8b/example/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/michael-rapp/AndroidMaterialValidation/ac8693baeac7abddf2e38a47da4c1849d4661a8b/example/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/example/src/main/res/values-large-land/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
17 |
18 | 288dp
19 | 48dp
20 |
21 |
--------------------------------------------------------------------------------
/example/src/main/res/values-large/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
17 |
18 | 96dp
19 | 96dp
20 |
21 |
--------------------------------------------------------------------------------
/example/src/main/res/values/arrays.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
17 |
18 |
19 | Male
20 | Female
21 |
22 |
23 |
--------------------------------------------------------------------------------
/example/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
17 |
18 | #ff000000
19 | #ffde340a
20 | #fff57f17
21 | #ff7cb342
22 | #ff2e7d32
23 |
24 |
25 |
--------------------------------------------------------------------------------
/example/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
17 |
18 | 8dp
19 | 8dp
20 | 16dp
21 | 24dp
22 |
23 |
--------------------------------------------------------------------------------
/example/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
17 |
18 | AndroidMaterialValidation Example
19 | Account
20 | Username
21 | [a–z, A–Z, 0–9, -, _]
22 | Password
23 | Password repetition
24 | Personal data
25 | Gender
26 | First name
27 | Last name
28 | Additional information
29 | May not be empty
30 | Must contain at maximum 32 characters
31 | Must contain at least 4 characters
32 | Must contain at least 2 characters
33 | [a–z, A–Z, 0–9, -, _]
34 | May not contain spaces
35 | Passwords are not equal
36 | An item must be selected
37 | Must begin with an uppercase letter
38 | Must only contain letters
39 | Bad
40 | Low
41 | Sufficient
42 | Good
43 | High
44 | Validate
45 | Safety
46 |
47 |
--------------------------------------------------------------------------------
/example/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
17 |
18 |
23 |
24 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | VERSION_NAME=3.0.1
2 | VERSION_CODE=17
3 | GROUP=com.github.michael-rapp
4 | BUILD_TOOLS_VERSION=28.0.3
5 | MIN_SDK_VERSION=14
6 | TARGET_SDK_VERSION=28
7 |
8 | POM_DESCRIPTION=Provides views, which can be validated according to the Material Design guidelines.
9 | POM_URL=https://github.com/michael-rapp/AndroidMaterialValidation
10 | POM_SCM_URL=https://github.com/michael-rapp/AndroidMaterialValidation.git
11 | POM_SCM_CONNECTION=scm:git@github.com:michael-rapp/AndroidMaterialValidation.git
12 | POM_SCM_DEV_CONNECTION=scm:git@github.com:michael-rapp/AndroidMaterialValidation.git
13 | POM_LICENCE_NAME=Apache License version 2.0
14 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt
15 | POM_LICENCE_DIST=repo
16 | POM_DEVELOPER_ID=michael-rapp
17 | POM_DEVELOPER_NAME=Michael Rapp
18 |
19 | RELEASE_REPOSITORY_URL=https://oss.sonatype.org/service/local/staging/deploy/maven2
20 | SNAPSHOT_REPOSITORY_URL=https://oss.sonatype.org/content/repositories/snapshots
21 |
22 | android.useAndroidX=true
23 | android.enableJetifier=true
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/michael-rapp/AndroidMaterialValidation/ac8693baeac7abddf2e38a47da4c1849d4661a8b/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sat Feb 23 19:20:21 CET 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
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=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # For Cygwin, ensure paths are in UNIX format before anything is touched.
46 | if $cygwin ; then
47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
48 | fi
49 |
50 | # Attempt to set APP_HOME
51 | # Resolve links: $0 may be a link
52 | PRG="$0"
53 | # Need this for relative symlinks.
54 | while [ -h "$PRG" ] ; do
55 | ls=`ls -ld "$PRG"`
56 | link=`expr "$ls" : '.*-> \(.*\)$'`
57 | if expr "$link" : '/.*' > /dev/null; then
58 | PRG="$link"
59 | else
60 | PRG=`dirname "$PRG"`"/$link"
61 | fi
62 | done
63 | SAVED="`pwd`"
64 | cd "`dirname \"$PRG\"`/" >&-
65 | APP_HOME="`pwd -P`"
66 | cd "$SAVED" >&-
67 |
68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
69 |
70 | # Determine the Java command to use to start the JVM.
71 | if [ -n "$JAVA_HOME" ] ; then
72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
73 | # IBM's JDK on AIX uses strange locations for the executables
74 | JAVACMD="$JAVA_HOME/jre/sh/java"
75 | else
76 | JAVACMD="$JAVA_HOME/bin/java"
77 | fi
78 | if [ ! -x "$JAVACMD" ] ; then
79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
80 |
81 | Please set the JAVA_HOME variable in your environment to match the
82 | location of your Java installation."
83 | fi
84 | else
85 | JAVACMD="java"
86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
87 |
88 | Please set the JAVA_HOME variable in your environment to match the
89 | location of your Java installation."
90 | fi
91 |
92 | # Increase the maximum file descriptors if we can.
93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
94 | MAX_FD_LIMIT=`ulimit -H -n`
95 | if [ $? -eq 0 ] ; then
96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
97 | MAX_FD="$MAX_FD_LIMIT"
98 | fi
99 | ulimit -n $MAX_FD
100 | if [ $? -ne 0 ] ; then
101 | warn "Could not set maximum file descriptor limit: $MAX_FD"
102 | fi
103 | else
104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
105 | fi
106 | fi
107 |
108 | # For Darwin, add options to specify how the application appears in the dock
109 | if $darwin; then
110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
111 | fi
112 |
113 | # For Cygwin, switch paths to Windows format before running java
114 | if $cygwin ; then
115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
158 | function splitJvmOpts() {
159 | JVM_OPTS=("$@")
160 | }
161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
163 |
164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
165 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
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 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | 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=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply from: 'https://raw.github.com/chrisbanes/gradle-mvn-push/master/gradle-mvn-push.gradle'
3 |
4 | android {
5 | compileSdkVersion project.TARGET_SDK_VERSION.toInteger()
6 | buildToolsVersion project.BUILD_TOOLS_VERSION
7 |
8 | defaultConfig {
9 | minSdkVersion project.MIN_SDK_VERSION.toInteger()
10 | targetSdkVersion project.TARGET_SDK_VERSION.toInteger()
11 | versionName project.VERSION_NAME
12 | versionCode project.VERSION_CODE.toInteger()
13 | }
14 |
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | api 'androidx.appcompat:appcompat:1.0.2'
24 | api 'com.github.michael-rapp:android-util:2.0.1'
25 | }
26 |
--------------------------------------------------------------------------------
/library/gradle.properties:
--------------------------------------------------------------------------------
1 | POM_NAME=AndroidMaterialValidation
2 | POM_ARTIFACT_ID=android-material-validation
3 | POM_PACKAGING=aar
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/Constraint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation;
15 |
16 | /**
17 | * Defines the interface, a class, which should be able to verify, whether a specific type satisfies
18 | * a constraint, must implement.
19 | *
20 | * @param
21 | * The type of the values, which should be verified
22 | * @author Michael Rapp
23 | * @since 1.0.0
24 | */
25 | public interface Constraint {
26 |
27 | /**
28 | * Returns, whether a specific value satisfies the constraint, or not.
29 | *
30 | * @param value
31 | * The value, which should be verified, as an instance of the generic type Type
32 | * @return True, if the given value satisfies the constraint, false otherwise
33 | */
34 | boolean isSatisfied(Type value);
35 |
36 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/ValidationListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation;
15 |
16 | import androidx.annotation.NonNull;
17 |
18 | /**
19 | * Defines the interface, a class, which should be notified, when a view has been validated, must
20 | * implement.
21 | *
22 | * @param
23 | * The type of the values, which should be validated
24 | * @author Michael Rapp
25 | * @since 1.0.0
26 | */
27 | public interface ValidationListener {
28 |
29 | /**
30 | * The method, which is invoked, when a validation succeeded.
31 | *
32 | * @param view
33 | * The view, whose value has been validated, as an instance of the type {@link
34 | * Validateable}
35 | */
36 | void onValidationSuccess(@NonNull Validateable view);
37 |
38 | /**
39 | * The method, which is invoked, when a validation failed.
40 | *
41 | * @param view
42 | * The view, whose value has been validated, as an instance of the type {@link
43 | * Validateable}
44 | * @param validator
45 | * The validator, which has been failed, as an instance of the type {@link Validator}
46 | */
47 | void onValidationFailure(@NonNull Validateable view, @NonNull Validator validator);
48 |
49 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/Validator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation;
15 |
16 | import android.graphics.drawable.Drawable;
17 |
18 | /**
19 | * Defines the interface, a class, which should be able to validate values of a specific type, must
20 | * implement.
21 | *
22 | * @param
23 | * The type of the values, which should be validated
24 | * @author Michael Rapp
25 | * @since 1.0.0
26 | */
27 | public interface Validator {
28 |
29 | /**
30 | * Validates a specific value.
31 | *
32 | * @param value
33 | * The value, which should be validated, as an instance of the generic type Type
34 | * @return True, if the validation succeeded, false otherwise
35 | */
36 | boolean validate(Type value);
37 |
38 | /**
39 | * Returns the error message, which should be shown, if the validation fails.
40 | *
41 | * @return The error message, which should be shown, if the validation fails, as an instance of
42 | * the type {@link CharSequence}. The error message may not be null
43 | */
44 | CharSequence getErrorMessage();
45 |
46 | /**
47 | * Returns the icon, which should be shown, if the validation fails.
48 | *
49 | * @return The icon, which should be shown, if the validation fails, as an instance of the class
50 | * {@link Drawable} or null, if no icon should be shown
51 | */
52 | Drawable getIcon();
53 |
54 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/constraints/ConjunctiveConstraint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation.constraints;
15 |
16 | import androidx.annotation.NonNull;
17 | import de.mrapp.android.validation.Constraint;
18 | import de.mrapp.util.Condition;
19 |
20 | /**
21 | * A validator, which allows to combine multiple constraints in a conjunctive manner. Only if all
22 | * single constraints are satisfied, the resulting constraint will also be satisfied.
23 | *
24 | * @param
25 | * The type of the values, which should be verified
26 | * @author Michael Rapp
27 | * @since 1.0.0
28 | */
29 | public class ConjunctiveConstraint implements Constraint {
30 |
31 | /**
32 | * A array, which contains the single constraints, the constraint consists of.
33 | */
34 | private Constraint[] constraints;
35 |
36 | /**
37 | * Creates a new constraint, which allows to combine multiple constraints in a conjunctive
38 | * manner.
39 | *
40 | * @param constraints
41 | * The single constraints, the constraint should consist of, as an array of the type
42 | * {@link Constraint}. The constraints may neither be null, nor empty
43 | */
44 | public ConjunctiveConstraint(@NonNull final Constraint[] constraints) {
45 | setConstraints(constraints);
46 | }
47 |
48 | /**
49 | * Creates and returns a constraint, which allows to combine multiple constraints in a
50 | * conjunctive manner.
51 | *
52 | * @param
53 | * The type of the values, which should be verified
54 | * @param constraints
55 | * The single constraints, the constraint should consist of, as an array of the type
56 | * {@link Constraint}. The constraints may neither be null, nor empty
57 | * @return The constraint, which has been created, as an instance of the class {@link
58 | * ConjunctiveConstraint}
59 | */
60 | public static ConjunctiveConstraint create(
61 | @NonNull final Constraint[] constraints) {
62 | return new ConjunctiveConstraint<>(constraints);
63 | }
64 |
65 | /**
66 | * Returns the single constraints, the constraint consists of.
67 | *
68 | * @return The single constraints, the constraint consists of, as an array of the type {@link
69 | * Constraint}
70 | */
71 | public final Constraint[] getConstraints() {
72 | return constraints;
73 | }
74 |
75 | /**
76 | * Sets the single constraints, the constraint should consist of.
77 | *
78 | * @param constraints
79 | * The single constraints, which should be set, as an array of the type {@link
80 | * Constraint}. The constraints may neither be null, nor empty
81 | */
82 | public final void setConstraints(@NonNull final Constraint[] constraints) {
83 | Condition.INSTANCE.ensureNotNull(constraints, "The constraints may not be null");
84 | Condition.INSTANCE.ensureAtLeast(constraints.length, 1, "The constraints may not be empty");
85 | this.constraints = constraints;
86 | }
87 |
88 | @Override
89 | public final boolean isSatisfied(final Type value) {
90 | for (Constraint constraint : constraints) {
91 | if (!constraint.isSatisfied(value)) {
92 | return false;
93 | }
94 | }
95 |
96 | return true;
97 | }
98 |
99 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/constraints/DisjunctiveConstraint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation.constraints;
15 |
16 | import androidx.annotation.NonNull;
17 | import de.mrapp.android.validation.Constraint;
18 | import de.mrapp.util.Condition;
19 |
20 | /**
21 | * A validator, which allows to combine multiple constraints in a disjunctive manner. If at least
22 | * one constraint is satisfied, the resulting constraint will also be satisfied.
23 | *
24 | * @param
25 | * The type of the values, which should be verified
26 | * @author Michael Rapp
27 | * @since 1.0.0
28 | */
29 | public class DisjunctiveConstraint implements Constraint {
30 |
31 | /**
32 | * A array, which contains the single constraints, the constraint consists of.
33 | */
34 | private Constraint[] constraints;
35 |
36 | /**
37 | * Creates a new constraint, which allows to combine multiple constraints in a disjunctive
38 | * manner.
39 | *
40 | * @param constraints
41 | * The single constraints, the constraint should consist of, as an array of the type
42 | * {@link Constraint}. The constraints may neither be null, nor empty
43 | */
44 | public DisjunctiveConstraint(@NonNull final Constraint[] constraints) {
45 | setConstraints(constraints);
46 | }
47 |
48 | /**
49 | * Creates and returns a constraint, which allows to combine multiple constraints in a
50 | * disjunctive manner.
51 | *
52 | * @param
53 | * The type of the values, which should be verified
54 | * @param constraints
55 | * The single constraints, the constraint should consist of, as an array of the type
56 | * {@link Constraint}. The constraints may neither be null, nor empty
57 | * @return The constraint, which has been created, as an instance of the class {@link
58 | * DisjunctiveConstraint}
59 | */
60 | public static DisjunctiveConstraint create(
61 | @NonNull final Constraint[] constraints) {
62 | return new DisjunctiveConstraint<>(constraints);
63 | }
64 |
65 | /**
66 | * Returns the single constraints, the constraint consists of.
67 | *
68 | * @return The single constraints, the constraint consists of, as an array of the type {@link
69 | * Constraint}
70 | */
71 | public final Constraint[] getConstraints() {
72 | return constraints;
73 | }
74 |
75 | /**
76 | * Sets the single constraints, the constraint should consist of.
77 | *
78 | * @param constraints
79 | * The single constraints, which should be set, as an array of the type {@link
80 | * Constraint}. The constraints may neither be null, nor empty
81 | */
82 | public final void setConstraints(@NonNull final Constraint[] constraints) {
83 | Condition.INSTANCE.ensureNotNull(constraints, "The constraints may not be null");
84 | Condition.INSTANCE.ensureAtLeast(constraints.length, 1, "The constraints may not be empty");
85 | this.constraints = constraints;
86 | }
87 |
88 | @Override
89 | public final boolean isSatisfied(final Type value) {
90 | for (Constraint constraint : constraints) {
91 | if (constraint.isSatisfied(value)) {
92 | return true;
93 | }
94 | }
95 |
96 | return false;
97 | }
98 |
99 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/constraints/NegateConstraint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation.constraints;
15 |
16 | import androidx.annotation.NonNull;
17 | import de.mrapp.android.validation.Constraint;
18 | import de.mrapp.util.Condition;
19 |
20 | /**
21 | * A constraint, which allows to negate the result of an other constraint.
22 | *
23 | * @param
24 | * The type of the values, which should be verified
25 | * @author Michael Rapp
26 | * @since 1.0.0
27 | */
28 | public class NegateConstraint implements Constraint {
29 |
30 | /**
31 | * The constraint, whose result is negated.
32 | */
33 | private Constraint constraint;
34 |
35 | /**
36 | * Creates a new constraint, which allows to negate the result of an other constraint.
37 | *
38 | * @param constraint
39 | * The constraint, whose result should be negated, as an instance of the type {@link
40 | * Constraint}. The constraint may not be null
41 | */
42 | public NegateConstraint(@NonNull final Constraint constraint) {
43 | setConstraint(constraint);
44 | }
45 |
46 | /**
47 | * Creates and returns a constraint, which allows to negate the result of an other constraint.
48 | *
49 | * @param
50 | * The type of the values, which should be verified
51 | * @param constraint
52 | * The constraint, whose result should be negated, as an instance of the type {@link
53 | * Constraint}. The constraint may not be null
54 | * @return The constraint, which has been created, as an instance of the class {@link
55 | * NegateConstraint}
56 | */
57 | public static final NegateConstraint create(
58 | @NonNull final Constraint constraint) {
59 | return new NegateConstraint<>(constraint);
60 | }
61 |
62 | /**
63 | * Returns the constraint, whose result is negated.
64 | *
65 | * @return The constraint, whose result is negated, as an instance of the type {@link
66 | * Constraint}
67 | */
68 | public final Constraint getConstraint() {
69 | return constraint;
70 | }
71 |
72 | /**
73 | * Sets the constraint, whose result should be negated.
74 | *
75 | * @param constraint
76 | * The constraint, which should be set, as an instance of the type {@link Constraint}.
77 | * The constraint may not be null
78 | */
79 | public final void setConstraint(@NonNull final Constraint constraint) {
80 | Condition.INSTANCE.ensureNotNull(constraint, "The constraint may not be null");
81 | this.constraint = constraint;
82 | }
83 |
84 | @Override
85 | public final boolean isSatisfied(final Type value) {
86 | return !getConstraint().isSatisfied(value);
87 | }
88 |
89 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/constraints/text/ContainsLetterConstraint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation.constraints.text;
15 |
16 | import java.util.regex.Pattern;
17 |
18 | /**
19 | * A constraint, which allows to verify texts in order to check, if they contain at least one
20 | * letter.
21 | *
22 | * @author Michael Rapp
23 | * @since 1.0.0
24 | */
25 | public class ContainsLetterConstraint extends RegexConstraint {
26 |
27 | /**
28 | * The regular expression, which is used by the constraint.
29 | */
30 | private static final Pattern REGEX = Pattern.compile("(.)*([a-zA-Z])(.)*");
31 |
32 | /**
33 | * Creates a new constraint, which allows to verify texts in order to check, if they contain at
34 | * least one letter.
35 | */
36 | public ContainsLetterConstraint() {
37 | super(REGEX);
38 | }
39 |
40 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/constraints/text/ContainsNumberConstraint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation.constraints.text;
15 |
16 | import java.util.regex.Pattern;
17 |
18 | /**
19 | * A constraint, which allows to verify texts in order to check, if they contain at least one
20 | * number.
21 | *
22 | * @author Michael Rapp
23 | * @since 1.0.0
24 | */
25 | public class ContainsNumberConstraint extends RegexConstraint {
26 |
27 | /**
28 | * The regular expression, which is used by the constraint.
29 | */
30 | private static final Pattern REGEX = Pattern.compile("(.)*(\\d)(.)*");
31 |
32 | /**
33 | * Creates a new constraint, which allows to verify texts in order to check, if they contain at
34 | * least one number.
35 | */
36 | public ContainsNumberConstraint() {
37 | super(REGEX);
38 | }
39 |
40 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/constraints/text/ContainsSymbolConstraint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation.constraints.text;
15 |
16 | import java.util.regex.Pattern;
17 |
18 | /**
19 | * A constraint, which allows to verify texts in order to check, if they contain at least one
20 | * symbol. Symbols are considered to be all characters except lower and uppercase letters from A to
21 | * Z and numbers.
22 | *
23 | * @author Michael Rapp
24 | * @since 1.0.0
25 | */
26 | public class ContainsSymbolConstraint extends RegexConstraint {
27 |
28 | /**
29 | * The regular expression, which is used by the constraint.
30 | */
31 | private static final Pattern REGEX = Pattern.compile("(.)*([^a-zA-Z0-9])(.)*");
32 |
33 | /**
34 | * Creates a new constraint, which allows to verify texts in order to check, if they contain at
35 | * least one symbol.
36 | */
37 | public ContainsSymbolConstraint() {
38 | super(REGEX);
39 | }
40 |
41 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/constraints/text/MinLengthConstraint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation.constraints.text;
15 |
16 | import de.mrapp.android.validation.Constraint;
17 | import de.mrapp.util.Condition;
18 |
19 | /**
20 | * A constraint, which allows to verify texts in order to check, if they have at least a specific
21 | * length.
22 | *
23 | * @author Michael Rapp
24 | * @since 1.0.0
25 | */
26 | public class MinLengthConstraint implements Constraint {
27 |
28 | /**
29 | * The minimum length a text must have.
30 | */
31 | private int minLength;
32 |
33 | /**
34 | * Creates a new constraint, which allows to verify texts in order to check, if they have at
35 | * least a specific length.
36 | *
37 | * @param minLength
38 | * The minimum length a text must have as an {@link Integer} value. The minimum length
39 | * must be at least 1
40 | */
41 | public MinLengthConstraint(final int minLength) {
42 | setMinLength(minLength);
43 | }
44 |
45 | /**
46 | * Returns the minimum length a text must have.
47 | *
48 | * @return The minimum length a text must have as an {@link Integer} value
49 | */
50 | public final int getMinLength() {
51 | return minLength;
52 | }
53 |
54 | /**
55 | * Sets the minimum length a text must have.
56 | *
57 | * @param minLength
58 | * The minimum length, which should be set, as an {@link Integer} value. The minimum
59 | * length must be at least 1
60 | */
61 | public final void setMinLength(final int minLength) {
62 | Condition.INSTANCE.ensureAtLeast(minLength, 1, "The minimum length must be at least 1");
63 | this.minLength = minLength;
64 | }
65 |
66 | @Override
67 | public final boolean isSatisfied(final CharSequence value) {
68 | return value.length() >= getMinLength();
69 | }
70 |
71 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/constraints/text/RegexConstraint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation.constraints.text;
15 |
16 | import java.util.regex.Matcher;
17 | import java.util.regex.Pattern;
18 |
19 | import androidx.annotation.NonNull;
20 | import de.mrapp.android.validation.Constraint;
21 | import de.mrapp.util.Condition;
22 |
23 | /**
24 | * A constraint, which allows to verify a text in order to check, if it matches a certain regular
25 | * expression.
26 | *
27 | * @author Michael Rapp
28 | * @since 1.0.0
29 | */
30 | public class RegexConstraint implements Constraint {
31 |
32 | /**
33 | * The regular expression, which is used to verify the texts.
34 | */
35 | private Pattern regex;
36 |
37 | /**
38 | * Creates a new constraint, which allows to verify a text in order to check, if it matches a
39 | * certain regular expression.
40 | *
41 | * @param regex
42 | * The regular expression, which should be used to verify the texts, as an instance of
43 | * the class {@link Pattern}. The regular expression may not be null
44 | */
45 | public RegexConstraint(@NonNull final Pattern regex) {
46 | setRegex(regex);
47 | }
48 |
49 | /**
50 | * Returns the regular expression, which is used to verify the texts.
51 | *
52 | * @return The regular expression, which is used to verify the texts, as an instance of the
53 | * class {@link Pattern}
54 | */
55 | public final Pattern getRegex() {
56 | return regex;
57 | }
58 |
59 | /**
60 | * Sets the regular expression, which should be used to verify the texts.
61 | *
62 | * @param regex
63 | * The regular expression, which should be set, as an instance of the class {@link
64 | * Pattern}. The regular expression may not be null
65 | */
66 | public final void setRegex(@NonNull final Pattern regex) {
67 | Condition.INSTANCE.ensureNotNull(regex, "The regular expression may not be null");
68 | this.regex = regex;
69 | }
70 |
71 | @Override
72 | public final boolean isSatisfied(final CharSequence value) {
73 | Matcher matcher = getRegex().matcher(value);
74 | return matcher.matches();
75 | }
76 |
77 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/validators/AbstractValidator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation.validators;
15 |
16 | import android.content.Context;
17 | import android.graphics.drawable.Drawable;
18 |
19 | import androidx.annotation.DrawableRes;
20 | import androidx.annotation.NonNull;
21 | import androidx.annotation.Nullable;
22 | import androidx.annotation.StringRes;
23 | import androidx.core.content.ContextCompat;
24 | import de.mrapp.android.validation.Validator;
25 | import de.mrapp.util.Condition;
26 |
27 | /**
28 | * An abstract base class for all validators, which should be able to validate values of a specific
29 | * type.
30 | *
31 | * @param
32 | * The type of the values, which should be validated
33 | * @author Michael Rapp
34 | * @since 1.0.0
35 | */
36 | public abstract class AbstractValidator implements Validator {
37 |
38 | /**
39 | * The error message, which should be shown, if the validation fails.
40 | */
41 | private CharSequence errorMessage;
42 |
43 | /**
44 | * The icon, which should be shown, if the validation fails.
45 | */
46 | private Drawable icon;
47 |
48 | /**
49 | * Creates a new validator, which should be able to validate values of a specific type.
50 | *
51 | * @param errorMessage
52 | * The error message, which should be shown, if the validation fails, as an instance of
53 | * the type {@link CharSequence}. The error message may not be null
54 | */
55 | public AbstractValidator(@NonNull final CharSequence errorMessage) {
56 | setErrorMessage(errorMessage);
57 | }
58 |
59 | /**
60 | * Creates a new validator, which should be able to validate values of a specific type.
61 | *
62 | * @param context
63 | * The context, which should be used to retrieve the error message, as an instance of
64 | * the class {@link Context}. The context may not be null
65 | * @param resourceId
66 | * The resource ID of the string resource, which contains the error message, which
67 | * should be set, as an {@link Integer} value. The resource ID must correspond to a
68 | * valid string resource
69 | */
70 | public AbstractValidator(@NonNull final Context context, @StringRes final int resourceId) {
71 | setErrorMessage(context, resourceId);
72 | }
73 |
74 | /**
75 | * Sets the error message, which should be shown, if the validation fails.
76 | *
77 | * @param errorMessage
78 | * The error message, which should be set, as an instance of the type {@link
79 | * CharSequence}. The error message may not be null
80 | */
81 | public final void setErrorMessage(@NonNull final CharSequence errorMessage) {
82 | Condition.INSTANCE.ensureNotNull(errorMessage, "The error message may not be null");
83 | Condition.INSTANCE.ensureNotEmpty(errorMessage, "The error message may not be empty");
84 | this.errorMessage = errorMessage;
85 | }
86 |
87 | /**
88 | * Sets the error message, which should be shown, if the validation fails.
89 | *
90 | * @param context
91 | * The context, which should be used to retrieve the error message, as an instance of
92 | * the class {@link Context}. The context may not be null
93 | * @param resourceId
94 | * The resource ID of the string resource, which contains the error message, which
95 | * should be set, as an {@link Integer} value. The resource ID must correspond to a
96 | * valid string resource
97 | */
98 | public final void setErrorMessage(@NonNull final Context context,
99 | @StringRes final int resourceId) {
100 | Condition.INSTANCE.ensureNotNull(context, "The context may not be null");
101 | this.errorMessage = context.getText(resourceId);
102 | }
103 |
104 | /**
105 | * Sets the icon, which should be shown, if the validation fails.
106 | *
107 | * @param icon
108 | * The icon, which should be set, as an instance of the class {@link Drawable} or null,
109 | * if no icon should be shown
110 | */
111 | public final void setIcon(@Nullable final Drawable icon) {
112 | this.icon = icon;
113 | }
114 |
115 | /**
116 | * Sets the icon, which should be shown, if the validation fails.
117 | *
118 | * @param context
119 | * The context, which should be used to retrieve the icon, as an instance of the class
120 | * {@link Context}. The context may not be null
121 | * @param resourceId
122 | * The resource ID of the drawable resource, which contains the icon, which should be
123 | * set, as an {@link Integer} value. The resource ID must correspond to a valid drawable
124 | * resource
125 | */
126 | public final void setIcon(@NonNull final Context context, @DrawableRes final int resourceId) {
127 | Condition.INSTANCE.ensureNotNull(context, "The context may not be null");
128 | this.icon = ContextCompat.getDrawable(context, resourceId);
129 | }
130 |
131 | @Override
132 | public final CharSequence getErrorMessage() {
133 | return errorMessage;
134 | }
135 |
136 | @Override
137 | public final Drawable getIcon() {
138 | return icon;
139 | }
140 |
141 | }
--------------------------------------------------------------------------------
/library/src/main/java/de/mrapp/android/validation/validators/NotNullValidator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2015 - 2019 Michael Rapp
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 | * in compliance with the License. You may obtain a copy of the License at
6 | *
7 | * http://www.apache.org/licenses/LICENSE-2.0
8 | *
9 | * Unless required by applicable law or agreed to in writing, software distributed under the License
10 | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 | * or implied. See the License for the specific language governing permissions and limitations under
12 | * the License.
13 | */
14 | package de.mrapp.android.validation.validators;
15 |
16 | import android.content.Context;
17 | import androidx.annotation.NonNull;
18 | import androidx.annotation.StringRes;
19 |
20 | /**
21 | * A validator, which allows to ensure, that values are not null.
22 | *
23 | * @author Michael Rapp
24 | * @since 1.0.0
25 | */
26 | public class NotNullValidator extends AbstractValidator