├── .gitignore
├── .mvn
└── wrapper
│ ├── maven-wrapper.jar
│ └── maven-wrapper.properties
├── .travis.yml
├── pom.xml
├── mvnw.cmd
├── LICENSE.txt
├── mvnw
├── src
├── test
│ └── java
│ │ └── snippets
│ │ └── SnippetsTests.java
└── main
│ └── java
│ └── snippets
│ └── Snippets.java
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | .settings/
2 | .idea/
3 | *.iml
4 | .classpath
5 | .project
6 | build/
7 | target/
8 |
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/shekhargulati/30-seconds-of-java/HEAD/.mvn/wrapper/maven-wrapper.jar
--------------------------------------------------------------------------------
/.mvn/wrapper/maven-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionUrl=https://repo1.maven.org/maven2/org/apache/maven/apache-maven/3.5.0/apache-maven-3.5.0-bin.zip
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: java
2 | install: ./mvnw install -DskipTests=true -Dmaven.javadoc.skip=true -B -V
3 | script: ./mvnw test -B
4 | jdk:
5 | - oraclejdk8
6 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.shekhargulati
8 | little-java-functions
9 | 1.0-SNAPSHOT
10 |
11 |
12 | 1.8
13 | 1.8
14 | 4.12
15 | 3.8.0
16 |
17 |
18 |
19 |
20 | junit
21 | junit
22 | ${junit.version}
23 | test
24 |
25 |
26 | org.assertj
27 | assertj-core
28 | ${assertj.version}
29 | test
30 |
31 |
32 |
33 |
34 |
35 |
36 | org.apache.maven.plugins
37 | maven-surefire-plugin
38 | 2.20
39 |
40 |
41 | **/Test*.java
42 | **/*Test.java
43 | **/*Tests.java
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/mvnw.cmd:
--------------------------------------------------------------------------------
1 | @REM ----------------------------------------------------------------------------
2 | @REM Licensed to the Apache Software Foundation (ASF) under one
3 | @REM or more contributor license agreements. See the NOTICE file
4 | @REM distributed with this work for additional information
5 | @REM regarding copyright ownership. The ASF licenses this file
6 | @REM to you under the Apache License, Version 2.0 (the
7 | @REM "License"); you may not use this file except in compliance
8 | @REM with the License. You may obtain a copy of the License at
9 | @REM
10 | @REM http://www.apache.org/licenses/LICENSE-2.0
11 | @REM
12 | @REM Unless required by applicable law or agreed to in writing,
13 | @REM software distributed under the License is distributed on an
14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | @REM KIND, either express or implied. See the License for the
16 | @REM specific language governing permissions and limitations
17 | @REM under the License.
18 | @REM ----------------------------------------------------------------------------
19 |
20 | @REM ----------------------------------------------------------------------------
21 | @REM Maven2 Start Up Batch script
22 | @REM
23 | @REM Required ENV vars:
24 | @REM JAVA_HOME - location of a JDK home dir
25 | @REM
26 | @REM Optional ENV vars
27 | @REM M2_HOME - location of maven2's installed home dir
28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending
30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
31 | @REM e.g. to debug Maven itself, use
32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
34 | @REM ----------------------------------------------------------------------------
35 |
36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
37 | @echo off
38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on'
39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
40 |
41 | @REM set %HOME% to equivalent of $HOME
42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
43 |
44 | @REM Execute a user defined script before this one
45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending
47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
49 | :skipRcPre
50 |
51 | @setlocal
52 |
53 | set ERROR_CODE=0
54 |
55 | @REM To isolate internal variables from possible post scripts, we use another setlocal
56 | @setlocal
57 |
58 | @REM ==== START VALIDATION ====
59 | if not "%JAVA_HOME%" == "" goto OkJHome
60 |
61 | echo.
62 | echo Error: JAVA_HOME not found in your environment. >&2
63 | echo Please set the JAVA_HOME variable in your environment to match the >&2
64 | echo location of your Java installation. >&2
65 | echo.
66 | goto error
67 |
68 | :OkJHome
69 | if exist "%JAVA_HOME%\bin\java.exe" goto init
70 |
71 | echo.
72 | echo Error: JAVA_HOME is set to an invalid directory. >&2
73 | echo JAVA_HOME = "%JAVA_HOME%" >&2
74 | echo Please set the JAVA_HOME variable in your environment to match the >&2
75 | echo location of your Java installation. >&2
76 | echo.
77 | goto error
78 |
79 | @REM ==== END VALIDATION ====
80 |
81 | :init
82 |
83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
84 | @REM Fallback to current working directory if not found.
85 |
86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
88 |
89 | set EXEC_DIR=%CD%
90 | set WDIR=%EXEC_DIR%
91 | :findBaseDir
92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound
93 | cd ..
94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound
95 | set WDIR=%CD%
96 | goto findBaseDir
97 |
98 | :baseDirFound
99 | set MAVEN_PROJECTBASEDIR=%WDIR%
100 | cd "%EXEC_DIR%"
101 | goto endDetectBaseDir
102 |
103 | :baseDirNotFound
104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
105 | cd "%EXEC_DIR%"
106 |
107 | :endDetectBaseDir
108 |
109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
110 |
111 | @setlocal EnableExtensions EnableDelayedExpansion
112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
114 |
115 | :endReadAdditionalConfig
116 |
117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
118 |
119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
121 |
122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
123 | if ERRORLEVEL 1 goto error
124 | goto end
125 |
126 | :error
127 | set ERROR_CODE=1
128 |
129 | :end
130 | @endlocal & set ERROR_CODE=%ERROR_CODE%
131 |
132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending
134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
136 | :skipRcPost
137 |
138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause
140 |
141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
142 |
143 | exit /B %ERROR_CODE%
144 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | CC0 1.0 Universal
2 |
3 | Statement of Purpose
4 |
5 | The laws of most jurisdictions throughout the world automatically confer
6 | exclusive Copyright and Related Rights (defined below) upon the creator and
7 | subsequent owner(s) (each and all, an "owner") of an original work of
8 | authorship and/or a database (each, a "Work").
9 |
10 | Certain owners wish to permanently relinquish those rights to a Work for the
11 | purpose of contributing to a commons of creative, cultural and scientific
12 | works ("Commons") that the public can reliably and without fear of later
13 | claims of infringement build upon, modify, incorporate in other works, reuse
14 | and redistribute as freely as possible in any form whatsoever and for any
15 | purposes, including without limitation commercial purposes. These owners may
16 | contribute to the Commons to promote the ideal of a free culture and the
17 | further production of creative, cultural and scientific works, or to gain
18 | reputation or greater distribution for their Work in part through the use and
19 | efforts of others.
20 |
21 | For these and/or other purposes and motivations, and without any expectation
22 | of additional consideration or compensation, the person associating CC0 with a
23 | Work (the "Affirmer"), to the extent that he or she is an owner of Copyright
24 | and Related Rights in the Work, voluntarily elects to apply CC0 to the Work
25 | and publicly distribute the Work under its terms, with knowledge of his or her
26 | Copyright and Related Rights in the Work and the meaning and intended legal
27 | effect of CC0 on those rights.
28 |
29 | 1. Copyright and Related Rights. A Work made available under CC0 may be
30 | protected by copyright and related or neighboring rights ("Copyright and
31 | Related Rights"). Copyright and Related Rights include, but are not limited
32 | to, the following:
33 |
34 | i. the right to reproduce, adapt, distribute, perform, display, communicate,
35 | and translate a Work;
36 |
37 | ii. moral rights retained by the original author(s) and/or performer(s);
38 |
39 | iii. publicity and privacy rights pertaining to a person's image or likeness
40 | depicted in a Work;
41 |
42 | iv. rights protecting against unfair competition in regards to a Work,
43 | subject to the limitations in paragraph 4(a), below;
44 |
45 | v. rights protecting the extraction, dissemination, use and reuse of data in
46 | a Work;
47 |
48 | vi. database rights (such as those arising under Directive 96/9/EC of the
49 | European Parliament and of the Council of 11 March 1996 on the legal
50 | protection of databases, and under any national implementation thereof,
51 | including any amended or successor version of such directive); and
52 |
53 | vii. other similar, equivalent or corresponding rights throughout the world
54 | based on applicable law or treaty, and any national implementations thereof.
55 |
56 | 2. Waiver. To the greatest extent permitted by, but not in contravention of,
57 | applicable law, Affirmer hereby overtly, fully, permanently, irrevocably and
58 | unconditionally waives, abandons, and surrenders all of Affirmer's Copyright
59 | and Related Rights and associated claims and causes of action, whether now
60 | known or unknown (including existing as well as future claims and causes of
61 | action), in the Work (i) in all territories worldwide, (ii) for the maximum
62 | duration provided by applicable law or treaty (including future time
63 | extensions), (iii) in any current or future medium and for any number of
64 | copies, and (iv) for any purpose whatsoever, including without limitation
65 | commercial, advertising or promotional purposes (the "Waiver"). Affirmer makes
66 | the Waiver for the benefit of each member of the public at large and to the
67 | detriment of Affirmer's heirs and successors, fully intending that such Waiver
68 | shall not be subject to revocation, rescission, cancellation, termination, or
69 | any other legal or equitable action to disrupt the quiet enjoyment of the Work
70 | by the public as contemplated by Affirmer's express Statement of Purpose.
71 |
72 | 3. Public License Fallback. Should any part of the Waiver for any reason be
73 | judged legally invalid or ineffective under applicable law, then the Waiver
74 | shall be preserved to the maximum extent permitted taking into account
75 | Affirmer's express Statement of Purpose. In addition, to the extent the Waiver
76 | is so judged Affirmer hereby grants to each affected person a royalty-free,
77 | non transferable, non sublicensable, non exclusive, irrevocable and
78 | unconditional license to exercise Affirmer's Copyright and Related Rights in
79 | the Work (i) in all territories worldwide, (ii) for the maximum duration
80 | provided by applicable law or treaty (including future time extensions), (iii)
81 | in any current or future medium and for any number of copies, and (iv) for any
82 | purpose whatsoever, including without limitation commercial, advertising or
83 | promotional purposes (the "License"). The License shall be deemed effective as
84 | of the date CC0 was applied by Affirmer to the Work. Should any part of the
85 | License for any reason be judged legally invalid or ineffective under
86 | applicable law, such partial invalidity or ineffectiveness shall not
87 | invalidate the remainder of the License, and in such case Affirmer hereby
88 | affirms that he or she will not (i) exercise any of his or her remaining
89 | Copyright and Related Rights in the Work or (ii) assert any associated claims
90 | and causes of action with respect to the Work, in either case contrary to
91 | Affirmer's express Statement of Purpose.
92 |
93 | 4. Limitations and Disclaimers.
94 |
95 | a. No trademark or patent rights held by Affirmer are waived, abandoned,
96 | surrendered, licensed or otherwise affected by this document.
97 |
98 | b. Affirmer offers the Work as-is and makes no representations or warranties
99 | of any kind concerning the Work, express, implied, statutory or otherwise,
100 | including without limitation warranties of title, merchantability, fitness
101 | for a particular purpose, non infringement, or the absence of latent or
102 | other defects, accuracy, or the present or absence of errors, whether or not
103 | discoverable, all to the greatest extent permissible under applicable law.
104 |
105 | c. Affirmer disclaims responsibility for clearing rights of other persons
106 | that may apply to the Work or any use thereof, including without limitation
107 | any person's Copyright and Related Rights in the Work. Further, Affirmer
108 | disclaims responsibility for obtaining any necessary consents, permissions
109 | or other rights required for any use of the Work.
110 |
111 | d. Affirmer understands and acknowledges that Creative Commons is not a
112 | party to this document and has no duty or obligation with respect to this
113 | CC0 or use of the Work.
114 |
115 | For more information, please see
116 |
117 |
--------------------------------------------------------------------------------
/mvnw:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # ----------------------------------------------------------------------------
3 | # Licensed to the Apache Software Foundation (ASF) under one
4 | # or more contributor license agreements. See the NOTICE file
5 | # distributed with this work for additional information
6 | # regarding copyright ownership. The ASF licenses this file
7 | # to you under the Apache License, Version 2.0 (the
8 | # "License"); you may not use this file except in compliance
9 | # with the License. You may obtain a copy of the License at
10 | #
11 | # http://www.apache.org/licenses/LICENSE-2.0
12 | #
13 | # Unless required by applicable law or agreed to in writing,
14 | # software distributed under the License is distributed on an
15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | # KIND, either express or implied. See the License for the
17 | # specific language governing permissions and limitations
18 | # under the License.
19 | # ----------------------------------------------------------------------------
20 |
21 | # ----------------------------------------------------------------------------
22 | # Maven2 Start Up Batch script
23 | #
24 | # Required ENV vars:
25 | # ------------------
26 | # JAVA_HOME - location of a JDK home dir
27 | #
28 | # Optional ENV vars
29 | # -----------------
30 | # M2_HOME - location of maven2's installed home dir
31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven
32 | # e.g. to debug Maven itself, use
33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files
35 | # ----------------------------------------------------------------------------
36 |
37 | if [ -z "$MAVEN_SKIP_RC" ] ; then
38 |
39 | if [ -f /etc/mavenrc ] ; then
40 | . /etc/mavenrc
41 | fi
42 |
43 | if [ -f "$HOME/.mavenrc" ] ; then
44 | . "$HOME/.mavenrc"
45 | fi
46 |
47 | fi
48 |
49 | # OS specific support. $var _must_ be set to either true or false.
50 | cygwin=false;
51 | darwin=false;
52 | mingw=false
53 | case "`uname`" in
54 | CYGWIN*) cygwin=true ;;
55 | MINGW*) mingw=true;;
56 | Darwin*) darwin=true
57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html
59 | if [ -z "$JAVA_HOME" ]; then
60 | if [ -x "/usr/libexec/java_home" ]; then
61 | export JAVA_HOME="`/usr/libexec/java_home`"
62 | else
63 | export JAVA_HOME="/Library/Java/Home"
64 | fi
65 | fi
66 | ;;
67 | esac
68 |
69 | if [ -z "$JAVA_HOME" ] ; then
70 | if [ -r /etc/gentoo-release ] ; then
71 | JAVA_HOME=`java-config --jre-home`
72 | fi
73 | fi
74 |
75 | if [ -z "$M2_HOME" ] ; then
76 | ## resolve links - $0 may be a link to maven's home
77 | PRG="$0"
78 |
79 | # need this for relative symlinks
80 | while [ -h "$PRG" ] ; do
81 | ls=`ls -ld "$PRG"`
82 | link=`expr "$ls" : '.*-> \(.*\)$'`
83 | if expr "$link" : '/.*' > /dev/null; then
84 | PRG="$link"
85 | else
86 | PRG="`dirname "$PRG"`/$link"
87 | fi
88 | done
89 |
90 | saveddir=`pwd`
91 |
92 | M2_HOME=`dirname "$PRG"`/..
93 |
94 | # make it fully qualified
95 | M2_HOME=`cd "$M2_HOME" && pwd`
96 |
97 | cd "$saveddir"
98 | # echo Using m2 at $M2_HOME
99 | fi
100 |
101 | # For Cygwin, ensure paths are in UNIX format before anything is touched
102 | if $cygwin ; then
103 | [ -n "$M2_HOME" ] &&
104 | M2_HOME=`cygpath --unix "$M2_HOME"`
105 | [ -n "$JAVA_HOME" ] &&
106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
107 | [ -n "$CLASSPATH" ] &&
108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
109 | fi
110 |
111 | # For Migwn, ensure paths are in UNIX format before anything is touched
112 | if $mingw ; then
113 | [ -n "$M2_HOME" ] &&
114 | M2_HOME="`(cd "$M2_HOME"; pwd)`"
115 | [ -n "$JAVA_HOME" ] &&
116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
117 | # TODO classpath?
118 | fi
119 |
120 | if [ -z "$JAVA_HOME" ]; then
121 | javaExecutable="`which javac`"
122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
123 | # readlink(1) is not available as standard on Solaris 10.
124 | readLink=`which readlink`
125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
126 | if $darwin ; then
127 | javaHome="`dirname \"$javaExecutable\"`"
128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
129 | else
130 | javaExecutable="`readlink -f \"$javaExecutable\"`"
131 | fi
132 | javaHome="`dirname \"$javaExecutable\"`"
133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'`
134 | JAVA_HOME="$javaHome"
135 | export JAVA_HOME
136 | fi
137 | fi
138 | fi
139 |
140 | if [ -z "$JAVACMD" ] ; then
141 | if [ -n "$JAVA_HOME" ] ; then
142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
143 | # IBM's JDK on AIX uses strange locations for the executables
144 | JAVACMD="$JAVA_HOME/jre/sh/java"
145 | else
146 | JAVACMD="$JAVA_HOME/bin/java"
147 | fi
148 | else
149 | JAVACMD="`which java`"
150 | fi
151 | fi
152 |
153 | if [ ! -x "$JAVACMD" ] ; then
154 | echo "Error: JAVA_HOME is not defined correctly." >&2
155 | echo " We cannot execute $JAVACMD" >&2
156 | exit 1
157 | fi
158 |
159 | if [ -z "$JAVA_HOME" ] ; then
160 | echo "Warning: JAVA_HOME environment variable is not set."
161 | fi
162 |
163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
164 |
165 | # traverses directory structure from process work directory to filesystem root
166 | # first directory with .mvn subdirectory is considered project base directory
167 | find_maven_basedir() {
168 |
169 | if [ -z "$1" ]
170 | then
171 | echo "Path not specified to find_maven_basedir"
172 | return 1
173 | fi
174 |
175 | basedir="$1"
176 | wdir="$1"
177 | while [ "$wdir" != '/' ] ; do
178 | if [ -d "$wdir"/.mvn ] ; then
179 | basedir=$wdir
180 | break
181 | fi
182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc)
183 | if [ -d "${wdir}" ]; then
184 | wdir=`cd "$wdir/.."; pwd`
185 | fi
186 | # end of workaround
187 | done
188 | echo "${basedir}"
189 | }
190 |
191 | # concatenates all lines of a file
192 | concat_lines() {
193 | if [ -f "$1" ]; then
194 | echo "$(tr -s '\n' ' ' < "$1")"
195 | fi
196 | }
197 |
198 | BASE_DIR=`find_maven_basedir "$(pwd)"`
199 | if [ -z "$BASE_DIR" ]; then
200 | exit 1;
201 | fi
202 |
203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
204 | echo $MAVEN_PROJECTBASEDIR
205 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
206 |
207 | # For Cygwin, switch paths to Windows format before running java
208 | if $cygwin; then
209 | [ -n "$M2_HOME" ] &&
210 | M2_HOME=`cygpath --path --windows "$M2_HOME"`
211 | [ -n "$JAVA_HOME" ] &&
212 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
213 | [ -n "$CLASSPATH" ] &&
214 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
215 | [ -n "$MAVEN_PROJECTBASEDIR" ] &&
216 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
217 | fi
218 |
219 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
220 |
221 | exec "$JAVACMD" \
222 | $MAVEN_OPTS \
223 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
224 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
225 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
226 |
--------------------------------------------------------------------------------
/src/test/java/snippets/SnippetsTests.java:
--------------------------------------------------------------------------------
1 | package snippets;
2 |
3 | import java.util.AbstractMap.SimpleEntry;
4 | import java.util.Arrays;
5 | import java.util.Collections;
6 | import java.util.HashMap;
7 | import java.util.List;
8 | import java.util.Map;
9 | import java.util.OptionalInt;
10 | import java.util.stream.IntStream;
11 |
12 | import org.junit.Test;
13 |
14 | import static org.assertj.core.api.Assertions.assertThat;
15 |
16 | public class SnippetsTests {
17 |
18 | @Test
19 | public void gcd_of_array_containing_1_to_5_is_1() throws Exception {
20 | OptionalInt gcd = Snippets.gcd(new int[]{1, 2, 3, 4, 5});
21 | assertThat(gcd).isNotEmpty();
22 | assertThat(gcd).hasValue(1);
23 | }
24 |
25 | @Test
26 | public void gcd_of_array_containing_4_8_and_12_is_4() throws Exception {
27 | OptionalInt gcd = Snippets.gcd(new int[]{4, 8, 12});
28 | assertThat(gcd).isNotEmpty();
29 | assertThat(gcd).hasValue(4);
30 | }
31 |
32 | @Test
33 | public void lcm_of_array_containing_1_to_5_is_60() throws Exception {
34 | OptionalInt lcm = Snippets.lcm(new int[]{1, 2, 3, 4, 5});
35 | assertThat(lcm).isNotEmpty();
36 | assertThat(lcm).hasValue(60);
37 | }
38 |
39 | @Test
40 | public void lcm_of_array_containing_4_8_and_12_is_24() throws Exception {
41 | OptionalInt lcm = Snippets.lcm(new int[]{4, 8, 12});
42 | assertThat(lcm).isNotEmpty();
43 | assertThat(lcm).hasValue(24);
44 | }
45 |
46 | @Test
47 | public void max_of_array_containing_10_1_and_5_is_10() throws Exception {
48 | OptionalInt max = Snippets.arrayMax(new int[]{10, 1, 5});
49 | assertThat(max).hasValue(10);
50 | }
51 |
52 | @Test
53 | public void min_of_array_containing_10_1_and_5_is_10() throws Exception {
54 | OptionalInt min = Snippets.arrayMin(new int[]{10, 1, 5});
55 | assertThat(min).hasValue(1);
56 | }
57 |
58 | @Test
59 | public void chunk_breaks_input_array__with_odd_length() throws Exception {
60 | int[][] chunks = Snippets.chunk(new int[]{1, 2, 3, 4, 5}, 2);
61 | assertThat(chunks)
62 | .containsExactly(
63 | new int[]{1, 2},
64 | new int[]{3, 4},
65 | new int[]{5}
66 | );
67 | }
68 |
69 | @Test
70 | public void chunk_breaks_input_array__with_event_length() throws Exception {
71 | int[][] chunks = Snippets.chunk(new int[]{1, 2, 3, 4, 5, 6}, 2);
72 | assertThat(chunks)
73 | .containsExactly(
74 | new int[]{1, 2},
75 | new int[]{3, 4},
76 | new int[]{5, 6}
77 | );
78 | }
79 |
80 | @Test
81 | public void countOccurrences_counts_occurrences_of_a_value() throws Exception {
82 | long count = Snippets.countOccurrences(new int[]{1, 1, 2, 1, 2, 3}, 1);
83 | assertThat(count).isEqualTo(3);
84 | }
85 |
86 | @Test
87 | public void deepFlatten_flatten_a_deeply_nested_array() throws Exception {
88 | int[] flatten = Snippets.deepFlatten(
89 | new Object[]{1, new Object[]{2}, new Object[]{3, 4, 5}}
90 | );
91 |
92 | assertThat(flatten).isEqualTo(new int[]{1, 2, 3, 4, 5});
93 | }
94 |
95 | @Test
96 | public void difference_between_array_with_1_2_3_and_array_with_1_2_4_is_3() throws Exception {
97 | int[] difference = Snippets.difference(new int[]{1, 2, 3}, new int[]{1, 2, 4});
98 | assertThat(difference).isEqualTo(new int[]{3});
99 | }
100 |
101 | @Test
102 | public void difference_between_array_with_1_2_3_and_array_with_1_2_3_is_empty_array() throws Exception {
103 | int[] difference = Snippets.difference(new int[]{1, 2, 3}, new int[]{1, 2, 3});
104 | assertThat(difference).isEmpty();
105 | }
106 |
107 | @Test
108 | public void differenceWith_return_all_squares_that_do_not_exist_in_second() throws Exception {
109 | int[] difference = Snippets.differenceWith(
110 | new int[]{1, 4, 9, 16, 25},
111 | new int[]{1, 2, 3, 6, 7},
112 | (o1, o2) -> o1 - (o2 * o2)
113 | );
114 |
115 | assertThat(difference).isEqualTo(new int[]{16, 25});
116 | }
117 |
118 | @Test
119 | public void differenceWith_returns_empty_array_when_two_arrays_are_equal_as_per_comparison_operation() throws Exception {
120 | int[] difference = Snippets.differenceWith(
121 | new int[]{1, 2, 3},
122 | new int[]{1, 2, 3},
123 | (o1, o2) -> o1 - o2
124 | );
125 |
126 | assertThat(difference).isEmpty();
127 | }
128 |
129 | @Test
130 | public void differenceWith_returns_first_array_when_elements_in_second_array_are_not_comparable_as_per_comparison_operation() throws Exception {
131 | int[] difference = Snippets.differenceWith(
132 | new int[]{1, 2, 3},
133 | new int[]{10, 11, 12},
134 | (o1, o2) -> o1 - o2
135 | );
136 |
137 | assertThat(difference).isEqualTo(new int[]{1, 2, 3});
138 | }
139 |
140 | @Test
141 | public void distinct_remove_all_duplicate_values_from_an_array() throws Exception {
142 | int[] distinct = Snippets.distinctValuesOfArray(new int[]{1, 2, 2, 3, 4, 4, 5});
143 | assertThat(distinct).isEqualTo(new int[]{1, 2, 3, 4, 5});
144 | }
145 |
146 | @Test
147 | public void drop_elements_less_than_3() throws Exception {
148 | int[] elements = Snippets.dropElements(new int[]{1, 2, 3, 4}, i -> i >= 3);
149 | assertThat(elements).isEqualTo(new int[]{3, 4});
150 | }
151 |
152 | @Test
153 | public void drop_elements_returns_empty_array_when_no_element_match_the_condition() throws Exception {
154 | int[] elements = Snippets.dropElements(new int[]{1, 2, 3, 4}, i -> i < 1);
155 | assertThat(elements).isEmpty();
156 | }
157 |
158 | @Test
159 | public void drop_elements__return_all_elements_when_all_elements_match_the_condition() throws Exception {
160 | int[] elements = Snippets.dropElements(new int[]{1, 2, 3, 4}, i -> i <= 4);
161 | assertThat(elements).isEqualTo(new int[]{1, 2, 3, 4});
162 | }
163 |
164 | @Test
165 | public void dropRight_remove_n_elements_from_right() throws Exception {
166 | int[] elements = Snippets.dropRight(new int[]{1, 2, 3}, 1);
167 | assertThat(elements).isEqualTo(new int[]{1, 2});
168 |
169 | elements = Snippets.dropRight(new int[]{1, 2, 3}, 2);
170 | assertThat(elements).isEqualTo(new int[]{1});
171 |
172 | elements = Snippets.dropRight(new int[]{1, 2, 3}, 3);
173 | assertThat(elements).isEmpty();
174 |
175 | elements = Snippets.dropRight(new int[]{1, 2, 3}, 42);
176 | assertThat(elements).isEmpty();
177 | }
178 |
179 | @Test
180 | public void everyNth_return_every_2nd_element() throws Exception {
181 | int[] elements = Snippets.everyNth(new int[]{1, 2, 3, 4, 5, 6}, 2);
182 | assertThat(elements).isEqualTo(new int[]{2, 4, 6});
183 | }
184 |
185 | @Test
186 | public void filterNonUnique_return_unique_elements() throws Exception {
187 | int[] elements = Snippets.filterNonUnique(new int[]{1, 2, 2, 3, 4, 4, 5});
188 | assertThat(elements).isEqualTo(new int[]{1, 3, 5});
189 | }
190 |
191 | @Test
192 | public void filterNonUnique_return_same_array_when_all_unique() throws Exception {
193 | int[] elements = Snippets.filterNonUnique(new int[]{1, 2, 3, 4, 5});
194 | assertThat(elements).isEqualTo(new int[]{1, 2, 3, 4, 5});
195 | }
196 |
197 | @Test
198 | public void filterNonUnique_return_empty_array_when_all_duplicated() throws Exception {
199 | int[] elements = Snippets.filterNonUnique(new int[]{1, 1, 2, 2, 3, 3, 4, 4, 5, 5});
200 | assertThat(elements).isEmpty();
201 | }
202 |
203 | @Test
204 | public void flatten_flat_one_level_array() throws Exception {
205 | int[] flatten = Snippets.flatten(new Object[]{1, new int[]{2}, 3, 4});
206 | assertThat(flatten).isEqualTo(new int[]{1, 2, 3, 4});
207 | }
208 |
209 | @Test
210 | public void flattenDepth_flatten_to_specified_depth() throws Exception {
211 | Object[] input = {
212 | 1,
213 | new Object[]{2},
214 | new Object[]{
215 | new Object[]{
216 | new Object[]{
217 | 3
218 | },
219 | 4
220 | }, 5
221 | }
222 | };
223 |
224 | Object[] flatten = Snippets.flattenDepth(input, 2);
225 | assertThat(flatten).isEqualTo(new Object[]{1, 2, new Object[]{3}, 4, 5});
226 | }
227 |
228 | @Test
229 | public void group_elements_by_length() throws Exception {
230 | Map> groups = Snippets.groupBy(new String[]{"one", "two", "three"}, String::length);
231 | assertThat(groups)
232 | .containsExactly(
233 | new SimpleEntry<>(3, Arrays.asList("one", "two")),
234 | new SimpleEntry<>(5, Collections.singletonList("three"))
235 | );
236 | }
237 |
238 | @Test
239 | public void initial_return_array_except_last_element() throws Exception {
240 | Integer[] initial = Snippets.initial(new Integer[]{1, 2, 3});
241 | assertThat(initial).isEqualTo(new Integer[]{1, 2});
242 | }
243 |
244 | @Test
245 | public void initializeArrayWithRange_from_1_to_5() throws Exception {
246 | int[] numbers = Snippets.initializeArrayWithRange(5, 1);
247 | assertThat(numbers).isEqualTo(new int[]{1, 2, 3, 4, 5});
248 | }
249 |
250 | @Test
251 | public void initializeArrayWithValues() throws Exception {
252 | int[] elements = Snippets.initializeArrayWithValues(5, 2);
253 | assertThat(elements).isEqualTo(new int[]{2, 2, 2, 2, 2});
254 | }
255 |
256 | @Test
257 | public void intersection_between_two_arrays() throws Exception {
258 | int[] elements = Snippets.intersection(new int[]{1, 2, 3}, new int[]{4, 3, 2});
259 | assertThat(elements).isEqualTo(new int[]{2, 3});
260 | }
261 |
262 | @Test
263 | public void isSorted_return_1_when_array_sorted_is_ascending_order() throws Exception {
264 | int sorted = Snippets.isSorted(new Integer[]{0, 1, 2, 3});
265 | assertThat(sorted).isEqualTo(1);
266 |
267 | sorted = Snippets.isSorted(new Integer[]{0, 1, 2, 2});
268 | assertThat(sorted).isEqualTo(1);
269 | }
270 |
271 | @Test
272 | public void isSorted_return_minus_1_when_array_sorted_in_descending_order() throws Exception {
273 | int sorted = Snippets.isSorted(new Integer[]{3, 2, 1, 0});
274 | assertThat(sorted).isEqualTo(-1);
275 |
276 | sorted = Snippets.isSorted(new Integer[]{3, 3, 2, 1, 0});
277 | assertThat(sorted).isEqualTo(-1);
278 | }
279 |
280 | @Test
281 | public void isSorted_returns_0_when_array_is_not_sorted() throws Exception {
282 | int sorted = Snippets.isSorted(new Integer[]{3, 4, 1, 0});
283 | assertThat(sorted).isEqualTo(0);
284 | }
285 |
286 | @Test
287 | public void join_should_create_string_from_an_array_with_different_sep_and_end() throws Exception {
288 | String joined = Snippets.join(new String[]{"pen", "pineapple", "apple", "pen"}, ",", "&");
289 | assertThat(joined).isEqualTo("pen,pineapple,apple&pen");
290 | }
291 |
292 | @Test
293 | public void join_should_create_string_from_an_array_with_sep_only() throws Exception {
294 | String joined = Snippets.join(new String[]{"pen", "pineapple", "apple", "pen"}, ",");
295 | assertThat(joined).isEqualTo("pen,pineapple,apple,pen");
296 | }
297 |
298 | @Test
299 | public void join_should_create_string_from_an_array_with_default_sep() throws Exception {
300 | String joined = Snippets.join(new String[]{"pen", "pineapple", "apple", "pen"});
301 | assertThat(joined).isEqualTo("pen,pineapple,apple,pen");
302 | }
303 |
304 | @Test
305 | public void join_should_create_empty_string_with_empty_array() throws Exception {
306 | String joined = Snippets.join(new String[]{});
307 | assertThat(joined).isEqualTo("");
308 | }
309 |
310 | @Test
311 | public void nthElement_return_nth_element_from_start_when_n_is_greater_than_0() throws Exception {
312 | String nthElement = Snippets.nthElement(new String[]{"a", "b", "c"}, 1);
313 | assertThat(nthElement).isEqualTo("b");
314 | }
315 |
316 |
317 | @Test
318 | public void nthElement_return_nth_element_from_end_when_n_is_less_than_0() throws Exception {
319 | String nthElement = Snippets.nthElement(new String[]{"a", "b", "c"}, -3);
320 | assertThat(nthElement).isEqualTo("a");
321 | }
322 |
323 | @Test
324 | public void pick_should_pick_key_pairs_corresponding_to_keys() throws Exception {
325 | Map obj = new HashMap<>();
326 | obj.put("a", 1);
327 | obj.put("b", 2);
328 | obj.put("c", 3);
329 |
330 | Map picked = Snippets.pick(obj, new String[]{"a", "c"});
331 | assertThat(picked).containsExactly(new SimpleEntry<>("a", 1), new SimpleEntry<>("c", 3));
332 | }
333 |
334 | @Test
335 | public void reducedFilter_Test() throws Exception {
336 | Map item1 = new HashMap<>();
337 | item1.put("id", 1);
338 | item1.put("name", "john");
339 | item1.put("age", 24);
340 |
341 | Map item2 = new HashMap<>();
342 | item2.put("id", 2);
343 | item2.put("name", "mike");
344 | item2.put("age", 50);
345 |
346 | Map[] filtered = Snippets.reducedFilter((Map[]) new Map[]{item1, item2}, new String[]{"id", "name"}, item -> (Integer) item.get("age") > 24);
347 | assertThat(filtered).hasSize(1);
348 | assertThat(filtered[0])
349 | .containsOnly(
350 | new SimpleEntry("id", 2),
351 | new SimpleEntry("name", "mike"));
352 | }
353 |
354 | @Test
355 | public void sample_should_return_random_element() throws Exception {
356 | Integer sample = Snippets.sample(new Integer[]{3, 7, 9, 11});
357 | assertThat(sample).isIn(Arrays.asList(3, 7, 9, 11));
358 | }
359 |
360 | @Test
361 | public void sampleSize_should_return_sample_of_size_array_length_when_sample_size_is_greater_than_array_size() throws Exception {
362 | Integer[] sample = Snippets.sampleSize(new Integer[]{1, 2, 3}, 4);
363 | assertThat(sample).hasSize(3);
364 | }
365 |
366 | @Test
367 | public void similarity_test() throws Exception {
368 | Integer[] arr = Snippets.similarity(new Integer[]{1, 2, 3}, new Integer[]{1, 2, 4});
369 | assertThat(arr).isEqualTo(new Integer[]{1, 2});
370 | }
371 |
372 | @Test
373 | public void empty_array() throws Exception {
374 | String[] empty = Snippets.emptyArray(String.class);
375 | assertThat(empty).isEmpty();
376 | }
377 |
378 | @Test
379 | public void sortedIndex_descending() throws Exception {
380 | int index = Snippets.sortedIndex(new Integer[]{5, 3, 2, 1}, 4);
381 | assertThat(index).isEqualTo(1);
382 | }
383 |
384 | @Test
385 | public void sortedIndex_ascending() throws Exception {
386 | int index = Snippets.sortedIndex(new Integer[]{30, 50}, 40);
387 | assertThat(index).isEqualTo(1);
388 | }
389 |
390 | @Test
391 | public void sortedIndex_ascending_at_end() throws Exception {
392 | int index = Snippets.sortedIndex(new Integer[]{30, 50}, 60);
393 | assertThat(index).isEqualTo(2);
394 | }
395 |
396 | @Test
397 | public void symmetricDifference_test() throws Exception {
398 | Integer[] diff = Snippets.symmetricDifference(
399 | new Integer[]{1, 2, 3},
400 | new Integer[]{1, 2, 4}
401 | );
402 | assertThat(diff).isEqualTo(new Integer[]{3, 4});
403 | }
404 |
405 | @Test
406 | public void union_test() throws Exception {
407 | Integer[] union = Snippets.union(
408 | new Integer[]{1, 2, 3},
409 | new Integer[]{1, 2, 4}
410 | );
411 |
412 | assertThat(union).isEqualTo(new Integer[]{1, 2, 3, 4});
413 | }
414 |
415 | @Test
416 | public void without_test() throws Exception {
417 | Integer[] without = Snippets.without(
418 | new Integer[]{2, 1, 2, 3},
419 | 1, 2
420 |
421 | );
422 |
423 | assertThat(without).isEqualTo(new Integer[]{3});
424 | }
425 |
426 | @Test
427 | public void zip_test() throws Exception {
428 | List