├── .gitattributes ├── ballerina ├── icon.png ├── README.md ├── Ballerina.toml ├── random_errors.bal ├── Dependencies.toml ├── natives.bal ├── build.gradle └── tests │ └── random.bal ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── codecov.yml ├── .github ├── pull_request_template.md ├── CODEOWNERS └── workflows │ ├── trivy-scan.yml │ ├── build-timestamped-master.yml │ ├── pull-request.yml │ ├── publish-release.yml │ ├── fossa_scan.yml │ ├── central-publish.yml │ ├── build-with-bal-test-graalvm.yml │ └── update_specs.yml ├── gradle.properties ├── .gitignore ├── spotbugs-exclude.xml ├── changelog.md ├── settings.gradle ├── docs └── spec │ └── spec.md ├── gradlew.bat ├── README.md ├── gradlew └── LICENSE /.gitattributes: -------------------------------------------------------------------------------- 1 | # Ensure all Java files use LF. 2 | *.java eol=lf 3 | -------------------------------------------------------------------------------- /ballerina/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ballerina-platform/module-ballerina-random/HEAD/ballerina/icon.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ballerina-platform/module-ballerina-random/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | coverage: 2 | precision: 2 3 | round: down 4 | range: "60...80" 5 | status: 6 | project: 7 | default: 8 | target: 80 -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | ## Purpose 2 | 3 | ## Examples 4 | 5 | ## Checklist 6 | - [ ] Linked to an issue 7 | - [ ] Updated the changelog 8 | - [ ] Added tests 9 | - [ ] Updated the spec 10 | - [ ] Checked native-image compatibility 11 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.11.1-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # Lines starting with '#' are comments. 2 | # Each line is a file pattern followed by one or more owners. 3 | 4 | # See: https://help.github.com/articles/about-codeowners/ 5 | 6 | # These owners will be the default owners for everything in the repo. 7 | * @niveathika @aashikam @RDPerera @shafreenAnfar 8 | -------------------------------------------------------------------------------- /ballerina/README.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | This module provides APIs to generate pseudo-random numbers. 4 | 5 | The generated values are selected pseudo-randomly with uniform distribution from a particular range. 6 | 7 | For information on the operations, which you can perform with the `random` module, see the below **Functions**. 8 | -------------------------------------------------------------------------------- /ballerina/Ballerina.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | org = "ballerina" 3 | name = "random" 4 | version = "1.7.0" 5 | authors = ["Ballerina"] 6 | keywords = ["pseudo-random"] 7 | repository = "https://github.com/ballerina-platform/module-ballerina-random" 8 | icon = "icon.png" 9 | license = ["Apache-2.0"] 10 | distribution = "2201.12.0" 11 | 12 | [platform.java21] 13 | graalvmCompatible = true 14 | -------------------------------------------------------------------------------- /.github/workflows/trivy-scan.yml: -------------------------------------------------------------------------------- 1 | name: Trivy 2 | 3 | on: 4 | workflow_dispatch: 5 | schedule: 6 | - cron: "30 20 * * *" 7 | 8 | jobs: 9 | call_workflow: 10 | name: Run Trivy Scan Workflow 11 | if: ${{ github.repository_owner == 'ballerina-platform' }} 12 | uses: ballerina-platform/ballerina-library/.github/workflows/trivy-scan-template.yml@main 13 | secrets: inherit 14 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | org.gradle.caching=true 2 | group=io.ballerina.stdlib 3 | version=1.7.1-SNAPSHOT 4 | 5 | checkstylePluginVersion=10.12.0 6 | spotbugsPluginVersion=6.0.18 7 | shadowJarPluginVersion=7.1.2 8 | downloadPluginVersion=5.4.0 9 | releasePluginVersion=2.6.0 10 | ballerinaGradlePluginVersion=2.3.0 11 | 12 | ballerinaLangVersion=2201.12.0 13 | 14 | #stdlib dependencies 15 | stdlibTimeVersion=2.7.0 16 | -------------------------------------------------------------------------------- /.github/workflows/build-timestamped-master.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | push: 5 | branches: 6 | - main 7 | paths-ignore: 8 | - "*.md" 9 | workflow_dispatch: 10 | 11 | jobs: 12 | call_workflow: 13 | name: Run Build Workflow 14 | if: ${{ github.repository_owner == 'ballerina-platform' }} 15 | uses: ballerina-platform/ballerina-library/.github/workflows/build-timestamp-master-template.yml@main 16 | secrets: inherit 17 | -------------------------------------------------------------------------------- /.github/workflows/pull-request.yml: -------------------------------------------------------------------------------- 1 | name: PR Build 2 | 3 | concurrency: 4 | group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.run_id }} 5 | cancel-in-progress: true 6 | 7 | on: [pull_request] 8 | 9 | jobs: 10 | call_workflow: 11 | name: Run PR Build Workflow 12 | if: ${{ github.repository_owner == 'ballerina-platform' }} 13 | uses: ballerina-platform/ballerina-library/.github/workflows/pull-request-build-template.yml@main 14 | -------------------------------------------------------------------------------- /.github/workflows/publish-release.yml: -------------------------------------------------------------------------------- 1 | name: Publish Release 2 | 3 | on: 4 | workflow_dispatch: 5 | repository_dispatch: 6 | types: [stdlib-release-pipeline] 7 | 8 | jobs: 9 | call_workflow: 10 | name: Run Release Workflow 11 | if: ${{ github.repository_owner == 'ballerina-platform' }} 12 | uses: ballerina-platform/ballerina-library/.github/workflows/release-package-template.yml@main 13 | secrets: inherit 14 | with: 15 | package-name: random 16 | package-org: ballerina 17 | -------------------------------------------------------------------------------- /.github/workflows/fossa_scan.yml: -------------------------------------------------------------------------------- 1 | name: Fossa Scan 2 | on: 3 | workflow_dispatch: 4 | schedule: 5 | - cron: '30 18 * * *' # 00:00 in LK time (GMT+5:30) 6 | jobs: 7 | fossa-scan: 8 | runs-on: ubuntu-latest 9 | steps: 10 | - uses: actions/checkout@v3 11 | - uses: fossas/fossa-action@main 12 | env: 13 | packageUser: ${{ secrets.BALLERINA_BOT_USERNAME }} 14 | packagePAT: ${{ secrets.BALLERINA_BOT_TOKEN }} 15 | with: 16 | api-key: ${{secrets.FOSSA_APIKEY}} 17 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # BlueJ files 8 | *.ctxt 9 | 10 | # Mobile Tools for Java (J2ME) 11 | .mtj.tmp/ 12 | 13 | # Package Files # 14 | !gradle/wrapper/gradle-wrapper.jar 15 | *.jar 16 | *.war 17 | *.nar 18 | *.ear 19 | *.zip 20 | *.tar.gz 21 | *.rar 22 | 23 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 24 | hs_err_pid* 25 | 26 | build 27 | .gradle/ 28 | target 29 | # IDEA Files 30 | .idea/ 31 | *.iml 32 | *.ipr 33 | *.iws 34 | 35 | # MacOS 36 | *.DS_Store 37 | 38 | # Ballerina 39 | velocity.log* 40 | *Ballerina.lock 41 | -------------------------------------------------------------------------------- /.github/workflows/central-publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish to the Ballerina central 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | environment: 7 | type: choice 8 | description: Select Environment 9 | required: true 10 | options: 11 | - DEV CENTRAL 12 | - STAGE CENTRAL 13 | 14 | jobs: 15 | call_workflow: 16 | name: Run Central Publish Workflow 17 | if: ${{ github.repository_owner == 'ballerina-platform' }} 18 | uses: ballerina-platform/ballerina-library/.github/workflows/central-publish-template.yml@main 19 | secrets: inherit 20 | with: 21 | environment: ${{ github.event.inputs.environment }} 22 | -------------------------------------------------------------------------------- /spotbugs-exclude.xml: -------------------------------------------------------------------------------- 1 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /ballerina/random_errors.bal: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 2 | // 3 | // WSO2 Inc. licenses this file to you under the Apache License, 4 | // Version 2.0 (the "License"); you may not use this file except 5 | // in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, 11 | // software distributed under the License is distributed on an 12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 13 | // KIND, either express or implied. See the License for the 14 | // specific language governing permissions and limitations 15 | // under the License. 16 | 17 | # Represents Random module related errors. 18 | public type Error distinct error; 19 | 20 | # Represents the arithmetic error. 21 | public type ArithmeticError distinct Error; 22 | -------------------------------------------------------------------------------- /.github/workflows/build-with-bal-test-graalvm.yml: -------------------------------------------------------------------------------- 1 | name: GraalVM Check 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | lang_tag: 7 | description: Branch/Release Tag of the Ballerina Lang 8 | required: true 9 | default: master 10 | lang_version: 11 | description: Ballerina Lang Version (If given ballerina lang buid will be skipped) 12 | required: false 13 | default: '' 14 | native_image_options: 15 | description: Default native-image options 16 | required: false 17 | default: '' 18 | schedule: 19 | - cron: '30 18 * * *' 20 | pull_request: 21 | branches: 22 | - main 23 | types: [ opened, synchronize, reopened, labeled, unlabeled ] 24 | 25 | concurrency: 26 | group: ${{ github.workflow }}-${{ github.ref }} 27 | cancel-in-progress: true 28 | 29 | jobs: 30 | call_stdlib_workflow: 31 | name: Run StdLib Workflow 32 | if: ${{ github.event_name != 'schedule' || (github.event_name == 'schedule' && github.repository_owner == 'ballerina-platform') }} 33 | uses: ballerina-platform/ballerina-library/.github/workflows/build-with-bal-test-graalvm-template.yml@main 34 | with: 35 | lang_tag: ${{ inputs.lang_tag }} 36 | lang_version: ${{ inputs.lang_version }} 37 | native_image_options: ${{ inputs.native_image_options }} 38 | -------------------------------------------------------------------------------- /changelog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | This file contains all the notable changes done to the Ballerina TCP package through the releases. 3 | 4 | ## [Unreleased] 5 | 6 | ### Added 7 | 8 | ### Changed 9 | 10 | - [Make `createIntInRange` work for full int range ](https://github.com/ballerina-platform/ballerina-standard-library/issues/4744) 11 | - [Remove duplicated codecov badge](https://github.com/ballerina-platform/ballerina-standard-library/issues/4893) 12 | - [Prohibit calling `createIntInRange` with empty range](https://github.com/ballerina-platform/ballerina-standard-library/issues/4892) 13 | 14 | ## [1.5.0] - 2023-09-15 15 | 16 | ### Added 17 | - Make Java21 compatible 18 | 19 | ## [1.4.0] - 2023-06-30 20 | 21 | ### Added 22 | - Make GraalVM compatible 23 | 24 | ## [1.3.1] - 2022-11-29 25 | 26 | ### Changed 27 | - [API docs updated](https://github.com/ballerina-platform/ballerina-standard-library/issues/3463) 28 | 29 | ## [1.3.0] - 2022-05-30 30 | 31 | ### Changed 32 | - [Re-implement Random module in Ballerina](https://github.com/ballerina-platform/ballerina-standard-library/issues/2661) 33 | - [Update createDecimal function to be cryptographically secure](https://github.com/ballerina-platform/ballerina-standard-library/issues/2876) 34 | 35 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 36 | -------------------------------------------------------------------------------- /.github/workflows/update_specs.yml: -------------------------------------------------------------------------------- 1 | name: Update Specifications 2 | 3 | env: 4 | SPEC_FOLDER_PATH: 'docs/spec' 5 | 6 | on: 7 | workflow_dispatch: 8 | push: 9 | branches: 10 | - main 11 | paths: 12 | - 'docs/spec/**' 13 | 14 | jobs: 15 | update_specs: 16 | name: Update Specifications 17 | if: github.repository_owner == 'ballerina-platform' 18 | runs-on: ubuntu-latest 19 | 20 | steps: 21 | - name: Checkout Repository 22 | uses: actions/checkout@v2 23 | 24 | - name: Get current date 25 | id: date 26 | run: echo "::set-output name=date::$(date +'%Y-%m-%d')" 27 | 28 | - name: Get Repo Name 29 | id: repo_name 30 | run: | 31 | MODULE=${{ github.event.repository.name }} 32 | echo "::set-output name=short_name::${MODULE##*-}" 33 | 34 | - name: Trigger Workflow 35 | run: | 36 | curl --request POST \ 37 | 'https://api.github.com/repos/ballerina-platform/ballerina-dev-website/dispatches' \ 38 | -H 'Accept: application/vnd.github.v3+json' \ 39 | -H 'Authorization: Bearer ${{ secrets.BALLERINA_BOT_TOKEN }}' \ 40 | --data "{ 41 | \"event_type\": \"update-stdlib-specs\", 42 | \"client_payload\": { 43 | \"module_name\": \"${{ github.event.repository.name }}\", 44 | \"short_name\": \"${{ steps.repo_name.outputs.short_name }}\", 45 | \"file_dir\": \"${{ github.event.repository.name }}/${{ env.SPEC_FOLDER_PATH }}\", 46 | \"release_date\": \"${{ steps.date.outputs.date }}\" 47 | } 48 | }" 49 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * This file was generated by the Gradle 'init' task. 3 | * 4 | * The settings file is used to specify which projects to include in your build. 5 | * 6 | * Detailed information about configuring a multi-project build in Gradle can be found 7 | * in the user manual at https://docs.gradle.org/6.3/userguide/multi_project_builds.html 8 | */ 9 | 10 | pluginManagement { 11 | plugins { 12 | id "com.github.spotbugs" version "${spotbugsPluginVersion}" 13 | id "com.github.johnrengelman.shadow" version "${shadowJarPluginVersion}" 14 | id "de.undercouch.download" version "${downloadPluginVersion}" 15 | id "net.researchgate.release" version "${releasePluginVersion}" 16 | id "io.ballerina.plugin" version "${ballerinaGradlePluginVersion}" 17 | } 18 | 19 | repositories { 20 | gradlePluginPortal() 21 | maven { 22 | url = 'https://maven.pkg.github.com/ballerina-platform/*' 23 | credentials { 24 | username System.getenv("packageUser") 25 | password System.getenv("packagePAT") 26 | } 27 | } 28 | } 29 | } 30 | 31 | plugins { 32 | id "com.gradle.enterprise" version "3.13.2" 33 | } 34 | 35 | include ':checkstyle' 36 | include ':random-native' 37 | include ':random-ballerina' 38 | 39 | project(':checkstyle').projectDir = file("build-config${File.separator}checkstyle") 40 | project(':random-native').projectDir = file('native') 41 | project(':random-ballerina').projectDir = file('ballerina') 42 | 43 | gradleEnterprise { 44 | buildScan { 45 | termsOfServiceUrl = 'https://gradle.com/terms-of-service' 46 | termsOfServiceAgree = 'yes' 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /ballerina/Dependencies.toml: -------------------------------------------------------------------------------- 1 | # AUTO-GENERATED FILE. DO NOT MODIFY. 2 | 3 | # This file is auto-generated by Ballerina for managing dependency versions. 4 | # It should not be modified by hand. 5 | 6 | [ballerina] 7 | dependencies-toml-version = "2" 8 | distribution-version = "2201.12.0" 9 | 10 | [[package]] 11 | org = "ballerina" 12 | name = "jballerina.java" 13 | version = "0.0.0" 14 | modules = [ 15 | {org = "ballerina", packageName = "jballerina.java", moduleName = "jballerina.java"} 16 | ] 17 | 18 | [[package]] 19 | org = "ballerina" 20 | name = "lang.__internal" 21 | version = "0.0.0" 22 | scope = "testOnly" 23 | dependencies = [ 24 | {org = "ballerina", name = "jballerina.java"}, 25 | {org = "ballerina", name = "lang.object"} 26 | ] 27 | 28 | [[package]] 29 | org = "ballerina" 30 | name = "lang.array" 31 | version = "0.0.0" 32 | scope = "testOnly" 33 | dependencies = [ 34 | {org = "ballerina", name = "jballerina.java"}, 35 | {org = "ballerina", name = "lang.__internal"} 36 | ] 37 | 38 | [[package]] 39 | org = "ballerina" 40 | name = "lang.error" 41 | version = "0.0.0" 42 | scope = "testOnly" 43 | dependencies = [ 44 | {org = "ballerina", name = "jballerina.java"} 45 | ] 46 | 47 | [[package]] 48 | org = "ballerina" 49 | name = "lang.object" 50 | version = "0.0.0" 51 | scope = "testOnly" 52 | 53 | [[package]] 54 | org = "ballerina" 55 | name = "random" 56 | version = "1.7.0" 57 | dependencies = [ 58 | {org = "ballerina", name = "jballerina.java"}, 59 | {org = "ballerina", name = "test"}, 60 | {org = "ballerina", name = "time"} 61 | ] 62 | modules = [ 63 | {org = "ballerina", packageName = "random", moduleName = "random"} 64 | ] 65 | 66 | [[package]] 67 | org = "ballerina" 68 | name = "test" 69 | version = "0.0.0" 70 | scope = "testOnly" 71 | dependencies = [ 72 | {org = "ballerina", name = "jballerina.java"}, 73 | {org = "ballerina", name = "lang.array"}, 74 | {org = "ballerina", name = "lang.error"} 75 | ] 76 | modules = [ 77 | {org = "ballerina", packageName = "test", moduleName = "test"} 78 | ] 79 | 80 | [[package]] 81 | org = "ballerina" 82 | name = "time" 83 | version = "2.7.0" 84 | dependencies = [ 85 | {org = "ballerina", name = "jballerina.java"} 86 | ] 87 | modules = [ 88 | {org = "ballerina", packageName = "time", moduleName = "time"} 89 | ] 90 | 91 | -------------------------------------------------------------------------------- /docs/spec/spec.md: -------------------------------------------------------------------------------- 1 | # Specification: Ballerina Random Library 2 | 3 | _Owners_: @daneshk @MadhukaHarith92 4 | _Reviewers_: @daneshk 5 | _Created_: 2021/11/09 6 | _Updated_: 2022/02/08 7 | _Edition_: Swan Lake 8 | 9 | ## Introduction 10 | This is the specification for the Random standard library of [Ballerina language](https://ballerina.io/), which provides APIs to generate pseudo-random numbers. 11 | 12 | The Random library specification has evolved and may continue to evolve in the future. The released versions of the specification can be found under the relevant Github tag. 13 | 14 | If you have any feedback or suggestions about the library, start a discussion via a [GitHub issue](https://github.com/ballerina-platform/ballerina-standard-library/issues) or in the [Discord server](https://discord.gg/ballerinalang). Based on the outcome of the discussion, the specification and implementation can be updated. Community feedback is always welcome. Any accepted proposal, which affects the specification is stored under `/docs/proposals`. Proposals under discussion can be found with the label `type/proposal` in GitHub. 15 | 16 | The conforming implementation of the specification is released and included in the distribution. Any deviation from the specification is considered a bug. 17 | 18 | ## Contents 19 | 20 | 1. [Overview](#1-overview) 21 | 2. [Random Number Generation](#2-random-number-generation) 22 | 23 | ## 1. Overview 24 | This specification elaborates on the random number generation functions available in the Random library. 25 | 26 | ## 2. Linear Congruential Generator 27 | The [Linear Congruential Generator](https://en.wikipedia.org/wiki/Linear_congruential_generator) algorithm is used to generate random numbers in range. The generator is defined by, 28 | ``` 29 | x1 = (a * x0 + c) % m 30 | ``` 31 | 32 | where `x0` is the seed, `a` is the multiplier, `c` is the increment and `m` is the modulus. The following values are used in the formula. 33 | - a = 25214903917 34 | - c = 11 35 | - m = 2^48 36 | 37 | For x0 (seed), the current time in milliseconds is used. 38 | 39 | ## 3. Random number generation 40 | A random decimal number between 0.0 and 1.0 can be generated using the `random:createDecimal()` function. 41 | ```ballerina 42 | float randomValue = random:createDecimal(); 43 | ``` 44 | 45 | A random integer between the given start(inclusive) and end(exclusive) values can be generated using the `random:createIntInRange()` function. 46 | ```ballerina 47 | int randomInteger = check random:createIntInRange(1, 100); 48 | ``` 49 | 50 | -------------------------------------------------------------------------------- /ballerina/natives.bal: -------------------------------------------------------------------------------- 1 | import ballerina/jballerina.java; 2 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | // 4 | // WSO2 Inc. licenses this file to you under the Apache License, 5 | // Version 2.0 (the "License"); you may not use this file except 6 | // in compliance with the License. 7 | // You may obtain a copy of the License at 8 | // 9 | // http://www.apache.org/licenses/LICENSE-2.0 10 | // 11 | // Unless required by applicable law or agreed to in writing, 12 | // software distributed under the License is distributed on an 13 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 | // KIND, either express or implied. See the License for the 15 | // specific language governing permissions and limitations 16 | // under the License. 17 | import ballerina/time; 18 | 19 | const decimal a = 25214903917; 20 | const decimal c = 17; 21 | final decimal & readonly m = float:pow(2, 48); 22 | isolated decimal x0 = currentTimeInMilliSeconds(); 23 | 24 | # Generates a random decimal number between 0.0 and 1.0. 25 | # ```ballerina 26 | # float randomValue = random:createDecimal(); 27 | # ``` 28 | # 29 | # + return - The random decimal number generated 30 | public isolated function createDecimal() returns float { 31 | return nextFloat(); 32 | } 33 | 34 | # Generates a random number between the given start(inclusive) and end(exclusive) values. 35 | # Please note that the generated number is not cryptographically secured. 36 | # ```ballerina 37 | # int randomInteger = check random:createIntInRange(1, 100); 38 | # ``` 39 | # 40 | # + startRange - The start range value 41 | # + endRange - The end range value 42 | # + return - The random number generated within the given range, or an error if the end range value is less than or equal to the start range value 43 | public isolated function createIntInRange(int startRange, int endRange) returns int|Error { 44 | if startRange >= endRange { 45 | return error Error("End range value must be greater than the start range value"); 46 | } 47 | return (lcg() / m * ((endRange - 1) - startRange) + startRange); 48 | } 49 | 50 | isolated function lcg() returns decimal { 51 | decimal x1; 52 | lock { 53 | x1 = (a * x0 + c) % m; 54 | x0 = x1; 55 | } 56 | return x1; 57 | } 58 | 59 | isolated function currentTimeInMilliSeconds() returns decimal { 60 | time:Utc utc = time:utcNow(); 61 | decimal mills = (utc[0] * 1000) + utc[1] * 1000; 62 | return decimal:round(mills); 63 | } 64 | 65 | isolated function nextFloat() returns float { 66 | handle secureRandomObj = newSecureRandom(); 67 | return nextFloatExtern(secureRandomObj); 68 | } 69 | 70 | isolated function newSecureRandom() returns handle = @java:Constructor { 71 | 'class: "java.security.SecureRandom" 72 | } external; 73 | 74 | isolated function nextFloatExtern(handle secureRandomObj) returns float = @java:Method { 75 | name: "nextFloat", 76 | 'class: "java.security.SecureRandom" 77 | } external; 78 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 48 | echo. 49 | echo Please set the JAVA_HOME variable in your environment to match the 50 | echo location of your Java installation. 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 62 | echo. 63 | echo Please set the JAVA_HOME variable in your environment to match the 64 | echo location of your Java installation. 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /ballerina/build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2021, WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | * 16 | */ 17 | import org.apache.tools.ant.taskdefs.condition.Os 18 | 19 | plugins { 20 | id 'io.ballerina.plugin' 21 | } 22 | 23 | description = 'Ballerina - Random Ballerina Generator' 24 | 25 | def packageName = "random" 26 | def packageOrg = "ballerina" 27 | def tomlVersion = stripBallerinaExtensionVersion("${project.version}") 28 | def ballerinaTomlFilePlaceHolder = new File("${project.rootDir}/build-config/resources/Ballerina.toml") 29 | def ballerinaTomlFile = new File("$project.projectDir/Ballerina.toml") 30 | 31 | def stripBallerinaExtensionVersion(String extVersion) { 32 | if (extVersion.matches(project.ext.timestampedVersionRegex)) { 33 | def splitVersion = extVersion.split('-') 34 | if (splitVersion.length > 3) { 35 | def strippedValues = splitVersion[0..-4] 36 | return strippedValues.join('-') 37 | } else { 38 | return extVersion 39 | } 40 | } else { 41 | return extVersion.replace("${project.ext.snapshotVersion}", "") 42 | } 43 | } 44 | 45 | ballerina { 46 | packageOrganization = packageOrg 47 | module = packageName 48 | langVersion = ballerinaLangVersion 49 | } 50 | 51 | task updateTomlFiles { 52 | doLast { 53 | def newConfig = ballerinaTomlFilePlaceHolder.text.replace("@project.version@", project.version) 54 | newConfig = newConfig.replace("@toml.version@", tomlVersion) 55 | ballerinaTomlFile.text = newConfig 56 | } 57 | } 58 | 59 | task commitTomlFiles { 60 | doLast { 61 | project.exec { 62 | ignoreExitValue true 63 | if (Os.isFamily(Os.FAMILY_WINDOWS)) { 64 | commandLine 'cmd', '/c', "git commit -m \"[Automated] Update the native jar versions\" Ballerina.toml Dependencies.toml" 65 | } else { 66 | commandLine 'sh', '-c', "git commit -m '[Automated] Update the native jar versions' Ballerina.toml Dependencies.toml" 67 | } 68 | } 69 | } 70 | } 71 | 72 | publishing { 73 | publications { 74 | maven(MavenPublication) { 75 | artifact source: createArtifactZip, extension: 'zip' 76 | } 77 | } 78 | 79 | repositories { 80 | maven { 81 | name = "GitHubPackages" 82 | url = uri("https://maven.pkg.github.com/ballerina-platform/module-${packageOrg}-${packageName}") 83 | credentials { 84 | username = System.getenv("publishUser") 85 | password = System.getenv("publishPAT") 86 | } 87 | } 88 | } 89 | } 90 | 91 | updateTomlFiles.dependsOn copyStdlibs 92 | 93 | build.dependsOn "generatePomFileForMavenPublication" 94 | publishToMavenLocal.dependsOn build 95 | publish.dependsOn build 96 | -------------------------------------------------------------------------------- /ballerina/tests/random.bal: -------------------------------------------------------------------------------- 1 | // Copyright (c) 2021 WSO2 Inc. (http://www.wso2.org) All Rights Reserved. 2 | // 3 | // WSO2 Inc. licenses this file to you under the Apache License, 4 | // Version 2.0 (the "License"); you may not use this file except 5 | // in compliance with the License. 6 | // You may obtain a copy of the License at 7 | // 8 | // http://www.apache.org/licenses/LICENSE-2.0 9 | // 10 | // Unless required by applicable law or agreed to in writing, 11 | // software distributed under the License is distributed on an 12 | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 13 | // KIND, either express or implied. See the License for the 14 | // specific language governing permissions and limitations 15 | // under the License. 16 | import ballerina/test; 17 | 18 | @test:Config {} 19 | isolated function createDecimalTest() { 20 | float result = createDecimal(); 21 | test:assertTrue(result > 0.0 && result < 1.0, msg = "createDecimalTest result is not between 0 and 1"); 22 | } 23 | 24 | @test:Config {} 25 | isolated function createIntInRangeTest() { 26 | int|error result = createIntInRange(5, 10); 27 | if result is int { 28 | test:assertTrue(result >= 5 && result < 10, msg = "createIntInRangeTest result is not within 5 and 10"); 29 | } else { 30 | test:assertFail("createIntInRangeTest result is not int"); 31 | } 32 | } 33 | 34 | @test:Config {} 35 | isolated function createIntInRangeWithSingleElementTest() { 36 | final int someInt = 123; 37 | int|error result = createIntInRange(someInt, someInt + 1); 38 | if result is int { 39 | test:assertTrue(result == someInt, msg = "createIntInRangeWithSingleElementTest result is not " + someInt.toString()); 40 | } else { 41 | test:assertFail("createIntInRangeTest result is not int"); 42 | } 43 | } 44 | 45 | @test:Config {} 46 | isolated function negativeTestforCreateIntInRangeTest() { 47 | int|error result = createIntInRange(5000, 10); 48 | if result is error { 49 | test:assertTrue(result.message().includes("End range value must be greater than the start range value"), 50 | msg = "negativeTestforCreateIntInRangeTest result incorrect"); 51 | } else { 52 | test:assertFail("Result is not mismatch"); 53 | } 54 | } 55 | 56 | @test:Config {} 57 | isolated function negativeTestforCreateIntInRange2Test() { 58 | int|error result = createIntInRange(17, 17); 59 | if result is error { 60 | test:assertTrue(result.message().includes("End range value must be greater than the start range value"), 61 | msg = "negativeTestforCreateIntInRange2Test result incorrect"); 62 | } else { 63 | test:assertFail("Result is not mismatch"); 64 | } 65 | } 66 | 67 | @test:Config {} 68 | isolated function doesNotThrowForFullIntRangeTest() { 69 | int|error result = createIntInRange(int:MIN_VALUE, int:MAX_VALUE); 70 | if result is int { 71 | test:assertTrue(result >= int:MIN_VALUE && result < int:MAX_VALUE, 72 | msg = "createIntInRangeTest result is not within 5 and 10"); 73 | } else { 74 | test:assertFail("createIntInRangeTest result is not int"); 75 | } 76 | } 77 | 78 | @test:Config {} 79 | isolated function doesNotThrowForAlmostFullIntRangeRTest() { 80 | int|error result = createIntInRange(int:MIN_VALUE + 1, int:MAX_VALUE); 81 | if result is int { 82 | test:assertTrue(result >= int:MIN_VALUE + 1 && result < int:MAX_VALUE, 83 | msg = "createIntInRangeTest result is not within 5 and 10"); 84 | } else { 85 | test:assertFail("createIntInRangeTest result is not int"); 86 | } 87 | } 88 | 89 | @test:Config {} 90 | isolated function doesNotThrowForAlmostFullIntRangeLTest() { 91 | int|error result = createIntInRange(int:MIN_VALUE, int:MAX_VALUE - 1); 92 | if result is int { 93 | test:assertTrue(result >= int:MIN_VALUE && result < int:MAX_VALUE - 1, 94 | msg = "createIntInRangeTest result is not within 5 and 10"); 95 | } else { 96 | test:assertFail("createIntInRangeTest result is not int"); 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Ballerina Random Library 2 | =================== 3 | 4 | [![Build](https://github.com/ballerina-platform/module-ballerina-random/actions/workflows/build-timestamped-master.yml/badge.svg)](https://github.com/ballerina-platform/module-ballerina-random/actions/workflows/build-timestamped-master.yml) 5 | [![codecov](https://codecov.io/gh/ballerina-platform/module-ballerina-random/branch/main/graph/badge.svg)](https://codecov.io/gh/ballerina-platform/module-ballerina-random) 6 | [![Trivy](https://github.com/ballerina-platform/module-ballerina-random/actions/workflows/trivy-scan.yml/badge.svg)](https://github.com/ballerina-platform/module-ballerina-random/actions/workflows/trivy-scan.yml) 7 | [![GraalVM Check](https://github.com/ballerina-platform/module-ballerina-random/actions/workflows/build-with-bal-test-graalvm.yml/badge.svg)](https://github.com/ballerina-platform/module-ballerina-random/actions/workflows/build-with-bal-test-graalvm.yml) 8 | [![GitHub Last Commit](https://img.shields.io/github/last-commit/ballerina-platform/module-ballerina-random.svg)](https://github.com/ballerina-platform/module-ballerina-random/commits/main) 9 | [![Github issues](https://img.shields.io/github/issues/ballerina-platform/ballerina-standard-library/module/random.svg?label=Open%20Issues)](https://github.com/ballerina-platform/ballerina-standard-library/labels/module%2Frandom) 10 | 11 | This library provides APIs to generate pseudo-random numbers. 12 | 13 | The generated values are selected pseudo-randomly with uniform distribution from a particular range. 14 | 15 | ## Issues and projects 16 | 17 | The **Issues** and **Projects** tabs are disabled for this repository as this is part of the Ballerina Standard Library. To report bugs, request new features, start new discussions, view project boards, etc., go to the Ballerina Standard Library [parent repository](https://github.com/ballerina-platform/ballerina-standard-library). 18 | 19 | This repository contains only the source code of the package. 20 | 21 | ## Build from the source 22 | 23 | ### Set up the prerequisites 24 | 25 | 1. Download and install Java SE Development Kit (JDK) version 21 (from one of the following locations). 26 | * [Oracle](https://www.oracle.com/java/technologies/downloads/) 27 | 28 | * [OpenJDK](https://adoptium.net/) 29 | 30 | > **Note:** Set the JAVA_HOME environment variable to the path name of the directory into which you installed JDK. 31 | 32 | 2. Export your Github Personal access token with the read package permissions as follows. 33 | 34 | export packageUser= 35 | export packagePAT= 36 | 37 | ### Build the source 38 | 39 | Execute the commands below to build from source. 40 | 41 | 1. To build the library: 42 | ``` 43 | ./gradlew clean build 44 | ``` 45 | 46 | 2. To run the integration tests: 47 | ``` 48 | ./gradlew clean test 49 | ``` 50 | 3. To build the module without the tests: 51 | ``` 52 | ./gradlew clean build -x test 53 | ``` 54 | 4. To debug module implementation: 55 | ``` 56 | ./gradlew clean build -Pdebug= 57 | ./gradlew clean test -Pdebug= 58 | ``` 59 | 5. To debug the module with Ballerina language: 60 | ``` 61 | ./gradlew clean build -PbalJavaDebug= 62 | ./gradlew clean test -PbalJavaDebug= 63 | ``` 64 | 6. Publish ZIP artifact to the local `.m2` repository: 65 | ``` 66 | ./gradlew clean build publishToMavenLocal 67 | ``` 68 | 7. Publish the generated artifacts to the local Ballerina central repository: 69 | ``` 70 | ./gradlew clean build -PpublishToLocalCentral=true 71 | ``` 72 | 8. Publish the generated artifacts to the Ballerina central repository: 73 | ``` 74 | ./gradlew clean build -PpublishToCentral=true 75 | ``` 76 | 77 | ## Contribute to Ballerina 78 | 79 | As an open source project, Ballerina welcomes contributions from the community. 80 | 81 | For more information, go to the [contribution guidelines](https://github.com/ballerina-platform/ballerina-lang/blob/master/CONTRIBUTING.md). 82 | 83 | ## Code of conduct 84 | 85 | All contributors are encouraged to read the [Ballerina Code of Conduct](https://ballerina.io/code-of-conduct). 86 | 87 | ## Useful links 88 | 89 | * Chat live with us via our [Discord server](https://discord.gg/ballerinalang). 90 | * Post all technical questions on Stack Overflow with the [#ballerina](https://stackoverflow.com/questions/tagged/ballerina) tag. 91 | * For more information go to the [`random` library](https://lib.ballerina.io/ballerina/random/latest). 92 | * For example demonstrations of the usage, go to [Ballerina By Examples](https://ballerina.io/swan-lake/learn/by-example/). 93 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | # This is normally unused 84 | # shellcheck disable=SC2034 85 | APP_BASE_NAME=${0##*/} 86 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 87 | 88 | # Use the maximum available, or set MAX_FD != -1 to use that value. 89 | MAX_FD=maximum 90 | 91 | warn () { 92 | echo "$*" 93 | } >&2 94 | 95 | die () { 96 | echo 97 | echo "$*" 98 | echo 99 | exit 1 100 | } >&2 101 | 102 | # OS specific support (must be 'true' or 'false'). 103 | cygwin=false 104 | msys=false 105 | darwin=false 106 | nonstop=false 107 | case "$( uname )" in #( 108 | CYGWIN* ) cygwin=true ;; #( 109 | Darwin* ) darwin=true ;; #( 110 | MSYS* | MINGW* ) msys=true ;; #( 111 | NONSTOP* ) nonstop=true ;; 112 | esac 113 | 114 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 115 | 116 | 117 | # Determine the Java command to use to start the JVM. 118 | if [ -n "$JAVA_HOME" ] ; then 119 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 120 | # IBM's JDK on AIX uses strange locations for the executables 121 | JAVACMD=$JAVA_HOME/jre/sh/java 122 | else 123 | JAVACMD=$JAVA_HOME/bin/java 124 | fi 125 | if [ ! -x "$JAVACMD" ] ; then 126 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 127 | 128 | Please set the JAVA_HOME variable in your environment to match the 129 | location of your Java installation." 130 | fi 131 | else 132 | JAVACMD=java 133 | if ! command -v java >/dev/null 2>&1 134 | then 135 | die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 136 | 137 | Please set the JAVA_HOME variable in your environment to match the 138 | location of your Java installation." 139 | fi 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 147 | # shellcheck disable=SC3045 148 | MAX_FD=$( ulimit -H -n ) || 149 | warn "Could not query maximum file descriptor limit" 150 | esac 151 | case $MAX_FD in #( 152 | '' | soft) :;; #( 153 | *) 154 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 155 | # shellcheck disable=SC3045 156 | ulimit -n "$MAX_FD" || 157 | warn "Could not set maximum file descriptor limit to $MAX_FD" 158 | esac 159 | fi 160 | 161 | # Collect all arguments for the java command, stacking in reverse order: 162 | # * args from the command line 163 | # * the main class name 164 | # * -classpath 165 | # * -D...appname settings 166 | # * --module-path (only if needed) 167 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 168 | 169 | # For Cygwin or MSYS, switch paths to Windows format before running java 170 | if "$cygwin" || "$msys" ; then 171 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 172 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 173 | 174 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 175 | 176 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 177 | for arg do 178 | if 179 | case $arg in #( 180 | -*) false ;; # don't mess with options #( 181 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 182 | [ -e "$t" ] ;; #( 183 | *) false ;; 184 | esac 185 | then 186 | arg=$( cygpath --path --ignore --mixed "$arg" ) 187 | fi 188 | # Roll the args list around exactly as many times as the number of 189 | # args, so each arg winds up back in the position where it started, but 190 | # possibly modified. 191 | # 192 | # NB: a `for` loop captures its iteration list before it begins, so 193 | # changing the positional parameters here affects neither the number of 194 | # iterations, nor the values presented in `arg`. 195 | shift # remove old arg 196 | set -- "$@" "$arg" # push replacement arg 197 | done 198 | fi 199 | 200 | 201 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 202 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 203 | 204 | # Collect all arguments for the java command; 205 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 206 | # shell script including quotes and variable substitutions, so put them in 207 | # double quotes to make sure that they get re-expanded; and 208 | # * put everything else in single quotes, so that it's not re-expanded. 209 | 210 | set -- \ 211 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 212 | -classpath "$CLASSPATH" \ 213 | org.gradle.wrapper.GradleWrapperMain \ 214 | "$@" 215 | 216 | # Stop when "xargs" is not available. 217 | if ! command -v xargs >/dev/null 2>&1 218 | then 219 | die "xargs is not available" 220 | fi 221 | 222 | # Use "xargs" to parse quoted args. 223 | # 224 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 225 | # 226 | # In Bash we could simply go: 227 | # 228 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 229 | # set -- "${ARGS[@]}" "$@" 230 | # 231 | # but POSIX shell has neither arrays nor command substitution, so instead we 232 | # post-process each arg (as a line of input to sed) to backslash-escape any 233 | # character that might be a shell metacharacter, then use eval to reverse 234 | # that process (while maintaining the separation between arguments), and wrap 235 | # the whole thing up as a single "set" statement. 236 | # 237 | # This will of course break if any of these variables contains a newline or 238 | # an unmatched quote. 239 | # 240 | 241 | eval "set -- $( 242 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 243 | xargs -n1 | 244 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 245 | tr '\n' ' ' 246 | )" '"$@"' 247 | 248 | exec "$JAVACMD" "$@" 249 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------