refactor: migrate from repo onixbyte/onixbyte-toolbox to module repos
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
end_of_line = lf
|
||||||
|
indent_size = 4
|
||||||
|
indent_style = space
|
||||||
|
insert_final_newline = true
|
||||||
|
max_line_length = 100
|
||||||
|
tab_width = 4
|
||||||
|
|
||||||
|
[*.less]
|
||||||
|
indent_size = 2
|
||||||
|
|
||||||
|
[*.proto]
|
||||||
|
indent_size = 2
|
||||||
|
tab_width = 2
|
||||||
|
|
||||||
|
[*.sass]
|
||||||
|
indent_size = 2
|
||||||
|
|
||||||
|
[*.scss]
|
||||||
|
indent_size = 2
|
||||||
|
|
||||||
|
[*.vue]
|
||||||
|
indent_size = 2
|
||||||
|
tab_width = 2
|
||||||
|
|
||||||
|
[{*.bash,*.sh,*.zsh}]
|
||||||
|
indent_size = 2
|
||||||
|
tab_width = 2
|
||||||
|
|
||||||
|
[{*.default,*.yaml,*.yml}]
|
||||||
|
indent_size = 2
|
||||||
|
|
||||||
|
[{*.har,*.jsb2,*.jsb3,*.json,*.jsonc,*.postman_collection,*.postman_collection.json,*.postman_environment,*.postman_environment.json,.babelrc,.eslintrc,.prettierrc,.stylelintrc,.ws-context,jest.config}]
|
||||||
|
indent_size = 2
|
||||||
|
|
||||||
|
[{*.pb,*.textproto,*.txtpb}]
|
||||||
|
indent_size = 2
|
||||||
|
tab_width = 2
|
||||||
|
|
||||||
|
[{*.ps1,*.psd1,*.psm1}]
|
||||||
|
max_line_length = 115
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
# Publishes onixbyte-tuple to Maven Central when a version tag is pushed to main.
|
||||||
|
#
|
||||||
|
# Supported tag format: <major>.<minor>.<patch>[-suffix] (e.g. 3.3.1, 3.3.1-alpha)
|
||||||
|
|
||||||
|
name: Publish Package to Maven Central
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
tags:
|
||||||
|
- '[0-9]*.[0-9]*.[0-9]*'
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
publish:
|
||||||
|
name: Build and Publish
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4.2.2
|
||||||
|
with:
|
||||||
|
fetch-depth: 0
|
||||||
|
|
||||||
|
- name: Verify Tag is on Main Branch
|
||||||
|
run: |
|
||||||
|
if ! git merge-base --is-ancestor HEAD origin/main; then
|
||||||
|
echo "::error::Tag ${{ github.ref_name }} does not point to a commit on the main branch"
|
||||||
|
echo "Tags must be pushed after the commit is merged to main."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
echo "✓ Tag ${{ github.ref_name }} is on main"
|
||||||
|
|
||||||
|
- name: Extract Version
|
||||||
|
id: version
|
||||||
|
run: |
|
||||||
|
VERSION="${{ github.ref_name }}"
|
||||||
|
echo "version=${VERSION}" >> $GITHUB_OUTPUT
|
||||||
|
echo "Version: ${VERSION}"
|
||||||
|
|
||||||
|
- name: Setup GPG TTY
|
||||||
|
run: export GPG_TTY=$(tty)
|
||||||
|
|
||||||
|
- name: Import PGP Private Key
|
||||||
|
uses: crazy-max/ghaction-import-gpg@v6.3.0
|
||||||
|
with:
|
||||||
|
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
|
||||||
|
passphrase: ${{ secrets.GPG_PASSPHRASE }}
|
||||||
|
trust_level: 5
|
||||||
|
|
||||||
|
- name: Creating PGP Ring Key
|
||||||
|
run: |
|
||||||
|
mkdir -p ~/.gnupg
|
||||||
|
echo ${{ secrets.GPG_PASSPHRASE }} | gpg --batch --yes --pinentry-mode loopback --passphrase-fd 0 --export-secret-keys -o ~/.gnupg/gpg_key.ring
|
||||||
|
|
||||||
|
- name: Restore gradle.properties
|
||||||
|
env:
|
||||||
|
GRADLE_PROPERTIES: ${{ secrets.GRADLE_PROPERTIES }}
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
mkdir -p ~/.gradle/
|
||||||
|
echo "GRADLE_USER_HOME=${HOME}/.gradle" >> $GITHUB_ENV
|
||||||
|
echo "${GRADLE_PROPERTIES}" > ~/.gradle/gradle.properties
|
||||||
|
|
||||||
|
- name: Set up JDK 17
|
||||||
|
uses: actions/setup-java@v4
|
||||||
|
with:
|
||||||
|
java-version: "17"
|
||||||
|
distribution: "corretto"
|
||||||
|
|
||||||
|
- name: Setup Gradle
|
||||||
|
uses: gradle/actions/setup-gradle@v4.4.1
|
||||||
|
|
||||||
|
- name: Grant Execution Authority to Gradlew
|
||||||
|
run: chmod +x ./gradlew
|
||||||
|
|
||||||
|
- name: Build with Gradle
|
||||||
|
env:
|
||||||
|
VERSION: ${{ steps.version.outputs.version }}
|
||||||
|
run: ./gradlew build "-PartefactVersion=${VERSION}"
|
||||||
|
|
||||||
|
- name: List Output Items
|
||||||
|
run: ls -l ./build/libs
|
||||||
|
|
||||||
|
- name: Publish to Maven Central
|
||||||
|
env:
|
||||||
|
VERSION: ${{ steps.version.outputs.version }}
|
||||||
|
run: ./gradlew publish "-PartefactVersion=${VERSION}"
|
||||||
|
|
||||||
|
- name: Create Deployment on Central Publisher Portal
|
||||||
|
run: |
|
||||||
|
curl --fail -X 'POST' \
|
||||||
|
'https://ossrh-staging-api.central.sonatype.com/manual/upload/defaultRepository/com.onixbyte?publishing_type=user_managed' \
|
||||||
|
-H 'accept: */*' \
|
||||||
|
-H 'Authorization: Bearer ${{ secrets.MAVEN_PORTAL_TOKEN }}' \
|
||||||
|
-d ''
|
||||||
+42
@@ -0,0 +1,42 @@
|
|||||||
|
target/
|
||||||
|
!.mvn/wrapper/maven-wrapper.jar
|
||||||
|
!**/src/main/**/target/
|
||||||
|
!**/src/test/**/target/
|
||||||
|
|
||||||
|
### IntelliJ IDEA ###
|
||||||
|
.idea
|
||||||
|
.idea/modules.xml
|
||||||
|
.idea/jarRepositories.xml
|
||||||
|
.idea/compiler.xml
|
||||||
|
.idea/libraries/
|
||||||
|
*.iws
|
||||||
|
*.iml
|
||||||
|
*.ipr
|
||||||
|
|
||||||
|
### Eclipse ###
|
||||||
|
.apt_generated
|
||||||
|
.classpath
|
||||||
|
.factorypath
|
||||||
|
.project
|
||||||
|
.settings
|
||||||
|
.springBeans
|
||||||
|
.sts4-cache
|
||||||
|
|
||||||
|
### NetBeans ###
|
||||||
|
/nbproject/private/
|
||||||
|
/nbbuild/
|
||||||
|
/dist/
|
||||||
|
/nbdist/
|
||||||
|
/.nb-gradle/
|
||||||
|
build/
|
||||||
|
!**/src/main/**/build/
|
||||||
|
!**/src/test/**/build/
|
||||||
|
|
||||||
|
### VS Code ###
|
||||||
|
.vscode/
|
||||||
|
|
||||||
|
### Mac OS ###
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
### Gradle ###
|
||||||
|
.gradle
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
# Code of Conduct
|
||||||
|
|
||||||
|
## Our Pledge
|
||||||
|
|
||||||
|
We as members, contributors, and leaders pledge to make participation in our project
|
||||||
|
a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity,
|
||||||
|
gender identity and expression, level of experience, nationality, personal appearance, race,
|
||||||
|
religion, or sexual identity and orientation.
|
||||||
|
|
||||||
|
## Our Standards
|
||||||
|
|
||||||
|
Examples of behaviour that contributes to creating a positive environment include:
|
||||||
|
|
||||||
|
- Using welcoming and inclusive language
|
||||||
|
- Being respectful of differing viewpoints and experiences
|
||||||
|
- Gracefully accepting constructive criticism
|
||||||
|
- Focusing on what is best for the community
|
||||||
|
- Showing empathy towards other community members
|
||||||
|
|
||||||
|
Examples of unacceptable behaviour by participants include:
|
||||||
|
|
||||||
|
- The use of sexualised language or imagery, and unwelcome sexual attention or advances
|
||||||
|
- Trolling, insulting, or derogatory comments
|
||||||
|
- Personal or political attacks
|
||||||
|
- Public or private harassment
|
||||||
|
- Publishing others’ private information, such as a physical or electronic address, without
|
||||||
|
explicit permission
|
||||||
|
- Other conduct which could reasonably be considered inappropriate in a professional setting
|
||||||
|
|
||||||
|
## Enforcement Responsibilities
|
||||||
|
|
||||||
|
Project maintainers are responsible for clarifying the standards of acceptable behaviour and are
|
||||||
|
expected to take appropriate and fair corrective action in response to any instances of
|
||||||
|
unacceptable behaviour.
|
||||||
|
|
||||||
|
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits,
|
||||||
|
code, wiki edits, issues, and other contributions that do not align with this Code of Conduct, and
|
||||||
|
will communicate reasons for moderation decisions when appropriate.
|
||||||
|
|
||||||
|
## Scope
|
||||||
|
|
||||||
|
This Code of Conduct applies both within project spaces and in public spaces when an individual is
|
||||||
|
representing the project or its community. Examples of representing a project or community include
|
||||||
|
using an official project email address, posting via an official social media account, or acting as
|
||||||
|
an appointed representative at an online or offline event.
|
||||||
|
|
||||||
|
## Enforcement
|
||||||
|
|
||||||
|
Instances of abusive, harassing, or unacceptable behaviour may be reported by contacting the
|
||||||
|
project team at [opensource@onixbyte.com](mailto:opensource@onixbyte.com). All complaints will be
|
||||||
|
reviewed and investigated and will result in a response that is deemed necessary and appropriate to
|
||||||
|
the circumstances. The project team is obligated to maintain confidentiality with regard to the
|
||||||
|
reporter of an incident.
|
||||||
|
|
||||||
|
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face
|
||||||
|
temporary or permanent repercussions as determined by other members of the project's leadership.
|
||||||
|
|
||||||
|
## Attribution
|
||||||
|
|
||||||
|
This Code of Conduct is adapted from the [Contributor Covenant](https://www.contributor-covenant.org/),
|
||||||
|
version 1.4, available at https://www.contributor-covenant.org/version/1/4/
|
||||||
|
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
# Contributing to OnixByte Toolbox
|
||||||
|
|
||||||
|
We appreciate your interest in contributing to our Java Enterprise Utility Library. Contributions
|
||||||
|
are welcome in various forms such as code, documentation, bug reports, and test cases. To ensure a
|
||||||
|
smooth collaboration, please follow the guidelines outlined below.
|
||||||
|
|
||||||
|
## Getting Started
|
||||||
|
|
||||||
|
The OnixByte Toolbox is an open-source Java library designed to ease Java enterprise
|
||||||
|
application development. It is built with Java 17, and we require contributors to use OpenJDK 17.
|
||||||
|
|
||||||
|
## Development Setup
|
||||||
|
|
||||||
|
There is no need for manual setup beyond ensuring you have JDK 17 and a stable internet connection.
|
||||||
|
Our project uses Gradle Wrapper for building, simplifying the setup process.
|
||||||
|
|
||||||
|
```shell
|
||||||
|
./gradlew build
|
||||||
|
```
|
||||||
|
|
||||||
|
## Branching Strategy
|
||||||
|
|
||||||
|
We follow the `git-flow` branching model. Contributors should fork this repository and create their
|
||||||
|
work on separate branches prefixed with `feature/` or `hotfix/` as appropriate.
|
||||||
|
|
||||||
|
## Code Style
|
||||||
|
|
||||||
|
Please adhere to the coding standards specified in our `.editorconfig` file in the root of
|
||||||
|
the repository. Consistent style helps in maintaining readability and uniformity across the codebase.
|
||||||
|
|
||||||
|
## Commit Messages
|
||||||
|
|
||||||
|
We require that commit messages follow this structure:
|
||||||
|
|
||||||
|
```text
|
||||||
|
type[(scope)]: subject
|
||||||
|
|
||||||
|
[body]
|
||||||
|
|
||||||
|
[BREAKING CHANGE: breaking changes]
|
||||||
|
```
|
||||||
|
|
||||||
|
This format helps in auto-generating changelogs and understanding the purpose behind changes.
|
||||||
|
|
||||||
|
## Submitting Contributions
|
||||||
|
|
||||||
|
1. Fork the repository.
|
||||||
|
2. Create a branch: `git checkout -b feature/my-new-feature` or `git checkout -b hotfix/my-fix`.
|
||||||
|
3. Make your changes and commit them.
|
||||||
|
4. Push to the branch: `git push origin feature/my-new-feature`.
|
||||||
|
5. Create a Pull Request with a succinct subject and a detailed body.
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
We encourage using JUnit Jupiter for unit and integration tests. Pull Requests with accompanying
|
||||||
|
test reports are prioritised for review and merging.
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
By contributing to the OnixByte Toolbox, you agree that your contributions will be licensed under the
|
||||||
|
MIT license. If you do not agree to this, please refrain from contributing.
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2024-2025 OnixByte
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
# Common Toolbox
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
Common Toolbox is a Java SE utility library, that provides a collection of utility to streamline
|
||||||
|
your Java coding experience.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- AES encryption and decryption;
|
||||||
|
- Base64 encode and decode;
|
||||||
|
- Boolean calculation;
|
||||||
|
- Reduce `if...else...` with **lambdas**;
|
||||||
|
- Hash calculation for strings;
|
||||||
|
- Convert Java beans to map and map to Java beans;
|
||||||
|
- Simplified range generator.
|
||||||
|
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024-2026 OnixByte
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import java.net.URI
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
java
|
||||||
|
id("java-library")
|
||||||
|
id("maven-publish")
|
||||||
|
id("signing")
|
||||||
|
}
|
||||||
|
|
||||||
|
val artefactVersion: String by project
|
||||||
|
|
||||||
|
version = artefactVersion
|
||||||
|
val projectUrl: String by project
|
||||||
|
val projectGithubUrl: String by project
|
||||||
|
val licenseName: String by project
|
||||||
|
val licenseUrl: String by project
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
java {
|
||||||
|
sourceCompatibility = JavaVersion.VERSION_17
|
||||||
|
targetCompatibility = JavaVersion.VERSION_17
|
||||||
|
withSourcesJar()
|
||||||
|
withJavadocJar()
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.withType<JavaCompile> {
|
||||||
|
options.encoding = "UTF-8"
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.withType<Jar> {
|
||||||
|
exclude("logback.xml")
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
compileOnly(libs.slf4j)
|
||||||
|
implementation(libs.logback)
|
||||||
|
testImplementation(platform(libs.junit.bom))
|
||||||
|
testImplementation(libs.junit.jupiter)
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.test {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
|
||||||
|
publishing {
|
||||||
|
publications {
|
||||||
|
create<MavenPublication>("commonToolbox") {
|
||||||
|
groupId = group.toString()
|
||||||
|
artifactId = "common-toolbox"
|
||||||
|
version = artefactVersion
|
||||||
|
|
||||||
|
pom {
|
||||||
|
name = "OnixByte Common Toolbox"
|
||||||
|
description = "The utils module of OnixByte toolbox."
|
||||||
|
url = projectUrl
|
||||||
|
|
||||||
|
licenses {
|
||||||
|
license {
|
||||||
|
name = licenseName
|
||||||
|
url = licenseUrl
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
scm {
|
||||||
|
connection = "scm:git:git://github.com:onixbyte/onixbyte-toolbox.git"
|
||||||
|
developerConnection = "scm:git:git://github.com:onixbyte/onixbyte-toolbox.git"
|
||||||
|
url = projectGithubUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
developers {
|
||||||
|
developer {
|
||||||
|
id = "zihluwang"
|
||||||
|
name = "Zihlu Wang"
|
||||||
|
email = "real@zihluwang.me"
|
||||||
|
timezone = "Asia/Hong_Kong"
|
||||||
|
}
|
||||||
|
|
||||||
|
developer {
|
||||||
|
id = "siujamo"
|
||||||
|
name = "Siu Jam'o"
|
||||||
|
email = "jamo.siu@gmail.com"
|
||||||
|
timezone = "Asia/Shanghai"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
from(components["java"])
|
||||||
|
|
||||||
|
signing {
|
||||||
|
setRequired(project.hasProperty("signing.keyId"))
|
||||||
|
sign(publishing.publications["commonToolbox"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
maven {
|
||||||
|
name = "sonatypeNexus"
|
||||||
|
url = URI(providers.gradleProperty("repo.maven-central.host").get())
|
||||||
|
credentials {
|
||||||
|
username = providers.gradleProperty("repo.maven-central.username").get()
|
||||||
|
password = providers.gradleProperty("repo.maven-central.password").get()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
#
|
||||||
|
# Copyright (c) 2024-2025 OnixByte
|
||||||
|
#
|
||||||
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
# of this software and associated documentation files (the "Software"), to deal
|
||||||
|
# in the Software without restriction, including without limitation the rights
|
||||||
|
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
# copies of the Software, and to permit persons to whom the Software is
|
||||||
|
# furnished to do so, subject to the following conditions:
|
||||||
|
#
|
||||||
|
# The above copyright notice and this permission notice shall be included in all
|
||||||
|
# copies or substantial portions of the Software.
|
||||||
|
#
|
||||||
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
# SOFTWARE.
|
||||||
|
#
|
||||||
|
|
||||||
|
artefactVersion=3.4.0
|
||||||
|
projectUrl=https://github.com/onixbyte/onixbyte-common-toolbox
|
||||||
|
projectGithubUrl=git@github.com:onixbyte/onixbyte-common-toolbox.git
|
||||||
|
licenseName=MIT
|
||||||
|
licenseUrl=
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
[versions]
|
||||||
|
slf4j = "2.0.17"
|
||||||
|
logback = "1.5.26"
|
||||||
|
junit = "5.11.4"
|
||||||
|
|
||||||
|
[libraries]
|
||||||
|
slf4j = { group = "org.slf4j", name = "slf4j-api", version.ref = "slf4j" }
|
||||||
|
logback = { group = "ch.qos.logback", name = "logback-classic", version.ref = "logback" }
|
||||||
|
junit-bom = { group = "org.junit", name = "junit-bom", version.ref = "junit" }
|
||||||
|
junit-jupiter = { group = "org.junit.jupiter", name = "junit-jupiter", version.ref = "junit" }
|
||||||
Vendored
BIN
Binary file not shown.
+7
@@ -0,0 +1,7 @@
|
|||||||
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-all.zip
|
||||||
|
networkTimeout=10000
|
||||||
|
validateDistributionUrl=true
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
||||||
@@ -0,0 +1,252 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright © 2015-2021 the original authors.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
#
|
||||||
|
# SPDX-License-Identifier: Apache-2.0
|
||||||
|
#
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
#
|
||||||
|
# Gradle start up script for POSIX generated by Gradle.
|
||||||
|
#
|
||||||
|
# Important for running:
|
||||||
|
#
|
||||||
|
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||||
|
# noncompliant, but you have some other compliant shell such as ksh or
|
||||||
|
# bash, then to run this script, type that shell name before the whole
|
||||||
|
# command line, like:
|
||||||
|
#
|
||||||
|
# ksh Gradle
|
||||||
|
#
|
||||||
|
# Busybox and similar reduced shells will NOT work, because this script
|
||||||
|
# requires all of these POSIX shell features:
|
||||||
|
# * functions;
|
||||||
|
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||||
|
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||||
|
# * compound commands having a testable exit status, especially «case»;
|
||||||
|
# * various built-in commands including «command», «set», and «ulimit».
|
||||||
|
#
|
||||||
|
# Important for patching:
|
||||||
|
#
|
||||||
|
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||||
|
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||||
|
#
|
||||||
|
# The "traditional" practice of packing multiple parameters into a
|
||||||
|
# space-separated string is a well documented source of bugs and security
|
||||||
|
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||||
|
# options in "$@", and eventually passing that to Java.
|
||||||
|
#
|
||||||
|
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||||
|
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||||
|
# see the in-line comments for details.
|
||||||
|
#
|
||||||
|
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||||
|
# Darwin, MinGW, and NonStop.
|
||||||
|
#
|
||||||
|
# (3) This script is generated from the Groovy template
|
||||||
|
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||||
|
# within the Gradle project.
|
||||||
|
#
|
||||||
|
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||||
|
#
|
||||||
|
##############################################################################
|
||||||
|
|
||||||
|
# Attempt to set APP_HOME
|
||||||
|
|
||||||
|
# Resolve links: $0 may be a link
|
||||||
|
app_path=$0
|
||||||
|
|
||||||
|
# Need this for daisy-chained symlinks.
|
||||||
|
while
|
||||||
|
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||||
|
[ -h "$app_path" ]
|
||||||
|
do
|
||||||
|
ls=$( ls -ld "$app_path" )
|
||||||
|
link=${ls#*' -> '}
|
||||||
|
case $link in #(
|
||||||
|
/*) app_path=$link ;; #(
|
||||||
|
*) app_path=$APP_HOME$link ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# This is normally unused
|
||||||
|
# shellcheck disable=SC2034
|
||||||
|
APP_BASE_NAME=${0##*/}
|
||||||
|
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||||
|
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
|
||||||
|
' "$PWD" ) || exit
|
||||||
|
|
||||||
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
|
MAX_FD=maximum
|
||||||
|
|
||||||
|
warn () {
|
||||||
|
echo "$*"
|
||||||
|
} >&2
|
||||||
|
|
||||||
|
die () {
|
||||||
|
echo
|
||||||
|
echo "$*"
|
||||||
|
echo
|
||||||
|
exit 1
|
||||||
|
} >&2
|
||||||
|
|
||||||
|
# OS specific support (must be 'true' or 'false').
|
||||||
|
cygwin=false
|
||||||
|
msys=false
|
||||||
|
darwin=false
|
||||||
|
nonstop=false
|
||||||
|
case "$( uname )" in #(
|
||||||
|
CYGWIN* ) cygwin=true ;; #(
|
||||||
|
Darwin* ) darwin=true ;; #(
|
||||||
|
MSYS* | MINGW* ) msys=true ;; #(
|
||||||
|
NONSTOP* ) nonstop=true ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
# Determine the Java command to use to start the JVM.
|
||||||
|
if [ -n "$JAVA_HOME" ] ; then
|
||||||
|
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||||
|
# IBM's JDK on AIX uses strange locations for the executables
|
||||||
|
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||||
|
else
|
||||||
|
JAVACMD=$JAVA_HOME/bin/java
|
||||||
|
fi
|
||||||
|
if [ ! -x "$JAVACMD" ] ; then
|
||||||
|
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
JAVACMD=java
|
||||||
|
if ! command -v java >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Increase the maximum file descriptors if we can.
|
||||||
|
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||||
|
case $MAX_FD in #(
|
||||||
|
max*)
|
||||||
|
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||||
|
# shellcheck disable=SC2039,SC3045
|
||||||
|
MAX_FD=$( ulimit -H -n ) ||
|
||||||
|
warn "Could not query maximum file descriptor limit"
|
||||||
|
esac
|
||||||
|
case $MAX_FD in #(
|
||||||
|
'' | soft) :;; #(
|
||||||
|
*)
|
||||||
|
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||||
|
# shellcheck disable=SC2039,SC3045
|
||||||
|
ulimit -n "$MAX_FD" ||
|
||||||
|
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Collect all arguments for the java command, stacking in reverse order:
|
||||||
|
# * args from the command line
|
||||||
|
# * the main class name
|
||||||
|
# * -classpath
|
||||||
|
# * -D...appname settings
|
||||||
|
# * --module-path (only if needed)
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||||
|
|
||||||
|
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||||
|
if "$cygwin" || "$msys" ; then
|
||||||
|
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||||
|
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
||||||
|
|
||||||
|
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||||
|
|
||||||
|
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||||
|
for arg do
|
||||||
|
if
|
||||||
|
case $arg in #(
|
||||||
|
-*) false ;; # don't mess with options #(
|
||||||
|
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||||
|
[ -e "$t" ] ;; #(
|
||||||
|
*) false ;;
|
||||||
|
esac
|
||||||
|
then
|
||||||
|
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||||
|
fi
|
||||||
|
# Roll the args list around exactly as many times as the number of
|
||||||
|
# args, so each arg winds up back in the position where it started, but
|
||||||
|
# possibly modified.
|
||||||
|
#
|
||||||
|
# NB: a `for` loop captures its iteration list before it begins, so
|
||||||
|
# changing the positional parameters here affects neither the number of
|
||||||
|
# iterations, nor the values presented in `arg`.
|
||||||
|
shift # remove old arg
|
||||||
|
set -- "$@" "$arg" # push replacement arg
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
|
# Collect all arguments for the java command:
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||||
|
# and any embedded shellness will be escaped.
|
||||||
|
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||||
|
# treated as '${Hostname}' itself on the command line.
|
||||||
|
|
||||||
|
set -- \
|
||||||
|
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||||
|
-classpath "$CLASSPATH" \
|
||||||
|
org.gradle.wrapper.GradleWrapperMain \
|
||||||
|
"$@"
|
||||||
|
|
||||||
|
# Stop when "xargs" is not available.
|
||||||
|
if ! command -v xargs >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "xargs is not available"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Use "xargs" to parse quoted args.
|
||||||
|
#
|
||||||
|
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||||
|
#
|
||||||
|
# In Bash we could simply go:
|
||||||
|
#
|
||||||
|
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||||
|
# set -- "${ARGS[@]}" "$@"
|
||||||
|
#
|
||||||
|
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||||
|
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||||
|
# character that might be a shell metacharacter, then use eval to reverse
|
||||||
|
# that process (while maintaining the separation between arguments), and wrap
|
||||||
|
# the whole thing up as a single "set" statement.
|
||||||
|
#
|
||||||
|
# This will of course break if any of these variables contains a newline or
|
||||||
|
# an unmatched quote.
|
||||||
|
#
|
||||||
|
|
||||||
|
eval "set -- $(
|
||||||
|
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||||
|
xargs -n1 |
|
||||||
|
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||||
|
tr '\n' ' '
|
||||||
|
)" '"$@"'
|
||||||
|
|
||||||
|
exec "$JAVACMD" "$@"
|
||||||
Vendored
+94
@@ -0,0 +1,94 @@
|
|||||||
|
@rem
|
||||||
|
@rem Copyright 2015 the original author or authors.
|
||||||
|
@rem
|
||||||
|
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
@rem you may not use this file except in compliance with the License.
|
||||||
|
@rem You may obtain a copy of the License at
|
||||||
|
@rem
|
||||||
|
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
@rem
|
||||||
|
@rem Unless required by applicable law or agreed to in writing, software
|
||||||
|
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@rem See the License for the specific language governing permissions and
|
||||||
|
@rem limitations under the License.
|
||||||
|
@rem
|
||||||
|
@rem SPDX-License-Identifier: Apache-2.0
|
||||||
|
@rem
|
||||||
|
|
||||||
|
@if "%DEBUG%"=="" @echo off
|
||||||
|
@rem ##########################################################################
|
||||||
|
@rem
|
||||||
|
@rem Gradle startup script for Windows
|
||||||
|
@rem
|
||||||
|
@rem ##########################################################################
|
||||||
|
|
||||||
|
@rem Set local scope for the variables with windows NT shell
|
||||||
|
if "%OS%"=="Windows_NT" setlocal
|
||||||
|
|
||||||
|
set DIRNAME=%~dp0
|
||||||
|
if "%DIRNAME%"=="" set DIRNAME=.
|
||||||
|
@rem This is normally unused
|
||||||
|
set APP_BASE_NAME=%~n0
|
||||||
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
|
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||||
|
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||||
|
|
||||||
|
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||||
|
|
||||||
|
@rem Find java.exe
|
||||||
|
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||||
|
|
||||||
|
set JAVA_EXE=java.exe
|
||||||
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
|
if %ERRORLEVEL% equ 0 goto execute
|
||||||
|
|
||||||
|
echo. 1>&2
|
||||||
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
|
||||||
|
echo. 1>&2
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||||
|
echo location of your Java installation. 1>&2
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:findJavaFromJavaHome
|
||||||
|
set JAVA_HOME=%JAVA_HOME:"=%
|
||||||
|
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||||
|
|
||||||
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
|
echo. 1>&2
|
||||||
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
|
||||||
|
echo. 1>&2
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
|
||||||
|
echo location of your Java installation. 1>&2
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:execute
|
||||||
|
@rem Setup the command line
|
||||||
|
|
||||||
|
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
@rem Execute Gradle
|
||||||
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||||
|
|
||||||
|
:end
|
||||||
|
@rem End local scope for the variables with windows NT shell
|
||||||
|
if %ERRORLEVEL% equ 0 goto mainEnd
|
||||||
|
|
||||||
|
:fail
|
||||||
|
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||||
|
rem the _cmd.exe /c_ return code!
|
||||||
|
set EXIT_CODE=%ERRORLEVEL%
|
||||||
|
if %EXIT_CODE% equ 0 set EXIT_CODE=1
|
||||||
|
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
|
||||||
|
exit /b %EXIT_CODE%
|
||||||
|
|
||||||
|
:mainEnd
|
||||||
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
|
||||||
|
:omega
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
rootProject.name = "onixbyte-common-toolbox"
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024-2026 OnixByte
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.adapter;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link ObjectMapAdapter} interface provides methods to convert between objects and maps.
|
||||||
|
* This interface is useful for scenarios where objects need to be represented as maps for
|
||||||
|
* serialization, deserialization, or other purposes.
|
||||||
|
*
|
||||||
|
* <p>Implementations of this interface should provide the logic to convert an object of type
|
||||||
|
* {@code T} to a {@link Map} and vice versa.</p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>{@code
|
||||||
|
* public class User {
|
||||||
|
* private String name;
|
||||||
|
* private int age;
|
||||||
|
*
|
||||||
|
* // getters and setters
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* public class UserMapAdapter implements ObjectMapAdapter<User> {
|
||||||
|
* @Override
|
||||||
|
* public Map<String, Object> toMap(User user) {
|
||||||
|
* Map<String, Object> map = new HashMap<>();
|
||||||
|
* map.put("name", user.getName());
|
||||||
|
* map.put("age", user.getAge());
|
||||||
|
* return map;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* @Override
|
||||||
|
* public User fromMap(Map<String, Object> map) {
|
||||||
|
* User user = new User();
|
||||||
|
* user.setName((String) map.get("name"));
|
||||||
|
* user.setAge((Integer) map.get("age"));
|
||||||
|
* return user;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param <T> the type of the object to be converted
|
||||||
|
* @author zihluwang
|
||||||
|
* @version 3.0.0
|
||||||
|
*/
|
||||||
|
public interface ObjectMapAdapter<T> {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert an object to a map.
|
||||||
|
*
|
||||||
|
* @param element the element that will be converted to Map
|
||||||
|
* @return a Map that is converted from the element
|
||||||
|
*/
|
||||||
|
Map<String, Object> toMap(T element);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert a Map to an object.
|
||||||
|
*
|
||||||
|
* @param map the map that will be converted to an object
|
||||||
|
* @return the object that is converted from the Map
|
||||||
|
*/
|
||||||
|
T toObject(Map<String, Object> map);
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,206 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024-2026 OnixByte
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import javax.crypto.Cipher;
|
||||||
|
import javax.crypto.spec.IvParameterSpec;
|
||||||
|
import javax.crypto.spec.SecretKeySpec;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link AesUtil} class provides utility methods for encrypting and decrypting data using the
|
||||||
|
* AES algorithm. This class supports both byte array and string data, and uses a specified secret
|
||||||
|
* key for encryption and decryption.
|
||||||
|
* <p>
|
||||||
|
* The utility methods in this class are useful for scenarios where data needs to be securely
|
||||||
|
* encrypted and decrypted.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>{@code
|
||||||
|
* // Encrypting and decrypting byte array data
|
||||||
|
* byte[] secretKey = "43f72073956d4c81".getBytes(StandardCharsets.UTF_8);
|
||||||
|
* byte[] data = "Hello World".getBytes(StandardCharsets.UTF_8);
|
||||||
|
* byte[] encryptedData = AesUtil.encrypt(data, secretKey);
|
||||||
|
* byte[] decryptedData = AesUtil.decrypt(encryptedData, secretKey);
|
||||||
|
* System.out.println(new String(decryptedData, StandardCharsets.UTF_8)); // Output: Hello World
|
||||||
|
*
|
||||||
|
* // Encrypting and decrypting string data
|
||||||
|
* String secret = "43f72073956d4c81";
|
||||||
|
* String encryptedString = AesUtil.encrypt("Hello World", secret);
|
||||||
|
* String decryptedString = AesUtil.decrypt(encryptedString, secret);
|
||||||
|
* System.out.println(decryptedString); // Output: Hello World
|
||||||
|
*
|
||||||
|
* // Generating a random secret key
|
||||||
|
* String randomSecret = AesUtil.generateRandomSecret();
|
||||||
|
* System.out.println(randomSecret); // Output: A randomly generated 16-character long secret
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @author hubin
|
||||||
|
* @version 3.0.0
|
||||||
|
*/
|
||||||
|
public final class AesUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The algorithm AES.
|
||||||
|
*/
|
||||||
|
private static final String AES = "AES";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The algorithm AES/CBC/PKCS5Padding.
|
||||||
|
*/
|
||||||
|
private static final String AES_CBC_CIPHER = "AES/CBC/PKCS5Padding";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor to prevent instantiation of this utility class.
|
||||||
|
*/
|
||||||
|
private AesUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypts the specified data using the AES algorithm with the provided secret key.
|
||||||
|
*
|
||||||
|
* @param data the data to be encrypted
|
||||||
|
* @param secret the secret key used for encryption
|
||||||
|
* @param ivParam the iv param
|
||||||
|
* @return the encrypted data as a byte array
|
||||||
|
* @throws GeneralSecurityException if any cryptographic error occurs during encryption
|
||||||
|
*/
|
||||||
|
public static byte[] encrypt(byte[] data, byte[] secret, byte[] ivParam) throws GeneralSecurityException {
|
||||||
|
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
|
||||||
|
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
|
||||||
|
cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, new IvParameterSpec(ivParam));
|
||||||
|
return cipher.doFinal(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypts the specified data using the AES algorithm with the provided secret key.
|
||||||
|
*
|
||||||
|
* @param data the data to be encrypted
|
||||||
|
* @param secret the secret key used for encryption
|
||||||
|
* @return the encrypted data as a byte array
|
||||||
|
* @throws GeneralSecurityException if any cryptographic error occurs during encryption
|
||||||
|
*/
|
||||||
|
public static byte[] encrypt(byte[] data, byte[] secret) throws GeneralSecurityException {
|
||||||
|
return encrypt(data, secret, secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrypts the specified data using the AES algorithm with the provided secret key.
|
||||||
|
*
|
||||||
|
* @param data the data to be decrypted
|
||||||
|
* @param secret the secret key used for decryption
|
||||||
|
* @param ivParam the iv param
|
||||||
|
* @return the decrypted data as a byte array
|
||||||
|
* @throws GeneralSecurityException if any cryptographic error occurs during decryption
|
||||||
|
*/
|
||||||
|
public static byte[] decrypt(byte[] data, byte[] secret, byte[] ivParam) throws GeneralSecurityException {
|
||||||
|
var secretKeySpec = new SecretKeySpec(new SecretKeySpec(secret, AES).getEncoded(), AES);
|
||||||
|
var cipher = Cipher.getInstance(AES_CBC_CIPHER);
|
||||||
|
cipher.init(Cipher.DECRYPT_MODE, secretKeySpec, new IvParameterSpec(ivParam));
|
||||||
|
return cipher.doFinal(data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrypts the specified data using the AES algorithm with the provided secret key.
|
||||||
|
*
|
||||||
|
* @param data the data to be decrypted
|
||||||
|
* @param secret the secret key used for decryption
|
||||||
|
* @return the decrypted data as a byte array
|
||||||
|
* @throws GeneralSecurityException if any cryptographic error occurs during decryption
|
||||||
|
*/
|
||||||
|
public static byte[] decrypt(byte[] data, byte[] secret) throws GeneralSecurityException {
|
||||||
|
return decrypt(data, secret, secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypts the specified string data using the AES algorithm with the provided secret key.
|
||||||
|
*
|
||||||
|
* @param data the string data to be encrypted
|
||||||
|
* @param secret the secret key used for encryption
|
||||||
|
* @param ivParam the iv param
|
||||||
|
* @return the encrypted data encoded in Base64
|
||||||
|
* @throws GeneralSecurityException if any cryptographic error occurs during encryption
|
||||||
|
*/
|
||||||
|
public static String encrypt(String data, String secret, String ivParam) throws GeneralSecurityException {
|
||||||
|
return Base64.getEncoder().encodeToString(encrypt(
|
||||||
|
data.getBytes(StandardCharsets.UTF_8),
|
||||||
|
secret.getBytes(StandardCharsets.UTF_8),
|
||||||
|
ivParam.getBytes(StandardCharsets.UTF_8)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encrypts the specified string data using the AES algorithm with the provided secret key.
|
||||||
|
*
|
||||||
|
* @param data the string data to be encrypted
|
||||||
|
* @param secret the secret key used for encryption
|
||||||
|
* @return the encrypted data encoded in Base64
|
||||||
|
* @throws GeneralSecurityException if any cryptographic error occurs during encryption
|
||||||
|
*/
|
||||||
|
public static String encrypt(String data, String secret) throws GeneralSecurityException {
|
||||||
|
return encrypt(data, secret, secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrypts the specified Base64-encoded string data using the AES algorithm with the provided secret key.
|
||||||
|
*
|
||||||
|
* @param data the Base64-encoded string data to be decrypted
|
||||||
|
* @param secret the secret key used for decryption
|
||||||
|
* @param ivParam the initialization vector parameter used for AES decryption
|
||||||
|
* @return the decrypted string data
|
||||||
|
* @throws GeneralSecurityException if any cryptographic error occurs during decryption
|
||||||
|
*/
|
||||||
|
public static String decrypt(String data, String secret, String ivParam) throws GeneralSecurityException {
|
||||||
|
var decrypted = decrypt(
|
||||||
|
Base64.getDecoder().decode(data.getBytes(StandardCharsets.UTF_8)),
|
||||||
|
secret.getBytes(StandardCharsets.UTF_8),
|
||||||
|
ivParam.getBytes(StandardCharsets.UTF_8)
|
||||||
|
);
|
||||||
|
return new String(decrypted, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decrypts the specified Base64-encoded string data using the AES algorithm with the provided secret key.
|
||||||
|
*
|
||||||
|
* @param data the Base64-encoded string data to be decrypted
|
||||||
|
* @param secret the secret key used for decryption
|
||||||
|
* @return the decrypted string data
|
||||||
|
* @throws GeneralSecurityException if any cryptographic error occurs during decryption
|
||||||
|
*/
|
||||||
|
public static String decrypt(String data, String secret) throws GeneralSecurityException {
|
||||||
|
return decrypt(data, secret, secret);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates 16-character random secret.
|
||||||
|
*
|
||||||
|
* @return the generated secure secret
|
||||||
|
*/
|
||||||
|
public static String generateRandomSecret() {
|
||||||
|
return UUID.randomUUID().toString().replaceAll("-", "").substring(0, 16);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,212 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024-2026 OnixByte
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link Base64Util} class provides static methods to encode and decode strings with Base64
|
||||||
|
* encoding. It utilizes the {@link Base64} class from the Java standard library for performing the
|
||||||
|
* encoding and decoding operations. This utility class offers convenient methods to encode and
|
||||||
|
* decode strings with different character sets.
|
||||||
|
* <p>
|
||||||
|
* This class is designed as a final class with a private constructor to prevent instantiation.
|
||||||
|
* All methods in this class are static, allowing easy access to the Base64 encoding and
|
||||||
|
* decoding functionality.
|
||||||
|
* <p>
|
||||||
|
* Example usage:
|
||||||
|
* <pre>
|
||||||
|
* String original = "Hello, World!";
|
||||||
|
*
|
||||||
|
* // Encode the string using UTF-8 charset
|
||||||
|
* String encoded = Base64Util.encode(original);
|
||||||
|
* System.out.println("Encoded string: " + encoded);
|
||||||
|
*
|
||||||
|
* // Decode the encoded string using UTF-8 charset
|
||||||
|
* String decoded = Base64Util.decode(encoded);
|
||||||
|
* System.out.println("Decoded string: " + decoded);
|
||||||
|
* </pre>
|
||||||
|
* <p>
|
||||||
|
* <b>Note:</b> This utility class uses the default charset (UTF-8) if no specific charset is
|
||||||
|
* provided. It is recommended to specify the charset explicitly to ensure consistent
|
||||||
|
* encoding and decoding.
|
||||||
|
*
|
||||||
|
* @author zihluwang
|
||||||
|
* @version 3.0.0
|
||||||
|
*/
|
||||||
|
public final class Base64Util {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor to prevent instantiation of this utility class.
|
||||||
|
*/
|
||||||
|
private Base64Util() {
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Base64.Encoder encoder;
|
||||||
|
private static Base64.Decoder decoder;
|
||||||
|
private static Base64.Encoder urlEncoder;
|
||||||
|
private static Base64.Decoder urlDecoder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure that there is only one Base64 Encoder.
|
||||||
|
*
|
||||||
|
* @return the {@link Base64.Encoder} instance
|
||||||
|
*/
|
||||||
|
private static Base64.Encoder getEncoder() {
|
||||||
|
if (Objects.isNull(encoder)) {
|
||||||
|
encoder = Base64.getEncoder();
|
||||||
|
}
|
||||||
|
return encoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure that there is only one Base64 Encoder.
|
||||||
|
*
|
||||||
|
* @return the {@link Base64.Encoder} instance
|
||||||
|
*/
|
||||||
|
private static Base64.Decoder getDecoder() {
|
||||||
|
if (Objects.isNull(decoder)) {
|
||||||
|
decoder = Base64.getDecoder();
|
||||||
|
}
|
||||||
|
return decoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure that there is only one Base64 URL Encoder.
|
||||||
|
*
|
||||||
|
* @return the {@link Base64.Encoder} instance
|
||||||
|
*/
|
||||||
|
private static Base64.Encoder getUrlEncoder() {
|
||||||
|
if (Objects.isNull(urlEncoder)) {
|
||||||
|
urlEncoder = Base64.getUrlEncoder();
|
||||||
|
}
|
||||||
|
return urlEncoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Ensure that there is only one Base64 URL Decoder.
|
||||||
|
*
|
||||||
|
* @return the {@link Base64.Encoder} instance
|
||||||
|
*/
|
||||||
|
public static Base64.Decoder getUrlDecoder() {
|
||||||
|
if (Objects.isNull(urlDecoder)) {
|
||||||
|
urlDecoder = Base64.getUrlDecoder();
|
||||||
|
}
|
||||||
|
return urlDecoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes the given string using the specified charset.
|
||||||
|
*
|
||||||
|
* @param value the string to be encoded
|
||||||
|
* @param charset the charset to be used for encoding
|
||||||
|
* @return the Base64 encoded string
|
||||||
|
*/
|
||||||
|
public static String encode(String value, Charset charset) {
|
||||||
|
var encoded = getEncoder().encode(value.getBytes(charset));
|
||||||
|
|
||||||
|
return new String(encoded);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes the given string using the default UTF-8 charset.
|
||||||
|
*
|
||||||
|
* @param value the string to be encoded
|
||||||
|
* @return the Base64 encoded string
|
||||||
|
*/
|
||||||
|
public static String encode(String value) {
|
||||||
|
return encode(value, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes the given Base64 encoded string using the specified charset.
|
||||||
|
*
|
||||||
|
* @param value the Base64 encoded string to be decoded
|
||||||
|
* @param charset the charset to be used for decoding
|
||||||
|
* @return the decoded string
|
||||||
|
*/
|
||||||
|
public static String decode(String value, Charset charset) {
|
||||||
|
var decoded = getDecoder().decode(value.getBytes(charset));
|
||||||
|
|
||||||
|
return new String(decoded);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes the given Base64 encoded string using the default UTF-8 charset.
|
||||||
|
*
|
||||||
|
* @param value the Base64 encoded string to be decoded
|
||||||
|
* @return the decoded string
|
||||||
|
*/
|
||||||
|
public static String decode(String value) {
|
||||||
|
return decode(value, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes the given string using the specified charset.
|
||||||
|
*
|
||||||
|
* @param value the string to be encoded
|
||||||
|
* @param charset the charset to be used for encoding
|
||||||
|
* @return the Base64 encoded string
|
||||||
|
*/
|
||||||
|
public static String encodeUrlComponents(String value, Charset charset) {
|
||||||
|
var encoded = getUrlEncoder().encode(value.getBytes(charset));
|
||||||
|
|
||||||
|
return new String(encoded);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Encodes the given string using the default UTF-8 charset.
|
||||||
|
*
|
||||||
|
* @param value the string to be encoded
|
||||||
|
* @return the Base64 encoded string
|
||||||
|
*/
|
||||||
|
public static String encodeUrlComponents(String value) {
|
||||||
|
return encodeUrlComponents(value, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes the given Base64 encoded string using the specified charset.
|
||||||
|
*
|
||||||
|
* @param value the Base64 encoded string to be decoded
|
||||||
|
* @param charset the charset to be used for decoding
|
||||||
|
* @return the decoded string
|
||||||
|
*/
|
||||||
|
public static String decodeUrlComponents(String value, Charset charset) {
|
||||||
|
var decoded = getUrlDecoder().decode(value.getBytes(charset));
|
||||||
|
|
||||||
|
return new String(decoded);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Decodes the given Base64 encoded string using the default UTF-8 charset.
|
||||||
|
*
|
||||||
|
* @param value the Base64 encoded string to be decoded
|
||||||
|
* @return the decoded string
|
||||||
|
*/
|
||||||
|
public static String decodeUrlComponents(String value) {
|
||||||
|
return decodeUrlComponents(value, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024-2026 OnixByte
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link BoolUtil} class provides utility methods for boolean calculations.
|
||||||
|
* This class offers methods to perform logical operations such as AND, OR, and NOT on boolean values.
|
||||||
|
* <p>
|
||||||
|
* The utility methods in this class are useful for scenarios where multiple boolean values need to be
|
||||||
|
* evaluated together, and for simplifying complex boolean expressions.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>{@code
|
||||||
|
* boolean result1 = BoolUtil.and(true, true, false); // false
|
||||||
|
* boolean result2 = BoolUtil.or(true, false, false); // true
|
||||||
|
* boolean result3 = BoolUtil.not(false); // true
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @author zihluwang
|
||||||
|
* @version 3.0.0
|
||||||
|
*/
|
||||||
|
public final class BoolUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor to prevent instantiation of this utility class.
|
||||||
|
*/
|
||||||
|
private BoolUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logical and calculation.
|
||||||
|
*
|
||||||
|
* @param values the values to be calculated
|
||||||
|
* @return {@code true} if all value in values is {@code true}, otherwise {@code false}
|
||||||
|
*/
|
||||||
|
public static boolean and(Boolean... values) {
|
||||||
|
return Arrays.stream(values)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.allMatch(Boolean::booleanValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logical and calculation.
|
||||||
|
*
|
||||||
|
* @param valueSuppliers the suppliers of value to be calculated
|
||||||
|
* @return {@code true} if all value in values is {@code true}, otherwise {@code false}
|
||||||
|
*/
|
||||||
|
public static boolean and(BooleanSupplier... valueSuppliers) {
|
||||||
|
return Arrays.stream(valueSuppliers)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.allMatch(BooleanSupplier::getAsBoolean);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logical or calculation.
|
||||||
|
*
|
||||||
|
* @param values the values to be calculated
|
||||||
|
* @return {@code true} if any value in values is {@code true}, otherwise {@code false}
|
||||||
|
*/
|
||||||
|
public static boolean or(Boolean... values) {
|
||||||
|
return Arrays.stream(values)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.anyMatch(Boolean::booleanValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logical or calculation.
|
||||||
|
*
|
||||||
|
* @param valueSuppliers the suppliers of value to be calculated
|
||||||
|
* @return {@code true} if any value in values is {@code true}, otherwise {@code false}
|
||||||
|
*/
|
||||||
|
public static boolean or(BooleanSupplier... valueSuppliers) {
|
||||||
|
return Arrays.stream(valueSuppliers)
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.anyMatch(BooleanSupplier::getAsBoolean);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,213 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024-2026 OnixByte
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link BranchUtil} class provides static methods to simplify conditional logic in Java
|
||||||
|
* development by leveraging lambda expressions. It offers convenient methods to replace verbose
|
||||||
|
* {@code if...else} statements with more concise and expressive functional constructs.
|
||||||
|
* <p>
|
||||||
|
* Developers can use methods in this utility class to streamline their code, enhance readability,
|
||||||
|
* and promote a more functional style of programming when dealing with branching logic and
|
||||||
|
* conditional statements.
|
||||||
|
* <p>
|
||||||
|
* <b>Example:</b>
|
||||||
|
* <pre>
|
||||||
|
* // If you want to simplify an if (exp1 || exp2), you can use the following code:
|
||||||
|
* String r1 = BranchUtil.or(1 == 1, 2 == 1)
|
||||||
|
* .handle(() -> "1 is equal to 1 or 2 is equal to 1.");
|
||||||
|
*
|
||||||
|
* // If you have an else branch, you can use the following code:
|
||||||
|
* String r2 = BranchUtil.or(1 == 1, 2 == 1)
|
||||||
|
* .handle(() -> "1 is equal to 1 or 2 is equal to 1.",
|
||||||
|
* () -> "1 is not equal to 1 and 2 is not equal to 1.");
|
||||||
|
*
|
||||||
|
* // If you only need to execute code without a return value:
|
||||||
|
* BranchUtil.or(1 == 1, 2 == 1)
|
||||||
|
* .handle(() -> {
|
||||||
|
* // do something
|
||||||
|
* }, () -> {
|
||||||
|
* // do something
|
||||||
|
* });
|
||||||
|
* // If you only need an if branch, you can remove the second Supplier instance.
|
||||||
|
*
|
||||||
|
* // To check if all boolean expressions are true, use the 'and' method:
|
||||||
|
* BranchUtil.and(1 == 1, 2 == 1)
|
||||||
|
* .handle(() -> {
|
||||||
|
* // do something
|
||||||
|
* }, () -> {
|
||||||
|
* // do something
|
||||||
|
* });
|
||||||
|
* </pre>
|
||||||
|
* <p>
|
||||||
|
* <b>Note:</b>
|
||||||
|
* The {@link #and(Boolean...)} and {@link #or(Boolean...)} methods accept any number of boolean
|
||||||
|
* expressions.
|
||||||
|
*
|
||||||
|
* @author zihluwang
|
||||||
|
* @version 3.0.0
|
||||||
|
* @see java.util.function.Supplier
|
||||||
|
* @see java.util.function.BooleanSupplier
|
||||||
|
* @see java.lang.Runnable
|
||||||
|
*/
|
||||||
|
public final class BranchUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The final result of the boolean expression.
|
||||||
|
*/
|
||||||
|
private final boolean result;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a {@code BranchUtil} instance.
|
||||||
|
*
|
||||||
|
* @param result the result of the boolean expressions.
|
||||||
|
*/
|
||||||
|
private BranchUtil(boolean result) {
|
||||||
|
this.result = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@code BranchUtil} instance to evaluate a logical OR operation on the provided
|
||||||
|
* boolean expressions.
|
||||||
|
*
|
||||||
|
* @param values the boolean expressions to be evaluated
|
||||||
|
* @return a {@code BranchUtil} instance representing the result of the logical OR operation
|
||||||
|
*/
|
||||||
|
public static BranchUtil or(Boolean... values) {
|
||||||
|
return new BranchUtil(BoolUtil.or(values));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@code BranchUtil} instance to evaluate a logical AND operation on the provided
|
||||||
|
* boolean expressions.
|
||||||
|
*
|
||||||
|
* @param values the boolean expressions to be evaluated
|
||||||
|
* @return a {@code BranchUtil} instance representing the result of the logical AND operation
|
||||||
|
*/
|
||||||
|
public static BranchUtil and(Boolean... values) {
|
||||||
|
return new BranchUtil(BoolUtil.and(values));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@code BranchUtil} instance to evaluate a logical OR operation on the provided
|
||||||
|
* boolean suppliers.
|
||||||
|
*
|
||||||
|
* @param valueSuppliers the boolean suppliers to be evaluated
|
||||||
|
* @return a {@code BranchUtil} instance representing the result of the
|
||||||
|
* logical OR operation
|
||||||
|
*/
|
||||||
|
public static BranchUtil or(BooleanSupplier... valueSuppliers) {
|
||||||
|
return new BranchUtil(BoolUtil.or(valueSuppliers));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@code BranchUtil} instance to evaluate a logical AND operation on the provided
|
||||||
|
* boolean suppliers.
|
||||||
|
*
|
||||||
|
* @param valueSuppliers the boolean suppliers to be evaluated
|
||||||
|
* @return a {@code BranchUtil} instance representing the result of the
|
||||||
|
* logical AND operation
|
||||||
|
*/
|
||||||
|
public static BranchUtil and(BooleanSupplier... valueSuppliers) {
|
||||||
|
return new BranchUtil(BoolUtil.and(valueSuppliers));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the result of the boolean expressions by executing the appropriate handler based
|
||||||
|
* on the result.
|
||||||
|
* <p>
|
||||||
|
* If the result is {@code true}, the {@code trueSupplier} is executed. If the result is
|
||||||
|
* {@code false} and an {@code falseSupplier} is provided, it is executed.
|
||||||
|
* <p>
|
||||||
|
* Returns the result of the executed supplier.
|
||||||
|
*
|
||||||
|
* @param <T> the type of the result to be handled by the methods
|
||||||
|
* @param trueSupplier the supplier to be executed if the result is {@code true}
|
||||||
|
* @param falseSupplier the supplier to be executed if the result is {@code false} (optional)
|
||||||
|
* @return the result of the executed supplier, or {@code null} if no {@code falseSupplier} is
|
||||||
|
* provided and the result of the evaluation is {@code false}
|
||||||
|
*/
|
||||||
|
public <T> T thenSupply(Supplier<T> trueSupplier, Supplier<T> falseSupplier) {
|
||||||
|
if (this.result && Objects.nonNull(trueSupplier)) {
|
||||||
|
return trueSupplier.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Objects.isNull(falseSupplier)) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return falseSupplier.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the result of the boolean expressions by executing the provided handler if the
|
||||||
|
* result is {@code true}.
|
||||||
|
* <p>
|
||||||
|
* Returns the result of the executed handler.
|
||||||
|
*
|
||||||
|
* @param <T> the type of the result to be handled by the methods
|
||||||
|
* @param trueSupplier the supplier to be executed if the result is {@code true}
|
||||||
|
* @return the result of the executed handler, or {@code null} if result of evaluation
|
||||||
|
* is {@code false}
|
||||||
|
*/
|
||||||
|
public <T> T thenSupply(Supplier<T> trueSupplier) {
|
||||||
|
return thenSupply(trueSupplier, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the result of the boolean expressions by executing the appropriate handler based
|
||||||
|
* on the result.
|
||||||
|
* <p>
|
||||||
|
* If the result is {@code true}, the {@code ifHandler} is executed. If the result is
|
||||||
|
* {@code false} and an {@code elseHandler} is provided, it is executed.
|
||||||
|
*
|
||||||
|
* @param trueHandler the handler to be executed if the result is {@code true}
|
||||||
|
* @param falseHandler the handler to be executed if the result is {@code false} (optional)
|
||||||
|
*/
|
||||||
|
public void then(Runnable trueHandler, Runnable falseHandler) {
|
||||||
|
if (this.result && Objects.nonNull(trueHandler)) {
|
||||||
|
trueHandler.run();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Objects.isNull(falseHandler)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
falseHandler.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the result of the boolean expressions by executing the provided handler if the
|
||||||
|
* result is {@code true}.
|
||||||
|
*
|
||||||
|
* @param trueHandler the handler to be executed if the result is {@code true}
|
||||||
|
*/
|
||||||
|
public void then(Runnable trueHandler) {
|
||||||
|
then(trueHandler, null);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,135 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024-2026 OnixByte
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Objects;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A utility class providing static methods for manipulating collections.
|
||||||
|
*
|
||||||
|
* @author zihluwang
|
||||||
|
* @version 3.0.0
|
||||||
|
*/
|
||||||
|
public final class CollectionUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor to prevent instantiation of this utility class.
|
||||||
|
*/
|
||||||
|
private CollectionUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Splits a collection into a list of sub-collections, each with a maximum size specified by
|
||||||
|
* the caller.
|
||||||
|
* <p>
|
||||||
|
* This method takes an original collection and divides it into smaller sub-collections,
|
||||||
|
* ensuring that each sub-collection contains no more than the specified maximum size. If the
|
||||||
|
* original collection's size is less than or equal to the maximum size, it is returned as a
|
||||||
|
* single sub-collection. The sub-collections are created using the provided collection factory.
|
||||||
|
*
|
||||||
|
* @param <T> the type of elements in the collection
|
||||||
|
* @param <C> the type of the collection, which must extend {@link Collection}
|
||||||
|
* @param originalCollection the collection to be split into sub-collections
|
||||||
|
* @param maxSize the maximum number of elements allowed in each sub-collection
|
||||||
|
* @param collectionFactory a supplier that creates new instances of the sub-collection type
|
||||||
|
* @return a list of sub-collections, each containing up to {@code maxSize} elements
|
||||||
|
* @throws IllegalArgumentException if {@code originalCollection} is {@code null},
|
||||||
|
* {@code maxSize} is less than zero, or
|
||||||
|
* {@code collectionFactory} is {@code null}
|
||||||
|
*/
|
||||||
|
public static <T, C extends Collection<T>> List<C> chunk(
|
||||||
|
C originalCollection,
|
||||||
|
int maxSize,
|
||||||
|
Supplier<C> collectionFactory
|
||||||
|
) {
|
||||||
|
// check inputs
|
||||||
|
if (Objects.isNull(originalCollection)) {
|
||||||
|
throw new IllegalArgumentException("Collection must not be null.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (maxSize <= 0) {
|
||||||
|
throw new IllegalArgumentException("maxSize must greater than 0.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Objects.isNull(collectionFactory)) {
|
||||||
|
throw new IllegalArgumentException("Factory method cannot be null.");
|
||||||
|
}
|
||||||
|
|
||||||
|
var result = new ArrayList<C>();
|
||||||
|
var size = originalCollection.size();
|
||||||
|
|
||||||
|
// if original collection is empty or the size less than maxSize, return it as a single
|
||||||
|
// sub collection
|
||||||
|
if (size <= maxSize) {
|
||||||
|
var singleCollection = collectionFactory.get();
|
||||||
|
singleCollection.addAll(originalCollection);
|
||||||
|
result.add(singleCollection);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// use iterator to split the given collection
|
||||||
|
var iter = originalCollection.iterator();
|
||||||
|
var count = 0;
|
||||||
|
var currentSubCollection = collectionFactory.get();
|
||||||
|
|
||||||
|
while (iter.hasNext()) {
|
||||||
|
var element = iter.next();
|
||||||
|
currentSubCollection.add(element);
|
||||||
|
count++;
|
||||||
|
|
||||||
|
// add sub collection to result when current sub collection reached maxSize or
|
||||||
|
// collection traverse is completed
|
||||||
|
if (count % maxSize == 0 || !iter.hasNext()) {
|
||||||
|
result.add(currentSubCollection);
|
||||||
|
currentSubCollection = collectionFactory.get();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a collection is not null and not empty.
|
||||||
|
*
|
||||||
|
* @param collection the collection to check
|
||||||
|
* @return {@code true} if the collection is not null and not empty, {@code false} otherwise
|
||||||
|
*/
|
||||||
|
public static boolean notEmpty(Collection<?> collection) {
|
||||||
|
return Objects.nonNull(collection) && !collection.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check if a collection is null or empty.
|
||||||
|
*
|
||||||
|
* @param collection the collection to check
|
||||||
|
* @return {@code true} if the collection is null or empty, {@code false} otherwise
|
||||||
|
*/
|
||||||
|
public static boolean isEmpty(Collection<?> collection) {
|
||||||
|
return Objects.isNull(collection) || collection.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024-2026 OnixByte
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import com.onixbyte.common.adapter.ObjectMapAdapter;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link MapUtil} class provides utility methods for converting between objects and maps.
|
||||||
|
* This class leverages the {@link ObjectMapAdapter} interface to perform the conversions.
|
||||||
|
* <p>
|
||||||
|
* The utility methods in this class are useful for scenarios where objects need to be represented
|
||||||
|
* as maps for serialization, deserialization, or other purposes.
|
||||||
|
* </p>
|
||||||
|
*
|
||||||
|
* <p><b>Example usage:</b></p>
|
||||||
|
* <pre>{@code
|
||||||
|
* // User.java
|
||||||
|
* public class User {
|
||||||
|
* private String name;
|
||||||
|
* private int age;
|
||||||
|
*
|
||||||
|
* // getters and setters
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* // UserMapAdapter.java
|
||||||
|
* public class UserMapAdapter implements ObjectMapAdapter<User> {
|
||||||
|
* @Override
|
||||||
|
* public Map<String, Object> toMap(User user) {
|
||||||
|
* Map<String, Object> map = new HashMap<>();
|
||||||
|
* map.put("name", user.getName());
|
||||||
|
* map.put("age", user.getAge());
|
||||||
|
* return map;
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* @Override
|
||||||
|
* public User fromMap(Map<String, Object> map) {
|
||||||
|
* User user = new User();
|
||||||
|
* user.setName((String) map.get("name"));
|
||||||
|
* user.setAge((Integer) map.get("age"));
|
||||||
|
* return user;
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
* public class Example {
|
||||||
|
* public static void main(String[] args) {
|
||||||
|
* User user = new User();
|
||||||
|
* user.setName("John");
|
||||||
|
* user.setAge(30);
|
||||||
|
*
|
||||||
|
* UserMapAdapter adapter = new UserMapAdapter();
|
||||||
|
*
|
||||||
|
* // Convert object to map
|
||||||
|
* Map<String, Object> userMap = MapUtil.objectToMap(user, adapter);
|
||||||
|
* System.out.println(userMap); // Output: {name=John, age=30}
|
||||||
|
*
|
||||||
|
* // Convert map to object
|
||||||
|
* User newUser = MapUtil.mapToObject(userMap, adapter);
|
||||||
|
* System.out.println(newUser.getName()); // Output: John
|
||||||
|
* System.out.println(newUser.getAge()); // Output: 30
|
||||||
|
* }
|
||||||
|
* }
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @author zihluwang
|
||||||
|
* @version 3.0.0
|
||||||
|
*/
|
||||||
|
public final class MapUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor to prevent instantiation of this utility class.
|
||||||
|
*/
|
||||||
|
private MapUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts an object to a map by mapping the field names to their corresponding values.
|
||||||
|
*
|
||||||
|
* @param <T> the type of the object
|
||||||
|
* @param entity the object to be converted to a map
|
||||||
|
* @param adapter adapts the entity for mapping to a map
|
||||||
|
* @return a map representing the fields and their values of the object
|
||||||
|
*/
|
||||||
|
public static <T> Map<String, Object> objectToMap(T entity, ObjectMapAdapter<T> adapter) {
|
||||||
|
return adapter.toMap(entity);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts a map to an object of the specified type by setting the field values using the
|
||||||
|
* map entries.
|
||||||
|
*
|
||||||
|
* @param objectMap the map representing the fields and their values
|
||||||
|
* @param adapter the adapter to execute the setter for the entity
|
||||||
|
* @param <T> the type of the object to be created
|
||||||
|
* @return an object of the specified type with the field values set from the map
|
||||||
|
*/
|
||||||
|
public static <T> T mapToObject(Map<String, Object> objectMap, ObjectMapAdapter<T> adapter) {
|
||||||
|
return adapter.toObject(objectMap);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,207 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2024-2026 OnixByte
|
||||||
|
*
|
||||||
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
* of this software and associated documentation files (the "Software"), to deal
|
||||||
|
* in the Software without restriction, including without limitation the rights
|
||||||
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
* copies of the Software, and to permit persons to whom the Software is
|
||||||
|
* furnished to do so, subject to the following conditions:
|
||||||
|
*
|
||||||
|
* The above copyright notice and this permission notice shall be included in all
|
||||||
|
* copies or substantial portions of the Software.
|
||||||
|
*
|
||||||
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
* SOFTWARE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code RangeUtil} is a utility class providing methods for generating streams of integers that
|
||||||
|
* emulate the behaviour of Python's {@code range} function.
|
||||||
|
* <p>
|
||||||
|
* This class offers static methods to create ranges with various configurations. These methods
|
||||||
|
* leverage the {@link IntStream} to provide efficient and versatile integer sequences.
|
||||||
|
*
|
||||||
|
* @author zihluwang
|
||||||
|
* @version 3.0.0
|
||||||
|
* @see IntStream
|
||||||
|
*/
|
||||||
|
public final class RangeUtil {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor to prevent instantiation of this utility class.
|
||||||
|
*/
|
||||||
|
private RangeUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a stream of integers starting from {@code 0} up to the specified {@code end} value.
|
||||||
|
* <p>
|
||||||
|
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
|
||||||
|
* further processing.
|
||||||
|
* <p>
|
||||||
|
* <b>Example Usage:</b>
|
||||||
|
* <pre>{@code
|
||||||
|
* RangeUtil.range(5).forEach(System.out::println);
|
||||||
|
*
|
||||||
|
* // Output:
|
||||||
|
* // 0
|
||||||
|
* // 1
|
||||||
|
* // 2
|
||||||
|
* // 3
|
||||||
|
* // 4
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param end upper-bound of the range (exclusive)
|
||||||
|
* @return an {@code IntStream} of integers from {@code 0} (inclusive) to
|
||||||
|
* {@code end} (exclusive)
|
||||||
|
* @throws IllegalArgumentException if the given {@code end} value is less equal to 0
|
||||||
|
* @see IntStream
|
||||||
|
*/
|
||||||
|
public static IntStream range(int end) {
|
||||||
|
if (end <= 0) {
|
||||||
|
throw new IllegalArgumentException("Parameter [end] should not be less than or equal to 0, provided: " +
|
||||||
|
end);
|
||||||
|
}
|
||||||
|
return IntStream.range(0, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a stream of integers starting from the specified {@code start} value up to the
|
||||||
|
* specified {@code end} value.
|
||||||
|
* <p>
|
||||||
|
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
|
||||||
|
* further processing.
|
||||||
|
* <p>
|
||||||
|
* If {@code start} is less than {@code end}, an ascending range (exclusive of {@code end})
|
||||||
|
* is generated. If {@code start} is greater than {@code end}, a descending range (exclusive
|
||||||
|
* of {@code end}) is generated. If {@code start} equals {@code end}, an empty stream
|
||||||
|
* is returned.
|
||||||
|
* <p>
|
||||||
|
* <b>Example Usage:</b>
|
||||||
|
* <pre>{@code
|
||||||
|
* RangeUtil.range(3, 8).forEach(System.out::println);
|
||||||
|
*
|
||||||
|
* // Output:
|
||||||
|
* // 3
|
||||||
|
* // 4
|
||||||
|
* // 5
|
||||||
|
* // 6
|
||||||
|
* // 7
|
||||||
|
*
|
||||||
|
* RangeUtil.range(8, 3).forEach(System.out::println);
|
||||||
|
*
|
||||||
|
* // Output:
|
||||||
|
* // 8
|
||||||
|
* // 7
|
||||||
|
* // 6
|
||||||
|
* // 5
|
||||||
|
* // 4
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param start the starting value of the range (inclusive)
|
||||||
|
* @param end upper-bound of the range (exclusive)
|
||||||
|
* @return an {@code IntStream} of integers in ascending or descending order, exclusive
|
||||||
|
* of {@code end}
|
||||||
|
* @see IntStream
|
||||||
|
*/
|
||||||
|
public static IntStream range(int start, int end) {
|
||||||
|
if (start == end) {
|
||||||
|
return IntStream.empty();
|
||||||
|
}
|
||||||
|
if (start < end) {
|
||||||
|
return IntStream.range(start, end);
|
||||||
|
} else {
|
||||||
|
// Descending range (exclusive of end)
|
||||||
|
return IntStream.iterate(start, (n) -> n > end, (n) -> n - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a stream of integers starting from the specified {@code start} value up to the
|
||||||
|
* specified {@code end} value.
|
||||||
|
* <p>
|
||||||
|
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
|
||||||
|
* further processing.
|
||||||
|
* <p>
|
||||||
|
* The range includes both {@code start} and {@code end}.
|
||||||
|
* <p>
|
||||||
|
* <b>Example Usage:</b>
|
||||||
|
* <pre>{@code
|
||||||
|
* RangeUtil.rangeClosed(3, 8).forEach(System.out::println);
|
||||||
|
*
|
||||||
|
* // Output:
|
||||||
|
* // 3
|
||||||
|
* // 4
|
||||||
|
* // 5
|
||||||
|
* // 6
|
||||||
|
* // 7
|
||||||
|
* // 8
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param start the starting value of the range (inclusive)
|
||||||
|
* @param end upper-bound of the range (inclusive)
|
||||||
|
* @return an {@code IntStream} of integers from {@code start} to {@code end} inclusive
|
||||||
|
* @see IntStream
|
||||||
|
*/
|
||||||
|
public static IntStream rangeClosed(int start, int end) {
|
||||||
|
return IntStream.rangeClosed(start, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generates a stream of integers starting from the specified {@code start} value, incremented
|
||||||
|
* by the specified {@code step}, up to the specified {@code end} value.
|
||||||
|
* <p>
|
||||||
|
* It creates a sequential, ordered {@code IntStream} that can be used for iteration or
|
||||||
|
* further processing.
|
||||||
|
* <p>
|
||||||
|
* The stream excludes the {@code end} value.
|
||||||
|
* <p>
|
||||||
|
* <b>Example Usage:</b>
|
||||||
|
* <pre>{@code
|
||||||
|
* RangeUtil.range(3, 10, 2).forEach(System.out::println);
|
||||||
|
*
|
||||||
|
* // Output:
|
||||||
|
* // 3
|
||||||
|
* // 5
|
||||||
|
* // 7
|
||||||
|
* // 9
|
||||||
|
*
|
||||||
|
* RangeUtil.range(10, 3, -2).forEach(System.out::println);
|
||||||
|
*
|
||||||
|
* // Output:
|
||||||
|
* // 10
|
||||||
|
* // 8
|
||||||
|
* // 6
|
||||||
|
* // 4
|
||||||
|
* }</pre>
|
||||||
|
*
|
||||||
|
* @param start the starting value of the range (inclusive)
|
||||||
|
* @param end upper-bound of the range (exclusive)
|
||||||
|
* @param step the increment or decrement between each value (non-zero)
|
||||||
|
* @return an {@code IntStream} of integers from {@code start} to {@code end} exclusive stepping
|
||||||
|
* by {@code step}
|
||||||
|
* @throws IllegalArgumentException if {@code step} is zero or if {@code start} and {@code end}
|
||||||
|
* are inconsistent with the direction imposed by {@code step}
|
||||||
|
* @see IntStream
|
||||||
|
*/
|
||||||
|
public static IntStream range(int start, int end, int step) {
|
||||||
|
if (step == 0) {
|
||||||
|
throw new IllegalArgumentException("Step value must not be zero.");
|
||||||
|
}
|
||||||
|
if ((step > 0 && start >= end) || (step < 0 && start <= end)) {
|
||||||
|
throw new IllegalArgumentException("Range parameters are inconsistent with the step value.");
|
||||||
|
}
|
||||||
|
return IntStream.iterate(start, (n) -> step > 0 ? n < end : n > end, (n) -> n + step);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!--
|
||||||
|
~ Copyright (c) 2024-2026 OnixByte
|
||||||
|
~
|
||||||
|
~ Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
~ of this software and associated documentation files (the "Software"), to deal
|
||||||
|
~ in the Software without restriction, including without limitation the rights
|
||||||
|
~ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
~ copies of the Software, and to permit persons to whom the Software is
|
||||||
|
~ furnished to do so, subject to the following conditions:
|
||||||
|
~
|
||||||
|
~ The above copyright notice and this permission notice shall be included in all
|
||||||
|
~ copies or substantial portions of the Software.
|
||||||
|
~
|
||||||
|
~ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
~ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
~ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
~ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
~ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
~ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
~ SOFTWARE.
|
||||||
|
-->
|
||||||
|
|
||||||
|
<configuration>
|
||||||
|
<property name="COLOURFUL_OUTPUT"
|
||||||
|
value="%black(%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK}) %highlight(%-5level) %black(---) %black([%10.10t]) %cyan(%-20.20logger{20}) %black(:) %msg%n"/>
|
||||||
|
<property name="STANDARD_OUTPUT"
|
||||||
|
value="%date{'dd MMM, yyyy HH:mm:ss', Asia/Hong_Kong, en-UK} %-5level %black(---) [%10.10t] %-20.20logger{20} : %msg%n"/>
|
||||||
|
|
||||||
|
<statusListener class="ch.qos.logback.core.status.NopStatusListener"/>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||||
|
<pattern>${COLOURFUL_OUTPUT}</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT"/>
|
||||||
|
</root>
|
||||||
|
</configuration>
|
||||||
@@ -0,0 +1,79 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.GeneralSecurityException;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class AesUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEncryptAndDecryptByte() throws GeneralSecurityException {
|
||||||
|
byte[] secretKey = "43f72073956d4c81".getBytes(StandardCharsets.UTF_8);
|
||||||
|
byte[] originalData = "Hello World".getBytes(StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
byte[] encryptedData = AesUtil.encrypt(originalData, secretKey);
|
||||||
|
assertNotNull(encryptedData);
|
||||||
|
|
||||||
|
byte[] decryptedData = AesUtil.decrypt(encryptedData, secretKey);
|
||||||
|
assertNotNull(decryptedData);
|
||||||
|
|
||||||
|
assertArrayEquals(originalData, decryptedData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEncryptAndDecryptString() throws GeneralSecurityException {
|
||||||
|
var secret = "43f72073956d4c81";
|
||||||
|
var originalData = "Hello World";
|
||||||
|
|
||||||
|
var encryptedData = AesUtil.encrypt(originalData, secret);
|
||||||
|
assertNotNull(encryptedData);
|
||||||
|
assertNotEquals(originalData, encryptedData);
|
||||||
|
|
||||||
|
var decryptedData = AesUtil.decrypt(encryptedData, secret);
|
||||||
|
assertNotNull(decryptedData);
|
||||||
|
assertEquals(originalData, decryptedData);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEncryptWithWrongKeyFails() throws GeneralSecurityException {
|
||||||
|
var secret = "43f72073956d4c81";
|
||||||
|
var wrongSecret = "0000000000000000";
|
||||||
|
var originalData = "Hello World";
|
||||||
|
|
||||||
|
var encryptedData = AesUtil.encrypt(originalData.getBytes(StandardCharsets.UTF_8),
|
||||||
|
secret.getBytes(StandardCharsets.UTF_8));
|
||||||
|
assertNotNull(encryptedData);
|
||||||
|
|
||||||
|
// When decrypting with the wrong key, a BadPaddingException or IllegalBlockSizeException is expected to be thrown
|
||||||
|
assertThrows(GeneralSecurityException.class, () -> {
|
||||||
|
AesUtil.decrypt(encryptedData, wrongSecret.getBytes(StandardCharsets.UTF_8));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testGenerateRandomSecret() {
|
||||||
|
var randomSecret = AesUtil.generateRandomSecret();
|
||||||
|
assertNotNull(randomSecret);
|
||||||
|
assertEquals(16, randomSecret.length());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,100 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
public class Base64UtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEncodeAndDecodeWithUtf8() {
|
||||||
|
var original = "Hello, Base64!";
|
||||||
|
var encoded = Base64Util.encode(original);
|
||||||
|
assertNotNull(encoded);
|
||||||
|
assertNotEquals(original, encoded);
|
||||||
|
|
||||||
|
var decoded = Base64Util.decode(encoded);
|
||||||
|
assertNotNull(decoded);
|
||||||
|
assertEquals(original, decoded);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEncodeAndDecodeWithCharset() {
|
||||||
|
var original = "编码测试"; // Some unicode characters (Chinese)
|
||||||
|
var charset = StandardCharsets.UTF_8;
|
||||||
|
|
||||||
|
var encoded = Base64Util.encode(original, charset);
|
||||||
|
assertNotNull(encoded);
|
||||||
|
assertNotEquals(original, encoded);
|
||||||
|
|
||||||
|
var decoded = Base64Util.decode(encoded, charset);
|
||||||
|
assertNotNull(decoded);
|
||||||
|
assertEquals(original, decoded);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEncodeUrlComponentsAndDecodeWithUtf8() {
|
||||||
|
var original = "This is a test for URL-safe Base64 encoding+!";
|
||||||
|
|
||||||
|
var encodedUrl = Base64Util.encodeUrlComponents(original);
|
||||||
|
assertNotNull(encodedUrl);
|
||||||
|
assertNotEquals(original, encodedUrl);
|
||||||
|
// URL-safe encoding should not contain '+' or '/' characters
|
||||||
|
assertFalse(encodedUrl.contains("+"));
|
||||||
|
assertFalse(encodedUrl.contains("/"));
|
||||||
|
|
||||||
|
var decodedUrl = Base64Util.decodeUrlComponents(encodedUrl);
|
||||||
|
assertNotNull(decodedUrl);
|
||||||
|
assertEquals(original, decodedUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEncodeUrlComponentsAndDecodeWithCharset() {
|
||||||
|
var original = "测试 URL 安全编码"; // Unicode string
|
||||||
|
var charset = StandardCharsets.UTF_8;
|
||||||
|
|
||||||
|
var encodedUrl = Base64Util.encodeUrlComponents(original, charset);
|
||||||
|
assertNotNull(encodedUrl);
|
||||||
|
assertNotEquals(original, encodedUrl);
|
||||||
|
|
||||||
|
var decodedUrl = Base64Util.decodeUrlComponents(encodedUrl, charset);
|
||||||
|
assertNotNull(decodedUrl);
|
||||||
|
assertEquals(original, decodedUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEncodeAndDecodeEmptyString() {
|
||||||
|
var original = "";
|
||||||
|
|
||||||
|
var encoded = Base64Util.encode(original);
|
||||||
|
assertNotNull(encoded);
|
||||||
|
assertEquals("", Base64Util.decode(encoded));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEncodeAndDecodeNullSafety() {
|
||||||
|
// Since Base64Util does not explicitly handle null, the test expects NPE if null is input
|
||||||
|
assertThrows(NullPointerException.class, () -> Base64Util.encode(null));
|
||||||
|
assertThrows(NullPointerException.class, () -> Base64Util.decode(null));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,137 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class BoolUtilTest {
|
||||||
|
|
||||||
|
// Tests for and(Boolean... values)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void and_AllTrueValues_ReturnsTrue() {
|
||||||
|
assertTrue(BoolUtil.and(true, true, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void and_SomeFalseValues_ReturnsFalse() {
|
||||||
|
assertFalse(BoolUtil.and(true, false, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void and_AllFalseValues_ReturnsFalse() {
|
||||||
|
assertFalse(BoolUtil.and(false, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void and_WithNullValues_IgnoresNulls() {
|
||||||
|
assertTrue(BoolUtil.and(true, null, true));
|
||||||
|
assertFalse(BoolUtil.and(true, null, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void and_AllNullValues_ReturnsTrue() {
|
||||||
|
// Stream after filtering null is empty, allMatch on empty returns true
|
||||||
|
assertTrue(BoolUtil.and((Boolean) null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests for and(BooleanSupplier... valueSuppliers)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void and_AllSuppliersTrue_ReturnsTrue() {
|
||||||
|
BooleanSupplier trueSupplier = () -> true;
|
||||||
|
BooleanSupplier falseSupplier = () -> false;
|
||||||
|
|
||||||
|
assertTrue(BoolUtil.and(trueSupplier, trueSupplier));
|
||||||
|
assertFalse(BoolUtil.and(trueSupplier, falseSupplier));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void and_WithNullSuppliers_IgnoresNull() {
|
||||||
|
BooleanSupplier trueSupplier = () -> true;
|
||||||
|
|
||||||
|
assertTrue(BoolUtil.and(trueSupplier, null, trueSupplier));
|
||||||
|
assertFalse(BoolUtil.and(trueSupplier, null, () -> false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void and_AllNullSuppliers_ReturnsTrue() {
|
||||||
|
assertTrue(BoolUtil.and((BooleanSupplier) null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Tests for or(Boolean... values)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void or_AllTrueValues_ReturnsTrue() {
|
||||||
|
assertTrue(BoolUtil.or(true, true, true));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void or_SomeTrueValues_ReturnsTrue() {
|
||||||
|
assertTrue(BoolUtil.or(false, true, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void or_AllFalseValues_ReturnsFalse() {
|
||||||
|
assertFalse(BoolUtil.or(false, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void or_WithNullValues_IgnoresNull() {
|
||||||
|
assertTrue(BoolUtil.or(false, null, true));
|
||||||
|
assertFalse(BoolUtil.or(false, null, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void or_AllNullValues_ReturnsFalse() {
|
||||||
|
// Stream after filtering null is empty, anyMatch on empty returns false
|
||||||
|
assertFalse(BoolUtil.or((Boolean) null, null));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests for or(BooleanSupplier... valueSuppliers)
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void or_AllSuppliersTrue_ReturnsTrue() {
|
||||||
|
BooleanSupplier trueSupplier = () -> true;
|
||||||
|
BooleanSupplier falseSupplier = () -> false;
|
||||||
|
|
||||||
|
assertTrue(BoolUtil.or(trueSupplier, trueSupplier));
|
||||||
|
assertTrue(BoolUtil.or(falseSupplier, trueSupplier));
|
||||||
|
assertFalse(BoolUtil.or(falseSupplier, falseSupplier));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void or_WithNullSuppliers_IgnoresNull() {
|
||||||
|
BooleanSupplier trueSupplier = () -> true;
|
||||||
|
BooleanSupplier falseSupplier = () -> false;
|
||||||
|
|
||||||
|
assertTrue(BoolUtil.or(falseSupplier, null, trueSupplier));
|
||||||
|
assertFalse(BoolUtil.or(falseSupplier, null, falseSupplier));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void or_AllNullSuppliers_ReturnsFalse() {
|
||||||
|
assertFalse(BoolUtil.or((BooleanSupplier) null, null));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,161 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import java.util.function.BooleanSupplier;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class BranchUtilTest {
|
||||||
|
|
||||||
|
// Test the static methods or(Boolean... values) and and(Boolean... values)
|
||||||
|
@Test
|
||||||
|
void testOrWithBooleanValues() {
|
||||||
|
BranchUtil trueResult = BranchUtil.or(true, false, false);
|
||||||
|
assertNotNull(trueResult);
|
||||||
|
|
||||||
|
BranchUtil falseResult = BranchUtil.or(false, false, false);
|
||||||
|
assertNotNull(falseResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAndWithBooleanValues() {
|
||||||
|
BranchUtil trueResult = BranchUtil.and(true, true, true);
|
||||||
|
assertNotNull(trueResult);
|
||||||
|
|
||||||
|
BranchUtil falseResult = BranchUtil.and(true, false, true);
|
||||||
|
assertNotNull(falseResult);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test the static methods or(BooleanSupplier... valueSuppliers) and and(BooleanSupplier... valueSuppliers)
|
||||||
|
@Test
|
||||||
|
void testOrWithBooleanSuppliers() {
|
||||||
|
BooleanSupplier trueSupplier = () -> true;
|
||||||
|
BooleanSupplier falseSupplier = () -> false;
|
||||||
|
|
||||||
|
BranchUtil trueResult = BranchUtil.or(falseSupplier, trueSupplier);
|
||||||
|
|
||||||
|
BranchUtil falseResult = BranchUtil.or(falseSupplier, falseSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testAndWithBooleanSuppliers() {
|
||||||
|
BooleanSupplier trueSupplier = () -> true;
|
||||||
|
BooleanSupplier falseSupplier = () -> false;
|
||||||
|
|
||||||
|
BranchUtil trueResult = BranchUtil.and(trueSupplier, trueSupplier);
|
||||||
|
|
||||||
|
BranchUtil falseResult = BranchUtil.and(trueSupplier, falseSupplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test thenSupply(T, T)
|
||||||
|
@Test
|
||||||
|
void testThenSupplyBothSuppliers_ResultTrue() {
|
||||||
|
BranchUtil b = BranchUtil.and(true);
|
||||||
|
String trueVal = "yes";
|
||||||
|
String falseVal = "no";
|
||||||
|
|
||||||
|
String result = b.thenSupply(() -> trueVal, () -> falseVal);
|
||||||
|
assertEquals(trueVal, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testThenSupplyBothSuppliers_ResultFalse_WithFalseSupplier() {
|
||||||
|
BranchUtil b = BranchUtil.and(false);
|
||||||
|
String trueVal = "yes";
|
||||||
|
String falseVal = "no";
|
||||||
|
|
||||||
|
String result = b.thenSupply(() -> trueVal, () -> falseVal);
|
||||||
|
assertEquals(falseVal, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testThenSupplyBothSuppliers_ResultFalse_NoFalseSupplier() {
|
||||||
|
BranchUtil b = BranchUtil.and(false);
|
||||||
|
String trueVal = "yes";
|
||||||
|
|
||||||
|
String result = b.thenSupply(() -> trueVal, null);
|
||||||
|
assertNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testThenSupplySingleTrueSupplier_ResultTrue() {
|
||||||
|
BranchUtil b = BranchUtil.and(true);
|
||||||
|
String trueVal = "success";
|
||||||
|
|
||||||
|
String result = b.thenSupply(() -> trueVal);
|
||||||
|
assertEquals(trueVal, result);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testThenSupplySingleTrueSupplier_ResultFalse() {
|
||||||
|
BranchUtil b = BranchUtil.and(false);
|
||||||
|
String trueVal = "success";
|
||||||
|
|
||||||
|
String result = b.thenSupply(() -> trueVal);
|
||||||
|
assertNull(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test then(Runnable, Runnable)
|
||||||
|
@Test
|
||||||
|
void testThenWithBothHandlers_ResultTrue() {
|
||||||
|
BranchUtil b = BranchUtil.and(true);
|
||||||
|
AtomicBoolean trueRun = new AtomicBoolean(false);
|
||||||
|
AtomicBoolean falseRun = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
b.then(() -> trueRun.set(true), () -> falseRun.set(true));
|
||||||
|
|
||||||
|
assertTrue(trueRun.get());
|
||||||
|
assertFalse(falseRun.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testThenWithBothHandlers_ResultFalse() {
|
||||||
|
BranchUtil b = BranchUtil.and(false);
|
||||||
|
AtomicBoolean trueRun = new AtomicBoolean(false);
|
||||||
|
AtomicBoolean falseRun = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
b.then(() -> trueRun.set(true), () -> falseRun.set(true));
|
||||||
|
|
||||||
|
assertFalse(trueRun.get());
|
||||||
|
assertTrue(falseRun.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testThenWithOnlyTrueHandler_ResultTrue() {
|
||||||
|
BranchUtil b = BranchUtil.and(true);
|
||||||
|
AtomicBoolean trueRun = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
b.then(() -> trueRun.set(true));
|
||||||
|
|
||||||
|
assertTrue(trueRun.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testThenWithOnlyTrueHandler_ResultFalse() {
|
||||||
|
BranchUtil b = BranchUtil.and(false);
|
||||||
|
AtomicBoolean trueRun = new AtomicBoolean(false);
|
||||||
|
|
||||||
|
b.then(() -> trueRun.set(true));
|
||||||
|
|
||||||
|
assertFalse(trueRun.get());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,112 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class CollectionUtilTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chunk_NullOriginalCollection_ThrowsException() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> CollectionUtil.chunk(null, 3, ArrayList::new));
|
||||||
|
assertEquals("Collection must not be null.", ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chunk_NegativeMaxSize_ThrowsException() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> CollectionUtil.chunk(List.of(1, 2), -1, ArrayList::new));
|
||||||
|
assertEquals("maxSize must greater than 0.", ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chunk_NullCollectionFactory_ThrowsException() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> CollectionUtil.chunk(List.of(1, 2), 2, null));
|
||||||
|
assertEquals("Factory method cannot be null.", ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chunk_EmptyCollection_ReturnsOneEmptySubCollection() {
|
||||||
|
List<List<Integer>> chunks = CollectionUtil.chunk(Collections.emptyList(), 3, ArrayList::new);
|
||||||
|
assertEquals(1, chunks.size());
|
||||||
|
assertTrue(chunks.get(0).isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chunk_CollectionSizeLessThanMaxSize_ReturnsOneSubCollectionWithAllElements() {
|
||||||
|
List<Integer> list = List.of(1, 2);
|
||||||
|
List<List<Integer>> chunks = CollectionUtil.chunk(list, 5, ArrayList::new);
|
||||||
|
assertEquals(1, chunks.size());
|
||||||
|
assertEquals(list, chunks.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chunk_CollectionSizeEqualMaxSize_ReturnsOneSubCollectionWithAllElements() {
|
||||||
|
List<Integer> list = List.of(1, 2, 3);
|
||||||
|
List<List<Integer>> chunks = CollectionUtil.chunk(list, 3, ArrayList::new);
|
||||||
|
assertEquals(1, chunks.size());
|
||||||
|
assertEquals(list, chunks.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chunk_CollectionSizeGreaterThanMaxSize_ReturnsMultipleSubCollections() {
|
||||||
|
List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
|
||||||
|
int maxSize = 3;
|
||||||
|
List<List<Integer>> chunks = CollectionUtil.chunk(list, maxSize, ArrayList::new);
|
||||||
|
|
||||||
|
// Expect 3 subcollections: [1,2,3], [4,5,6], [7]
|
||||||
|
assertEquals(3, chunks.size());
|
||||||
|
assertEquals(List.of(1, 2, 3), chunks.get(0));
|
||||||
|
assertEquals(List.of(4, 5, 6), chunks.get(1));
|
||||||
|
assertEquals(List.of(7), chunks.get(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chunk_UsesDifferentCollectionTypeAsSubCollections() {
|
||||||
|
LinkedList<Integer> list = new LinkedList<>(List.of(1, 2, 3, 4));
|
||||||
|
Supplier<LinkedList<Integer>> factory = LinkedList::new;
|
||||||
|
List<LinkedList<Integer>> chunks = CollectionUtil.chunk(list, 2, factory);
|
||||||
|
assertEquals(2, chunks.size());
|
||||||
|
assertInstanceOf(LinkedList.class, chunks.get(0));
|
||||||
|
assertInstanceOf(LinkedList.class, chunks.get(1));
|
||||||
|
assertEquals(List.of(1, 2), chunks.get(0));
|
||||||
|
assertEquals(List.of(3, 4), chunks.get(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chunk_CollectionWithOneElementAndMaxSizeOne_ReturnsOneSubCollection() {
|
||||||
|
List<String> list = List.of("a");
|
||||||
|
List<List<String>> chunks = CollectionUtil.chunk(list, 1, ArrayList::new);
|
||||||
|
assertEquals(1, chunks.size());
|
||||||
|
assertEquals(list, chunks.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void chunk_MaxSizeZero_ThrowsException() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> CollectionUtil.chunk(List.of(1), 0, ArrayList::new));
|
||||||
|
assertEquals("maxSize must greater than 0.", ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,131 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024-2025 OnixByte.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package com.onixbyte.common.util;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class RangeUtilTest {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests generating ascending range from 0 up to end (exclusive).
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangeEndValid() {
|
||||||
|
int[] expected = {0, 1, 2, 3, 4};
|
||||||
|
assertArrayEquals(expected, RangeUtil.range(5).toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that range(end) throws IllegalArgumentException for end less than or equal to zero.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangeEndInvalidThrows() {
|
||||||
|
IllegalArgumentException ex1 = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> RangeUtil.range(0));
|
||||||
|
assertTrue(ex1.getMessage().contains("should not be less than or equal to 0"));
|
||||||
|
|
||||||
|
IllegalArgumentException ex2 = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> RangeUtil.range(-3));
|
||||||
|
assertTrue(ex2.getMessage().contains("should not be less than or equal to 0"));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests ascending range where start is less than end.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangeStartEndAscending() {
|
||||||
|
int[] expected = {3, 4, 5, 6, 7};
|
||||||
|
assertArrayEquals(expected, RangeUtil.range(3, 8).toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests descending range where start is greater than end.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangeStartEndDescending() {
|
||||||
|
int[] expected = {8, 7, 6, 5, 4};
|
||||||
|
assertArrayEquals(expected, RangeUtil.range(8, 3).toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests empty stream when start equals end.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangeStartEqualsEndReturnsEmpty() {
|
||||||
|
assertEquals(0, RangeUtil.range(5, 5).count());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that rangeClosed generates inclusive range in ascending order.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangeClosedAscending() {
|
||||||
|
int[] expected = {3, 4, 5, 6, 7, 8};
|
||||||
|
assertArrayEquals(expected, RangeUtil.rangeClosed(3, 8).toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests range method with positive step generating ascending sequence.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangeWithPositiveStep() {
|
||||||
|
int[] expected = {2, 4, 6, 8};
|
||||||
|
assertArrayEquals(expected, RangeUtil.range(2, 10, 2).toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests range method with negative step generating descending sequence.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangeWithNegativeStep() {
|
||||||
|
int[] expected = {10, 7, 4, 1};
|
||||||
|
assertArrayEquals(expected, RangeUtil.range(10, 0, -3).toArray());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that passing zero step throws IllegalArgumentException.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangeStepZeroThrows() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> RangeUtil.range(0, 10, 0));
|
||||||
|
assertEquals("Step value must not be zero.", ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that range with positive step but invalid start/end throws IllegalArgumentException.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangePositiveStepInvalidRangeThrows() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> RangeUtil.range(10, 5, 1));
|
||||||
|
assertEquals("Range parameters are inconsistent with the step value.", ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests that range with negative step but invalid start/end throws IllegalArgumentException.
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
void testRangeNegativeStepInvalidRangeThrows() {
|
||||||
|
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
|
||||||
|
() -> RangeUtil.range(5, 10, -1));
|
||||||
|
assertEquals("Range parameters are inconsistent with the step value.", ex.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user