├── .0pdd.yml
├── .gitattributes
├── .github
├── CODE_OF_CONDUCT.md
├── CONTRIBUTING.md
├── ISSUE_TEMPLATE.md
├── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── PULL_REQUEST_TEMPLATE.md
├── lock.yml
├── profanity.yml
└── workflows
│ ├── build.yml
│ └── codeql-analysis.yml
├── .gitignore
├── .pdd
├── .rultor.yml
├── build.sh
├── license.txt
├── mvnw
├── mvnw.cmd
├── pom.xml
├── readme.md
└── src
├── main
└── java
│ └── io
│ └── github
│ └── dgroup
│ └── enumerable4j
│ ├── Empty.java
│ ├── Enumerable.java
│ ├── Joined.java
│ ├── Linked.java
│ ├── Unique.java
│ └── package-info.java
└── test
└── java
└── io
└── github
└── dgroup
└── enumerable4j
├── AfterTest.java
├── AllTest.java
├── AnyTest.java
├── ChainTest.java
├── CountTest.java
├── DropTest.java
├── EachTest.java
├── FindTest.java
├── JoinedTest.java
├── MapTest.java
├── NextTest.java
├── NoneTest.java
├── OneTest.java
├── ReduceTest.java
├── RejectTest.java
├── SelectTest.java
├── TakeTest.java
├── UniqTest.java
├── UniqueTest.java
├── ZipTest.java
└── package-info.java
/.0pdd.yml:
--------------------------------------------------------------------------------
1 | errors:
2 | - yurii.dubinka@gmail.com
3 |
4 | tags:
5 | - pdd
--------------------------------------------------------------------------------
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Set the default behavior, in case people don't have core.autocrlf set.
2 | * text=auto eol=lf
3 |
4 |
5 | # Add readily-visible signature (in SHA format) to all files (tagged by $Id$) with extension below.
6 | # Read more https://gist.github.com/treyharris/f98b708c9f5b753d60a2 .
7 | *.java ident
8 | *.yml ident
9 | *.xml ident
10 |
11 |
12 | # Explicitly declare text files we want to always be normalized and converted
13 | # to native line endings on checkout.
14 | *.txt text
15 | *.java text
16 | *.groovy text
17 | *.xml text
18 | *.md text
19 | *.pom text
20 | *.properties text
21 | *.tex text
22 | *.vm text
23 | *.xsl text
24 | *.yml text
25 |
26 |
27 | # Denote all files that are truly binary and should not be modified.
28 | *.png binary
29 | *.jpg binary
--------------------------------------------------------------------------------
/.github/CODE_OF_CONDUCT.md:
--------------------------------------------------------------------------------
1 | # Contributor Covenant Code of Conduct
2 |
3 | ## Our Pledge
4 |
5 | In the interest of fostering an open and welcoming environment, we as
6 | contributors and maintainers pledge to making participation in our project and
7 | our community a harassment-free experience for everyone, regardless of age, body
8 | size, disability, ethnicity, sex characteristics, gender identity and expression,
9 | level of experience, education, socio-economic status, nationality, personal
10 | appearance, race, religion, or sexual identity and orientation.
11 |
12 | ## Our Standards
13 |
14 | Examples of behavior that contributes to creating a positive environment
15 | include:
16 |
17 | * Using welcoming and inclusive language
18 | * Being respectful of differing viewpoints and experiences
19 | * Gracefully accepting constructive criticism
20 | * Focusing on what is best for the community
21 | * Showing empathy towards other community members
22 |
23 | Examples of unacceptable behavior by participants include:
24 |
25 | * The use of sexualized language or imagery and unwelcome sexual attention or
26 | advances
27 | * Trolling, insulting/derogatory comments, and personal or political attacks
28 | * Public or private harassment
29 | * Publishing others' private information, such as a physical or electronic
30 | address, without explicit permission
31 | * Other conduct which could reasonably be considered inappropriate in a
32 | professional setting
33 |
34 | ## Our Responsibilities
35 |
36 | Project maintainers are responsible for clarifying the standards of acceptable
37 | behavior and are expected to take appropriate and fair corrective action in
38 | response to any instances of unacceptable behavior.
39 |
40 | Project maintainers have the right and responsibility to remove, edit, or
41 | reject comments, commits, code, wiki edits, issues, and other contributions
42 | that are not aligned to this Code of Conduct, or to ban temporarily or
43 | permanently any contributor for other behaviors that they deem inappropriate,
44 | threatening, offensive, or harmful.
45 |
46 | ## Scope
47 |
48 | This Code of Conduct applies both within project spaces and in public spaces
49 | when an individual is representing the project or its community. Examples of
50 | representing a project or community include using an official project e-mail
51 | address, posting via an official social media account, or acting as an appointed
52 | representative at an online or offline event. Representation of a project may be
53 | further defined and clarified by project maintainers.
54 |
55 | ## Enforcement
56 |
57 | Instances of abusive, harassing, or otherwise unacceptable behavior may be
58 | reported by contacting the github@dgroup. All
59 | complaints will be reviewed and investigated and will result in a response that
60 | is deemed necessary and appropriate to the circumstances. The project team is
61 | obligated to maintain confidentiality with regard to the reporter of an incident.
62 | Further details of specific enforcement policies may be posted separately.
63 |
64 | Project maintainers who do not follow or enforce the Code of Conduct in good
65 | faith may face temporary or permanent repercussions as determined by other
66 | members of the project's leadership.
67 |
68 | ## Attribution
69 |
70 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71 | available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72 |
73 | [homepage]: https://www.contributor-covenant.org
74 |
75 | For answers to common questions about this code of conduct, see
76 | https://www.contributor-covenant.org/faq
77 |
--------------------------------------------------------------------------------
/.github/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | Just send a pull request and call it a day.
2 |
3 |
4 |
5 |
6 |
7 | Only if the build is green, you send us your pull request.
8 |
9 | Thanks!
10 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | Make sure the title of the issue explains the problem you are having. Also, the description of the issue must clearly explain what is broken, not what you want us to implement. Go through this checklist and make sure you answer "YES" to all points:
2 |
3 | - You have all pre-requisites listed in README.md installed
4 | - You are sure that you are not reporting a duplicate (search all issues)
5 | - You say "is broken" or "doesn't work" in the title
6 | - You tell us what you are trying to do
7 | - You explain the results you are getting
8 | - You suggest an alternative result you would like to see
9 |
10 | These articles will help you understand what we are looking for:
11 | * http://www.yegor256.com/2014/11/24/principles-of-bug-tracking.html
12 | * https://www.chiark.greenend.org.uk/~sgtatham/bugs.html
13 |
14 | Thank you for your contribution!
15 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | **Desktop (please complete the following information):**
27 | - OS: [e.g. iOS]
28 | - Browser [e.g. chrome, safari]
29 | - Version [e.g. 22]
30 |
31 | **Smartphone (please complete the following information):**
32 | - Device: [e.g. iPhone6]
33 | - OS: [e.g. iOS8.1]
34 | - Browser [e.g. stock browser, safari]
35 | - Version [e.g. 22]
36 |
37 | **Additional context**
38 | Add any other context about the problem here.
39 |
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: ''
6 | assignees: ''
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
21 |
--------------------------------------------------------------------------------
/.github/PULL_REQUEST_TEMPLATE.md:
--------------------------------------------------------------------------------
1 | Many thanks for your contribution, we truly appreciate it. We will appreciate it even more, if you make sure that you can say "YES" to each point in this short checklist:
2 |
3 | - You made a small amount of changes (less than 100 lines, less than 10 files)
4 | - You made changes related to only one bug (create separate PRs for separate problems)
5 | - You are ready to defend your changes (there will be a code review)
6 | - You don't touch what you don't understand
7 | - You ran the build locally and it passed
8 |
9 | This article will help you understand what we are looking for: http://www.yegor256.com/2015/02/09/serious-code-reviewer.html
10 |
11 | Thank you for your contribution!
12 |
--------------------------------------------------------------------------------
/.github/lock.yml:
--------------------------------------------------------------------------------
1 | # Configuration for Lock Threads - https://github.com/dessant/lock-threads-app
2 |
3 | # Number of days of inactivity before a closed issue or pull request is locked
4 | daysUntilLock: 365
5 |
6 | # Skip issues and pull requests created before a given timestamp. Timestamp must
7 | # follow ISO 8601 (`YYYY-MM-DD`). Set to `false` to disable
8 | skipCreatedBefore: false
9 |
10 | # Issues and pull requests with these labels will be ignored. Set to `[]` to disable
11 | exemptLabels: []
12 |
13 | # Label to add before locking, such as `outdated`. Set to `false` to disable
14 | lockLabel: false
15 |
16 | # Comment to post before locking. Set to `false` to disable
17 | lockComment: >
18 | This thread has been automatically locked since there has not been
19 | any recent activity after it was closed. Please open a new issue for
20 | related bugs.
21 |
22 | # Assign `resolved` as the reason for locking. Set to `false` to disable
23 | setLockReason: true
24 |
25 | # Limit to only `issues` or `pulls`
26 | # only: issues
27 |
28 | # Optionally, specify configuration settings just for `issues` or `pulls`
29 | # issues:
30 | # exemptLabels:
31 | # - help-wanted
32 | # lockLabel: outdated
33 |
34 | # pulls:
35 | # daysUntilLock: 30
36 |
37 | # Repository to extend settings from
38 | # _extends: repo
--------------------------------------------------------------------------------
/.github/profanity.yml:
--------------------------------------------------------------------------------
1 | # Configuration for probot-profanity - https://github.com/nickgarlis/probot-profanity
2 |
3 | # Language to detect profanity in.
4 | # Choose one of the following: 'de', en', 'es', 'fr', 'it', 'nl', 'pt', 'ru'
5 | language: 'en'
6 |
7 | # Set to true to censor issues (defaults to false)
8 | censor: false
9 |
10 | # Placeholder to replace the letters of a forbidden word
11 | placeholder: '*'
12 |
13 | # A list of extra forbidden words. Set to `[]` to disable
14 | extraWords: []
15 |
16 | # A list of forbidden words to be ignored. Set to `[]` to disable
17 | exemptWords: []
18 |
19 | # Number of days of inactivity before an inappropriate Issue or Pull Request is closed.
20 | # Set to false to disable. If disabled, issues still need to be closed manually, but will remain marked as inappropriate.
21 | daysUntilClose: 2
22 |
23 | # Issues or Pull Requests with these labels will never be considered inappropriate. Set to `[]` to disable
24 | exemptLabels: []
25 |
26 | # Set to true to ignore issues in a project (defaults to false)
27 | exemptProjects: false
28 |
29 | # Set to true to ignore issues in a milestone (defaults to false)
30 | exemptMilestones: false
31 |
32 | # Label to use when marking as inappropriate
33 | profanityLabel: inappropriate
34 |
35 | # Comment to post when marking as profanity. Set to `false` to disable
36 | markComment: >
37 | This issue has been automatically marked as inappropriate because
38 | it contains forbidden words. It will be closed if no further edit
39 | occurs. Thank you for your contributions.
40 |
41 | # Comment to post when removing the inappropriate label.
42 | # unmarkComment: >
43 | # Your comment here.
44 |
45 | # Comment to post when closing an inappropriate Issue or Pull Request.
46 | # closeComment: >
47 | # Your comment here.
48 |
49 | # Limit the number of actions per hour, from 1-30. Default is 30
50 | limitPerRun: 30
51 |
52 | # Limit to only `issues` or `pulls`
53 | # only: issues
54 |
55 | # Optionally, specify configuration settings that are specific to just 'issues' or 'pulls':
56 | # pulls:
57 | # markComment: >
58 | # This issue has been automatically marked as inappropriate because
59 | # it contains forbidden words. It will be closed if no further edit
60 | # occurs. Thank you for your contributions.
61 |
62 | # issues:
63 | # exemptLabels:
64 | # - somelabel
--------------------------------------------------------------------------------
/.github/workflows/build.yml:
--------------------------------------------------------------------------------
1 | name: CI
2 | on:
3 | push:
4 | branches:
5 | - master
6 | pull_request:
7 | types: [ opened, synchronize, reopened ]
8 | jobs:
9 | build:
10 | runs-on: ubuntu-latest
11 | steps:
12 | - uses: actions/checkout@v2
13 | with:
14 | fetch-depth: 0 # Shallow clones should be disabled for a better relevancy of analysis
15 | - name: Set up JDK 11
16 | uses: actions/setup-java@v1
17 | with:
18 | java-version: 11
19 | - name: Cache Maven packages
20 | uses: actions/cache@v1
21 | with:
22 | path: ~/.m2
23 | key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
24 | restore-keys: ${{ runner.os }}-m2
25 | - name: Cache SonarCloud packages
26 | uses: actions/cache@v1
27 | with:
28 | path: ~/.sonar/cache
29 | key: ${{ runner.os }}-sonar
30 | restore-keys: ${{ runner.os }}-sonar
31 | - name: Cache Maven packages
32 | uses: actions/cache@v1
33 | with:
34 | path: ~/.m2
35 | key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
36 | restore-keys: ${{ runner.os }}-m2
37 | - name: Build and analyze
38 | env:
39 | GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
40 | SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
41 | CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
42 | MAVEN_OPTS: |
43 | -Xmx1024m
44 | -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss,SSS
45 | -Dorg.slf4j.simpleLogger.showDateTime=true
46 | run: |
47 | mvn -B -P qulice clean install
48 | bash <(curl -s https://codecov.io/bash)
49 | if [ -n "${{ secrets.SONAR_TOKEN }}" ]; then
50 | mvn -B -P qulice org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
51 | fi
52 | if [ -n "${{ secrets.CODECOV_TOKEN }}" ]; then
53 | bash <(curl -s https://codecov.io/bash)
54 | fi
55 |
--------------------------------------------------------------------------------
/.github/workflows/codeql-analysis.yml:
--------------------------------------------------------------------------------
1 | # For most projects, this workflow file will not need changing; you simply need
2 | # to commit it to your repository.
3 | #
4 | # You may wish to alter this file to override the set of languages analyzed,
5 | # or to provide custom queries or build logic.
6 | #
7 | # ******** NOTE ********
8 | # We have attempted to detect the languages in your repository. Please check
9 | # the `language` matrix defined below to confirm you have the correct set of
10 | # supported CodeQL languages.
11 | #
12 | name: "CodeQL"
13 |
14 | env:
15 | java: 15
16 |
17 | on:
18 | push:
19 | branches: [ master ]
20 | pull_request:
21 | # The branches below must be a subset of the branches above
22 | branches: [ master ]
23 | schedule:
24 | - cron: '20 7 * * 6'
25 |
26 | jobs:
27 | analyze:
28 | name: Analyze
29 | runs-on: ubuntu-latest
30 |
31 | strategy:
32 | fail-fast: false
33 | matrix:
34 | language: [ 'java' ]
35 | # CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python' ]
36 | # Learn more:
37 | # https://docs.github.com/en/free-pro-team@latest/github/finding-security-vulnerabilities-and-errors-in-your-code/configuring-code-scanning#changing-the-languages-that-are-analyzed
38 |
39 | steps:
40 | - name: Checkout repository
41 | uses: actions/checkout@v2
42 |
43 | - name: Setup Java
44 | uses: actions/setup-java@v1
45 | with:
46 | java-version: '${{ env.java }}'
47 |
48 | # Initializes the CodeQL tools for scanning.
49 | - name: Initialize CodeQL
50 | uses: github/codeql-action/init@v1
51 | with:
52 | languages: ${{ matrix.language }}
53 | # If you wish to specify custom queries, you can do so here or in a config file.
54 | # By default, queries listed here will override any specified in a config file.
55 | # Prefix the list here with "+" to use these queries and those in the config file.
56 | # queries: ./path/to/local/query, your-org/your-repo/queries@main
57 |
58 | # Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
59 | # If this step fails, then you should remove it and run the build manually (see below)
60 | - name: Autobuild
61 | uses: github/codeql-action/autobuild@v1
62 |
63 | # ℹ️ Command-line programs to run using the OS shell.
64 | # 📚 https://git.io/JvXDl
65 |
66 | # ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
67 | # and modify them (or add more) to build your code if your project
68 | # uses a compiled language
69 |
70 | #- run: |
71 | # make bootstrap
72 | # make release
73 |
74 | - name: Perform CodeQL Analysis
75 | uses: github/codeql-action/analyze@v1
76 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /target
2 | *.iml
3 | *.iws
4 | *.class
5 | /.idea/
6 | /.tmp/
7 | *.jar
--------------------------------------------------------------------------------
/.pdd:
--------------------------------------------------------------------------------
1 | --source=.
2 | --verbose
3 | --exclude target/**/*
4 | --exclude docs
5 |
--------------------------------------------------------------------------------
/.rultor.yml:
--------------------------------------------------------------------------------
1 | docker:
2 | image: g4s8/rultor:alpine3.10
3 | architect:
4 | - dgroup
5 |
6 | readers:
7 | - "urn:github:1651114"
8 |
9 | # @todo #/DEV Configure the deployment to dev maven repository in Github in order to work with RC and snapshots.
10 | # The dev repo should be Github for master branch + bugfix branches if needed.
11 | # As a plan B, we can consider promoting using Nexus+OSSRH:
12 | # https://help.sonatype.com/repomanager2/staging-releases/staging-overview
13 | # https://help.sonatype.com/repomanager2/staging-releases/configuring-your-project-for-deployment
14 | # https://nvie.com/posts/a-successful-git-branching-model
15 | # https://github.com/nvie/gitflow
16 | # https://gist.github.com/lemiorhan/97b4f827c08aed58a9d8
17 | # https://github.com/jgitver/jgitver
18 | # https://danielkummer.github.io/git-flow-cheatsheet/index.html
19 | assets:
20 | settings.xml: "dgroup/home#settings.xml"
21 | pubring.gpg: "dgroup/home#8B01AA62_enumerable4j_pubring.gpg"
22 | secring.gpg: "dgroup/home#8B01AA62_enumerable4j_secring.gpg"
23 |
24 | merge:
25 | env:
26 | MAVEN_OPTS: "-XX:MaxPermSize=256m -Xmx512m"
27 | script:
28 | - mvn -Pqulice clean package
29 |
30 | release:
31 | sensitive:
32 | - settings.xml
33 | - pubring.gpg
34 | - secring.gpg
35 | script: |
36 | mvn versions:set "-DnewVersion=${tag}"
37 | git add pom.xml
38 | git commit -am "${tag}"
39 | mvn -X -V -Dgpg.homedir="$HOME" -P qulice,ossrh-enumerable4j -s ../settings.xml clean deploy
40 |
--------------------------------------------------------------------------------
/build.sh:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 | #
3 | # MIT License
4 | #
5 | # Copyright (c) 2019-2021 Yurii Dubinka
6 | #
7 | # Permission is hereby granted, free of charge, to any person obtaining a copy
8 | # of this software and associated documentation files (the "Software"),
9 | # to deal in the Software without restriction, including without limitation
10 | # the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 | # and/or sell copies of the Software, and to permit persons to whom
12 | # the Software is furnished to do so, subject to the following conditions:
13 | #
14 | # The above copyright notice and this permission notice shall be included
15 | # in all copies or substantial portions of the Software.
16 | #
17 | # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | # FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
20 | # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
22 | # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
23 | # OR OTHER DEALINGS IN THE SOFTWARE.
24 | #
25 |
26 | #
27 | # Build project
28 | #
29 | MAVEN_OPTS="-Xmx256m"
30 | MAVEN_OPTS="${MAVEN_OPTS} -Dorg.slf4j.simpleLogger.dateTimeFormat=HH:mm:ss,SSS"
31 | MAVEN_OPTS="${MAVEN_OPTS} -Dorg.slf4j.simpleLogger.showDateTime=true"
32 | mvn -P qulice clean install
--------------------------------------------------------------------------------
/license.txt:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2019-2021 Yurii Dubinka
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"),
7 | to deal in the Software without restriction, including without limitation
8 | the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 | and/or sell copies of the Software, and to permit persons to whom
10 | the Software is furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included
13 | in all copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 | ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
21 | OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/mvnw:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dgroup/enumerable4j/0b9552ced7f808f23c046823e02ef2b3a185dfdf/mvnw
--------------------------------------------------------------------------------
/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 set title of command window
39 | title %0
40 | @REM enable echoing by setting MAVEN_BATCH_ECHO to 'on'
41 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
42 |
43 | @REM set %HOME% to equivalent of $HOME
44 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
45 |
46 | @REM Execute a user defined script before this one
47 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
48 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending
49 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
50 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
51 | :skipRcPre
52 |
53 | @setlocal
54 |
55 | set ERROR_CODE=0
56 |
57 | @REM To isolate internal variables from possible post scripts, we use another setlocal
58 | @setlocal
59 |
60 | @REM ==== START VALIDATION ====
61 | if not "%JAVA_HOME%" == "" goto OkJHome
62 |
63 | echo.
64 | echo Error: JAVA_HOME not found in your environment. >&2
65 | echo Please set the JAVA_HOME variable in your environment to match the >&2
66 | echo location of your Java installation. >&2
67 | echo.
68 | goto error
69 |
70 | :OkJHome
71 | if exist "%JAVA_HOME%\bin\java.exe" goto init
72 |
73 | echo.
74 | echo Error: JAVA_HOME is set to an invalid directory. >&2
75 | echo JAVA_HOME = "%JAVA_HOME%" >&2
76 | echo Please set the JAVA_HOME variable in your environment to match the >&2
77 | echo location of your Java installation. >&2
78 | echo.
79 | goto error
80 |
81 | @REM ==== END VALIDATION ====
82 |
83 | :init
84 |
85 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
86 | @REM Fallback to current working directory if not found.
87 |
88 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
89 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
90 |
91 | set EXEC_DIR=%CD%
92 | set WDIR=%EXEC_DIR%
93 | :findBaseDir
94 | IF EXIST "%WDIR%"\.mvn goto baseDirFound
95 | cd ..
96 | IF "%WDIR%"=="%CD%" goto baseDirNotFound
97 | set WDIR=%CD%
98 | goto findBaseDir
99 |
100 | :baseDirFound
101 | set MAVEN_PROJECTBASEDIR=%WDIR%
102 | cd "%EXEC_DIR%"
103 | goto endDetectBaseDir
104 |
105 | :baseDirNotFound
106 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
107 | cd "%EXEC_DIR%"
108 |
109 | :endDetectBaseDir
110 |
111 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
112 |
113 | @setlocal EnableExtensions EnableDelayedExpansion
114 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
115 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
116 |
117 | :endReadAdditionalConfig
118 |
119 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
120 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
121 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
122 |
123 | set DOWNLOAD_URL="https://repo.maven.apache.org/maven2/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar"
124 |
125 | FOR /F "tokens=1,2 delims==" %%A IN ("%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.properties") DO (
126 | IF "%%A"=="wrapperUrl" SET DOWNLOAD_URL=%%B
127 | )
128 |
129 | @REM Extension to allow automatically downloading the maven-wrapper.jar from Maven-central
130 | @REM This allows using the maven wrapper in projects that prohibit checking in binary data.
131 | if exist %WRAPPER_JAR% (
132 | if "%MVNW_VERBOSE%" == "true" (
133 | echo Found %WRAPPER_JAR%
134 | )
135 | ) else (
136 | if not "%MVNW_REPOURL%" == "" (
137 | SET DOWNLOAD_URL="%MVNW_REPOURL%/io/takari/maven-wrapper/0.5.5/maven-wrapper-0.5.5.jar"
138 | )
139 | if "%MVNW_VERBOSE%" == "true" (
140 | echo Couldn't find %WRAPPER_JAR%, downloading it ...
141 | echo Downloading from: %DOWNLOAD_URL%
142 | )
143 |
144 | powershell -Command "&{"^
145 | "$webclient = new-object System.Net.WebClient;"^
146 | "if (-not ([string]::IsNullOrEmpty('%MVNW_USERNAME%') -and [string]::IsNullOrEmpty('%MVNW_PASSWORD%'))) {"^
147 | "$webclient.Credentials = new-object System.Net.NetworkCredential('%MVNW_USERNAME%', '%MVNW_PASSWORD%');"^
148 | "}"^
149 | "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; $webclient.DownloadFile('%DOWNLOAD_URL%', '%WRAPPER_JAR%')"^
150 | "}"
151 | if "%MVNW_VERBOSE%" == "true" (
152 | echo Finished downloading %WRAPPER_JAR%
153 | )
154 | )
155 | @REM End of extension
156 |
157 | @REM Provide a "standardized" way to retrieve the CLI args that will
158 | @REM work with both Windows and non-Windows executions.
159 | set MAVEN_CMD_LINE_ARGS=%*
160 |
161 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
162 | if ERRORLEVEL 1 goto error
163 | goto end
164 |
165 | :error
166 | set ERROR_CODE=1
167 |
168 | :end
169 | @endlocal & set ERROR_CODE=%ERROR_CODE%
170 |
171 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
172 | @REM check for post script, once with legacy .bat ending and once with .cmd ending
173 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
174 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
175 | :skipRcPost
176 |
177 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
178 | if "%MAVEN_BATCH_PAUSE%" == "on" pause
179 |
180 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
181 |
182 | exit /B %ERROR_CODE%
183 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
24 |
28 | 4.0.0
29 | io.github.dgroup
30 | enumerable4j
31 | 0.0.0
32 | jar
33 | ${project.artifactId}
34 | Amazing Ruby's "Enumerable" ported to Java.
35 | http://github.com/dgroup/enumerable4j
36 |
37 |
38 | ${project.licence}
39 | ${project.licence.url}
40 | repo
41 |
42 |
43 |
44 |
45 | Yurii Dubinka
46 | yurii.dubinka@gmail.com
47 | io.github.dgroup
48 | https://github.com/dgroup
49 |
50 |
51 |
52 | scm:git:git:github.com/dgroup/enumerable4j.git
53 | scm:git:ssh://github.com:dgroup/enumerable4j.git
54 | http://github.com/dgroup/enumerable4j/tree/master
55 | HEAD
56 |
57 |
58 |
59 | ossrh
60 | https://oss.sonatype.org/content/repositories/snapshots
61 |
62 |
63 | ossrh
64 | https://oss.sonatype.org/service/local/staging/deploy/maven2/
65 |
66 |
67 |
68 |
69 | ${project.version}
70 | MIT License
71 | https://github.com/dgroup/enumerable4j/blob/master/license.txt
72 |
73 | 1.8
74 | 1.8
75 | 1.8
76 | UTF-8
77 | 3.7.0
78 | 3.0.2
79 | 3.1.0
80 | 3.0.2
81 | 1.2.0
82 | 1.6.8
83 | 2.9.1
84 | 2.2.1
85 | 3.0.0-M3
86 | 3.0.0-M3
87 | 1.5
88 | 1.0.0
89 | 3.8.1
90 | 0.8.6
91 | 0.18.19
92 | 3.4.0.905
93 | 1.2
94 |
95 | 0.49
96 |
97 | 2.2
98 | 5.7.1
99 | 0.22
100 | 1.37
101 | enumerable4j-benchmarks
102 |
103 | 3
104 | 0.6
105 | 0.55
106 | 0.5
107 | 0.65
108 | 0.5
109 | dgroup_enumerable4j
110 | dgroup-github
111 | https://sonarcloud.io
112 |
113 | **/*Exception.java,
114 | **/*Fake*.java,**/*.Fake*.java,**/**.Fake*.java
115 |
116 | ${project.basedir}/target/site/jacoco/jacoco.xml
117 |
118 |
119 |
120 | org.cactoos
121 | cactoos
122 | ${cactoos.version}
123 |
124 |
125 | org.hamcrest
126 | hamcrest
127 | ${hamcrest.version}
128 | test
129 |
130 |
131 | org.junit.jupiter
132 | junit-jupiter-engine
133 | ${junit.version}
134 | test
135 |
136 |
137 | org.llorllale
138 | cactoos-matchers
139 | ${cactoos-matchers.version}
140 | compile
141 |
142 |
143 | org.openjdk.jmh
144 | jmh-core
145 | ${jmh.version}
146 |
147 |
148 | org.openjdk.jmh
149 | jmh-generator-annprocess
150 | ${jmh.version}
151 | provided
152 |
153 |
154 |
155 |
156 |
157 |
158 | org.apache.maven.plugins
159 | maven-compiler-plugin
160 | ${mvn.compiler.version}
161 |
162 | ${jdk.version}
163 | ${jdk.version}
164 |
165 |
166 |
167 | org.apache.maven.plugins
168 | maven-source-plugin
169 | ${mvn.source.version}
170 |
171 |
172 | attach-sources
173 |
174 | jar-no-fork
175 |
176 |
177 |
178 |
179 |
180 | org.apache.maven.plugins
181 | maven-javadoc-plugin
182 | ${mvn.javadoc.version}
183 |
184 |
185 | attach-javadocs
186 |
187 | jar
188 |
189 |
190 |
191 |
192 | -Xdoclint:none
193 |
194 |
195 |
196 | org.apache.maven.plugins
197 | maven-surefire-plugin
198 | ${mvn.surefire.version}
199 |
200 | true
201 | false
202 | 4
203 |
204 |
205 |
210 |
211 |
212 |
213 |
214 | qulice
215 |
216 |
217 |
218 | com.qulice
219 | qulice-maven-plugin
220 | ${qulice.version}
221 |
222 | file:${basedir}/license.txt
223 |
224 | duplicatefinder:.*
225 | dependencies:io.quarkus:quarkus-picocli
226 | dependencies:io.quarkus:quarkus-arc
227 | dependencies:org.cactoos:cactoos
228 | dependencies:org.llorllale:cactoos-matchers
229 | dependencies:javax.ws.rs:jsr311-api
230 | dependencies:org.openjdk.jmh
231 | org.junit.jupiter:junit-jupiter-api
232 |
233 |
234 |
235 |
236 | apply-qulice-validations
237 | validate
238 |
239 | check
240 |
241 |
242 |
243 |
244 |
245 | commons-logging
246 | commons-logging
247 | ${commons-logging.version}
248 |
249 |
250 |
251 |
252 | org.jacoco
253 | jacoco-maven-plugin
254 | ${jacoco.version}
255 |
256 |
257 |
258 | io/dgroup/**/*Exception.*
259 | io/dgroup/**/*/*Fake*.*
260 | io/dgroup/**/*Envelope.*
261 |
262 |
263 |
264 |
265 | jacoco-initialize
266 | process-resources
267 |
268 | prepare-agent
269 |
270 |
271 |
272 | jacoco-check
273 | test
274 |
275 | check
276 |
277 |
278 |
279 |
280 | BUNDLE
281 |
282 |
283 | INSTRUCTION
284 | COVEREDRATIO
285 | ${code.cvrg.instructions}
286 |
287 |
288 | LINE
289 | COVEREDRATIO
290 | ${code.cvrg.line}
291 |
292 |
293 | BRANCH
294 | COVEREDRATIO
295 | ${code.cvrg.branch}
296 |
297 |
298 | COMPLEXITY
299 | COVEREDRATIO
300 | ${code.cvrg.complexity}
301 |
302 |
303 | METHOD
304 | COVEREDRATIO
305 | ${code.cvrg.methods}
306 |
307 |
308 | CLASS
309 | MISSEDCOUNT
310 | ${code.cvrg.classes-without-tests}
311 |
312 |
313 |
314 |
315 |
316 |
317 |
318 | jacoco-report
319 | install
320 |
321 | report
322 |
323 |
324 |
325 |
326 |
327 |
328 |
329 |
330 | ossrh-enumerable4j
331 |
332 |
333 |
334 | org.apache.maven.plugins
335 | maven-gpg-plugin
336 | ${mvn.gpg.version}
337 |
338 |
339 | sign-artifacts
340 | verify
341 |
342 | sign
343 |
344 |
345 |
346 |
347 |
348 | --pinentry-mode
349 | loopback
350 |
351 |
352 |
353 |
354 | org.sonatype.plugins
355 | nexus-staging-maven-plugin
356 | ${mvn.nexus.version}
357 | true
358 |
359 | ossrh
360 | https://oss.sonatype.org/
361 | true
362 |
363 |
364 |
365 |
366 |
367 |
368 |
369 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | [](https://mvnrepository.com/artifact/io.github.dgroup/enumerable4j)
2 | [](http://www.javadoc.io/doc/io.github.dgroup/enumerable4j)
3 | [](./license.txt)
4 | [](https://github.com/dgroup/enumerable4j/graphs/commit-activity)
5 | [](https://hitsofcode.com/view/github/dgroup/enumerable4j)
6 |
7 | [](https://github.com/dgroup/enumerable4j/actions/workflows/build.yml)
8 | [](http://www.0pdd.com/p?name=dgroup/enumerable4j)
9 | [](https://requires.io/github/dgroup/enumerable4j/requirements/?branch=master)
10 | [](https://snyk.io/org/dgroup/project/4a9f3433-7da5-4c24-985e-ee1d3077c458/?tab=dependencies\&vulns=vulnerable)
11 |
12 | [](http://www.rultor.com/p/dgroup/enumerable4j)
13 | [](http://www.elegantobjects.org/#principles)
14 | [](https://www.jetbrains.com/idea/)
15 |
16 | [](http://www.qulice.com/)
17 | [](https://sonarcloud.io/dashboard?id=dgroup_enumerable4j)
18 | [](https://codebeat.co/projects/github-com-dgroup-enumerable4j-master)
19 | [](https://www.codacy.com/gh/dgroup/enumerable4j/dashboard?utm_source=github.com\&utm_medium=referral\&utm_content=dgroup/enumerable4j\&utm_campaign=Badge_Grade)
20 | [](https://codecov.io/gh/dgroup/enumerable4j)
21 |
22 | * [Overview](#overview)
23 |
24 | * [How to use?](#how-to-use)
25 | * [.all](#all)
26 | * [.any](#any)
27 | * [.none](#none)
28 | * [.select](#select)
29 | * [.map](#map)
30 | * [.count](#count)
31 | * [.reject](#reject)
32 | * [.find](#find)
33 | * [.reduce](#reduce)
34 | * [.after](#after)
35 | * [.next](#next)
36 | * [.chain](#chain)
37 | * [.each](#each)
38 | * [.take](#take)
39 | * [.drop](#drop)
40 | * [.one](#one)
41 | * [.uniq](#uniq)
42 | * [.zip](#zip)
43 |
44 | * [How to contribute?](#how-to-contribute)
45 |
46 | * [Contributors](#contributors)
47 |
48 | ### Overview
49 |
50 | **enumerable4j** is a Ruby's well known [Enumerable](https://ruby-doc.org/core-2.6/Enumerable.html)
51 | ported to `java` as interface with set of default methods which simplify typical operations with collections.
52 | ```java
53 | /**
54 | * The iterable with primitive operations witch simplify typical actions like count, map, etc.
55 | *
56 | * The API is based on Ruby's Enumerable:
57 | * https://ruby-doc.org/core-2.6/Enumerable.html.
58 | *
59 | * The Enumerable provides methods with several traversal and searching features, and with the
60 | * ability to sort. The class must provide a method each, which yields successive members of the
61 | * collection.
62 | *
63 | * @param The type of entities.
64 | * @since 0.1.0
65 | */
66 | public interface Enumerable extends Collection {
67 | /**
68 | * Passes each element of the collection to the each given function.
69 | * The given null predicates are skipped.
70 | * If no predicate (null) is given, then false is returned instead.
71 | * @param first The function to match each element.
72 | * @param other The array of functions to match each element.
73 | * @return True if the functions never return false.
74 | */
75 | default boolean all(Predicate first, Predicate... other) {
76 | // ...
77 | }
78 |
79 | /**
80 | * Passes at least one element of the collection to the each given function.
81 | * The given null predicates are skipped.
82 | * If no predicate (null) is given, then false is returned instead.
83 | * @param first The function to match at least one element.
84 | * @param other The array of functions to match at least one element.
85 | * @return True if functions never return true at least once.
86 | */
87 | default boolean any(Predicate first, Predicate... other) {
88 | // ...
89 | }
90 |
91 | /**
92 | * Doesn't passes elements of the collection to the each given function.
93 | * The given null predicates are skipped.
94 | * If no predicate (null) is given, then true is returned instead.
95 | * @param first The function to match none elements.
96 | * @param other The array of functions to match none elements.
97 | * @return True if the functions never returns false or nil.
98 | */
99 | default boolean none(Predicate first, Predicate... other) {
100 | // ...
101 | }
102 |
103 | /**
104 | * Returns an enumerable containing all elements of enumerable for which the given functions
105 | * return a true value.
106 | * The given null predicates are skipped.
107 | * If no predicate (null) is given, then an empty enumerable is returned instead.
108 | * @param first The function to match each element.
109 | * @param other The array of functions to match each element.
110 | * @return The enumerable.
111 | */
112 | default Enumerable select(Predicate first, Predicate... other) {
113 | // ...
114 | }
115 |
116 | /**
117 | * Returns an enumerable containing all elements of enumerable for which the given function
118 | * returns a false value.
119 | * The given null predicates are skipped.
120 | * If no predicate (null) is given, then 'this' is returned instead.
121 | * @param first The function to match each element.
122 | * @param other The array of functions to match each element.
123 | * @return The enumerable.
124 | */
125 | default Enumerable reject(Predicate first, Predicate... other) {
126 | // ...
127 | }
128 |
129 | /**
130 | * Returns an enumerable containing first element of enumerable for which the given function
131 | * returns a true value.
132 | * The given null predicates are skipped.
133 | * If no predicate (null) is given, or no element found then null is returned instead.
134 | * @param first The function to match each element.
135 | * @param other The array of functions to match each element.
136 | * @return The first element of enumerable, that matches predicate.
137 | */
138 | default T find(Predicate first, Predicate... other) {
139 | // ...
140 | }
141 |
142 | /**
143 | * Returns an enumerable containing first element of enumerable for which the given function
144 | * returns a true value.
145 | * The given null predicates are skipped.
146 | * If no predicate (null) is given, or no element found then alternative is returned instead.
147 | * @param alt The alternative to return in case of null predicate or no element found.
148 | * @param first The function to match each element.
149 | * @param other The array of functions to match each element.
150 | * @return The first element of enumerable, that matches predicate.
151 | */
152 | default T find(X alt, Predicate first, Predicate... other) {
153 | // ...
154 | }
155 |
156 | /**
157 | * Returns an enumerable containing all elements, on which given function was applied.
158 | * If no function (null) is given, then 'this' is returned instead.
159 | * @param fnc The function to apply to each element.
160 | * @param The type of target entity.
161 | * @return The enumerable.
162 | */
163 | default Enumerable map(Function super T, ? extends R> fnc) {
164 | // ...
165 | }
166 |
167 | /**
168 | * Returns the number of elements that are present in enumerable for which the given
169 | * function return true.
170 | * The given null predicates are skipped.
171 | * If no function (null) is given, then 0 is returned instead.
172 | * @param first The function to match each element.
173 | * @param other The array of functions to match each element.
174 | * @return Number of elements satisfying the given function.
175 | */
176 | default long count(Predicate first, Predicate... other) {
177 | // ...
178 | }
179 |
180 | /**
181 | * Returns a result of the reduction of the elements in this stream,
182 | * using provided identity value and accumulation function operator.
183 | * If no function (null) is given, then identity is returned instead.
184 | * @param idn The identity value of the accumulation function.
185 | * @param opr The accumulation function operator which combining previous and current values.
186 | * @return Result of of combining elements.
187 | */
188 | default T reduce(T idn, BinaryOperator opr) {
189 | // ...
190 | }
191 |
192 | /**
193 | * Returns an enumerable containing all elements of enumerable
194 | * after the first one which corresponds the condition.
195 | * If no predicate (null) is given, then empty enumerable is returned instead.
196 | * @param prd The function to match element after which enumerable elements should be returned.
197 | * @return The enumerable.
198 | */
199 | default Enumerable after(Predicate prd) {
200 | // ...
201 | }
202 |
203 | /**
204 | * Returns an enumerable containing a certain number of elements of enumerable
205 | * after the first one which corresponds the condition.
206 | * If no predicate (null) is given, then empty enumerable is returned instead.
207 | * @param prd The function to match element after which enumerable elements should be returned.
208 | * @param size The number of elements the enumerable should be limited to.
209 | * @return The enumerable.
210 | * @throws IllegalArgumentException If the size is negative.
211 | */
212 | default Enumerable after(Predicate prd, long size) {
213 | // ...
214 | }
215 |
216 | /**
217 | * Returns the next element of enumerable after the first one which corresponds the condition.
218 | * If no predicate (null) is given, or no element found then null is returned instead.
219 | * @param prd The function to match element after which enumerable element should be returned.
220 | * @return The next element of enumerable after the first one which corresponds the condition.
221 | */
222 | default T next(Predicate prd) {
223 | // ...
224 | }
225 |
226 | /**
227 | * Returns the next element of enumerable after the first one which corresponds the condition.
228 | * If no predicate (null) is given, or no element found then alternative is returned instead.
229 | * @param prd The function to match element after which enumerable element should be returned.
230 | * @param alt The alternative to return in case of null predicate or no element found.
231 | * @return The next element of enumerable after the first one which corresponds the condition.
232 | */
233 | default T next(Predicate prd, T alt) {
234 | // ...
235 | }
236 |
237 | /**
238 | * Returns a new enumerable which contains the items of the original collection
239 | * and the added items of the given enumerable.
240 | * If no enumerable (null) is given, then 'this' is returned instead.
241 | * @param enm The given enumerable.
242 | * @return The enumerable.
243 | */
244 | default Enumerable chain(Enumerable enm) {
245 | // ...
246 | }
247 |
248 | /**
249 | * Returns an enumerable consisting of the elements of the collection,
250 | * additionally performing the provided action on each element of the enumerable.
251 | * @param act An action to perform on the elements.
252 | * @return The enumerable.
253 | */
254 | default Enumerable each(Consumer act) {
255 | // ...
256 | }
257 |
258 | /**
259 | * Returns an enumerable containing first elements of specified size from the enumerable.
260 | * @param num The number of elements the enumerable should be limited to.
261 | * @return The enumerable.
262 | * @throws IllegalArgumentException If the size is negative.
263 | */
264 | default Enumerable take(long num) {
265 | // ...
266 | }
267 |
268 | /**
269 | * Drops first elements of specified size,
270 | * and returns an enumerable containing the rest of the elements.
271 | * @param num The number of elements to be dropped.
272 | * @return The enumerable.
273 | * @throws IllegalArgumentException If the size is negative.
274 | */
275 | default Enumerable drop(long num) {
276 | // ...
277 | }
278 |
279 | /**
280 | * The method returns true if the functions return true exactly once.
281 | * The given null predicates are skipped.
282 | * If no predicate (null) is given, then false is returned instead.
283 | * @param first The function to match each element.
284 | * @param other The array of functions to match each element.
285 | * @return True if the functions returns true exactly once.
286 | */
287 | default boolean one(Predicate first, Predicate... other) {
288 | // ...
289 | }
290 |
291 | /**
292 | * Returns a new enumerable containing the unique elements.
293 | * It compares values using the {@link #hashCode} and {@link #equals} methods for efficiency.
294 | * @return The enumerable.
295 | */
296 | default Enumerable uniq() {
297 | // ...
298 | }
299 |
300 | /**
301 | * Returns a new enumerable containing the unique elements which corresponds the condition
302 | * of the given function.
303 | * If no function (null) is given, then empty enumerable is returned instead.
304 | * @param fnc The function to apply to each element.
305 | * @param The type of function result entity.
306 | * @return The enumerable.
307 | */
308 | default Enumerable uniq(Function super T, ? extends R> fnc) {
309 | // ...
310 | }
311 |
312 | /**
313 | * Returns an enumerable of collections which contains each element of original collection
314 | * and corresponding elements from each argument collections.
315 | * The length of the resulting enumerable is {@link Enumerable#size}.
316 | * If the size of any argument is less than {@link Enumerable#size}, null values are supplied.
317 | * @param first The array of enumerable to merge.
318 | * @param other The array of enumerable to merge.
319 | * @return The enumerable.
320 | */
321 | default Enumerable> zip(Enumerable first, Enumerable... other) {
322 | // ...
323 | }
324 | }
325 | ```
326 | See [more](./src/main/java/io/github/dgroup/enumerable4j/Enumerable.java).
327 |
328 | ### How to use
329 |
330 | 1. Get the latest version [here](https://github.com/dgroup/enumerable4j/releases):
331 | ```xml
332 |
333 | io.github.dgroup
334 | enumerable4j
335 | ${version}
336 |
337 | ```
338 | 2. Assign the [Enumerable](./src/main/java/io/github/dgroup/enumerable4j/Enumerable.java) interface with default methods to your own collection
339 | ```java
340 | /**
341 | * The collection which you implemented in your project for some purposes.
342 | */
343 | public class YourOwnCollection extends Collection implements Enumerable {
344 | //
345 | }
346 | ```
347 | You may (but not required) override the default implementations of methods from [Enumerable](./src/main/java/io/github/dgroup/enumerable4j/Enumerable.java) if needed.
348 |
349 | 3. Java version required: 1.8+.
350 | 4. Comparing matrix with other libs:
351 |
352 | enumerable4j (MIT) | Java 8 | [cactoos](https://github.com/yegor256/cactoos) (MIT) | [eclipse-collections]() (EDL)
353 | |------ | ------ | ------ |------ |
354 | `.all(...)` | `.stream().allMatch(...)` | `new And<>(...,...).value()`| tbd |
355 | `.any(...)` | `.stream().anyMatch(...)` | `new Or<>(...,...).value()`| tbd |
356 | `.none(...)` | `.stream().noneMatch(...)` | `new And<>(...,...).value()`| tbd |
357 | `.select(...)` | `.stream().filter(...).collect(Collectors.toList())` | `new Filtered<>(...,...)` | tbd |
358 | `.reject(...)` | `.stream().filter((...).negate()).collect(Collectors.toList())` | `new Filtered<>(...,...)` | tbd |
359 | `.map(...)` | `.stream().map(...).collect(Collectors.toList())` | `new Mapped<>(...,...)` | tbd |
360 | `.count(...)` | `.stream().filter(...).count()` | `new Filtered<>(...).size()` | tbd |
361 | `.find(...)` | `.stream().filter(...).findFirst().orElse(...)` | `new FirstOf<>(...,...).value()` | tbd |
362 | `.reduce(...,...)` | `.stream().reduce(...,...)` | `new Reduced<>(...,...).value()` | tbd |
363 | `.after(...)` | | | tbd |
364 | `.next(...)` | | | tbd |
365 | `.chain(...)` | | | tbd |
366 | `.each(...)` | `.stream().forEach(...)` | `new ForEach<>(...).exec(...)` | tbd |
367 | `.take(...)` | `.stream().limit(...).collect(Collectors.toList())` | `new Sliced<>(0,...,...)` | tbd |
368 | `.drop(...)` | `.stream().skip(...).collect(Collectors.toList())` | `new Sliced<>(...,...,...)`| tbd |
369 | `.one(...)` | `.stream().filter(...).count() == 1` | `new Filtered<>(...).size() == 1` | tbd |
370 | `.uniq(...)` | `.stream().skip(...).collect(Collectors.toSet())` | | tbd |
371 | `.zip(...)` | | | tbd |
372 |
373 | #### .all
374 |
375 | ```java
376 | YourOwnCollection src = ... // with elements [1, 2, 3]
377 | boolean allPositive = src.all(val -> val > 0); // true
378 | ```
379 |
380 | #### .any
381 |
382 | ```java
383 | YourOwnCollection src = ... // with elements [-1, 0, 1]
384 | boolean oneIsPositive = src.any(val -> val > 0); // true
385 | ```
386 |
387 | #### .none
388 |
389 | ```java
390 | YourOwnCollection src = ... // with elements [-2, -1, 0]
391 | boolean noneIsPositive = src.none(val -> val > 0); // true
392 | ```
393 |
394 | #### .select
395 |
396 | ```java
397 | YourOwnCollection src = ... // with elements [-1, 1, 2]
398 | Enumerable positive = src.select(val -> val > 0); // [1, 2]
399 | ```
400 |
401 | #### .reject
402 |
403 | ```java
404 | YourOwnCollection src = ... // with elements [-1, 1, 2]
405 | Enumerable negative = src.reject(val -> val > 0); // [-1]
406 | ```
407 |
408 | #### .map
409 |
410 | ```java
411 | YourOwnCollection src = ... // with elements [0, 1, 2]
412 | Enumerable positive = src.map(val -> val + 1); // [1, 2, 3]
413 | ```
414 |
415 | #### .count
416 |
417 | ```java
418 | YourOwnCollection src = ... // with elements [-1, 0, 1]
419 | long countNegative = src.count(val -> val < 0); // 1
420 | ```
421 |
422 | #### .find
423 |
424 | ```java
425 | YourOwnCollection src = ... // with elements [-1, 0, 1]
426 | Integer first = src.find(val -> val > 0); // 1
427 | Integer alternative = src.find(50, val -> val > 5); // 50
428 | ```
429 |
430 | #### .reduce
431 |
432 | ```java
433 | YourOwnCollection src = ... // with elements [1, 2, 3]
434 | Integer sum = src.reduce(0, Integer::sum); // 6
435 | ```
436 |
437 | #### .after
438 |
439 | ```java
440 | YourOwnCollection src = ... // with elements [2, 3, 4, 5, 6]
441 | Enumerable afterThree = src.after(val -> val == 3); // [4, 5, 6]
442 | Enumerable firstTwoAfterThree = src.after(val -> val == 3, 2); // [4, 5]
443 | ```
444 |
445 | #### .next
446 |
447 | ```java
448 | YourOwnCollection src = ... // with elements [1, 2, 3, 4]
449 | Integer next = src.next(val -> val == 2); // 3
450 | Integer alternative = src.next(val -> val > 5, -1); // -1
451 | ```
452 |
453 | #### .chain
454 |
455 | ```java
456 | YourOwnCollection src = ... // with elements [1, 2]
457 | Enumerable joined = src.chain(new Linked<>(3)).chain(new Linked<>(4, 5)); // [1, 2, 3, 4, 5]
458 | ```
459 |
460 | #### .each
461 |
462 | ```java
463 | YourOwnCollection src = ... // with elements [1, 2, 3]
464 | Enumerable buf = src.each(System.out::print); // [1, 2, 3] , printed: "123"
465 | ```
466 |
467 | #### .take
468 |
469 | ```java
470 | YourOwnCollection src = ... // with elements [1, 2, 3]
471 | Enumerable taken = src.take(2); // [1, 2]
472 | ```
473 |
474 | #### .drop
475 |
476 | ```java
477 | YourOwnCollection src = ... // with elements [1, 2, 3]
478 | Enumerable taken = src.drop(2); // [3]
479 | ```
480 |
481 | #### .one
482 |
483 | ```java
484 | YourOwnCollection src = ... // with elements [-1, 0, 1]
485 | boolean onePositive = src.one(val -> val > 0); // true
486 | ```
487 |
488 | #### .uniq
489 |
490 | ```java
491 | YourOwnCollection> src = ... // with elements [[1, 2], [3, 4], [1, 2], [3, 4, 5]]
492 | Enumerable> unique = src.uniq(); // [[1, 2], [3, 4], [3, 4, 5]]
493 | Enumerable> uniqueByKey = src.uniq(enm -> enm.get(0)); // [[1, 2], [3, 4]]
494 | ```
495 |
496 | #### .zip
497 |
498 | ```java
499 | YourOwnCollection src = ... // with elements [1, 2, 3]
500 | YourOwnCollection src2 = ... // with elements [4, 5, 6]
501 | Enumerable> zip = src.zip(src2); // [[1, 4]], [2, 5], [3, 6]]
502 | ```
503 |
504 | ### How to contribute?
505 |
506 | [](http://www.elegantobjects.org/#principles)
507 |
508 | 1. Pull requests are welcome! Don't forget to add your name to contribution section and run this,
509 | beforehand:
510 | ```bash
511 | mvn -Pqulice clean install
512 | ```
513 | 2. Everyone interacting in this project’s codebases, issue trackers, chat rooms is expected to
514 | follow the [code of conduct](.github/CODE_OF_CONDUCT.md).
515 | 3. Latest maven coordinates [here](https://github.com/dgroup/enumerable4j/releases):
516 | ```xml
517 |
518 | io.github.dgroup
519 | enumerable4j
520 | ${version}
521 |
522 | ```
523 |
524 | ### Contributors
525 |
526 | * [dgroup](https://github.com/dgroup) as Yurii Dubinka ()
527 | * [smithros](https://github.com/smithros) as Rostyslav Koval ()
528 | * [ashutosh](https://github.com/singhashutosh96) as Ashutosh Singh ()
529 | * [dykov](https://github.com/dykov) as Oleksii Dykov ()
530 |
--------------------------------------------------------------------------------
/src/main/java/io/github/dgroup/enumerable4j/Empty.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 | package io.github.dgroup.enumerable4j;
25 |
26 | import java.util.Collections;
27 | import org.cactoos.list.ListEnvelope;
28 |
29 | /**
30 | * The empty enumerable.
31 | *
32 | * @param The type of entities.
33 | * @since 0.1.0
34 | */
35 | public class Empty extends ListEnvelope implements Enumerable {
36 | /**
37 | * Ctor.
38 | */
39 | public Empty() {
40 | super(Collections.emptyList());
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/src/main/java/io/github/dgroup/enumerable4j/Enumerable.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 | package io.github.dgroup.enumerable4j;
25 |
26 | import java.util.Collection;
27 | import java.util.HashSet;
28 | import java.util.List;
29 | import java.util.Set;
30 | import java.util.function.BinaryOperator;
31 | import java.util.function.Consumer;
32 | import java.util.function.Function;
33 | import java.util.function.Predicate;
34 | import org.cactoos.list.ListOf;
35 |
36 | /**
37 | * The iterable with primitive operations witch simplify typical actions like count, map, etc.
38 | *
39 | * The API is based on Ruby's Enumerable:
40 | * https://ruby-doc.org/core-2.6/Enumerable.html.
41 | *
42 | * The Enumerable provides methods with several traversal and searching features, and with the
43 | * ability to sort. The class must provide a method each, which yields successive members of the
44 | * collection.
45 | *
46 | * @param The type of entities.
47 | * @since 0.1.0
48 | */
49 | @SuppressWarnings("PMD.TooManyMethods")
50 | public interface Enumerable extends Collection {
51 |
52 | /**
53 | * Passes each element of the collection to the each given function.
54 | * The given null predicates are skipped.
55 | * If no predicate (null) is given, then false is returned instead.
56 | * @param first The function to match each element.
57 | * @param other The array of functions to match each element.
58 | * @return True if the functions never return false.
59 | */
60 | default boolean all(Predicate first, Predicate... other) {
61 | return this.stream().allMatch(new Joined<>(first, other));
62 | }
63 |
64 | /**
65 | * Passes at least one element of the collection to the each given function.
66 | * The given null predicates are skipped.
67 | * If no predicate (null) is given, then false is returned instead.
68 | * @param first The function to match at least one element.
69 | * @param other The array of functions to match at least one element.
70 | * @return True if functions never return true at least once.
71 | */
72 | default boolean any(Predicate first, Predicate... other) {
73 | return this.count(first, other) != 0;
74 | }
75 |
76 | /**
77 | * Doesn't passes elements of the collection to the each given function.
78 | * The given null predicates are skipped.
79 | * If no predicate (null) is given, then true is returned instead.
80 | * @param first The function to match none elements.
81 | * @param other The array of functions to match none elements.
82 | * @return True if the functions never returns false or nil.
83 | */
84 | default boolean none(Predicate first, Predicate... other) {
85 | return this.stream().noneMatch(new Joined<>(first, other));
86 | }
87 |
88 | /**
89 | * Returns an enumerable containing all elements of enumerable for which the given functions
90 | * return a true value.
91 | * The given null predicates are skipped.
92 | * If no predicate (null) is given, then an empty enumerable is returned instead.
93 | * @param first The function to match each element.
94 | * @param other The array of functions to match each element.
95 | * @return The enumerable.
96 | */
97 | default Enumerable select(Predicate first, Predicate... other) {
98 | return new Linked<>(
99 | this.stream().filter(new Joined<>(first, other)).iterator()
100 | );
101 | }
102 |
103 | /**
104 | * Returns an enumerable containing all elements of enumerable for which the given function
105 | * returns a false value.
106 | * The given null predicates are skipped.
107 | * If no predicate (null) is given, then 'this' is returned instead.
108 | * @param first The function to match each element.
109 | * @param other The array of functions to match each element.
110 | * @return The enumerable.
111 | */
112 | default Enumerable reject(Predicate first, Predicate... other) {
113 | Predicate prd = val -> true;
114 | if (first != null) {
115 | prd = first.negate();
116 | }
117 | if (other != null) {
118 | for (final Predicate oth : other) {
119 | if (oth != null) {
120 | prd = prd.and(oth.negate());
121 | }
122 | }
123 | }
124 | return new Linked<>(this.stream().filter(prd).iterator());
125 | }
126 |
127 | /**
128 | * Returns an enumerable containing first element of enumerable for which the given function
129 | * returns a true value.
130 | * The given null predicates are skipped.
131 | * If no predicate (null) is given, or no element found then null is returned instead.
132 | * @param first The function to match each element.
133 | * @param other The array of functions to match each element.
134 | * @return The first element of enumerable, that matches predicate.
135 | */
136 | default X find(Predicate first, Predicate... other) {
137 | return this.find(null, first, other);
138 | }
139 |
140 | /**
141 | * Returns an enumerable containing first element of enumerable for which the given function
142 | * returns a true value.
143 | * The given null predicates are skipped.
144 | * If no predicate (null) is given, or no element found then alternative is returned instead.
145 | * @param alt The alternative to return in case of null predicate or no element found.
146 | * @param first The function to match each element.
147 | * @param other The array of functions to match each element.
148 | * @return The first element of enumerable, that matches predicate.
149 | */
150 | default X find(X alt, Predicate first, Predicate... other) {
151 | return this.stream().filter(new Joined<>(first, other)).findFirst().orElse(alt);
152 | }
153 |
154 | /**
155 | * Returns an enumerable containing all elements, on which given function was applied.
156 | * If no function (null) is given, then empty enumerable is returned instead.
157 | * @param fnc The function to apply to each element.
158 | * @param The type of target entity.
159 | * @return The enumerable.
160 | */
161 | default Enumerable map(Function super X, Y> fnc) {
162 | final Enumerable out;
163 | if (fnc == null) {
164 | out = new Empty<>();
165 | } else {
166 | out = new Linked<>(this.stream().map(fnc).iterator());
167 | }
168 | return out;
169 | }
170 |
171 | /**
172 | * Returns the number of elements that are present in enumerable for which the given
173 | * function return true.
174 | * The given null predicates are skipped.
175 | * If no function (null) is given, then 0 is returned instead.
176 | * @param first The function to match each element.
177 | * @param other The array of functions to match each element.
178 | * @return Number of elements satisfying the given function.
179 | */
180 | default long count(Predicate first, Predicate... other) {
181 | return this.stream().filter(new Joined<>(first, other)).count();
182 | }
183 |
184 | /**
185 | * Returns a result of the reduction of the elements in this stream,
186 | * using provided identity value and accumulation function operator.
187 | * If no function (null) is given, then identity is returned instead.
188 | * @param idn The identity value of the accumulation function.
189 | * @param opr The accumulation function operator which combining previous and current values.
190 | * @return Result of of combining elements.
191 | */
192 | default X reduce(X idn, BinaryOperator opr) {
193 | final X res;
194 | if (opr == null) {
195 | res = idn;
196 | } else {
197 | res = this.stream().reduce(idn, opr);
198 | }
199 | return res;
200 | }
201 |
202 | /**
203 | * Returns an enumerable containing all elements of enumerable
204 | * after the first one which corresponds the condition.
205 | * If no predicate (null) is given, then empty enumerable is returned instead.
206 | * @param prd The function to match element after which enumerable elements should be returned.
207 | * @return The enumerable.
208 | */
209 | default Enumerable after(Predicate prd) {
210 | return this.after(prd, this.size());
211 | }
212 |
213 | /**
214 | * Returns an enumerable containing a certain number of elements of enumerable
215 | * after the first one which corresponds the condition.
216 | * If no predicate (null) is given, then empty enumerable is returned instead.
217 | * @param prd The function to match element after which enumerable elements should be returned.
218 | * @param size The number of elements the enumerable should be limited to.
219 | * @return The enumerable.
220 | * @throws IllegalArgumentException If the size is negative.
221 | */
222 | default Enumerable after(Predicate prd, long size) {
223 | final Enumerable out;
224 | if (size < 0) {
225 | throw new IllegalArgumentException(Long.toString(size));
226 | } else if (size == 0 || prd == null) {
227 | out = new Empty<>();
228 | } else {
229 | boolean found = false;
230 | out = new Linked<>();
231 | long cnt = size;
232 | for (final X elem : this) {
233 | if (!found && prd.negate().test(elem)) {
234 | continue;
235 | } else if (!found) {
236 | found = true;
237 | continue;
238 | }
239 | if (cnt > 0) {
240 | --cnt;
241 | out.add(elem);
242 | }
243 | }
244 | }
245 | return out;
246 | }
247 |
248 | /**
249 | * Returns the next element of enumerable after the first one which corresponds the condition.
250 | * If no predicate (null) is given, or no element found then null is returned instead.
251 | * @param prd The function to match element after which enumerable element should be returned.
252 | * @return The next element of enumerable after the first one which corresponds the condition.
253 | */
254 | default X next(Predicate prd) {
255 | return this.next(prd, null);
256 | }
257 |
258 | /**
259 | * Returns the next element of enumerable after the first one which corresponds the condition.
260 | * If no predicate (null) is given, or no element found then alternative is returned instead.
261 | * @param prd The function to match element after which enumerable element should be returned.
262 | * @param alt The alternative to return in case of null predicate or no element found.
263 | * @return The next element of enumerable after the first one which corresponds the condition.
264 | */
265 | default X next(Predicate prd, X alt) {
266 | final X out;
267 | if (prd == null) {
268 | out = alt;
269 | } else {
270 | out = this.after(prd, 1).stream().findFirst().orElse(alt);
271 | }
272 | return out;
273 | }
274 |
275 | /**
276 | * Returns a new enumerable which contains the items of the original collection
277 | * and the added items of the given enumerable.
278 | * If no enumerable (null) is given, then 'this' is returned instead.
279 | * @param enm The given enumerable.
280 | * @return The enumerable.
281 | */
282 | default Enumerable chain(Enumerable enm) {
283 | final Enumerable out = this;
284 | if (enm != null && !enm.isEmpty()) {
285 | out.addAll(enm);
286 | }
287 | return out;
288 | }
289 |
290 | /**
291 | * Returns an enumerable consisting of the elements of the collection,
292 | * additionally performing the provided action on each element of the enumerable.
293 | * @param act An action to perform on the elements.
294 | * @return The enumerable.
295 | */
296 | default Enumerable each(Consumer act) {
297 | if (act != null && !this.isEmpty()) {
298 | this.forEach(act);
299 | }
300 | return this;
301 | }
302 |
303 | /**
304 | * Returns an enumerable containing first elements of specified size from the enumerable.
305 | * @param num The number of elements the enumerable should be limited to.
306 | * @return The enumerable.
307 | * @throws IllegalArgumentException If the size is negative.
308 | */
309 | default Enumerable take(long num) {
310 | final Enumerable out;
311 | if (num < 0) {
312 | throw new IllegalArgumentException(Long.toString(num));
313 | } else if (num == 0) {
314 | out = new Empty<>();
315 | } else {
316 | out = new Linked<>(this.stream().limit(num).iterator());
317 | }
318 | return out;
319 | }
320 |
321 | /**
322 | * Drops first elements of specified size,
323 | * and returns an enumerable containing the rest of the elements.
324 | * @param num The number of elements to be dropped.
325 | * @return The enumerable.
326 | * @throws IllegalArgumentException If the size is negative.
327 | */
328 | default Enumerable drop(long num) {
329 | final Enumerable out;
330 | if (num < 0) {
331 | throw new IllegalArgumentException(Long.toString(num));
332 | } else if (num == 0) {
333 | out = this;
334 | } else {
335 | out = new Linked<>(this.stream().skip(num).iterator());
336 | }
337 | return out;
338 | }
339 |
340 | /**
341 | * The method returns true if the functions return true exactly once.
342 | * The given null predicates are skipped.
343 | * If no predicate (null) is given, then false is returned instead.
344 | * @param first The function to match each element.
345 | * @param other The array of functions to match each element.
346 | * @return True if the functions returns true exactly once.
347 | */
348 | default boolean one(Predicate first, Predicate... other) {
349 | return this.count(first, other) == 1;
350 | }
351 |
352 | /**
353 | * Returns a new enumerable containing the unique elements.
354 | * It compares values using the {@link #hashCode} and {@link #equals} methods for efficiency.
355 | * @return The enumerable.
356 | */
357 | default Enumerable uniq() {
358 | return new Linked<>(new HashSet<>(this));
359 | }
360 |
361 | /**
362 | * Returns a new enumerable containing the unique elements which corresponds the condition
363 | * of the given function.
364 | * If no function (null) is given, then empty enumerable is returned instead.
365 | * @param fnc The function to apply to each element.
366 | * @param The type of function result entity.
367 | * @return The enumerable.
368 | */
369 | default Enumerable uniq(Function super X, ? extends Y> fnc) {
370 | final Enumerable out;
371 | if (fnc == null) {
372 | out = new Empty<>();
373 | } else {
374 | final Set keys = new HashSet<>(0);
375 | out = new Linked<>(
376 | this.stream().filter(val -> keys.add(fnc.apply(val))).iterator()
377 | );
378 | }
379 | return out;
380 | }
381 |
382 | /**
383 | * Returns an enumerable of collections which contains each element of original collection
384 | * and corresponding elements from each argument collections.
385 | * The length of the resulting enumerable is {@link Enumerable#size}.
386 | * If the size of any argument is less than {@link Enumerable#size}, null values are supplied.
387 | * @param first The array of enumerable to merge.
388 | * @param other The array of enumerable to merge.
389 | * @return The enumerable.
390 | */
391 | default Enumerable extends Enumerable> zip(Enumerable first, Enumerable... other) {
392 | final Collection> provided = new ListOf<>();
393 | if (first == null) {
394 | provided.add(null);
395 | } else {
396 | provided.add(new ListOf<>(first));
397 | }
398 | if (other == null) {
399 | provided.add(null);
400 | } else {
401 | for (final Enumerable oth : other) {
402 | if (oth == null) {
403 | provided.add(null);
404 | } else {
405 | provided.add(new ListOf<>(oth));
406 | }
407 | }
408 | }
409 | final Enumerable> out = new Linked<>();
410 | int idx = 0;
411 | for (final X val : this) {
412 | final Enumerable inner = new Linked<>(val);
413 | for (final List prov : provided) {
414 | if (prov != null && idx < prov.size()) {
415 | inner.add(prov.get(idx));
416 | } else {
417 | inner.add(null);
418 | }
419 | }
420 | out.add(inner);
421 | ++idx;
422 | }
423 | return out;
424 | }
425 | }
426 |
--------------------------------------------------------------------------------
/src/main/java/io/github/dgroup/enumerable4j/Joined.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import java.util.function.Function;
28 | import java.util.function.Predicate;
29 |
30 | /**
31 | * The class combines all predicates into one common predicate.
32 | *
33 | * @param The type of entities.
34 | * @since 0.1.0
35 | */
36 | public final class Joined implements Predicate {
37 |
38 | /**
39 | * The function that returns the result of executing a predicates for the provided value.
40 | */
41 | private final Function fnc;
42 |
43 | /**
44 | * Ctor.
45 | * The null predicates are skipped.
46 | * If all predicates are null, it contains the false-returned predicate instead.
47 | * @param first The source predicate.
48 | * @param other The source predicates.
49 | */
50 | @SafeVarargs
51 | @SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
52 | public Joined(final Predicate first, final Predicate... other) {
53 | this.fnc = val -> {
54 | Predicate prd = v -> false;
55 | if (first != null) {
56 | prd = first;
57 | }
58 | if (other != null) {
59 | for (final Predicate oth : other) {
60 | if (oth != null) {
61 | prd = prd.and(oth);
62 | }
63 | }
64 | }
65 | return prd.test(val);
66 | };
67 | }
68 |
69 | @Override
70 | public boolean test(final X val) {
71 | return this.fnc.apply(val);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/io/github/dgroup/enumerable4j/Linked.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 | package io.github.dgroup.enumerable4j;
25 |
26 | import java.util.Iterator;
27 | import org.cactoos.list.ListEnvelope;
28 | import org.cactoos.list.ListOf;
29 |
30 | /**
31 | * The enumerable based on linked list.
32 | *
33 | * @param The type of entities.
34 | * @since 0.1.0
35 | */
36 | public class Linked extends ListEnvelope implements Enumerable {
37 |
38 | /**
39 | * Ctor.
40 | * @param src The source items.
41 | */
42 | @SafeVarargs
43 | public Linked(final X... src) {
44 | super(new ListOf<>(src));
45 | }
46 |
47 | /**
48 | * Ctor.
49 | * @param src The source items.
50 | */
51 | public Linked(final Iterable src) {
52 | super(new ListOf<>(src));
53 | }
54 |
55 | /**
56 | * Ctor.
57 | * @param src The source items.
58 | */
59 | public Linked(final Iterator src) {
60 | super(new ListOf<>(src));
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/src/main/java/io/github/dgroup/enumerable4j/Unique.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.cactoos.set.SetEnvelope;
28 | import org.cactoos.set.SetOf;
29 |
30 | /**
31 | * The enumerable based on set.
32 | *
33 | * @param The type of entities.
34 | * @since 0.1.0
35 | */
36 | public class Unique extends SetEnvelope implements Enumerable {
37 |
38 | /**
39 | * Ctor.
40 | * @param src The source items.
41 | */
42 | @SafeVarargs
43 | public Unique(final X... src) {
44 | super(new SetOf<>(src));
45 | }
46 |
47 | /**
48 | * Ctor.
49 | * @param src The source items.
50 | */
51 | public Unique(final Iterable src) {
52 | super(new SetOf<>(src));
53 | }
54 |
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/io/github/dgroup/enumerable4j/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * A Ruby's Enumerable ported to java.
27 | *
28 | * @author Yurii Dubinka (yurii.dubinka@gmail.com)
29 | * @since 0.1.0
30 | */
31 | package io.github.dgroup.enumerable4j;
32 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/AfterTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.collection.IsEmptyIterable;
28 | import org.hamcrest.core.AllOf;
29 | import org.junit.jupiter.api.Test;
30 | import org.llorllale.cactoos.matchers.Assertion;
31 | import org.llorllale.cactoos.matchers.HasSize;
32 | import org.llorllale.cactoos.matchers.HasValues;
33 | import org.llorllale.cactoos.matchers.Throws;
34 |
35 | /**
36 | * Test cases for {@link Enumerable#after}.
37 | *
38 | * @since 0.1.0
39 | * @checkstyle MagicNumberCheck (500 lines)
40 | * @checkstyle JavadocMethodCheck (500 lines)
41 | */
42 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
43 | public final class AfterTest {
44 |
45 | @Test
46 | void nullPredicate() {
47 | new Assertion<>(
48 | "In case of a null predicate, an empty enumeration is expected",
49 | new Linked<>(1, 2, 3).after(null),
50 | new IsEmptyIterable<>()
51 | ).affirm();
52 | }
53 |
54 | @Test
55 | void allAfter() {
56 | new Assertion<>(
57 | "Returns elements after the first one which corresponds the condition",
58 | new Linked<>(1, 2, 3, 4, 5).after(n -> n > 1),
59 | new AllOf<>(
60 | new HasSize(3),
61 | new HasValues<>(3, 4, 5)
62 | )
63 | ).affirm();
64 | }
65 |
66 | @Test
67 | void firstTwoAfter() {
68 | new Assertion<>(
69 | "Returns first 2 elements after the first one which corresponds the condition",
70 | new Linked<>("a", "b", "c", "d", "e").after(s -> s.equals("b"), 2),
71 | new AllOf<>(
72 | new HasSize(2),
73 | new HasValues<>("c", "d")
74 | )
75 | ).affirm();
76 | }
77 |
78 | @Test
79 | void firstTenAfter() {
80 | new Assertion<>(
81 | "The specified size can be greater than an actual enumerable size",
82 | new Linked<>(1, 2, 3, 4, 5).after(n -> n == 2, 10),
83 | new AllOf<>(
84 | new HasSize(3),
85 | new HasValues<>(3, 4, 5)
86 | )
87 | ).affirm();
88 | }
89 |
90 | @Test
91 | void allAfterFirst() {
92 | new Assertion<>(
93 | "The first element of enumerable corresponds the condition",
94 | new Linked<>(1, 2, 3, 4, 5).after(n -> n < 10),
95 | new AllOf<>(
96 | new HasSize(4),
97 | new HasValues<>(2, 3, 4, 5)
98 | )
99 | ).affirm();
100 | }
101 |
102 | @Test
103 | void noneMatch() {
104 | new Assertion<>(
105 | "No one element corresponds the condition, an empty collection is returned",
106 | new Linked<>(1, 2, 3).after(n -> n < 0),
107 | new IsEmptyIterable<>()
108 | ).affirm();
109 | }
110 |
111 | @Test
112 | void zeroSize() {
113 | new Assertion<>(
114 | "If the size value is 0, an empty collection is returned",
115 | new Linked<>(1, 2, 3).after(n -> n < 10, 0),
116 | new IsEmptyIterable<>()
117 | ).affirm();
118 | }
119 |
120 | @Test
121 | void nullPredicateWithSize() {
122 | new Assertion<>(
123 | "In case of a null predicate, an empty enumeration is expected",
124 | new Linked<>(1, 2, 3, 4).after(null, 2),
125 | new IsEmptyIterable<>()
126 | ).affirm();
127 | }
128 |
129 | @Test
130 | void nullPredicateAndZeroSize() {
131 | new Assertion<>(
132 | "In case of a null predicate or zero size, an empty enumeration is expected",
133 | new Linked<>(1, 2, 3).after(null, 0),
134 | new IsEmptyIterable<>()
135 | ).affirm();
136 | }
137 |
138 | @Test
139 | void throwExceptionOnNegativeSize() {
140 | new Assertion<>(
141 | "Must throw IllegalArgumentException if the size value is negative",
142 | () -> new Linked<>(1, 2, 3).after(null, -1),
143 | new Throws<>("-1", IllegalArgumentException.class)
144 | ).affirm();
145 | }
146 |
147 | }
148 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/AllTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import java.util.function.Predicate;
28 | import org.hamcrest.core.IsEqual;
29 | import org.junit.jupiter.api.Test;
30 | import org.llorllale.cactoos.matchers.Assertion;
31 | import org.llorllale.cactoos.matchers.IsTrue;
32 |
33 | /**
34 | * Test cases for {@link Enumerable#all}.
35 | *
36 | * @since 0.1.0
37 | * @checkstyle MagicNumberCheck (500 lines)
38 | * @checkstyle JavadocMethodCheck (500 lines)
39 | */
40 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
41 | final class AllTest {
42 |
43 | @Test
44 | void all() {
45 | new Assertion<>(
46 | "All values in enumerable are positive",
47 | new Linked<>(1, 2, 3).all(val -> val > 0),
48 | new IsTrue()
49 | ).affirm();
50 | }
51 |
52 | @Test
53 | void negative() {
54 | new Assertion<>(
55 | "Not all values in enumerable are negative",
56 | new Linked<>(1, 2, 3).all(val -> val < 0),
57 | new IsEqual<>(false)
58 | ).affirm();
59 | }
60 |
61 | @Test
62 | void nullPredicate() {
63 | new Assertion<>(
64 | "In case of null predicate we get false",
65 | new Linked<>(1, 2, 3).all(null),
66 | new IsEqual<>(false)
67 | ).affirm();
68 | }
69 |
70 | @Test
71 | void varArgsAll() {
72 | final Predicate positive = val -> val > 0;
73 | final Predicate even = val -> (val & 1) == 0;
74 | final Predicate lessthan = val -> val < 7;
75 | new Assertion<>(
76 | "All values in enumerable are positive, even and less than 7",
77 | new Linked<>(2, 4, 6).all(positive, even, lessthan),
78 | new IsTrue()
79 | ).affirm();
80 | }
81 |
82 | @Test
83 | void varArgsNegative() {
84 | final Predicate positive = val -> val > 0;
85 | final Predicate negative = val -> val < 0;
86 | new Assertion<>(
87 | "There are positive values in the enumerable, but there are no negative",
88 | new Linked<>(1, 2, 3).all(positive, negative),
89 | new IsEqual<>(false)
90 | ).affirm();
91 | }
92 |
93 | @Test
94 | void varArgsNullPredicates() {
95 | new Assertion<>(
96 | "In case of varargs null predicates we get false",
97 | new Linked<>(1, 2, 3).all(null, null, null),
98 | new IsEqual<>(false)
99 | ).affirm();
100 | }
101 |
102 | @Test
103 | void nullVarArgs() {
104 | new Assertion<>(
105 | "In case of null varargs we get false",
106 | new Linked<>(1, 2, 3).all(null, null),
107 | new IsEqual<>(false)
108 | ).affirm();
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/AnyTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import java.util.function.Predicate;
28 | import org.hamcrest.core.IsEqual;
29 | import org.junit.jupiter.api.Test;
30 | import org.llorllale.cactoos.matchers.Assertion;
31 | import org.llorllale.cactoos.matchers.IsTrue;
32 |
33 | /**
34 | * Test cases for {@link Enumerable#any}.
35 | *
36 | * @since 0.1.0
37 | * @checkstyle MagicNumberCheck (500 lines)
38 | * @checkstyle JavadocMethodCheck (500 lines)
39 | */
40 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
41 | final class AnyTest {
42 | @Test
43 | void any() {
44 | new Assertion<>(
45 | "At least one element in enumerable is less than 0",
46 | new Linked<>(-1, 0, 1, 2).any(val -> val < 0),
47 | new IsTrue()
48 | ).affirm();
49 | }
50 |
51 | @Test
52 | void negative() {
53 | new Assertion<>(
54 | "At least one value in enumerable is negative",
55 | new Linked<>(1, 2, 3).any(val -> val < 0),
56 | new IsEqual<>(false)
57 | ).affirm();
58 | }
59 |
60 | @Test
61 | void nullPredicate() {
62 | new Assertion<>(
63 | "In case of null predicate we will get false",
64 | new Linked<>(1, 2, 3).any(null),
65 | new IsEqual<>(false)
66 | ).affirm();
67 | }
68 |
69 | @Test
70 | void varArgsAny() {
71 | final Predicate positive = val -> val > 0;
72 | final Predicate even = val -> (val & 1) == 0;
73 | final Predicate lessthan = val -> val < 3;
74 | new Assertion<>(
75 | "At least one element in enumerable is positive, even and less than 3",
76 | new Linked<>(0, 1, 2, 3, 4).any(positive, even, lessthan),
77 | new IsTrue()
78 | ).affirm();
79 | }
80 |
81 | @Test
82 | void varArgsNegative() {
83 | final Predicate negative = val -> val < 0;
84 | final Predicate even = val -> (val & 1) == 0;
85 | final Predicate lessthan = val -> val > 6;
86 | new Assertion<>(
87 | "There are no values in enumerable which are negative, even or greater than 6",
88 | new Linked<>(1, 3, 5).any(negative, even, lessthan),
89 | new IsEqual<>(false)
90 | ).affirm();
91 | }
92 |
93 | @Test
94 | void varArgsNullPredicates() {
95 | new Assertion<>(
96 | "In case of null predicate we will get false",
97 | new Linked<>(1, 2, 3).any(null, null, null),
98 | new IsEqual<>(false)
99 | ).affirm();
100 | }
101 |
102 | @Test
103 | void nullVarArgs() {
104 | new Assertion<>(
105 | "In case of null varargs we get false",
106 | new Linked<>(1, 2, 3).any(null, null),
107 | new IsEqual<>(false)
108 | ).affirm();
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/ChainTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.core.AllOf;
28 | import org.junit.jupiter.api.Test;
29 | import org.llorllale.cactoos.matchers.Assertion;
30 | import org.llorllale.cactoos.matchers.HasSize;
31 | import org.llorllale.cactoos.matchers.HasValues;
32 |
33 | /**
34 | * Test cases for {@link Enumerable#chain}.
35 | *
36 | * @since 0.1.0
37 | * @checkstyle MagicNumberCheck (500 lines)
38 | * @checkstyle JavadocMethodCheck (500 lines)
39 | */
40 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
41 | final class ChainTest {
42 | @Test
43 | void nullParameter() {
44 | new Assertion<>(
45 | "In case of null enumerable the same enumerable is expected",
46 | new Linked<>(1, 2, 3).chain(null),
47 | new AllOf<>(
48 | new HasSize(3),
49 | new HasValues<>(1, 2, 3)
50 | )
51 | ).affirm();
52 | }
53 |
54 | @Test
55 | void emptyParameter() {
56 | new Assertion<>(
57 | "If add an empty collection to the enumerable, the same enumerable is expected",
58 | new Linked<>(1, 2, 3).chain(new Empty<>()),
59 | new AllOf<>(
60 | new HasSize(3),
61 | new HasValues<>(1, 2, 3)
62 | )
63 | ).affirm();
64 | }
65 |
66 | @Test
67 | void chain() {
68 | new Assertion<>(
69 | "Add values to the enumerable",
70 | new Linked<>(1, 2, 3).chain(new Linked<>(4, 5)),
71 | new AllOf<>(
72 | new HasSize(5),
73 | new HasValues<>(1, 2, 3, 4, 5)
74 | )
75 | ).affirm();
76 | }
77 |
78 | @Test
79 | void manyChains() {
80 | new Assertion<>(
81 | "Add some chains of values to the enumerable",
82 | new Linked<>(1, 2)
83 | .chain(new Linked<>(3))
84 | .chain(new Empty<>())
85 | .chain(new Linked<>(4, 5)),
86 | new AllOf<>(
87 | new HasSize(5),
88 | new HasValues<>(1, 2, 3, 4, 5)
89 | )
90 | ).affirm();
91 | }
92 |
93 | }
94 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/CountTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import java.util.function.Predicate;
28 | import org.hamcrest.core.IsEqual;
29 | import org.junit.jupiter.api.Test;
30 | import org.llorllale.cactoos.matchers.Assertion;
31 |
32 | /**
33 | * Test cases for {@link Enumerable#count}.
34 | *
35 | * @checkstyle MagicNumberCheck (500 lines)
36 | * @checkstyle JavadocMethodCheck (500 lines)
37 | * @since 0.1.0
38 | */
39 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
40 | final class CountTest {
41 | @Test
42 | void count() {
43 | new Assertion<>(
44 | "One element in enumerable is less than 0",
45 | new Linked<>(-1, 0, 1, 2).count(val -> val < 0),
46 | new IsEqual<>(1L)
47 | ).affirm();
48 | }
49 |
50 | @Test
51 | void negative() {
52 | new Assertion<>(
53 | "No element in enumerable is less than 0",
54 | new Linked<>(7, 0, 1, 2).count(val -> val < 0),
55 | new IsEqual<>(0L)
56 | ).affirm();
57 | }
58 |
59 | @Test
60 | void nullPredicate() {
61 | new Assertion<>(
62 | "In case of null predicate we get 0",
63 | new Linked<>(1, 2, 3).count(null),
64 | new IsEqual<>(0L)
65 | ).affirm();
66 | }
67 |
68 | @Test
69 | void varArgsCount() {
70 | final Predicate positive = val -> val > 0;
71 | final Predicate odd = val -> (val & 1) == 1;
72 | new Assertion<>(
73 | "Two elements in enumerable are greater than 0 and odd",
74 | new Linked<>(-1, 0, 1, 2, 3).count(positive, odd),
75 | new IsEqual<>(2L)
76 | ).affirm();
77 | }
78 |
79 | @Test
80 | void varArgsNegative() {
81 | final Predicate positive = val -> val > 0;
82 | final Predicate odd = val -> (val & 1) == 1;
83 | new Assertion<>(
84 | "No element in enumerable are greater than 0 and odd",
85 | new Linked<>(0, 2, 4).count(positive, odd),
86 | new IsEqual<>(0L)
87 | ).affirm();
88 | }
89 |
90 | @Test
91 | void varArgsNullPredicate() {
92 | new Assertion<>(
93 | "In case of null predicate we get 0",
94 | new Linked<>(1, 2, 3).count(null, null, null),
95 | new IsEqual<>(0L)
96 | ).affirm();
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/DropTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.core.AllOf;
28 | import org.junit.jupiter.api.Test;
29 | import org.llorllale.cactoos.matchers.Assertion;
30 | import org.llorllale.cactoos.matchers.HasSize;
31 | import org.llorllale.cactoos.matchers.HasValues;
32 | import org.llorllale.cactoos.matchers.Throws;
33 |
34 | /**
35 | * Test cases for {@link Enumerable#drop}.
36 | *
37 | * @checkstyle MagicNumberCheck (500 lines)
38 | * @checkstyle JavadocMethodCheck (500 lines)
39 | * @since 0.1.0
40 | */
41 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
42 | final class DropTest {
43 | @Test
44 | void drop() {
45 | new Assertion<>(
46 | "Drops first 2 elements of the collection",
47 | new Linked<>(1, 2, 3, 4).drop(2),
48 | new AllOf<>(
49 | new HasSize(2),
50 | new HasValues<>(3, 4)
51 | )
52 | ).affirm();
53 | }
54 |
55 | @Test
56 | void zeroSize() {
57 | new Assertion<>(
58 | "In case of zero size, the original enumeration is expected",
59 | new Linked<>(1, 2, 3).drop(0),
60 | new AllOf<>(
61 | new HasSize(3),
62 | new HasValues<>(1, 2, 3)
63 | )
64 | ).affirm();
65 | }
66 |
67 | @Test
68 | void throwExceptionOnNegativeSize() {
69 | new Assertion<>(
70 | "Must throw IllegalArgumentException if the size value is negative",
71 | () -> new Linked<>(1, 2, 3).drop(-1),
72 | new Throws<>("-1", IllegalArgumentException.class)
73 | ).affirm();
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/EachTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import java.io.ByteArrayOutputStream;
28 | import java.io.PrintStream;
29 | import org.cactoos.text.Joined;
30 | import org.cactoos.text.TextOf;
31 | import org.hamcrest.core.IsEqual;
32 | import org.junit.jupiter.api.BeforeEach;
33 | import org.junit.jupiter.api.Test;
34 | import org.llorllale.cactoos.matchers.Assertion;
35 | import org.llorllale.cactoos.matchers.TextIs;
36 |
37 | /**
38 | * Test cases for {@link Enumerable#each}.
39 | *
40 | * @checkstyle MagicNumberCheck (500 lines)
41 | * @checkstyle JavadocMethodCheck (500 lines)
42 | * @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
43 | * @checkstyle JavadocVariableCheck (500 lines)
44 | * @since 0.1.0
45 | */
46 | @SuppressWarnings({"PMD.AvoidDuplicateLiterals", "PMD.StaticAccessToStaticFields",
47 | "PMD.SystemPrintln"})
48 | final class EachTest {
49 |
50 | private static ByteArrayOutputStream captor;
51 |
52 | @BeforeEach
53 | void setUp() {
54 | captor = new ByteArrayOutputStream();
55 | System.setOut(new PrintStream(captor));
56 | }
57 |
58 | @Test
59 | void nullParameter() {
60 | new Assertion<>(
61 | "In null-parameter case, no actions with the elements will be performed",
62 | new Linked<>(1, 2, 3).each(null),
63 | new IsEqual<>(new Linked<>(1, 2, 3))
64 | ).affirm();
65 | }
66 |
67 | @Test
68 | void print() {
69 | new Linked<>("a", "b", "c").each(System.out::println);
70 | new Assertion<>(
71 | "Print each element",
72 | new TextOf(captor.toString()),
73 | new TextIs(new Joined(System.lineSeparator(), "a", "b", "c", ""))
74 | ).affirm();
75 | }
76 |
77 | @Test
78 | void printEmpty() {
79 | new Empty<>().each(System.out::println);
80 | new Assertion<>(
81 | "In case of empty enumerable, no actions with the elements will be performed",
82 | new TextOf(captor.toString()),
83 | new TextIs("")
84 | ).affirm();
85 | }
86 |
87 | @Test
88 | void chain() {
89 | new Linked<>(1, 2, 3)
90 | .each(null)
91 | .each(System.out::println)
92 | .each(val -> System.out.println(val * 10));
93 | new Assertion<>(
94 | "The chain of invocations",
95 | new TextOf(captor.toString()),
96 | new TextIs(new Joined(System.lineSeparator(), "1", "2", "3", "10", "20", "30", ""))
97 | ).affirm();
98 | }
99 |
100 | }
101 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/FindTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.core.IsEqual;
28 | import org.hamcrest.core.IsNull;
29 | import org.junit.jupiter.api.Test;
30 | import org.llorllale.cactoos.matchers.Assertion;
31 |
32 | /**
33 | * Test cases for {@link Enumerable#find}.
34 | *
35 | * @since 0.1.0
36 | * @checkstyle MagicNumberCheck (500 lines)
37 | * @checkstyle JavadocMethodCheck (500 lines)
38 | */
39 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
40 | final class FindTest {
41 | @Test
42 | void find() {
43 | new Assertion<>(
44 | "First number bigger than 0",
45 | new Linked<>(-1, 0, 1, 2, 3).find(val -> val > 0),
46 | new IsEqual<>(1)
47 | ).affirm();
48 | }
49 |
50 | @Test
51 | void nullPredicate() {
52 | new Assertion<>(
53 | "In case of the null predicate, we get null as a result",
54 | new Linked<>(1, 2, 3).find(null),
55 | new IsNull<>()
56 | ).affirm();
57 | }
58 |
59 | @Test
60 | void negative() {
61 | new Assertion<>(
62 | "There are no prime numbers",
63 | new Linked<>(2, 4, 6, 8).find(val -> val % 2 != 0),
64 | new IsNull<>()
65 | ).affirm();
66 | }
67 |
68 | @Test
69 | void altFind() {
70 | new Assertion<>(
71 | "First number bigger than 0",
72 | new Linked<>(-1, 0, 1, 2, 3).find(100, val -> val > 0),
73 | new IsEqual<>(1)
74 | ).affirm();
75 | }
76 |
77 | @Test
78 | void altNullPredicate() {
79 | new Assertion<>(
80 | "In case of null predicate, we get the alternative result",
81 | new Linked<>(1, 2, 3).find(100, null),
82 | new IsEqual<>(100)
83 | ).affirm();
84 | }
85 |
86 | @Test
87 | void altNegative() {
88 | new Assertion<>(
89 | "There are no prime numbers",
90 | new Linked<>(2, 4, 6, 8).find(777, val -> val % 2 != 0),
91 | new IsEqual<>(777)
92 | ).affirm();
93 | }
94 |
95 | @Test
96 | void varArgsFind() {
97 | new Assertion<>(
98 | "First even number which is greater than 0",
99 | new Linked<>(-1, 0, 1, 2, 3).find(val -> val > 0, val -> (val & 1) == 0),
100 | new IsEqual<>(2)
101 | ).affirm();
102 | }
103 |
104 | @Test
105 | void varArgsNegative() {
106 | new Assertion<>(
107 | "There are no odd number which is less than 10",
108 | new Linked<>(2, 4, 6, 8).find(val -> (val & 1) == 1, val -> val < 10),
109 | new IsNull<>()
110 | ).affirm();
111 | }
112 |
113 | @Test
114 | void varArgsNullPredicate() {
115 | new Assertion<>(
116 | "In case of the null predicate, we get null as a result",
117 | new Linked<>(1, 2, 3).find(5, null, null),
118 | new IsEqual<>(5)
119 | ).affirm();
120 | }
121 |
122 | @Test
123 | void varArgsNullPredicates() {
124 | new Assertion<>(
125 | "In case of the null predicates, we get null as a result",
126 | new Linked<>(1, 2, 3).find(5, null, null, null),
127 | new IsEqual<>(5)
128 | ).affirm();
129 | }
130 | }
131 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/JoinedTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import java.util.function.Predicate;
28 | import java.util.stream.Collectors;
29 | import java.util.stream.Stream;
30 | import org.hamcrest.collection.IsEmptyIterable;
31 | import org.hamcrest.core.AllOf;
32 | import org.junit.jupiter.api.Test;
33 | import org.llorllale.cactoos.matchers.Assertion;
34 | import org.llorllale.cactoos.matchers.HasSize;
35 | import org.llorllale.cactoos.matchers.HasValues;
36 |
37 | /**
38 | * Test cases for {@link Joined}.
39 | *
40 | * @since 0.1.0
41 | * @checkstyle MagicNumberCheck (100 lines)
42 | * @checkstyle JavadocMethodCheck (100 lines)
43 | */
44 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
45 | final class JoinedTest {
46 |
47 | @Test
48 | void join() {
49 | final Predicate even = val -> (val & 1) == 0;
50 | final Predicate positive = val -> val > 0;
51 | final Joined joined = new Joined<>(even, positive);
52 | new Assertion<>(
53 | "Joined provided predicates",
54 | Stream.of(-2, 2, 3, 4).filter(joined).collect(Collectors.toList()),
55 | new AllOf<>(
56 | new HasSize(2),
57 | new HasValues<>(2, 4)
58 | )
59 | ).affirm();
60 | }
61 |
62 | @Test
63 | void negateJoin() {
64 | final Predicate odd = val -> (val & 1) == 1;
65 | final Predicate negative = val -> val < 0;
66 | final Joined joined = new Joined<>(odd.negate(), negative.negate());
67 | new Assertion<>(
68 | "Joined provided predicates with negation",
69 | Stream.of(-2, 0, 2, 3, 4).filter(joined).collect(Collectors.toList()),
70 | new AllOf<>(
71 | new HasSize(3),
72 | new HasValues<>(0, 2, 4)
73 | )
74 | ).affirm();
75 | }
76 |
77 | @Test
78 | void nullArgument() {
79 | final Joined joined = new Joined<>(null);
80 | new Assertion<>(
81 | "In case of null argument we get the false-returned predicate in filter",
82 | Stream.of(1, 2, 3).filter(joined).collect(Collectors.toList()),
83 | new IsEmptyIterable<>()
84 | ).affirm();
85 | }
86 |
87 | @Test
88 | void nullArguments() {
89 | final Joined joined = new Joined<>(null, null);
90 | new Assertion<>(
91 | "In case of null arguments we get the false-returned predicate in filter",
92 | Stream.of(1, 2, 3).filter(joined).collect(Collectors.toList()),
93 | new IsEmptyIterable<>()
94 | ).affirm();
95 | }
96 |
97 | @Test
98 | void nullPredicates() {
99 | final Joined joined = new Joined<>(null, null, null);
100 | new Assertion<>(
101 | "In case of null predicates we get the false-returned predicate in filter",
102 | Stream.of(1, 2, 3).filter(joined).collect(Collectors.toList()),
103 | new IsEmptyIterable<>()
104 | ).affirm();
105 | }
106 |
107 | @Test
108 | void orJoin() {
109 | final Predicate odd = val -> (val & 1) == 1;
110 | final Predicate lessthan = val -> val < 3;
111 | final Predicate zero = val -> val == 0;
112 | final Joined joined = new Joined<>(odd.or(zero), lessthan);
113 | new Assertion<>(
114 | "Joined the predicates to get values which are less than 3, and both odd or zero",
115 | Stream.of(-1, 0, 1, 2, 3).filter(joined).collect(Collectors.toList()),
116 | new AllOf<>(
117 | new HasValues<>(-1, 0, 1)
118 | )
119 | ).affirm();
120 | }
121 |
122 | }
123 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/MapTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.collection.IsEmptyIterable;
28 | import org.hamcrest.core.AllOf;
29 | import org.hamcrest.core.IsNot;
30 | import org.junit.jupiter.api.Test;
31 | import org.llorllale.cactoos.matchers.Assertion;
32 | import org.llorllale.cactoos.matchers.HasValues;
33 |
34 | /**
35 | * Test cases for {@link Enumerable#map}.
36 | *
37 | * @since 0.1.0
38 | * @checkstyle MagicNumberCheck (500 lines)
39 | * @checkstyle JavadocMethodCheck (500 lines)
40 | */
41 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
42 | final class MapTest {
43 | @Test
44 | void map() {
45 | new Assertion<>(
46 | "All numbers are multiplied by 10",
47 | new Linked<>(2, 3, 4).map(val -> val * 10),
48 | new HasValues<>(20, 30, 40)
49 | ).affirm();
50 | }
51 |
52 | @Test
53 | void nullFunction() {
54 | new Assertion<>(
55 | "In case null-function, an empty enumerable is returned",
56 | new Linked<>(2, 3, 4).map(null),
57 | new IsEmptyIterable<>()
58 | ).affirm();
59 | }
60 |
61 | @Test
62 | void negative() {
63 | new Assertion<>(
64 | "All numbers converted to numbers squares",
65 | new Linked<>(0, 1, 2, 3).map(val -> val * val),
66 | new AllOf<>(
67 | new IsNot<>(new HasValues<>(5, 7, 9)),
68 | new HasValues<>(0, 1, 4, 9)
69 | )
70 | ).affirm();
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/NextTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.core.IsEqual;
28 | import org.hamcrest.core.IsNull;
29 | import org.junit.jupiter.api.Test;
30 | import org.llorllale.cactoos.matchers.Assertion;
31 |
32 | /**
33 | * Test cases for {@link Enumerable#next}.
34 | *
35 | * @since 0.1.0
36 | * @checkstyle MagicNumberCheck (500 lines)
37 | * @checkstyle JavadocMethodCheck (500 lines)
38 | */
39 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
40 | public class NextTest {
41 |
42 | @Test
43 | void nullPredicate() {
44 | new Assertion<>(
45 | "In a case of the null predicate, we get null as a result",
46 | new Linked<>(1, 2, 3).next(null),
47 | new IsNull<>()
48 | ).affirm();
49 | }
50 |
51 | @Test
52 | void altNullPredicate() {
53 | new Assertion<>(
54 | "In a case of the null predicate, we get an alternative result",
55 | new Linked<>(1, 2, 3).next(null, -1),
56 | new IsEqual<>(-1)
57 | ).affirm();
58 | }
59 |
60 | @Test
61 | void getNext() {
62 | new Assertion<>(
63 | "Returns the next element after the first one which corresponds the condition",
64 | new Linked<>(1, 2, 3, 4, 5).next(n -> n > 1),
65 | new IsEqual<>(3)
66 | ).affirm();
67 | }
68 |
69 | @Test
70 | void altGetNext() {
71 | new Assertion<>(
72 | "Returns the next element after the first one which corresponds the condition",
73 | new Linked<>(1, 2, 3, 4, 5).next(n -> n > 1, -1),
74 | new IsEqual<>(3)
75 | ).affirm();
76 | }
77 |
78 | @Test
79 | void noneMatch() {
80 | new Assertion<>(
81 | "No one element corresponds the condition, the result is null",
82 | new Linked<>(1, 2, 3).next(n -> n > 10),
83 | new IsNull<>()
84 | ).affirm();
85 | }
86 |
87 | @Test
88 | void altNoneMatch() {
89 | new Assertion<>(
90 | "No one element corresponds the condition, we get an alternative result",
91 | new Linked<>(1, 2, 3).next(n -> n > 10, -1),
92 | new IsEqual<>(-1)
93 | ).affirm();
94 | }
95 |
96 | }
97 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/NoneTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import java.util.function.Predicate;
28 | import org.hamcrest.core.IsEqual;
29 | import org.junit.jupiter.api.Test;
30 | import org.llorllale.cactoos.matchers.Assertion;
31 | import org.llorllale.cactoos.matchers.IsTrue;
32 |
33 | /**
34 | * Test cases for {@link Enumerable#none}.
35 | *
36 | * @since 0.1.0
37 | * @checkstyle MagicNumberCheck (500 lines)
38 | * @checkstyle JavadocMethodCheck (500 lines)
39 | */
40 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
41 | final class NoneTest {
42 | @Test
43 | void none() {
44 | new Assertion<>(
45 | "There are no values in enumerable greater than 100",
46 | new Linked<>(-1, 2, 99).none(val -> val > 100),
47 | new IsTrue()
48 | ).affirm();
49 | }
50 |
51 | @Test
52 | void negative() {
53 | new Assertion<>(
54 | "There are some positive values in the enumerable",
55 | new Linked<>(1, 2, 3).none(val -> val > 0),
56 | new IsEqual<>(false)
57 | ).affirm();
58 | }
59 |
60 | @Test
61 | void nullPredicate() {
62 | new Assertion<>(
63 | "In case of null predicate we will get true",
64 | new Linked<>(1, 2, 3).none(null),
65 | new IsTrue()
66 | ).affirm();
67 | }
68 |
69 | @Test
70 | void varArgsNone() {
71 | final Predicate negative = val -> val < 0;
72 | final Predicate even = val -> (val & 1) == 0;
73 | final Predicate lessthan = val -> val > 6;
74 | new Assertion<>(
75 | "There are no values in enumerable which are negative, even or greater than 6",
76 | new Linked<>(1, 3, 5).none(negative, even, lessthan),
77 | new IsTrue()
78 | ).affirm();
79 | }
80 |
81 | @Test
82 | void varArgsNegative() {
83 | final Predicate greaterthan = val -> val > -1;
84 | final Predicate positive = val -> val > 0;
85 | new Assertion<>(
86 | "There are some positive and greater than -1 values in the enumerable",
87 | new Linked<>(1, 2, 3).none(greaterthan, positive),
88 | new IsEqual<>(false)
89 | ).affirm();
90 | }
91 |
92 | @Test
93 | void varArgsNullPredicates() {
94 | new Assertion<>(
95 | "In case of null predicate we will get true",
96 | new Linked<>(1, 2, 3).none(null, null, null),
97 | new IsTrue()
98 | ).affirm();
99 | }
100 |
101 | @Test
102 | void nullVarArgs() {
103 | new Assertion<>(
104 | "In null-vararg case we will get true",
105 | new Linked<>(1, 2, 3).none(null, null),
106 | new IsTrue()
107 | ).affirm();
108 | }
109 | }
110 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/OneTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import java.util.function.Predicate;
28 | import org.hamcrest.core.IsEqual;
29 | import org.junit.jupiter.api.Test;
30 | import org.llorllale.cactoos.matchers.Assertion;
31 | import org.llorllale.cactoos.matchers.IsTrue;
32 |
33 | /**
34 | * Test cases for {@link Enumerable#one}.
35 | *
36 | * @since 0.1.0
37 | * @checkstyle MagicNumberCheck (500 lines)
38 | * @checkstyle JavadocMethodCheck (500 lines)
39 | */
40 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
41 | final class OneTest {
42 |
43 | @Test
44 | void one() {
45 | new Assertion<>(
46 | "Only one value is even",
47 | new Linked<>(1, 2, 3).one(val -> (val & 1) == 0),
48 | new IsTrue()
49 | ).affirm();
50 | }
51 |
52 | @Test
53 | void negative() {
54 | new Assertion<>(
55 | "There are more than one positive value",
56 | new Linked<>(1, 2, 3).one(val -> val > 0),
57 | new IsEqual<>(false)
58 | ).affirm();
59 | }
60 |
61 | @Test
62 | void nullPredicate() {
63 | new Assertion<>(
64 | "In case of null predicate all values correspond to the conditions",
65 | new Linked<>(1, 2, 3).one(null),
66 | new IsEqual<>(false)
67 | ).affirm();
68 | }
69 |
70 | @Test
71 | void varArgsOne() {
72 | final Predicate even = val -> (val & 1) == 1;
73 | final Predicate lessthan = val -> val < 3;
74 | new Assertion<>(
75 | "Only one value is even and less than 3",
76 | new Linked<>(1, 2, 3, 4).one(even, lessthan),
77 | new IsTrue()
78 | ).affirm();
79 | }
80 |
81 | @Test
82 | void varArgsNegative() {
83 | final Predicate even = val -> (val & 1) == 1;
84 | final Predicate greaterthan = val -> val > 3;
85 | new Assertion<>(
86 | "No one value corresponds to the conditions",
87 | new Linked<>(1, 2, 3).one(even, greaterthan),
88 | new IsEqual<>(false)
89 | ).affirm();
90 | }
91 |
92 | @Test
93 | void varArgsNullPredicates() {
94 | new Assertion<>(
95 | "In case of null predicates all values correspond to the conditions",
96 | new Linked<>(1, 2, 3).one(null, null, null),
97 | new IsEqual<>(false)
98 | ).affirm();
99 | }
100 |
101 | }
102 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/ReduceTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.core.IsEqual;
28 | import org.junit.jupiter.api.Test;
29 | import org.llorllale.cactoos.matchers.Assertion;
30 |
31 | /**
32 | * Test cases for {@link Enumerable#reduce}.
33 | *
34 | * @since 0.1.0
35 | * @checkstyle MagicNumberCheck (500 lines)
36 | * @checkstyle JavadocMethodCheck (500 lines)
37 | */
38 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
39 | final class ReduceTest {
40 | @Test
41 | void sum() {
42 | new Assertion<>(
43 | "Sum of elements in enumerable",
44 | new Linked<>(-1, 0, 1, 2, 3).reduce(0, Integer::sum),
45 | new IsEqual<>(5)
46 | ).affirm();
47 | }
48 |
49 | @Test
50 | void multiplication() {
51 | new Assertion<>(
52 | "Multiplication of elements in enumerable",
53 | new Linked<>(-1, 1, 2, 3).reduce(1, (res, elem) -> res *= elem),
54 | new IsEqual<>(-6)
55 | ).affirm();
56 | }
57 |
58 | @Test
59 | void concatenation() {
60 | new Assertion<>(
61 | "Concatenation of string elements in enumerable",
62 | new Linked<>("b", "c", "d").reduce("a", String::concat),
63 | new IsEqual<>("abcd")
64 | ).affirm();
65 | }
66 |
67 | @Test
68 | void nullOperator() {
69 | new Assertion<>(
70 | "In case of null operator it returns the identity value",
71 | new Linked<>(1, 2, 3).reduce(10, null),
72 | new IsEqual<>(10)
73 | ).affirm();
74 | }
75 | }
76 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/RejectTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import java.util.function.Predicate;
28 | import org.hamcrest.collection.IsEmptyIterable;
29 | import org.hamcrest.core.AllOf;
30 | import org.junit.jupiter.api.Test;
31 | import org.llorllale.cactoos.matchers.Assertion;
32 | import org.llorllale.cactoos.matchers.HasSize;
33 | import org.llorllale.cactoos.matchers.HasValues;
34 |
35 | /**
36 | * Test cases for {@link Enumerable#reject}.
37 | *
38 | * @since 0.1.0
39 | * @checkstyle MagicNumberCheck (500 lines)
40 | * @checkstyle JavadocMethodCheck (500 lines)
41 | */
42 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
43 | final class RejectTest {
44 |
45 | @Test
46 | void reject() {
47 | new Assertion<>(
48 | "Negative values from enumerable found",
49 | new Linked<>(1, 2, 3, -1).reject(val -> val > 0),
50 | new AllOf<>(
51 | new HasSize(1),
52 | new HasValues<>(-1)
53 | )
54 | ).affirm();
55 | }
56 |
57 | @Test
58 | void negative() {
59 | new Assertion<>(
60 | "All values from enumerable found",
61 | new Linked<>(1, 2, 3).reject(val -> val > 0),
62 | new IsEmptyIterable<>()
63 | ).affirm();
64 | }
65 |
66 | @Test
67 | void nullFunction() {
68 | new Assertion<>(
69 | "In case null-function the self enumerable is expected",
70 | new Linked<>(3, 0, 2, -1).reject(null),
71 | new HasValues<>(3, 0, 2, -1)
72 | ).affirm();
73 | }
74 |
75 | @Test
76 | void varArgsReject() {
77 | final Predicate negative = val -> val < 0;
78 | final Predicate even = val -> (val & 1) == 0;
79 | new Assertion<>(
80 | "Negative values from enumerable found",
81 | new Linked<>(1, 2, 3, -1).reject(negative, even),
82 | new AllOf<>(
83 | new HasSize(2),
84 | new HasValues<>(1, 3)
85 | )
86 | ).affirm();
87 | }
88 |
89 | @Test
90 | void varArgsNegative() {
91 | final Predicate negative = val -> val < 0;
92 | final Predicate even = val -> (val & 1) == 0;
93 | new Assertion<>(
94 | "No values from enumerable found",
95 | new Linked<>(2, 4, 6).reject(negative, even),
96 | new IsEmptyIterable<>()
97 | ).affirm();
98 | }
99 |
100 | @Test
101 | void varArgsNullFunction() {
102 | new Assertion<>(
103 | "In case null-function the self enumerable is expected",
104 | new Linked<>(3, 0, 2, -1).reject(null, null, null),
105 | new HasValues<>(3, 0, 2, -1)
106 | ).affirm();
107 | }
108 |
109 | @Test
110 | void nullVarArgs() {
111 | new Assertion<>(
112 | "In null-vararg case the self enumerable is expected",
113 | new Linked<>(3, 0, 2, -1).reject(null, null),
114 | new HasValues<>(3, 0, 2, -1)
115 | ).affirm();
116 | }
117 | }
118 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/SelectTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import java.util.function.Predicate;
28 | import org.hamcrest.collection.IsEmptyIterable;
29 | import org.hamcrest.core.AllOf;
30 | import org.junit.jupiter.api.Test;
31 | import org.llorllale.cactoos.matchers.Assertion;
32 | import org.llorllale.cactoos.matchers.HasSize;
33 | import org.llorllale.cactoos.matchers.HasValues;
34 |
35 | /**
36 | * Test cases for {@link Enumerable#select}.
37 | *
38 | * @since 0.1.0
39 | * @checkstyle MagicNumberCheck (500 lines)
40 | * @checkstyle JavadocMethodCheck (500 lines)
41 | */
42 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
43 | final class SelectTest {
44 |
45 | @Test
46 | void select() {
47 | new Assertion<>(
48 | "Positive values from enumerable found",
49 | new Linked<>(3, 0, 2, -1).select(val -> val > 0),
50 | new AllOf<>(
51 | new HasSize(2),
52 | new HasValues<>(3, 2)
53 | )
54 | ).affirm();
55 | }
56 |
57 | @Test
58 | void negative() {
59 | new Assertion<>(
60 | "There are no negative values in the enumerable",
61 | new Linked<>(1, 2, 3).select(val -> val < 0),
62 | new IsEmptyIterable<>()
63 | ).affirm();
64 | }
65 |
66 | @Test
67 | void nullFunction() {
68 | new Assertion<>(
69 | "In case null-function an empty enumerable is returned",
70 | new Linked<>(3, 0, 2, -1).select(null),
71 | new IsEmptyIterable<>()
72 | ).affirm();
73 | }
74 |
75 | @Test
76 | void varArgsSelect() {
77 | final Predicate positive = val -> val > 0;
78 | final Predicate even = val -> (val & 1) == 0;
79 | final Predicate lessthan = val -> val < 5;
80 | new Assertion<>(
81 | "All values in enumerable are positive, even and less than 5",
82 | new Linked<>(-1, 0, 1, 2, 3, 4, 5, 6).select(positive, even, lessthan),
83 | new AllOf<>(
84 | new HasSize(2),
85 | new HasValues<>(2, 4)
86 | )
87 | ).affirm();
88 | }
89 |
90 | @Test
91 | void varArgsNegative() {
92 | final Predicate positive = val -> val > 0;
93 | final Predicate negative = val -> val < 0;
94 | final Predicate even = val -> (val & 1) == 0;
95 | new Assertion<>(
96 | "There are positive and even values in the enumerable, but there are no negative",
97 | new Linked<>(1, 2, 3).select(positive, negative, even),
98 | new IsEmptyIterable<>()
99 | ).affirm();
100 | }
101 |
102 | @Test
103 | void varArgsNullPredicates() {
104 | new Assertion<>(
105 | "In case null-function an empty enumerable is returned",
106 | new Linked<>(3, 0, 2, -1).select(null, null, null),
107 | new IsEmptyIterable<>()
108 | ).affirm();
109 | }
110 |
111 | @Test
112 | void nullVarArgs() {
113 | new Assertion<>(
114 | "In null-vararg case an empty enumerable is returned",
115 | new Linked<>(3, 0, 2, -1).select(null, null),
116 | new IsEmptyIterable<>()
117 | ).affirm();
118 | }
119 | }
120 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/TakeTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.collection.IsEmptyIterable;
28 | import org.hamcrest.core.AllOf;
29 | import org.junit.jupiter.api.Test;
30 | import org.llorllale.cactoos.matchers.Assertion;
31 | import org.llorllale.cactoos.matchers.HasSize;
32 | import org.llorllale.cactoos.matchers.HasValues;
33 | import org.llorllale.cactoos.matchers.Throws;
34 |
35 | /**
36 | * Test cases for {@link Enumerable#take}.
37 | *
38 | * @checkstyle MagicNumberCheck (500 lines)
39 | * @checkstyle JavadocMethodCheck (500 lines)
40 | * @since 0.1.0
41 | */
42 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
43 | final class TakeTest {
44 | @Test
45 | void take() {
46 | new Assertion<>(
47 | "Returns first 3 elements of the collection",
48 | new Linked<>(-1, 0, 1, 2).take(3),
49 | new AllOf<>(
50 | new HasSize(3),
51 | new HasValues<>(-1, 0, 1)
52 | )
53 | ).affirm();
54 | }
55 |
56 | @Test
57 | void zeroSize() {
58 | new Assertion<>(
59 | "In case of zero size, an empty enumeration is expected",
60 | new Linked<>(1, 2, 3).take(0),
61 | new IsEmptyIterable<>()
62 | ).affirm();
63 | }
64 |
65 | @Test
66 | void throwExceptionOnNegativeSize() {
67 | new Assertion<>(
68 | "Must throw IllegalArgumentException if the size value is negative",
69 | () -> new Linked<>(1, 2, 3).take(-1),
70 | new Throws<>("-1", IllegalArgumentException.class)
71 | ).affirm();
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/UniqTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.collection.IsEmptyIterable;
28 | import org.hamcrest.core.AllOf;
29 | import org.hamcrest.object.HasEqualValues;
30 | import org.junit.jupiter.api.Test;
31 | import org.llorllale.cactoos.matchers.Assertion;
32 | import org.llorllale.cactoos.matchers.HasSize;
33 | import org.llorllale.cactoos.matchers.HasValues;
34 |
35 | /**
36 | * Test cases for {@link Enumerable#uniq}.
37 | *
38 | * @since 0.1.0
39 | * @checkstyle MagicNumberCheck (100 lines)
40 | * @checkstyle JavadocMethodCheck (100 lines)
41 | * @checkstyle ClassDataAbstractionCouplingCheck (100 lines)
42 | */
43 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
44 | final class UniqTest {
45 |
46 | @Test
47 | void nullFunction() {
48 | final Enumerable> enumerable = new Linked<>(
49 | new Linked<>(1, 2, 3),
50 | new Linked<>(4, 5)
51 | );
52 | new Assertion<>(
53 | "In case of a null function, an empty enumeration is expected",
54 | enumerable.uniq(null),
55 | new IsEmptyIterable<>()
56 | ).affirm();
57 | }
58 |
59 | @Test
60 | void unique() {
61 | new Assertion<>(
62 | "Unique values of the enumerable",
63 | new Linked<>(4, 4, 6, 7, 7, 5).uniq(),
64 | new AllOf<>(
65 | new HasSize(4),
66 | new HasEqualValues<>(new Linked<>(4, 6, 7, 5))
67 | )
68 | ).affirm();
69 | }
70 |
71 | @Test
72 | void uniqueByKey() {
73 | final Enumerable> enumerable = new Linked<>(
74 | new Linked<>("a", "b", "c"),
75 | new Linked<>("c"),
76 | new Linked<>("c", "d", "e"),
77 | new Linked<>("a", "z")
78 | );
79 | new Assertion<>(
80 | "Got unique collections by their first elements",
81 | enumerable.uniq(e -> e.get(0)),
82 | new AllOf<>(
83 | new HasSize(2),
84 | new HasValues<>(
85 | new Linked<>("a", "b", "c"),
86 | new Linked<>("c")
87 | )
88 | )
89 | ).affirm();
90 | }
91 |
92 | @Test
93 | void emptyEnumerable() {
94 | final Enumerable enumerable = new Empty<>();
95 | new Assertion<>(
96 | "In case of a null function, an empty enumeration is expected",
97 | enumerable.uniq(null),
98 | new IsEmptyIterable<>()
99 | ).affirm();
100 | }
101 | }
102 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/UniqueTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.core.AllOf;
28 | import org.junit.jupiter.api.Test;
29 | import org.llorllale.cactoos.matchers.Assertion;
30 | import org.llorllale.cactoos.matchers.HasSize;
31 | import org.llorllale.cactoos.matchers.HasValues;
32 |
33 | /**
34 | * Test cases for {@link Unique}.
35 | *
36 | * @since 0.1.0
37 | * @checkstyle MagicNumberCheck (100 lines)
38 | * @checkstyle JavadocMethodCheck (100 lines)
39 | */
40 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
41 | final class UniqueTest {
42 |
43 | @Test
44 | void varargsConstructor() {
45 | final Unique unique = new Unique<>(1, 2, 1, 3, 3);
46 | new Assertion<>(
47 | "Got unique collections of integers",
48 | unique,
49 | new AllOf<>(
50 | new HasSize(3),
51 | new HasValues<>(1, 2, 3)
52 | )
53 | ).affirm();
54 | }
55 |
56 | @Test
57 | void iterableArgConstructor() {
58 | final Unique unique = new Unique<>(new Linked<>("a", "b", "b", "c", "a"));
59 | new Assertion<>(
60 | "Got unique collections of enumerables",
61 | unique,
62 | new AllOf<>(
63 | new HasSize(3),
64 | new HasValues<>("a", "b", "c")
65 | )
66 | ).affirm();
67 | }
68 |
69 | }
70 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/ZipTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | package io.github.dgroup.enumerable4j;
26 |
27 | import org.hamcrest.object.HasEqualValues;
28 | import org.junit.jupiter.api.Test;
29 | import org.llorllale.cactoos.matchers.Assertion;
30 |
31 | /**
32 | * Test cases for {@link Enumerable#zip}.
33 | *
34 | * @since 0.1.0
35 | * @checkstyle MagicNumberCheck (500 lines)
36 | * @checkstyle JavadocMethodCheck (500 lines)
37 | */
38 | @SuppressWarnings("PMD.AvoidDuplicateLiterals")
39 | final class ZipTest {
40 |
41 | @Test
42 | void zip() {
43 | new Assertion<>(
44 | "Returns enumerable of 3 inner collections of 2 element in each",
45 | new Linked<>(4, 5, 6).zip(new Linked<>(7, 8, 9)),
46 | new HasEqualValues<>(
47 | new Linked>(
48 | new Linked<>(4, 7),
49 | new Linked<>(5, 8),
50 | new Linked<>(6, 9)
51 | )
52 | )
53 | ).affirm();
54 | }
55 |
56 | @Test
57 | void varArgsZip() {
58 | new Assertion<>(
59 | "Returns enumerable of 3 inner collections of 3 element in each",
60 | new Linked<>(1, 2, 3).zip(new Linked<>(4, 5, 6), new Linked<>(7, 8, 9)),
61 | new HasEqualValues<>(
62 | new Linked>(
63 | new Linked<>(1, 4, 7),
64 | new Linked<>(2, 5, 8),
65 | new Linked<>(3, 6, 9)
66 | )
67 | )
68 | ).affirm();
69 | }
70 |
71 | @Test
72 | void differentSizes() {
73 | new Assertion<>(
74 | "The last values of argument collections are dropped",
75 | new Linked<>(1, 2).zip(new Linked<>(4, 5, 6), new Linked<>(7, 8, 9)),
76 | new HasEqualValues<>(
77 | new Linked>(
78 | new Linked<>(1, 4, 7),
79 | new Linked<>(2, 5, 8)
80 | )
81 | )
82 | ).affirm();
83 | }
84 |
85 | @Test
86 | void nullInResult() {
87 | new Assertion<>(
88 | "A lack of elements is replaced with null",
89 | new Linked<>(4, 5, 6).zip(new Linked<>(1, 2), new Linked<>(8)),
90 | new HasEqualValues<>(
91 | new Linked>(
92 | new Linked<>(4, 1, 8),
93 | new Linked<>(5, 2, null),
94 | new Linked<>(6, null, null)
95 | )
96 | )
97 | ).affirm();
98 | }
99 |
100 | @Test
101 | void nullFirstArg() {
102 | new Assertion<>(
103 | "A lack of elements is replaced with null",
104 | new Linked<>(1, 2, 3).zip(null, new Linked<>(8)),
105 | new HasEqualValues<>(
106 | new Linked>(
107 | new Linked<>(1, null, 8),
108 | new Linked<>(2, null, null),
109 | new Linked<>(3, null, null)
110 | )
111 | )
112 | ).affirm();
113 | }
114 |
115 | @Test
116 | void nullInFirstArg() {
117 | new Assertion<>(
118 | "A lack of elements is replaced with null",
119 | new Linked<>(1, 2, 3).zip(new Linked<>(null, null), new Linked<>(8)),
120 | new HasEqualValues<>(
121 | new Linked>(
122 | new Linked<>(1, null, 8),
123 | new Linked<>(2, null, null),
124 | new Linked<>(3, null, null)
125 | )
126 | )
127 | ).affirm();
128 | }
129 |
130 | @Test
131 | void nullOtherArgs() {
132 | new Assertion<>(
133 | "A lack of elements is replaced with null",
134 | new Linked<>(1, 2, 3).zip(new Linked<>(4, 5), null),
135 | new HasEqualValues<>(
136 | new Linked>(
137 | new Linked<>(1, 4, null),
138 | new Linked<>(2, 5, null),
139 | new Linked<>(3, null, null)
140 | )
141 | )
142 | ).affirm();
143 | }
144 |
145 | @Test
146 | void nullInOtherArgs() {
147 | new Assertion<>(
148 | "A lack of elements is replaced with null",
149 | new Linked<>(1, 2, 3).zip(new Linked<>(4, 5), new Linked<>(null, null)),
150 | new HasEqualValues<>(
151 | new Linked>(
152 | new Linked<>(1, 4, null),
153 | new Linked<>(2, 5, null),
154 | new Linked<>(3, null, null)
155 | )
156 | )
157 | ).affirm();
158 | }
159 |
160 | @Test
161 | void nullInArgs() {
162 | new Assertion<>(
163 | "A lack of elements is replaced with null",
164 | new Linked<>(1, 2, 3).zip(null, new Linked<>(null, 5), null),
165 | new HasEqualValues<>(
166 | new Linked>(
167 | new Linked<>(1, null, null, null),
168 | new Linked<>(2, null, 5, null),
169 | new Linked<>(3, null, null, null)
170 | )
171 | )
172 | ).affirm();
173 | }
174 | }
175 |
--------------------------------------------------------------------------------
/src/test/java/io/github/dgroup/enumerable4j/package-info.java:
--------------------------------------------------------------------------------
1 | /*
2 | * MIT License
3 | *
4 | * Copyright (c) 2019-2021 Yurii Dubinka
5 | *
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy
7 | * of this software and associated documentation files (the "Software"),
8 | * to deal in the Software without restriction, including without limitation
9 | * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 | * and/or sell copies of the Software, and to permit persons to whom
11 | * the Software is furnished to do so, subject to the following conditions:
12 | *
13 | * The above copyright notice and this permission notice shall be included
14 | * in all copies or substantial portions of the Software.
15 | *
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 | * OR OTHER DEALINGS IN THE SOFTWARE.
23 | */
24 |
25 | /**
26 | * Unit tests for a package {@link io.dgroup.enumerable4j}.
27 | *
28 | * @author Yurii Dubinka (yurii.dubinka@gmail.com)
29 | * @since 0.1.0
30 | */
31 | package io.github.dgroup.enumerable4j;
32 |
--------------------------------------------------------------------------------