├── .github ├── FUNDING.yml └── workflows │ ├── build.yml │ ├── early-access.yml │ └── release.yml ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.adoc ├── VERSION ├── build.gradle ├── docs └── guide │ ├── guide.gradle │ └── src │ └── docs │ └── asciidoc │ ├── _links.adoc │ ├── compatibility.adoc │ ├── configuration.adoc │ ├── index.adoc │ ├── introduction.adoc │ └── usage.adoc ├── gradle.properties ├── gradle ├── LICENSE_HEADER └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── jreleaser.yml ├── settings.gradle └── subprojects └── ezmorph-core ├── ezmorph-core.gradle └── src ├── main ├── java │ └── org │ │ └── kordamp │ │ └── ezmorph │ │ ├── MorphException.java │ │ ├── MorphUtils.java │ │ ├── Morpher.java │ │ ├── MorpherRegistry.java │ │ ├── ObjectMorpher.java │ │ ├── array │ │ ├── AbstractArrayMorpher.java │ │ ├── BooleanArrayMorpher.java │ │ ├── BooleanObjectArrayMorpher.java │ │ ├── ByteArrayMorpher.java │ │ ├── CharArrayMorpher.java │ │ ├── CharacterObjectArrayMorpher.java │ │ ├── DoubleArrayMorpher.java │ │ ├── FloatArrayMorpher.java │ │ ├── IntArrayMorpher.java │ │ ├── LongArrayMorpher.java │ │ ├── ObjectArrayMorpher.java │ │ ├── ShortArrayMorpher.java │ │ └── package.html │ │ ├── bean │ │ ├── BeanMorpher.java │ │ ├── MorphDynaBean.java │ │ ├── MorphDynaClass.java │ │ └── package.html │ │ ├── object │ │ ├── AbstractObjectMorpher.java │ │ ├── BigDecimalMorpher.java │ │ ├── BigIntegerMorpher.java │ │ ├── BooleanObjectMorpher.java │ │ ├── CharacterObjectMorpher.java │ │ ├── ClassMorpher.java │ │ ├── DateMorpher.java │ │ ├── IdentityObjectMorpher.java │ │ ├── MapToDateMorpher.java │ │ ├── NumberMorpher.java │ │ ├── ObjectListMorpher.java │ │ ├── StringMorpher.java │ │ ├── SwitchingMorpher.java │ │ └── package.html │ │ ├── package.html │ │ ├── primitive │ │ ├── AbstractDecimalMorpher.java │ │ ├── AbstractIntegerMorpher.java │ │ ├── AbstractPrimitiveMorpher.java │ │ ├── BooleanMorpher.java │ │ ├── ByteMorpher.java │ │ ├── CharMorpher.java │ │ ├── DoubleMorpher.java │ │ ├── FloatMorpher.java │ │ ├── IntMorpher.java │ │ ├── LongMorpher.java │ │ ├── ShortMorpher.java │ │ └── package.html │ │ └── test │ │ ├── ArrayAssertions.java │ │ └── package.html └── java11 │ └── module-info.java └── test └── java └── org └── kordamp └── ezmorph ├── MorpherRegistryTest.java ├── array ├── AbstractArrayMorpherTestCase.java ├── BooleanArrayMorpherTest.java ├── BooleanObjectArrayMorpherTest.java ├── ByteArrayMorpherTest.java ├── CharArrayMorpherTest.java ├── CharacterObjectArrayMorpherTest.java ├── DoubleArrayMorpherTest.java ├── FloatArrayMorpherTest.java ├── IntArrayMorpherTest.java ├── LongArrayMorpherTest.java ├── ObjectArrayMorpherTest.java └── ShortArrayMorpherTest.java ├── bean ├── BeanMorpherTest.java ├── MorphDynaBeanTest.java ├── MorphDynaClassTest.java └── sample │ ├── BeanA.java │ ├── BeanB.java │ ├── BeanC.java │ ├── BeanD.java │ ├── ObjectBean.java │ ├── PrimitiveBean.java │ └── TypedBean.java ├── object ├── AbstractObjectMorpherTestCase.java ├── BigDecimalMorpherTest.java ├── BigIntegerMorpherTest.java ├── BooleanObjectMorpherTest.java ├── CharacterObjectMorpherTest.java ├── ClassMorpherTest.java ├── DateMorpherTest.java ├── IdentityObjectMorpherTest.java ├── MapToDateMorpherTest.java ├── NumberMorpherTest.java ├── ObjectListMorpherTest.java ├── StringMorpherTest.java ├── SwitchingMorpherTest.java └── sample │ ├── BeanA.java │ ├── BeanB.java │ ├── WrapperA.java │ └── WrapperB.java ├── primitive ├── AbstractMorpherTestCase.java ├── BooleanMorpherTest.java ├── ByteMorpherTest.java ├── CharMorpherTest.java ├── DoubleMorpherTest.java ├── FloatMorpherTest.java ├── IntMorpherTest.java ├── LongMorpherTest.java └── ShortMorpherTest.java └── test ├── ArrayAssertionsTest.java ├── BooleanArrayAssertionsTest.java ├── ByteArrayAssertionsTest.java ├── CharArrayAssertionsTest.java ├── DoubleArrayAssertionsTest.java ├── FloatArrayAssertionsTest.java ├── IntArrayAssertionsTest.java ├── LongArrayAssertionsTest.java └── ShortArrayAssertionsTest.java /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | patreon: aalmiray 2 | github: aalmiray 3 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: Apache-2.0 3 | # 4 | # Copyright 2006-2024 the original author or 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 | name: Build 20 | 21 | on: 22 | pull_request: 23 | 24 | env: 25 | JAVA_VERSION: '11' 26 | JAVA_DISTRO: 'zulu' 27 | 28 | jobs: 29 | build: 30 | name: Build 31 | runs-on: ubuntu-latest 32 | if: startsWith(github.event.head_commit.message, 'Releasing version') != true 33 | 34 | steps: 35 | - uses: actions/checkout@v4 36 | 37 | - name: Setup Java 38 | uses: actions/setup-java@v4 39 | with: 40 | java-version: ${{ env.JAVA_VERSION }} 41 | distribution: ${{ env.JAVA_DISTRO }} 42 | cache: gradle 43 | 44 | - name: Build 45 | run: ./gradlew build -S 46 | -------------------------------------------------------------------------------- /.github/workflows/early-access.yml: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: Apache-2.0 3 | # 4 | # Copyright 2006-2024 the original author or 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 | name: EarlyAccess 20 | 21 | on: 22 | push: 23 | branches: [ master ] 24 | 25 | env: 26 | JAVA_VERSION: '11' 27 | JAVA_DISTRO: 'zulu' 28 | 29 | jobs: 30 | earlyaccess: 31 | name: EarlyAccess 32 | if: github.repository == 'kordamp/ezmorph' && startsWith(github.event.head_commit.message, 'Releasing version') != true 33 | runs-on: ubuntu-latest 34 | steps: 35 | - name: Checkout 36 | uses: actions/checkout@v4 37 | with: 38 | fetch-depth: 0 39 | 40 | - name: Cancel previous run 41 | uses: styfle/cancel-workflow-action@0.12.1 42 | with: 43 | access_token: ${{ secrets.GITHUB_TOKEN }} 44 | 45 | - name: Setup Java 46 | uses: actions/setup-java@v4 47 | with: 48 | java-version: ${{ env.JAVA_VERSION }} 49 | distribution: ${{ env.JAVA_DISTRO }} 50 | cache: gradle 51 | 52 | - name: Build 53 | run: ./gradlew -Prelease=true build -S 54 | 55 | - name: Version 56 | id: vars 57 | run: echo "VERSION=$(cat VERSION)" >> $GITHUB_OUTPUT 58 | 59 | - name: Release 60 | uses: jreleaser/release-action@v2 61 | with: 62 | arguments: release 63 | env: 64 | JRELEASER_PROJECT_VERSION: ${{ steps.vars.outputs.version }} 65 | JRELEASER_GITHUB_TOKEN: ${{ secrets.GIT_ACCESS_TOKEN }} 66 | JRELEASER_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} 67 | JRELEASER_GPG_PUBLIC_KEY: ${{ secrets.GPG_PUBLIC_KEY }} 68 | JRELEASER_GPG_SECRET_KEY: ${{ secrets.GPG_SECRET_KEY }} 69 | 70 | - name: JReleaser output 71 | if: always() 72 | uses: actions/upload-artifact@v4 73 | with: 74 | name: artifact 75 | path: | 76 | out/jreleaser/trace.log 77 | out/jreleaser/output.properties 78 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: Apache-2.0 3 | # 4 | # Copyright 2006-2024 the original author or 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 | name: Release 20 | 21 | on: 22 | workflow_dispatch: 23 | inputs: 24 | version: 25 | description: "Release version" 26 | required: true 27 | 28 | env: 29 | JAVA_VERSION: '11' 30 | JAVA_DISTRO: 'zulu' 31 | 32 | jobs: 33 | release: 34 | name: Release 35 | runs-on: ubuntu-latest 36 | steps: 37 | - uses: actions/checkout@v4 38 | with: 39 | fetch-depth: 0 40 | 41 | - name: Cancel previous run 42 | uses: styfle/cancel-workflow-action@0.12.1 43 | with: 44 | access_token: ${{ secrets.GITHUB_TOKEN }} 45 | 46 | - name: Set up Java 47 | uses: actions/setup-java@v4 48 | with: 49 | java-version: ${{ env.JAVA_VERSION }} 50 | distribution: ${{ env.JAVA_DISTRO }} 51 | cache: gradle 52 | 53 | - name: Version 54 | id: vars 55 | shell: bash 56 | run: | 57 | echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT 58 | echo ${{ github.event.inputs.version }} > VERSION 59 | git add VERSION 60 | sed -i -e "s/^\:project-version\:\ .*/:project-version: ${{ github.event.inputs.version }}/g" README.adoc 61 | git config --global user.email "${{ secrets.COMMIT_EMAIL }}" 62 | git config --global user.name "Andres Almiray" 63 | git commit -a -m "Releasing version ${{ github.event.inputs.version }}" 64 | git push origin master 65 | 66 | - name: Deploy 67 | run: | 68 | ./gradlew -Pprofile=sbom -PreproducibleBuild=true publish -S 69 | 70 | - name: Release 71 | uses: jreleaser/release-action@v2 72 | with: 73 | arguments: full-release 74 | env: 75 | JRELEASER_PROJECT_VERSION: ${{ github.event.inputs.version }} 76 | JRELEASER_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 77 | JRELEASER_GPG_PASSPHRASE: ${{ secrets.GPG_PASSPHRASE }} 78 | JRELEASER_GPG_PUBLIC_KEY: ${{ secrets.GPG_PUBLIC_KEY }} 79 | JRELEASER_GPG_SECRET_KEY: ${{ secrets.GPG_SECRET_KEY }} 80 | JRELEASER_NEXUS2_MAVEN_CENTRAL_USERNAME: ${{ secrets.SONATYPE_USERNAME }} 81 | JRELEASER_NEXUS2_MAVEN_CENTRAL_PASSWORD: ${{ secrets.SONATYPE_PASSWORD }} 82 | JRELEASER_TWITTER_CONSUMER_KEY: ${{ secrets.TWITTER_CONSUMER_KEY }} 83 | JRELEASER_TWITTER_CONSUMER_SECRET: ${{ secrets.TWITTER_CONSUMER_SECRET }} 84 | JRELEASER_TWITTER_ACCESS_TOKEN: ${{ secrets.TWITTER_ACCESS_TOKEN }} 85 | JRELEASER_TWITTER_ACCESS_TOKEN_SECRET: ${{ secrets.TWITTER_ACCESS_TOKEN_SECRET }} 86 | 87 | - name: JReleaser output 88 | if: always() 89 | uses: actions/upload-artifact@v4 90 | with: 91 | name: artifact 92 | path: | 93 | out/jreleaser/trace.log 94 | out/jreleaser/output.properties 95 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | target 3 | out 4 | .gradle 5 | .idea 6 | *.ipr 7 | *.iws 8 | *.iml 9 | .project 10 | .classpath 11 | .settings 12 | .lazybones 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | 3 | jdk: openjdk8 4 | 5 | install: true 6 | 7 | before_cache: 8 | - rm -f $HOME/.gradle/caches/modules-2/modules-2.lock 9 | 10 | cache: 11 | directories: 12 | - $HOME/.m2/ 13 | - $HOME/.gradle/caches/ 14 | - $HOME/.gradle/wrapper/ 15 | 16 | before_script: 17 | - ./gradlew --version 18 | 19 | script: ./gradlew build aggregateJacocoReport -S 20 | 21 | env: TERM=dumb 22 | 23 | after_success: ./gradlew coveralls 24 | -------------------------------------------------------------------------------- /README.adoc: -------------------------------------------------------------------------------- 1 | = ezmorph 2 | :author: Andres Almiray 3 | :linkattrs: 4 | :project-owner: kordamp 5 | :project-repo: maven 6 | :project-name: ezmorph 7 | :project-group: org.kordamp.ezmorph 8 | :project-artifactId: ezmorph-core 9 | :project-version: 3.1.0 10 | 11 | image:https://img.shields.io/github/actions/workflow/status/{project-owner}/{project-name}/early-access.yml?branch=master&logo=github&label=Build["Build Status", link="https://github.com/{project-owner}/{project-name}/actions"] 12 | image:https://img.shields.io/maven-central/v/{project-group}/{project-artifactId}?logo=apache%20maven[Download, link="https://search.maven.org/#search|ga|1|g:{project-group} AND a:{project-artifactId}"] 13 | 14 | --- 15 | 16 | Simple Java library for transforming an Object to another Object. 17 | 18 | Refer to the link:http://{project-owner}.github.io/{project-name}/[project guide, window="_blank"] for 19 | further information on configuration and usage. 20 | -------------------------------------------------------------------------------- /VERSION: -------------------------------------------------------------------------------- 1 | 3.2.0-SNAPSHOT 2 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | plugins { 19 | id 'org.kordamp.gradle.coveralls' 20 | } 21 | 22 | config { 23 | info { 24 | name = rootProject.name 25 | description = 'Simple Java library for transforming an Object to another Object' 26 | inceptionYear = '2006' 27 | tags = ['ezmorph', 'converter', 'transformer'] 28 | 29 | specification { enabled = false } 30 | } 31 | 32 | docs { 33 | javadoc { 34 | enabled = true 35 | autoLinks { 36 | enabled = false 37 | } 38 | } 39 | } 40 | 41 | coverage { 42 | jacoco { 43 | toolVersion = jacocoVersion 44 | } 45 | } 46 | } 47 | 48 | allprojects { 49 | repositories { 50 | mavenLocal() 51 | } 52 | 53 | tasks.withType(GenerateModuleMetadata) { 54 | enabled = false 55 | } 56 | 57 | tasks.withType(JavaCompile) { 58 | options.encoding = 'UTF-8' 59 | } 60 | } 61 | 62 | profiles { 63 | profile('sbom') { 64 | activation { 65 | property { 66 | key = 'sbom' 67 | value = true 68 | } 69 | } 70 | action { 71 | println 'SBOM generation is turned ON' 72 | 73 | gradleProjects { 74 | subprojects { 75 | dirs(['subprojects']) { 76 | cyclonedxBom { 77 | includeConfigs = ['runtimeClasspath'] 78 | projectType = 'library' 79 | outputName = "${project.name}-${project.version}-cyclonedx".toString() 80 | destination = file('build/reports/cyclonedx') 81 | includeLicenseText = false 82 | } 83 | 84 | publishing { 85 | publications { 86 | main(MavenPublication) { 87 | artifact classifier: 'cyclonedx', source: new File(cyclonedxBom.destination.get(), cyclonedxBom.outputName.get() + '.xml') 88 | artifact classifier: 'cyclonedx', source: new File(cyclonedxBom.destination.get(), cyclonedxBom.outputName.get() + '.json') 89 | } 90 | } 91 | } 92 | 93 | project.generatePomFileForMainPublication.dependsOn(cyclonedxBom) 94 | } 95 | } 96 | } 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /docs/guide/guide.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | plugins { 19 | id 'org.kordamp.gradle.guide' 20 | id 'org.ajoberstar.git-publish' 21 | } -------------------------------------------------------------------------------- /docs/guide/src/docs/asciidoc/_links.adoc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kordamp/ezmorph/2250db44a38f88dbd3a4b88e20361636af0de0b8/docs/guide/src/docs/asciidoc/_links.adoc -------------------------------------------------------------------------------- /docs/guide/src/docs/asciidoc/compatibility.adoc: -------------------------------------------------------------------------------- 1 | 2 | [[_compatibility]] 3 | = Compatibility 4 | 5 | The following lists summarizes the differences between EZMorph 2.x and 1.x 6 | 7 | * All classes have moved from package `net.sf.ezmorph` to `org.kordamp.ezmorph`. 8 | * JDK5 is the new binary base line. 9 | * Java Generics have been added to method signatures. 10 | 11 | -------------------------------------------------------------------------------- /docs/guide/src/docs/asciidoc/configuration.adoc: -------------------------------------------------------------------------------- 1 | 2 | [[_configuration]] 3 | = Build Configuration 4 | 5 | == Gradle 6 | 7 | [source,groovy,options="nowrap"] 8 | [subs="attributes"] 9 | ---- 10 | dependencies { 11 | compile '{project-group}:{project-name}:{project-version}' 12 | } 13 | ---- 14 | 15 | == Maven 16 | 17 | [source,xml,options="nowrap"] 18 | [subs="attributes,verbatim"] 19 | ---- 20 | 21 | {project-group} 22 | {project-name} 23 | {project-version} 24 | 25 | ---- 26 | 27 | -------------------------------------------------------------------------------- /docs/guide/src/docs/asciidoc/index.adoc: -------------------------------------------------------------------------------- 1 | = EZMorph 2 | :author: {project-author} 3 | :revnumber: {project-version} 4 | :toclevels: 10 5 | 6 | include::{includedir}/_links.adoc[] 7 | 8 | :leveloffset: 1 9 | include::{includedir}/introduction.adoc[] 10 | include::{includedir}/usage.adoc[] 11 | include::{includedir}/configuration.adoc[] 12 | include::{includedir}/compatibility.adoc[] 13 | 14 | = Links 15 | 16 | link:api/index.html[Javadoc, window="_blank"] 17 | -------------------------------------------------------------------------------- /docs/guide/src/docs/asciidoc/introduction.adoc: -------------------------------------------------------------------------------- 1 | 2 | [[_introduction]] 3 | = Introduction 4 | 5 | EZMorph is simple java library for transforming an Object to another Object. 6 | 7 | EZMorph's key strenghts are: 8 | 9 | * Supports transformations for primitives and Objects 10 | * Supports transformations for multidimensional arrays 11 | * Supports transformations with DynaBeans 12 | * Small memory footprint (~80K) 13 | 14 | EZMorph comes with another feature: `ArrayAssertions`. JUnit 3.x does not have an `assertEquals()` method for asserting 15 | array equality, and JUnit 4.x has a limited one (it only supports Object[] not primitive arrays). With ArrayAssertions 16 | is possible to compare a boolean[] with a boolean[] or even a Boolean[], an those arrays can be multidimensional too. 17 | 18 | EZMorph began life as the converter package on https://github.com/aalmiray/json-lib[Json-lib, window="_blank"] but seeing 19 | that the features provided were more generic than JSON parsing, it became a project on its own. 20 | 21 | == Related projects 22 | 23 | There are other projects that perform Objetc to Object conversion: 24 | 25 | [cols="1,3", options="header"] 26 | |=== 27 | 28 | | Project Name | Description 29 | 30 | | http://commons.apache.org/proper/commons-beanutils/["Commons Beanutils", window="_blank"] 31 | | `ConvertUtils`: Utility methods for converting scalar String values to objects of the specified Class, String arrays 32 | to arrays of the specified Class. 33 | 34 | | http://commons.apache.org/proper/commons-lang/["Commons Lang", window="_blank"] 35 | | `ArrayUtils`: Operations on arrays, primitive arrays (like int[]) and primitive wrapper arrays (like Integer[]). 36 | 37 | | http://commons.apache.org/dormant/convert/["Commons Convert", window="_blank"] 38 | | Commons-Convert aims to provide a single library dedicated to the task of converting an object of one type to another. 39 | The first stage will focus on Object to String and String to Object conversions. 40 | 41 | | http://morph.sourceforge.net/[Morph, window="_blank"] 42 | | Morph is a Java framework that eases the internal interoperability of an application. As information flows through an 43 | application, it undergoes multiple transformations. Morph provides a standard way to implement these transformations. 44 | 45 | | http://gleamynode.net/dev/lorentz/docs/index.html["Lorentz", window="_blank"] 46 | | Lorentz is a generic object-to-object conversion framework. It provides a simple API to convert a Java objects of one 47 | type into an object of another type. 48 | 49 | | http://projects.spring.io/spring-framework/["Spring Framework", window="_blank"] 50 | | Spring has an excellent support for PropertyEditors, that can also be used to transform Objects to/from Strings. 51 | 52 | | http://dozer.sourceforge.net/["Dozer", window="_blank"] 53 | | Dozer is a powerful, yet simple Java Bean to Java Bean mapper that recursively copies data from one object to another. 54 | Typically, these Java Beans will be of different complex types. 55 | 56 | |=== 57 | 58 | -------------------------------------------------------------------------------- /docs/guide/src/docs/asciidoc/usage.adoc: -------------------------------------------------------------------------------- 1 | 2 | [[_usage]] 3 | = Usage 4 | 5 | Morphing Objects is as easy as calling `morph()` on a `Morpher` or `ObjectMorpher` instance. You may have noticed that 6 | `Morpher` does not have a `morph()` method but `ObjectMorpher` does, that is because `Morpher` is used on primitive Morphers 7 | and we want to avoid the prize of auto-boxing. 8 | 9 | Using EZMorph is as straight forward as shown in the next example 10 | 11 | [source,java] 12 | ---- 13 | int i = new IntMorpher().morph("123"); 14 | assertEquals(123, i); // true! 15 | 16 | String str = new StringMorpher().morph(Integer.valueOf(123)); 17 | assertEquals("123", str); // true! 18 | ---- 19 | 20 | You can morph arrays too. It doesn't matter how many dimensions the arrays may have, EZMorph will take care of the rest. 21 | 22 | [source,java] 23 | ---- 24 | Boolean[] bools = new ObjectArrayMorpher( 25 | new BooleanObjectMorpher()).morph( 26 | new String[]{"true", "false"}); 27 | assertEquals(Boolean.TRUE, bools[0]); // true! 28 | assertEquals(Boolean.FALSE, bools[1]); // true! 29 | ---- 30 | 31 | EZMorph can also transform beans of different types, even `DynaBeans` from apache commons-beanutils 32 | 33 | [source,java] 34 | ---- 35 | // will morph a BeanA into a BeanB, where a property of BeanB is also a property of BeanA 36 | BeanA beanA = ... // initialized elsewhere 37 | morpherRegistry.registerMorpher(new BeanMorpher(BeanB.class, morpherRegistry)); 38 | BeanB beanB = (BeanB) morpherRegistry.morph(BeanB.class, beanA); 39 | 40 | // will morph a DynaBean into a MyBean instance 41 | DynaBean dynaBean = ... // initialized elsewhere 42 | morpherRegistry.registerMorpher( new BeanMorpher(MyBean.class, morpherRegistry)); 43 | MyBean myBean = (MyBean) morpherRegistry.moprh(MyBean.class, dynaBean); 44 | ---- 45 | 46 | EZMorph comes with a handy class for working with Morphers named `MorpherRegistry`. It works much like ConvertUtils on 47 | commons-beanutils. This class is not a singleton like ConvertUtils, so it is possible to have multiple registries with 48 | different Morphers that support the same target class, but take different default values or support different source classes. 49 | Another convenient class is `MorphUtils`, you can register standard Morphers to any MorpherRegistry with it. 50 | 51 | [source,java] 52 | ---- 53 | MorpherRegistry morperRegistry = new MorpherRegistry(); 54 | MorphUtils.registerStandardMorphers(morpherRegistry); 55 | Integer i = (Integer) morpherRegistry.morph(Integer.class, "A"); 56 | // "A" is not a number, so morph() returns Integer(0) 57 | assertEquals(Integer.valueOf(0), i); 58 | ---- 59 | 60 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: Apache-2.0 3 | # 4 | # Copyright 2006-2024 Andres Almiray. 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 | group = org.kordamp.ezmorph 20 | sourceCompatibility = 1.8 21 | targetCompatibility = 1.8 22 | 23 | slf4jVersion = 2.0.13 24 | kordampPluginVersion = 0.54.0 25 | kordampBuildVersion = 3.4.0 26 | gitPluginVersion = 3.0.0 27 | cyclonedxPluginVersion = 1.8.2 28 | moditectPluginVersion = 1.0.0-rc3 29 | jacocoVersion = 0.8.12 30 | commonsLang3Version = 3.14.0 31 | beanutilsVersion = 1.9.4 32 | 33 | org.gradle.daemon = true 34 | org.gradle.caching = true 35 | org.gradle.parallel = false 36 | -------------------------------------------------------------------------------- /gradle/LICENSE_HEADER: -------------------------------------------------------------------------------- 1 | SPDX-License-Identifier: Apache-2.0 2 | 3 | Copyright ${copyrightYear} ${author}. 4 | 5 | Licensed under the Apache License, Version 2.0 (the "License"); 6 | you may not use this file except in compliance with the License. 7 | You may obtain a copy of the License at 8 | 9 | https://www.apache.org/licenses/LICENSE-2.0 10 | 11 | Unless required by applicable law or agreed to in writing, software 12 | distributed under the License is distributed on an "AS IS" BASIS, 13 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 | See the License for the specific language governing permissions and 15 | limitations under the License. 16 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kordamp/ezmorph/2250db44a38f88dbd3a4b88e20361636af0de0b8/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip 4 | networkTimeout=10000 5 | validateDistributionUrl=true 6 | zipStoreBase=GRADLE_USER_HOME 7 | zipStorePath=wrapper/dists 8 | -------------------------------------------------------------------------------- /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. 1>&2 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2 48 | echo. 1>&2 49 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 50 | echo location of your Java installation. 1>&2 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. 1>&2 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2 62 | echo. 1>&2 63 | echo Please set the JAVA_HOME variable in your environment to match the 1>&2 64 | echo location of your Java installation. 1>&2 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 | -------------------------------------------------------------------------------- /jreleaser.yml: -------------------------------------------------------------------------------- 1 | # 2 | # SPDX-License-Identifier: Apache-2.0 3 | # 4 | # Copyright 2006-2024 Andres Almiray 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 | # http://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 | project: 20 | name: ezmorph 21 | description: Simple Java library for transforming an Object to another Object. 22 | links: 23 | homepage: https://github.com/kordamp/ezmorph 24 | authors: 25 | - Andres Almiray 26 | license: Apache-2.0 27 | inceptionYear: 2006 28 | stereotype: NONE 29 | vendor: Kordamp 30 | java: 31 | groupId: org.kordamp.ezmorph 32 | version: 8 33 | tags: 34 | - 'pojo' 35 | - 'convert' 36 | 37 | release: 38 | github: 39 | branch: master 40 | overwrite: true 41 | milestone: 42 | name: '{{projectVersion}}' 43 | issues: 44 | enabled: true 45 | changelog: 46 | formatted: ALWAYS 47 | preset: conventional-commits 48 | format: '- {{commitShortHash}} {{commitTitle}}' 49 | contributors: 50 | format: '- {{contributorName}}{{#contributorUsernameAsLink}} ({{.}}){{/contributorUsernameAsLink}}' 51 | labelers: 52 | - label: 'dependencies' 53 | title: 'regex:^(?:deps(?:\(.*\))?!?):\s.*' 54 | order: 120 55 | categories: 56 | - title: '⚙️ Dependencies' 57 | key: 'dependencies' 58 | order: 70 59 | labels: 60 | - 'dependencies' 61 | hide: 62 | categories: 63 | - 'merge' 64 | contributors: 65 | - 'GitHub' 66 | replacers: 67 | - search: 'deps: ' 68 | 69 | signing: 70 | active: ALWAYS 71 | armored: true 72 | 73 | deploy: 74 | maven: 75 | nexus2: 76 | maven-central: 77 | active: RELEASE 78 | url: https://s01.oss.sonatype.org/service/local 79 | closeRepository: true 80 | releaseRepository: true 81 | stagingRepositories: 82 | - build/repos/local/release 83 | 84 | announce: 85 | twitter: 86 | active: RELEASE 87 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | pluginManagement { 19 | repositories { 20 | gradlePluginPortal() 21 | mavenCentral() 22 | mavenLocal() 23 | } 24 | plugins { 25 | id 'org.kordamp.gradle.coveralls' version kordampPluginVersion 26 | id 'org.kordamp.gradle.guide' version kordampPluginVersion 27 | id 'org.ajoberstar.git-publish' version gitPluginVersion 28 | } 29 | } 30 | 31 | buildscript { 32 | repositories { 33 | gradlePluginPortal() 34 | mavenCentral() 35 | mavenLocal() 36 | } 37 | dependencies { 38 | classpath "org.kordamp.gradle:kordamp-parentbuild:$kordampBuildVersion" 39 | classpath "org.cyclonedx:cyclonedx-gradle-plugin:$cyclonedxPluginVersion" 40 | classpath "org.moditect:moditect-gradle-plugin:$moditectPluginVersion" 41 | } 42 | } 43 | apply plugin: 'org.kordamp.gradle.kordamp-parentbuild' 44 | 45 | rootProject.name = 'ezmorph' 46 | 47 | projects { 48 | directories = ['docs', 'subprojects'] 49 | 50 | plugins { 51 | all { 52 | id 'base' 53 | id 'idea' 54 | } 55 | path(':') { 56 | id 'org.kordamp.gradle.java-project' 57 | } 58 | dirs(['subprojects']) { 59 | id 'java-library' 60 | id 'org.cyclonedx.bom' 61 | id 'org.moditect.gradleplugin' 62 | } 63 | } 64 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/ezmorph-core.gradle: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | dependencies { 19 | api("org.apache.commons:commons-lang3:$commonsLang3Version") { 20 | exclude group: 'commons-logging', module: 'commons-logging' 21 | } 22 | api("commons-beanutils:commons-beanutils:$beanutilsVersion") { 23 | exclude group: 'commons-logging', module: 'commons-logging' 24 | } 25 | api "org.slf4j:slf4j-api:$slf4jVersion" 26 | api "org.slf4j:jcl-over-slf4j:$slf4jVersion" 27 | api 'junit:junit:4.13.1' 28 | 29 | testRuntimeOnly "org.slf4j:slf4j-simple:$slf4jVersion" 30 | } 31 | 32 | addMainModuleInfo { 33 | version = project.version 34 | jvmVersion = '11' 35 | overwriteExistingFiles = true 36 | jdepsExtraArgs = ['-q'] 37 | module { 38 | moduleInfoFile = file('src/main/java11/module-info.java') 39 | } 40 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/MorphException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph; 19 | 20 | /** 21 | * A MorphException indicates that a call to 22 | * Morpher.morph() has failed to complete successfully.
23 | * Based on common-beauntils ConversionException.
24 | * 25 | * @author Andres Almiray 26 | */ 27 | public class MorphException extends RuntimeException { 28 | // ----------------------------------------------------------- Constructors 29 | 30 | /** 31 | * The root cause of this ConversionException, compatible 32 | * with JDK 1.4's extensions to java.lang.Throwable. 33 | */ 34 | protected Throwable cause = null; 35 | 36 | /** 37 | * Construct a new exception with the specified message. 38 | * 39 | * @param message The message describing this exception 40 | */ 41 | public MorphException(String message) { 42 | super(message); 43 | } 44 | 45 | /** 46 | * Construct a new exception with the specified message and root cause. 47 | * 48 | * @param message The message describing this exception 49 | * @param cause The root cause of this exception 50 | */ 51 | public MorphException(String message, Throwable cause) { 52 | super(message); 53 | this.cause = cause; 54 | } 55 | 56 | // ------------------------------------------------------------- Properties 57 | 58 | /** 59 | * Construct a new exception with the specified root cause. 60 | * 61 | * @param cause The root cause of this exception 62 | */ 63 | public MorphException(Throwable cause) { 64 | super(cause.getMessage()); 65 | this.cause = cause; 66 | } 67 | 68 | /** 69 | * Returns the cause of this exception. 70 | * 71 | * @return a Throwable that represents the cause of this exception 72 | */ 73 | public Throwable getCause() { 74 | return this.cause; 75 | } 76 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/Morpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph; 19 | 20 | /** 21 | * Marker interface for morphers.
22 | * All implementations must have a morph(Object value) method 23 | * that returns the appropiate morphed value. 24 | * 25 | * @author Andres Almiray 26 | */ 27 | public interface Morpher { 28 | /** 29 | * Returns the target Class for conversion. 30 | * 31 | * @return the target Class for conversion. 32 | */ 33 | Class morphsTo(); 34 | 35 | /** 36 | * Returns true if the Morpher supports conversion from this Class. 37 | * 38 | * @param clazz the source Class 39 | * @return true if clazz is supported by this morpher, false otherwise. 40 | */ 41 | boolean supports(Class clazz); 42 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/ObjectMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph; 19 | 20 | /** 21 | * Marker interface for morphers that return an Object.
22 | * 23 | * @author Andres Almiray 24 | */ 25 | public interface ObjectMorpher extends Morpher { 26 | /** 27 | * Morphs the input object into an output object of the supported type. 28 | * 29 | * @param value The input value to be morphed 30 | * @throws MorphException if conversion cannot be performed successfully 31 | */ 32 | Object morph(Object value); 33 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/array/AbstractArrayMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import org.kordamp.ezmorph.ObjectMorpher; 21 | 22 | import java.lang.reflect.Array; 23 | 24 | /** 25 | * Base class for array Morphers. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public abstract class AbstractArrayMorpher implements ObjectMorpher { 30 | private boolean useDefault = false; 31 | 32 | public AbstractArrayMorpher() { 33 | } 34 | 35 | /** 36 | * @param useDefault if morph() should return a default value if the value to 37 | * be morphed is null 38 | */ 39 | public AbstractArrayMorpher(boolean useDefault) { 40 | this.useDefault = useDefault; 41 | } 42 | 43 | /** 44 | * Returns if this morpher will use a default value. 45 | */ 46 | public boolean isUseDefault() { 47 | return useDefault; 48 | } 49 | 50 | /** 51 | * Sets if this morpher will use a default value. 52 | */ 53 | public void setUseDefault(boolean useDefault) { 54 | this.useDefault = useDefault; 55 | } 56 | 57 | public boolean supports(Class clazz) { 58 | return clazz.isArray(); 59 | } 60 | 61 | /** 62 | * Creates an array representing the dimensions for conversion. 63 | */ 64 | protected int[] createDimensions(int length, int initial) { 65 | Object dims = Array.newInstance(int.class, length); 66 | Array.set(dims, 0, new Integer(initial)); 67 | return (int[]) dims; 68 | } 69 | 70 | /** 71 | * Returns the number of dimensions in an array class. 72 | */ 73 | protected int getDimensions(Class arrayClass) { 74 | if (arrayClass == null || !arrayClass.isArray()) { 75 | return 0; 76 | } 77 | 78 | return 1 + getDimensions(arrayClass.getComponentType()); 79 | } 80 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/array/BooleanArrayMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | import org.kordamp.ezmorph.primitive.BooleanMorpher; 24 | 25 | import java.lang.reflect.Array; 26 | 27 | /** 28 | * Morphs an array to a boolean[]. 29 | * 30 | * @author Andres Almiray 31 | */ 32 | public final class BooleanArrayMorpher extends AbstractArrayMorpher { 33 | private static final Class BOOLEAN_ARRAY_CLASS = boolean[].class; 34 | private boolean defaultValue; 35 | 36 | public BooleanArrayMorpher() { 37 | super(false); 38 | } 39 | 40 | /** 41 | * @param defaultValue return value if the value to be morphed is null 42 | */ 43 | public BooleanArrayMorpher(boolean defaultValue) { 44 | super(true); 45 | this.defaultValue = defaultValue; 46 | } 47 | 48 | public boolean equals(Object obj) { 49 | if (this == obj) { 50 | return true; 51 | } 52 | if (obj == null) { 53 | return false; 54 | } 55 | 56 | if (!(obj instanceof BooleanArrayMorpher)) { 57 | return false; 58 | } 59 | 60 | BooleanArrayMorpher other = (BooleanArrayMorpher) obj; 61 | EqualsBuilder builder = new EqualsBuilder(); 62 | if (isUseDefault() && other.isUseDefault()) { 63 | builder.append(getDefaultValue(), other.getDefaultValue()); 64 | return builder.isEquals(); 65 | } 66 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 67 | } 68 | 69 | public boolean getDefaultValue() { 70 | return defaultValue; 71 | } 72 | 73 | public int hashCode() { 74 | HashCodeBuilder builder = new HashCodeBuilder(); 75 | if (isUseDefault()) { 76 | builder.append(getDefaultValue()); 77 | } 78 | return builder.toHashCode(); 79 | } 80 | 81 | public Object morph(Object array) { 82 | if (array == null) { 83 | return null; 84 | } 85 | 86 | if (BOOLEAN_ARRAY_CLASS.isAssignableFrom(array.getClass())) { 87 | // no conversion needed 88 | return (boolean[]) array; 89 | } 90 | 91 | if (array.getClass() 92 | .isArray()) { 93 | int length = Array.getLength(array); 94 | int dims = getDimensions(array.getClass()); 95 | int[] dimensions = createDimensions(dims, length); 96 | Object result = Array.newInstance(boolean.class, dimensions); 97 | BooleanMorpher morpher = isUseDefault() ? new BooleanMorpher(defaultValue) 98 | : new BooleanMorpher(); 99 | if (dims == 1) { 100 | for (int index = 0; index < length; index++) { 101 | Array.set(result, index, morpher.morph(Array.get(array, index)) ? Boolean.TRUE 102 | : Boolean.FALSE); 103 | } 104 | } else { 105 | for (int index = 0; index < length; index++) { 106 | Array.set(result, index, morph(Array.get(array, index))); 107 | } 108 | } 109 | return result; 110 | } else { 111 | throw new MorphException("argument is not an array: " + array.getClass()); 112 | } 113 | } 114 | 115 | public Class morphsTo() { 116 | return BOOLEAN_ARRAY_CLASS; 117 | } 118 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/array/ByteArrayMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | import org.kordamp.ezmorph.primitive.ByteMorpher; 24 | 25 | import java.lang.reflect.Array; 26 | 27 | /** 28 | * Morphs an array to a byte[]. 29 | * 30 | * @author Andres Almiray 31 | */ 32 | public final class ByteArrayMorpher extends AbstractArrayMorpher { 33 | private static final Class BYTE_ARRAY_CLASS = byte[].class; 34 | private byte defaultValue; 35 | 36 | public ByteArrayMorpher() { 37 | super(false); 38 | } 39 | 40 | /** 41 | * @param defaultValue return value if the value to be morphed is null 42 | */ 43 | public ByteArrayMorpher(byte defaultValue) { 44 | super(true); 45 | this.defaultValue = defaultValue; 46 | } 47 | 48 | public boolean equals(Object obj) { 49 | if (this == obj) { 50 | return true; 51 | } 52 | if (obj == null) { 53 | return false; 54 | } 55 | 56 | if (!(obj instanceof ByteArrayMorpher)) { 57 | return false; 58 | } 59 | 60 | ByteArrayMorpher other = (ByteArrayMorpher) obj; 61 | EqualsBuilder builder = new EqualsBuilder(); 62 | if (isUseDefault() && other.isUseDefault()) { 63 | builder.append(getDefaultValue(), other.getDefaultValue()); 64 | return builder.isEquals(); 65 | } 66 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 67 | } 68 | 69 | public byte getDefaultValue() { 70 | return defaultValue; 71 | } 72 | 73 | public int hashCode() { 74 | HashCodeBuilder builder = new HashCodeBuilder(); 75 | if (isUseDefault()) { 76 | builder.append(getDefaultValue()); 77 | } 78 | return builder.toHashCode(); 79 | } 80 | 81 | public Object morph(Object array) { 82 | if (array == null) { 83 | return null; 84 | } 85 | 86 | if (BYTE_ARRAY_CLASS.isAssignableFrom(array.getClass())) { 87 | // no conversion needed 88 | return (byte[]) array; 89 | } 90 | 91 | if (array.getClass() 92 | .isArray()) { 93 | int length = Array.getLength(array); 94 | int dims = getDimensions(array.getClass()); 95 | int[] dimensions = createDimensions(dims, length); 96 | Object result = Array.newInstance(byte.class, dimensions); 97 | ByteMorpher morpher = isUseDefault() ? new ByteMorpher(defaultValue) : new ByteMorpher(); 98 | if (dims == 1) { 99 | for (int index = 0; index < length; index++) { 100 | Array.set(result, index, morpher.morph(Array.get(array, index))); 101 | } 102 | } else { 103 | for (int index = 0; index < length; index++) { 104 | Array.set(result, index, morph(Array.get(array, index))); 105 | } 106 | } 107 | return result; 108 | } else { 109 | throw new MorphException("argument is not an array: " + array.getClass()); 110 | } 111 | } 112 | 113 | public Class morphsTo() { 114 | return BYTE_ARRAY_CLASS; 115 | } 116 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/array/CharArrayMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | import org.kordamp.ezmorph.primitive.CharMorpher; 24 | 25 | import java.lang.reflect.Array; 26 | 27 | /** 28 | * Morphs an array to a char[]. 29 | * 30 | * @author Andres Almiray 31 | */ 32 | public final class CharArrayMorpher extends AbstractArrayMorpher { 33 | private static final Class CHAR_ARRAY_CLASS = char[].class; 34 | private char defaultValue; 35 | 36 | public CharArrayMorpher() { 37 | super(false); 38 | } 39 | 40 | /** 41 | * @param defaultValue return value if the value to be morphed is null 42 | */ 43 | public CharArrayMorpher(char defaultValue) { 44 | super(true); 45 | this.defaultValue = defaultValue; 46 | } 47 | 48 | public boolean equals(Object obj) { 49 | if (this == obj) { 50 | return true; 51 | } 52 | if (obj == null) { 53 | return false; 54 | } 55 | 56 | if (!(obj instanceof CharArrayMorpher)) { 57 | return false; 58 | } 59 | 60 | CharArrayMorpher other = (CharArrayMorpher) obj; 61 | EqualsBuilder builder = new EqualsBuilder(); 62 | if (isUseDefault() && other.isUseDefault()) { 63 | builder.append(getDefaultValue(), other.getDefaultValue()); 64 | return builder.isEquals(); 65 | } 66 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 67 | } 68 | 69 | /** 70 | * Returns the default value for this Morpher. 71 | */ 72 | public char getDefaultValue() { 73 | return defaultValue; 74 | } 75 | 76 | public int hashCode() { 77 | HashCodeBuilder builder = new HashCodeBuilder(); 78 | if (isUseDefault()) { 79 | builder.append(getDefaultValue()); 80 | } 81 | return builder.toHashCode(); 82 | } 83 | 84 | public Object morph(Object array) { 85 | if (array == null) { 86 | return null; 87 | } 88 | 89 | if (CHAR_ARRAY_CLASS.isAssignableFrom(array.getClass())) { 90 | // no conversion needed 91 | return (char[]) array; 92 | } 93 | 94 | if (array.getClass() 95 | .isArray()) { 96 | int length = Array.getLength(array); 97 | int dims = getDimensions(array.getClass()); 98 | int[] dimensions = createDimensions(dims, length); 99 | Object result = Array.newInstance(char.class, dimensions); 100 | CharMorpher morpher = isUseDefault() ? new CharMorpher(defaultValue) : new CharMorpher(); 101 | if (dims == 1) { 102 | for (int index = 0; index < length; index++) { 103 | Array.set(result, index, morpher.morph(Array.get(array, index))); 104 | } 105 | } else { 106 | for (int index = 0; index < length; index++) { 107 | Array.set(result, index, morph(Array.get(array, index))); 108 | } 109 | } 110 | return result; 111 | } else { 112 | throw new MorphException("argument is not an array: " + array.getClass()); 113 | } 114 | } 115 | 116 | public Class morphsTo() { 117 | return CHAR_ARRAY_CLASS; 118 | } 119 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/array/DoubleArrayMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | import org.kordamp.ezmorph.primitive.DoubleMorpher; 24 | 25 | import java.lang.reflect.Array; 26 | 27 | /** 28 | * Morphs an array to a double[]. 29 | * 30 | * @author Andres Almiray 31 | */ 32 | public final class DoubleArrayMorpher extends AbstractArrayMorpher { 33 | private static final Class DOUBLE_ARRAY_CLASS = double[].class; 34 | private double defaultValue; 35 | 36 | public DoubleArrayMorpher() { 37 | super(false); 38 | } 39 | 40 | /** 41 | * @param defaultValue return value if the value to be morphed is null 42 | */ 43 | public DoubleArrayMorpher(double defaultValue) { 44 | super(true); 45 | this.defaultValue = defaultValue; 46 | } 47 | 48 | public boolean equals(Object obj) { 49 | if (this == obj) { 50 | return true; 51 | } 52 | if (obj == null) { 53 | return false; 54 | } 55 | 56 | if (!(obj instanceof DoubleArrayMorpher)) { 57 | return false; 58 | } 59 | 60 | DoubleArrayMorpher other = (DoubleArrayMorpher) obj; 61 | EqualsBuilder builder = new EqualsBuilder(); 62 | if (isUseDefault() && other.isUseDefault()) { 63 | builder.append(getDefaultValue(), other.getDefaultValue()); 64 | return builder.isEquals(); 65 | } 66 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 67 | } 68 | 69 | /** 70 | * Returns the default value for this Morpher. 71 | */ 72 | public double getDefaultValue() { 73 | return defaultValue; 74 | } 75 | 76 | public int hashCode() { 77 | HashCodeBuilder builder = new HashCodeBuilder(); 78 | if (isUseDefault()) { 79 | builder.append(getDefaultValue()); 80 | } 81 | return builder.toHashCode(); 82 | } 83 | 84 | public Object morph(Object array) { 85 | if (array == null) { 86 | return null; 87 | } 88 | 89 | if (DOUBLE_ARRAY_CLASS.isAssignableFrom(array.getClass())) { 90 | // no conversion needed 91 | return (double[]) array; 92 | } 93 | 94 | if (array.getClass() 95 | .isArray()) { 96 | int length = Array.getLength(array); 97 | int dims = getDimensions(array.getClass()); 98 | int[] dimensions = createDimensions(dims, length); 99 | Object result = Array.newInstance(double.class, dimensions); 100 | DoubleMorpher morpher = isUseDefault() ? new DoubleMorpher(defaultValue) 101 | : new DoubleMorpher(); 102 | if (dims == 1) { 103 | for (int index = 0; index < length; index++) { 104 | Array.set(result, index, morpher.morph(Array.get(array, index))); 105 | } 106 | } else { 107 | for (int index = 0; index < length; index++) { 108 | Array.set(result, index, morph(Array.get(array, index))); 109 | } 110 | } 111 | return result; 112 | } else { 113 | throw new MorphException("argument is not an array: " + array.getClass()); 114 | } 115 | } 116 | 117 | public Class morphsTo() { 118 | return DOUBLE_ARRAY_CLASS; 119 | } 120 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/array/FloatArrayMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | import org.kordamp.ezmorph.primitive.FloatMorpher; 24 | 25 | import java.lang.reflect.Array; 26 | 27 | /** 28 | * Morphs an array to a float[]. 29 | * 30 | * @author Andres Almiray 31 | */ 32 | public final class FloatArrayMorpher extends AbstractArrayMorpher { 33 | private static final Class FLOAT_ARRAY_CLASS = float[].class; 34 | private float defaultValue; 35 | 36 | public FloatArrayMorpher() { 37 | super(false); 38 | } 39 | 40 | /** 41 | * @param defaultValue return value if the value to be morphed is null 42 | */ 43 | public FloatArrayMorpher(float defaultValue) { 44 | super(true); 45 | this.defaultValue = defaultValue; 46 | } 47 | 48 | public boolean equals(Object obj) { 49 | if (this == obj) { 50 | return true; 51 | } 52 | if (obj == null) { 53 | return false; 54 | } 55 | 56 | if (!(obj instanceof FloatArrayMorpher)) { 57 | return false; 58 | } 59 | 60 | FloatArrayMorpher other = (FloatArrayMorpher) obj; 61 | EqualsBuilder builder = new EqualsBuilder(); 62 | if (isUseDefault() && other.isUseDefault()) { 63 | builder.append(getDefaultValue(), other.getDefaultValue()); 64 | return builder.isEquals(); 65 | } 66 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 67 | } 68 | 69 | /** 70 | * Returns the default value for this Morpher. 71 | */ 72 | public float getDefaultValue() { 73 | return defaultValue; 74 | } 75 | 76 | public int hashCode() { 77 | HashCodeBuilder builder = new HashCodeBuilder(); 78 | if (isUseDefault()) { 79 | builder.append(getDefaultValue()); 80 | } 81 | return builder.toHashCode(); 82 | } 83 | 84 | public Object morph(Object array) { 85 | if (array == null) { 86 | return null; 87 | } 88 | 89 | if (FLOAT_ARRAY_CLASS.isAssignableFrom(array.getClass())) { 90 | // no conversion needed 91 | return (float[]) array; 92 | } 93 | 94 | if (array.getClass() 95 | .isArray()) { 96 | int length = Array.getLength(array); 97 | int dims = getDimensions(array.getClass()); 98 | int[] dimensions = createDimensions(dims, length); 99 | Object result = Array.newInstance(float.class, dimensions); 100 | FloatMorpher morpher = isUseDefault() ? new FloatMorpher(defaultValue) 101 | : new FloatMorpher(); 102 | if (dims == 1) { 103 | for (int index = 0; index < length; index++) { 104 | Array.set(result, index, morpher.morph(Array.get(array, index))); 105 | } 106 | } else { 107 | for (int index = 0; index < length; index++) { 108 | Array.set(result, index, morph(Array.get(array, index))); 109 | } 110 | } 111 | return result; 112 | } else { 113 | throw new MorphException("argument is not an array: " + array.getClass()); 114 | } 115 | } 116 | 117 | public Class morphsTo() { 118 | return FLOAT_ARRAY_CLASS; 119 | } 120 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/array/IntArrayMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | import org.kordamp.ezmorph.primitive.IntMorpher; 24 | 25 | import java.lang.reflect.Array; 26 | 27 | /** 28 | * Morphs an array to a int[]. 29 | * 30 | * @author Andres Almiray 31 | */ 32 | public final class IntArrayMorpher extends AbstractArrayMorpher { 33 | private static final Class INT_ARRAY_CLASS = int[].class; 34 | private int defaultValue; 35 | 36 | public IntArrayMorpher() { 37 | super(false); 38 | } 39 | 40 | /** 41 | * @param defaultValue return value if the value to be morphed is null 42 | */ 43 | public IntArrayMorpher(int defaultValue) { 44 | super(true); 45 | this.defaultValue = defaultValue; 46 | } 47 | 48 | public boolean equals(Object obj) { 49 | if (this == obj) { 50 | return true; 51 | } 52 | if (obj == null) { 53 | return false; 54 | } 55 | 56 | if (!(obj instanceof IntArrayMorpher)) { 57 | return false; 58 | } 59 | 60 | IntArrayMorpher other = (IntArrayMorpher) obj; 61 | EqualsBuilder builder = new EqualsBuilder(); 62 | if (isUseDefault() && other.isUseDefault()) { 63 | builder.append(getDefaultValue(), other.getDefaultValue()); 64 | return builder.isEquals(); 65 | } 66 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 67 | } 68 | 69 | /** 70 | * Returns the default value for this Morpher. 71 | */ 72 | public int getDefaultValue() { 73 | return defaultValue; 74 | } 75 | 76 | public int hashCode() { 77 | HashCodeBuilder builder = new HashCodeBuilder(); 78 | if (isUseDefault()) { 79 | builder.append(getDefaultValue()); 80 | } 81 | return builder.toHashCode(); 82 | } 83 | 84 | public Object morph(Object array) { 85 | if (array == null) { 86 | return null; 87 | } 88 | 89 | if (INT_ARRAY_CLASS.isAssignableFrom(array.getClass())) { 90 | // no conversion needed 91 | return (int[]) array; 92 | } 93 | 94 | if (array.getClass() 95 | .isArray()) { 96 | int length = Array.getLength(array); 97 | int dims = getDimensions(array.getClass()); 98 | int[] dimensions = createDimensions(dims, length); 99 | Object result = Array.newInstance(int.class, dimensions); 100 | IntMorpher morpher = isUseDefault() ? new IntMorpher(defaultValue) : new IntMorpher(); 101 | if (dims == 1) { 102 | for (int index = 0; index < length; index++) { 103 | Array.set(result, index, morpher.morph(Array.get(array, index))); 104 | } 105 | } else { 106 | for (int index = 0; index < length; index++) { 107 | Array.set(result, index, morph(Array.get(array, index))); 108 | } 109 | } 110 | return result; 111 | } else { 112 | throw new MorphException("argument is not an array: " + array.getClass()); 113 | } 114 | } 115 | 116 | public Class morphsTo() { 117 | return INT_ARRAY_CLASS; 118 | } 119 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/array/LongArrayMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | import org.kordamp.ezmorph.primitive.LongMorpher; 24 | 25 | import java.lang.reflect.Array; 26 | 27 | /** 28 | * Morphs an array to a long[]. 29 | * 30 | * @author Andres Almiray 31 | */ 32 | public final class LongArrayMorpher extends AbstractArrayMorpher { 33 | private static final Class LONG_ARRAY_CLASS = long[].class; 34 | private long defaultValue; 35 | 36 | public LongArrayMorpher() { 37 | super(false); 38 | } 39 | 40 | /** 41 | * @param defaultValue return value if the value to be morphed is null 42 | */ 43 | public LongArrayMorpher(long defaultValue) { 44 | super(true); 45 | this.defaultValue = defaultValue; 46 | } 47 | 48 | public boolean equals(Object obj) { 49 | if (this == obj) { 50 | return true; 51 | } 52 | if (obj == null) { 53 | return false; 54 | } 55 | 56 | if (!(obj instanceof LongArrayMorpher)) { 57 | return false; 58 | } 59 | 60 | LongArrayMorpher other = (LongArrayMorpher) obj; 61 | EqualsBuilder builder = new EqualsBuilder(); 62 | if (isUseDefault() && other.isUseDefault()) { 63 | builder.append(getDefaultValue(), other.getDefaultValue()); 64 | return builder.isEquals(); 65 | } 66 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 67 | } 68 | 69 | /** 70 | * Returns the default value for this Morpher. 71 | */ 72 | public long getDefaultValue() { 73 | return defaultValue; 74 | } 75 | 76 | public int hashCode() { 77 | HashCodeBuilder builder = new HashCodeBuilder(); 78 | if (isUseDefault()) { 79 | builder.append(getDefaultValue()); 80 | } 81 | return builder.toHashCode(); 82 | } 83 | 84 | public Object morph(Object array) { 85 | if (array == null) { 86 | return null; 87 | } 88 | 89 | if (LONG_ARRAY_CLASS.isAssignableFrom(array.getClass())) { 90 | // no conversion needed 91 | return (long[]) array; 92 | } 93 | 94 | if (array.getClass() 95 | .isArray()) { 96 | int length = Array.getLength(array); 97 | int dims = getDimensions(array.getClass()); 98 | int[] dimensions = createDimensions(dims, length); 99 | Object result = Array.newInstance(long.class, dimensions); 100 | LongMorpher morpher = isUseDefault() ? new LongMorpher(defaultValue) : new LongMorpher(); 101 | if (dims == 1) { 102 | for (int index = 0; index < length; index++) { 103 | Array.set(result, index, morpher.morph(Array.get(array, index))); 104 | } 105 | } else { 106 | for (int index = 0; index < length; index++) { 107 | Array.set(result, index, morph(Array.get(array, index))); 108 | } 109 | } 110 | return result; 111 | } else { 112 | throw new MorphException("argument is not an array: " + array.getClass()); 113 | } 114 | } 115 | 116 | public Class morphsTo() { 117 | return LONG_ARRAY_CLASS; 118 | } 119 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/array/ShortArrayMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | import org.kordamp.ezmorph.primitive.ShortMorpher; 24 | 25 | import java.lang.reflect.Array; 26 | 27 | /** 28 | * Morphs an array to a short[]. 29 | * 30 | * @author Andres Almiray 31 | */ 32 | public final class ShortArrayMorpher extends AbstractArrayMorpher { 33 | private static final Class SHORT_ARRAY_CLASS = short[].class; 34 | private short defaultValue; 35 | 36 | public ShortArrayMorpher() { 37 | super(false); 38 | } 39 | 40 | /** 41 | * @param defaultValue return value if the value to be morphed is null 42 | */ 43 | public ShortArrayMorpher(short defaultValue) { 44 | super(true); 45 | this.defaultValue = defaultValue; 46 | } 47 | 48 | public boolean equals(Object obj) { 49 | if (this == obj) { 50 | return true; 51 | } 52 | if (obj == null) { 53 | return false; 54 | } 55 | 56 | if (!(obj instanceof ShortArrayMorpher)) { 57 | return false; 58 | } 59 | 60 | ShortArrayMorpher other = (ShortArrayMorpher) obj; 61 | EqualsBuilder builder = new EqualsBuilder(); 62 | if (isUseDefault() && other.isUseDefault()) { 63 | builder.append(getDefaultValue(), other.getDefaultValue()); 64 | return builder.isEquals(); 65 | } 66 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 67 | } 68 | 69 | /** 70 | * Returns the default value for this Morpher. 71 | */ 72 | public short getDefaultValue() { 73 | return defaultValue; 74 | } 75 | 76 | public int hashCode() { 77 | HashCodeBuilder builder = new HashCodeBuilder(); 78 | if (isUseDefault()) { 79 | builder.append(getDefaultValue()); 80 | } 81 | return builder.toHashCode(); 82 | } 83 | 84 | public Object morph(Object array) { 85 | if (array == null) { 86 | return null; 87 | } 88 | 89 | if (SHORT_ARRAY_CLASS.isAssignableFrom(array.getClass())) { 90 | // no conversion needed 91 | return (short[]) array; 92 | } 93 | 94 | if (array.getClass() 95 | .isArray()) { 96 | int length = Array.getLength(array); 97 | int dims = getDimensions(array.getClass()); 98 | int[] dimensions = createDimensions(dims, length); 99 | Object result = Array.newInstance(short.class, dimensions); 100 | ShortMorpher morpher = isUseDefault() ? new ShortMorpher(defaultValue) 101 | : new ShortMorpher(); 102 | if (dims == 1) { 103 | for (int index = 0; index < length; index++) { 104 | Array.set(result, index, morpher.morph(Array.get(array, index))); 105 | } 106 | } else { 107 | for (int index = 0; index < length; index++) { 108 | Array.set(result, index, morph(Array.get(array, index))); 109 | } 110 | } 111 | return result; 112 | } else { 113 | throw new MorphException("argument is not an array: " + array.getClass()); 114 | } 115 | } 116 | 117 | public Class morphsTo() { 118 | return SHORT_ARRAY_CLASS; 119 | } 120 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/array/package.html: -------------------------------------------------------------------------------- 1 | 20 | 21 | 22 | 23 | 24 | 25 |

Morphers for arrays.

26 | 27 | 28 | -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/bean/package.html: -------------------------------------------------------------------------------- 1 | 20 | 21 | 22 | 23 | 24 | 25 |

Morphers for JavaBeans and DynaBeans.

26 | 27 | -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/object/AbstractObjectMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import org.kordamp.ezmorph.ObjectMorpher; 21 | 22 | /** 23 | * Base class for ObjectMorpher implementations. 24 | * 25 | * @author Andres Almiray 26 | */ 27 | public abstract class AbstractObjectMorpher implements ObjectMorpher { 28 | private boolean useDefault; 29 | 30 | public AbstractObjectMorpher() { 31 | 32 | } 33 | 34 | /** 35 | * @param useDefault if morph() should return a default value if the value to 36 | * be morphed is null 37 | */ 38 | public AbstractObjectMorpher(boolean useDefault) { 39 | this.useDefault = useDefault; 40 | } 41 | 42 | /** 43 | * Returns if this morpher will use a default value. 44 | */ 45 | public boolean isUseDefault() { 46 | return useDefault; 47 | } 48 | 49 | /** 50 | * Sets if this morpher will use a default value. 51 | */ 52 | public void setUseDefault(boolean useDefault) { 53 | this.useDefault = useDefault; 54 | } 55 | 56 | /** 57 | * Returns true if the Morpher supports conversion from this Class.
58 | * Supports any type that is not an Array. 59 | * 60 | * @param clazz the source Class 61 | * @return true if clazz is supported by this morpher, false otherwise. 62 | */ 63 | public boolean supports(Class clazz) { 64 | return !clazz.isArray(); 65 | } 66 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/object/BigDecimalMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | import java.math.BigDecimal; 25 | import java.math.BigInteger; 26 | 27 | /** 28 | * Morphs to a BigDecimal. 29 | * 30 | * @author Andres Almiray 31 | */ 32 | public final class BigDecimalMorpher extends AbstractObjectMorpher { 33 | private BigDecimal defaultValue; 34 | 35 | public BigDecimalMorpher() { 36 | super(); 37 | } 38 | 39 | /** 40 | * @param defaultValue return value if the value to be morphed is null 41 | */ 42 | public BigDecimalMorpher(BigDecimal defaultValue) { 43 | super(true); 44 | this.defaultValue = defaultValue; 45 | } 46 | 47 | public boolean equals(Object obj) { 48 | if (this == obj) { 49 | return true; 50 | } 51 | if (obj == null) { 52 | return false; 53 | } 54 | 55 | if (!(obj instanceof BigDecimalMorpher)) { 56 | return false; 57 | } 58 | 59 | BigDecimalMorpher other = (BigDecimalMorpher) obj; 60 | EqualsBuilder builder = new EqualsBuilder(); 61 | if (isUseDefault() && other.isUseDefault()) { 62 | builder.append(getDefaultValue(), other.getDefaultValue()); 63 | return builder.isEquals(); 64 | } 65 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 66 | } 67 | 68 | /** 69 | * Returns the default value for this Morpher. 70 | */ 71 | public BigDecimal getDefaultValue() { 72 | return defaultValue; 73 | } 74 | 75 | public int hashCode() { 76 | HashCodeBuilder builder = new HashCodeBuilder(); 77 | if (isUseDefault()) { 78 | builder.append(getDefaultValue()); 79 | } 80 | return builder.toHashCode(); 81 | } 82 | 83 | public Object morph(Object value) { 84 | if (value instanceof BigDecimal) { 85 | return value; 86 | } 87 | 88 | if (value == null) { 89 | if (isUseDefault()) { 90 | return defaultValue; 91 | } else { 92 | return (BigDecimal) null; 93 | } 94 | } 95 | 96 | if (value instanceof Number) { 97 | if (value instanceof Float) { 98 | Float f = ((Float) value); 99 | if (f.isInfinite() || f.isNaN()) { 100 | throw new MorphException("BigDecimal can not be infinite or NaN"); 101 | } 102 | } else if (value instanceof Double) { 103 | Double d = ((Double) value); 104 | if (d.isInfinite() || d.isNaN()) { 105 | throw new MorphException("BigDecimal can not be infinite or NaN"); 106 | } 107 | } else if (value instanceof BigInteger) { 108 | return new BigDecimal((BigInteger) value); 109 | } 110 | 111 | return new BigDecimal(((Number) value).doubleValue()); 112 | } else { 113 | try { 114 | String str = String.valueOf(value) 115 | .trim(); 116 | if (str.length() == 0 || str.equalsIgnoreCase("null")) { 117 | return (BigDecimal) null; 118 | } else { 119 | return new BigDecimal(str); 120 | } 121 | } catch (NumberFormatException nfe) { 122 | if (isUseDefault()) { 123 | return defaultValue; 124 | } else { 125 | throw new MorphException(nfe); 126 | } 127 | } 128 | } 129 | } 130 | 131 | public Class morphsTo() { 132 | return BigDecimal.class; 133 | } 134 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/object/BooleanObjectMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | /** 25 | * Morphs to a Boolean. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class BooleanObjectMorpher extends AbstractObjectMorpher { 30 | private Boolean defaultValue; 31 | 32 | public BooleanObjectMorpher() { 33 | super(); 34 | } 35 | 36 | /** 37 | * @param defaultValue return value if the value to be morphed is null 38 | */ 39 | public BooleanObjectMorpher(Boolean defaultValue) { 40 | super(true); 41 | this.defaultValue = defaultValue; 42 | } 43 | 44 | public boolean equals(Object obj) { 45 | if (this == obj) { 46 | return true; 47 | } 48 | if (obj == null) { 49 | return false; 50 | } 51 | 52 | if (!(obj instanceof BooleanObjectMorpher)) { 53 | return false; 54 | } 55 | 56 | BooleanObjectMorpher other = (BooleanObjectMorpher) obj; 57 | EqualsBuilder builder = new EqualsBuilder(); 58 | if (isUseDefault() && other.isUseDefault()) { 59 | builder.append(getDefaultValue(), other.getDefaultValue()); 60 | return builder.isEquals(); 61 | } 62 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 63 | } 64 | 65 | /** 66 | * Returns the default value for this Morpher. 67 | */ 68 | public Boolean getDefaultValue() { 69 | return defaultValue; 70 | } 71 | 72 | public int hashCode() { 73 | HashCodeBuilder builder = new HashCodeBuilder(); 74 | if (isUseDefault()) { 75 | builder.append(getDefaultValue()); 76 | } 77 | return builder.toHashCode(); 78 | } 79 | 80 | public Object morph(Object value) { 81 | if (value == null) { 82 | if (isUseDefault()) { 83 | return defaultValue; 84 | } else { 85 | throw new MorphException("value is null"); 86 | } 87 | } 88 | 89 | if (value instanceof Boolean) { 90 | return (Boolean) value; 91 | } else { 92 | String s = String.valueOf(value); 93 | 94 | if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("yes") 95 | || s.equalsIgnoreCase("on")) { 96 | return Boolean.TRUE; 97 | } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("no") 98 | || s.equalsIgnoreCase("off")) { 99 | return Boolean.FALSE; 100 | } else if (isUseDefault()) { 101 | return defaultValue; 102 | } 103 | } 104 | 105 | throw new MorphException("Can't morph value: " + value); 106 | } 107 | 108 | public Class morphsTo() { 109 | return Boolean.class; 110 | } 111 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/object/CharacterObjectMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | /** 25 | * Morphs to a Character. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class CharacterObjectMorpher extends AbstractObjectMorpher { 30 | private Character defaultValue; 31 | 32 | public CharacterObjectMorpher() { 33 | super(); 34 | } 35 | 36 | /** 37 | * @param defaultValue return value if the value to be morphed is null 38 | */ 39 | public CharacterObjectMorpher(Character defaultValue) { 40 | super(true); 41 | this.defaultValue = defaultValue; 42 | } 43 | 44 | public boolean equals(Object obj) { 45 | if (this == obj) { 46 | return true; 47 | } 48 | if (obj == null) { 49 | return false; 50 | } 51 | 52 | if (!(obj instanceof CharacterObjectMorpher)) { 53 | return false; 54 | } 55 | 56 | CharacterObjectMorpher other = (CharacterObjectMorpher) obj; 57 | EqualsBuilder builder = new EqualsBuilder(); 58 | if (isUseDefault() && other.isUseDefault()) { 59 | builder.append(getDefaultValue(), other.getDefaultValue()); 60 | return builder.isEquals(); 61 | } 62 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 63 | } 64 | 65 | /** 66 | * Returns the default value for this Morpher. 67 | */ 68 | public Character getDefaultValue() { 69 | return defaultValue; 70 | } 71 | 72 | public int hashCode() { 73 | HashCodeBuilder builder = new HashCodeBuilder(); 74 | if (isUseDefault()) { 75 | builder.append(getDefaultValue()); 76 | } 77 | return builder.toHashCode(); 78 | } 79 | 80 | public Object morph(Object value) { 81 | if (value == null) { 82 | if (isUseDefault()) { 83 | return defaultValue; 84 | } else { 85 | throw new MorphException("value is null"); 86 | } 87 | } 88 | 89 | if (value instanceof Character) { 90 | return (Character) value; 91 | } else { 92 | String s = String.valueOf(value); 93 | if (s.length() > 0) { 94 | return new Character(s.charAt(0)); 95 | } else { 96 | if (isUseDefault()) { 97 | return defaultValue; 98 | } else { 99 | throw new MorphException("Can't morph value: " + value); 100 | } 101 | } 102 | } 103 | } 104 | 105 | public Class morphsTo() { 106 | return Character.class; 107 | } 108 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/object/ClassMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import org.kordamp.ezmorph.MorphException; 21 | import org.kordamp.ezmorph.ObjectMorpher; 22 | 23 | /** 24 | * Morphs to a Class.
25 | * This morpher is a singleton. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class ClassMorpher implements ObjectMorpher { 30 | private static final ClassMorpher INSTANCE = new ClassMorpher(); 31 | 32 | /** 33 | * Returns the singleton instance 34 | */ 35 | public static ClassMorpher getInstance() { 36 | return INSTANCE; 37 | } 38 | 39 | private ClassMorpher() { 40 | } 41 | 42 | public boolean equals(Object obj) { 43 | return INSTANCE == obj; 44 | } 45 | 46 | public int hashCode() { 47 | return 42 + getClass().hashCode(); 48 | } 49 | 50 | public Object morph(Object value) { 51 | if (value == null) { 52 | return null; 53 | } 54 | 55 | if (value instanceof Class) { 56 | return (Class) value; 57 | } 58 | 59 | if ("null".equals(value)) { 60 | return null; 61 | } 62 | 63 | try { 64 | return Class.forName(value.toString()); 65 | } catch (Exception e) { 66 | throw new MorphException(e); 67 | } 68 | } 69 | 70 | public Class morphsTo() { 71 | return Class.class; 72 | } 73 | 74 | public boolean supports(Class clazz) { 75 | return true; 76 | } 77 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/object/IdentityObjectMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import org.kordamp.ezmorph.ObjectMorpher; 21 | 22 | /** 23 | * Morpher that performs no conversion.
24 | * This morpher is a singleton. 25 | * 26 | * @author Andres Almiray 27 | */ 28 | public final class IdentityObjectMorpher implements ObjectMorpher { 29 | private static final IdentityObjectMorpher INSTANCE = new IdentityObjectMorpher(); 30 | 31 | /** 32 | * Returns the singleton instance 33 | */ 34 | public static IdentityObjectMorpher getInstance() { 35 | return INSTANCE; 36 | } 37 | 38 | private IdentityObjectMorpher() { 39 | } 40 | 41 | public boolean equals(Object obj) { 42 | return INSTANCE == obj; 43 | } 44 | 45 | public int hashCode() { 46 | return 42 + getClass().hashCode(); 47 | } 48 | 49 | public Object morph(Object value) { 50 | return value; 51 | } 52 | 53 | public Class morphsTo() { 54 | return Object.class; 55 | } 56 | 57 | public boolean supports(Class clazz) { 58 | return true; 59 | } 60 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/object/ObjectListMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import org.apache.commons.lang3.builder.HashCodeBuilder; 21 | import org.kordamp.ezmorph.MorphException; 22 | import org.kordamp.ezmorph.Morpher; 23 | 24 | import java.lang.reflect.Method; 25 | import java.util.ArrayList; 26 | import java.util.Iterator; 27 | import java.util.List; 28 | 29 | /** 30 | * Morphs a List to another List using a Morpher. 31 | * 32 | * @author Andres Almiray 33 | */ 34 | public final class ObjectListMorpher extends AbstractObjectMorpher { 35 | private Object defaultValue; 36 | private Morpher morpher; 37 | private Method morphMethod; 38 | 39 | /** 40 | * Creates a new ArrayMorpher which will use another Morpher for its inner 41 | * type.
42 | * The inner morpher can not morph to an array. Multiple dimension arrays are 43 | * already handled by this class. 44 | * 45 | * @param morpher the Morpher that will handle the array's inner type. 46 | */ 47 | public ObjectListMorpher(Morpher morpher) { 48 | setMorpher(morpher); 49 | } 50 | 51 | public ObjectListMorpher(Morpher morpher, Object defaultValue) { 52 | super(true); 53 | this.defaultValue = defaultValue; 54 | setMorpher(morpher); 55 | } 56 | 57 | public boolean equals(Object obj) { 58 | if (this == obj) { 59 | return true; 60 | } 61 | if (obj == null) { 62 | return false; 63 | } 64 | 65 | if (!(obj instanceof ObjectListMorpher)) { 66 | return false; 67 | } 68 | 69 | ObjectListMorpher other = (ObjectListMorpher) obj; 70 | return morpher.equals(other.morpher); 71 | } 72 | 73 | public int hashCode() { 74 | return new HashCodeBuilder().append(morpher) 75 | .toHashCode(); 76 | } 77 | 78 | public Object morph(Object value) { 79 | if (value == null) { 80 | return null; 81 | } 82 | 83 | if (!supports(value.getClass())) { 84 | throw new MorphException(value.getClass() + " is not supported"); 85 | } 86 | 87 | List list = new ArrayList(); 88 | for (Object object : ((List) value)) { 89 | if (object == null) { 90 | if (isUseDefault()) { 91 | list.add(defaultValue); 92 | } else { 93 | list.add(object); 94 | } 95 | } else { 96 | if (!morpher.supports(object.getClass())) { 97 | throw new MorphException(object.getClass() + " is not supported"); 98 | } 99 | try { 100 | list.add(morphMethod.invoke(morpher, object)); 101 | } catch (MorphException me) { 102 | throw me; 103 | } catch (Exception e) { 104 | throw new MorphException(e); 105 | } 106 | } 107 | } 108 | 109 | return list; 110 | } 111 | 112 | public Class morphsTo() { 113 | return List.class; 114 | } 115 | 116 | public boolean supports(Class clazz) { 117 | return clazz != null && List.class.isAssignableFrom(clazz); 118 | } 119 | 120 | private void setMorpher(Morpher morpher) { 121 | if (morpher == null) { 122 | throw new IllegalArgumentException("morpher can not be null"); 123 | } 124 | this.morpher = morpher; 125 | 126 | // cache the morph method 127 | try { 128 | morphMethod = morpher.getClass() 129 | .getDeclaredMethod("morph", new Class[]{Object.class}); 130 | } catch (NoSuchMethodException nsme) { 131 | throw new IllegalArgumentException(nsme.getMessage()); 132 | } 133 | } 134 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/object/StringMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import org.kordamp.ezmorph.MorphException; 21 | import org.kordamp.ezmorph.ObjectMorpher; 22 | 23 | /** 24 | * Morphs to a String.
25 | * This morpher is a singleton. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class StringMorpher implements ObjectMorpher { 30 | private static final StringMorpher INSTANCE = new StringMorpher(); 31 | 32 | /** 33 | * Returns the singleton instance 34 | */ 35 | public static StringMorpher getInstance() { 36 | return INSTANCE; 37 | } 38 | 39 | private StringMorpher() { 40 | } 41 | 42 | public boolean equals(Object obj) { 43 | return INSTANCE == obj; 44 | } 45 | 46 | public int hashCode() { 47 | return 42 + getClass().hashCode(); 48 | } 49 | 50 | public Object morph(Object value) { 51 | if (value == null) { 52 | return null; 53 | } 54 | 55 | if (!supports(value.getClass())) { 56 | throw new MorphException("Class not supported. " + value.getClass()); 57 | } 58 | 59 | if (String.class.isAssignableFrom(value.getClass())) { 60 | return (String) value; 61 | } 62 | 63 | return String.valueOf(value); 64 | } 65 | 66 | public Class morphsTo() { 67 | return String.class; 68 | } 69 | 70 | public boolean supports(Class clazz) { 71 | return !clazz.isArray(); 72 | } 73 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/object/SwitchingMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import org.apache.commons.lang3.builder.HashCodeBuilder; 21 | import org.kordamp.ezmorph.MorphException; 22 | import org.kordamp.ezmorph.MorpherRegistry; 23 | import org.kordamp.ezmorph.ObjectMorpher; 24 | 25 | import java.util.HashMap; 26 | import java.util.Map; 27 | 28 | /** 29 | * An all-purpose Morpher that can morph to several classes.
30 | * Because this Morpher accepts any class and morphs to Object it should not be 31 | * added to a MorpherRegistry as it may be too generic for some cases and may 32 | * result in unwanted transformations. 33 | * 34 | * @author Andres Almiray 35 | */ 36 | public class SwitchingMorpher implements ObjectMorpher { 37 | private Map, Class> classMap = new HashMap, Class>(); 38 | private MorpherRegistry morpherRegistry; 39 | 40 | public SwitchingMorpher(Map, Class> classMap, MorpherRegistry morpherRegistry) { 41 | this.morpherRegistry = morpherRegistry; 42 | if (classMap == null || classMap.isEmpty()) { 43 | throw new MorphException("Must specify at least one mapping"); 44 | } 45 | this.classMap.putAll(classMap); 46 | } 47 | 48 | public boolean equals(Object obj) { 49 | if (this == obj) { 50 | return true; 51 | } 52 | if (obj == null) { 53 | return false; 54 | } 55 | 56 | if (!(obj instanceof SwitchingMorpher)) { 57 | return false; 58 | } 59 | 60 | SwitchingMorpher other = (SwitchingMorpher) obj; 61 | if (classMap.size() != other.classMap.size()) { 62 | return false; 63 | } 64 | for (Map.Entry, Class> entry : classMap.entrySet()) { 65 | if (!other.classMap.containsKey(entry.getKey())) { 66 | return false; 67 | } 68 | if (!entry.getValue() 69 | .equals(other.classMap.get(entry.getKey()))) { 70 | return false; 71 | } 72 | } 73 | return true; 74 | } 75 | 76 | public int hashCode() { 77 | HashCodeBuilder builder = new HashCodeBuilder(); 78 | for (Map.Entry, Class> entry : classMap.entrySet()) { 79 | builder.append(entry.getKey()); 80 | builder.append(entry.getValue()); 81 | } 82 | return builder.toHashCode(); 83 | } 84 | 85 | public Object morph(Object value) { 86 | if (value == null) { 87 | return null; 88 | } 89 | 90 | Class target = (Class) classMap.get(value.getClass()); 91 | return morpherRegistry.morph(target, value); 92 | } 93 | 94 | public Class morphsTo() { 95 | return Object.class; 96 | } 97 | 98 | public boolean supports(Class clazz) { 99 | return true; 100 | } 101 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/object/package.html: -------------------------------------------------------------------------------- 1 | 20 | 21 | 22 | 23 | 24 | 25 |

Morphers for Object types.

26 | 27 | 28 | -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/package.html: -------------------------------------------------------------------------------- 1 | 20 | 21 | 22 | 23 | 24 | 25 |

26 | 27 | 28 | -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/AbstractDecimalMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | /** 21 | * Base class for primitive decimal conversion. 22 | * 23 | * @author Andres Almiray 24 | */ 25 | public abstract class AbstractDecimalMorpher extends AbstractPrimitiveMorpher { 26 | public AbstractDecimalMorpher() { 27 | super(); 28 | } 29 | 30 | /** 31 | * @param useDefault if morph() should return a default value if the value to 32 | * be morphed is null 33 | */ 34 | public AbstractDecimalMorpher(boolean useDefault) { 35 | super(useDefault); 36 | } 37 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/AbstractIntegerMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import java.util.Locale; 21 | 22 | /** 23 | * Base class por primitive integer conversion. 24 | * 25 | * @author Andres Almiray 26 | */ 27 | public abstract class AbstractIntegerMorpher extends AbstractPrimitiveMorpher { 28 | public AbstractIntegerMorpher() { 29 | super(); 30 | } 31 | 32 | /** 33 | * @param useDefault if morph() should return a default value if the value to 34 | * be morphed is null 35 | */ 36 | public AbstractIntegerMorpher(boolean useDefault) { 37 | super(useDefault); 38 | } 39 | 40 | /** 41 | * Trims the String from the beginning to the first "." 42 | */ 43 | protected String getIntegerValue(Object obj) { 44 | // use en_US Locale 45 | Locale defaultLocale = Locale.getDefault(); 46 | String str = null; 47 | try { 48 | Locale.setDefault(Locale.US); 49 | str = String.valueOf(obj); 50 | } finally { 51 | Locale.setDefault(defaultLocale); 52 | } 53 | 54 | int index = str.indexOf("."); 55 | if (index != -1) { 56 | str = str.substring(0, index); 57 | } 58 | return str; 59 | } 60 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/AbstractPrimitiveMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import org.kordamp.ezmorph.Morpher; 21 | 22 | /** 23 | * Base class for primitive value conversion.
24 | * 25 | * @author Andres Almiray 26 | */ 27 | public abstract class AbstractPrimitiveMorpher implements Morpher { 28 | private boolean useDefault = false; 29 | 30 | public AbstractPrimitiveMorpher() { 31 | 32 | } 33 | 34 | /** 35 | * @param useDefault if morph() should return a default value if the value to 36 | * be morphed is null 37 | */ 38 | public AbstractPrimitiveMorpher(boolean useDefault) { 39 | this.useDefault = useDefault; 40 | } 41 | 42 | /** 43 | * Returns if this morpher will use a default value if the value to be 44 | * morphed is null 45 | */ 46 | public boolean isUseDefault() { 47 | return useDefault; 48 | } 49 | 50 | /** 51 | * Returns true if the Morpher supports conversion from this Class.
52 | * Supports any type that is not an Array. 53 | * 54 | * @param clazz the source Class 55 | * @return true if clazz is supported by this morpher, false otherwise. 56 | */ 57 | public boolean supports(Class clazz) { 58 | return !clazz.isArray(); 59 | } 60 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/BooleanMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | /** 25 | * Morphs to a boolean. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class BooleanMorpher extends AbstractPrimitiveMorpher { 30 | private boolean defaultValue; 31 | 32 | public BooleanMorpher() { 33 | super(); 34 | } 35 | 36 | /** 37 | * @param defaultValue return value if the value to be morphed is null 38 | */ 39 | public BooleanMorpher(boolean defaultValue) { 40 | super(true); 41 | this.defaultValue = defaultValue; 42 | } 43 | 44 | public boolean equals(Object obj) { 45 | if (this == obj) { 46 | return true; 47 | } 48 | if (obj == null) { 49 | return false; 50 | } 51 | 52 | if (!(obj instanceof BooleanMorpher)) { 53 | return false; 54 | } 55 | 56 | BooleanMorpher other = (BooleanMorpher) obj; 57 | EqualsBuilder builder = new EqualsBuilder(); 58 | if (isUseDefault() && other.isUseDefault()) { 59 | builder.append(getDefaultValue(), other.getDefaultValue()); 60 | return builder.isEquals(); 61 | } 62 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 63 | } 64 | 65 | /** 66 | * Returns the default value for this Morpher. 67 | */ 68 | public boolean getDefaultValue() { 69 | return defaultValue; 70 | } 71 | 72 | public int hashCode() { 73 | HashCodeBuilder builder = new HashCodeBuilder(); 74 | if (isUseDefault()) { 75 | builder.append(getDefaultValue()); 76 | } 77 | return builder.toHashCode(); 78 | } 79 | 80 | /** 81 | * Morphs the input object into an output object of the supported type. 82 | * 83 | * @param value The input value to be morphed 84 | * @throws MorphException if conversion cannot be performed successfully 85 | */ 86 | public boolean morph(Object value) { 87 | if (value == null) { 88 | if (isUseDefault()) { 89 | return defaultValue; 90 | } else { 91 | throw new MorphException("value is null"); 92 | } 93 | } 94 | 95 | if (value instanceof Boolean) { 96 | return ((Boolean) value).booleanValue(); 97 | } else if (value instanceof Number) { 98 | if (value instanceof Double 99 | && (Double.isInfinite(((Number) value).doubleValue()) || Double.isNaN(((Number) value).doubleValue()))) { 100 | return true; 101 | } 102 | if (value instanceof Float 103 | && (Float.isInfinite(((Number) value).floatValue()) || Float.isNaN(((Number) value).floatValue()))) { 104 | return true; 105 | } 106 | long l = ((Number) value).longValue(); 107 | return l != 0; 108 | } else { 109 | String s = String.valueOf(value); 110 | 111 | if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("yes") 112 | || s.equalsIgnoreCase("on")) { 113 | return true; 114 | } else if (s.equalsIgnoreCase("false") || s.equalsIgnoreCase("no") 115 | || s.equalsIgnoreCase("off")) { 116 | return false; 117 | } else if (isUseDefault()) { 118 | return defaultValue; 119 | } 120 | } 121 | 122 | throw new MorphException("Can't morph value: " + value); 123 | } 124 | 125 | public Class morphsTo() { 126 | return Boolean.TYPE; 127 | } 128 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/ByteMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | /** 25 | * Morphs to a byte. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class ByteMorpher extends AbstractIntegerMorpher { 30 | private byte defaultValue; 31 | 32 | public ByteMorpher() { 33 | super(); 34 | } 35 | 36 | /** 37 | * @param defaultValue return value if the value to be morphed is null 38 | */ 39 | public ByteMorpher(byte defaultValue) { 40 | super(true); 41 | this.defaultValue = defaultValue; 42 | } 43 | 44 | public boolean equals(Object obj) { 45 | if (this == obj) { 46 | return true; 47 | } 48 | if (obj == null) { 49 | return false; 50 | } 51 | 52 | if (!(obj instanceof ByteMorpher)) { 53 | return false; 54 | } 55 | 56 | ByteMorpher other = (ByteMorpher) obj; 57 | EqualsBuilder builder = new EqualsBuilder(); 58 | if (isUseDefault() && other.isUseDefault()) { 59 | builder.append(getDefaultValue(), other.getDefaultValue()); 60 | return builder.isEquals(); 61 | } 62 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 63 | } 64 | 65 | /** 66 | * Returns the default value for this Morpher. 67 | */ 68 | public byte getDefaultValue() { 69 | return defaultValue; 70 | } 71 | 72 | public int hashCode() { 73 | HashCodeBuilder builder = new HashCodeBuilder(); 74 | if (isUseDefault()) { 75 | builder.append(getDefaultValue()); 76 | } 77 | return builder.toHashCode(); 78 | } 79 | 80 | /** 81 | * Morphs the input object into an output object of the supported type. 82 | * 83 | * @param value The input value to be morphed 84 | * @throws MorphException if conversion cannot be performed successfully 85 | */ 86 | public byte morph(Object value) { 87 | if (value == null) { 88 | if (isUseDefault()) { 89 | return defaultValue; 90 | } else { 91 | throw new MorphException("value is null"); 92 | } 93 | } 94 | 95 | if (value instanceof Number) { 96 | return ((Number) value).byteValue(); 97 | } else { 98 | byte i = 0; 99 | try { 100 | i = Byte.parseByte(getIntegerValue(value)); 101 | return i; 102 | } catch (NumberFormatException nfe) { 103 | if (isUseDefault()) { 104 | return defaultValue; 105 | } else { 106 | throw new MorphException(nfe); 107 | } 108 | } 109 | } 110 | } 111 | 112 | public Class morphsTo() { 113 | return Byte.TYPE; 114 | } 115 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/CharMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | /** 25 | * Morphs to a char. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class CharMorpher extends AbstractPrimitiveMorpher { 30 | private char defaultValue; 31 | 32 | public CharMorpher() { 33 | super(); 34 | } 35 | 36 | /** 37 | * @param defaultValue return value if the value to be morphed is null 38 | */ 39 | public CharMorpher(char defaultValue) { 40 | super(true); 41 | this.defaultValue = defaultValue; 42 | } 43 | 44 | public boolean equals(Object obj) { 45 | if (this == obj) { 46 | return true; 47 | } 48 | if (obj == null) { 49 | return false; 50 | } 51 | 52 | if (!(obj instanceof CharMorpher)) { 53 | return false; 54 | } 55 | 56 | CharMorpher other = (CharMorpher) obj; 57 | EqualsBuilder builder = new EqualsBuilder(); 58 | if (isUseDefault() && other.isUseDefault()) { 59 | builder.append(getDefaultValue(), other.getDefaultValue()); 60 | return builder.isEquals(); 61 | } 62 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 63 | } 64 | 65 | /** 66 | * Returns the default value for this Morpher. 67 | */ 68 | public char getDefaultValue() { 69 | return defaultValue; 70 | } 71 | 72 | public int hashCode() { 73 | HashCodeBuilder builder = new HashCodeBuilder(); 74 | if (isUseDefault()) { 75 | builder.append(getDefaultValue()); 76 | } 77 | return builder.toHashCode(); 78 | } 79 | 80 | /** 81 | * Morphs the input object into an output object of the supported type. 82 | * 83 | * @param value The input value to be morphed 84 | * @throws MorphException if conversion cannot be performed successfully 85 | */ 86 | public char morph(Object value) { 87 | if (value == null) { 88 | if (isUseDefault()) { 89 | return defaultValue; 90 | } else { 91 | throw new MorphException("value is null"); 92 | } 93 | } 94 | 95 | if (value instanceof Character) { 96 | return ((Character) value).charValue(); 97 | } else { 98 | String s = String.valueOf(value); 99 | if (s.length() > 0) { 100 | return s.charAt(0); 101 | } else { 102 | if (isUseDefault()) { 103 | return defaultValue; 104 | } else { 105 | throw new MorphException("Can't morph value: " + value); 106 | } 107 | } 108 | } 109 | } 110 | 111 | public Class morphsTo() { 112 | return Character.TYPE; 113 | } 114 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/DoubleMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | /** 25 | * Morphs to a double. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class DoubleMorpher extends AbstractDecimalMorpher { 30 | private double defaultValue; 31 | 32 | public DoubleMorpher() { 33 | super(); 34 | } 35 | 36 | /** 37 | * @param defaultValue return value if the value to be morphed is null 38 | */ 39 | public DoubleMorpher(double defaultValue) { 40 | super(true); 41 | this.defaultValue = defaultValue; 42 | } 43 | 44 | public boolean equals(Object obj) { 45 | if (this == obj) { 46 | return true; 47 | } 48 | if (obj == null) { 49 | return false; 50 | } 51 | 52 | if (!(obj instanceof DoubleMorpher)) { 53 | return false; 54 | } 55 | 56 | DoubleMorpher other = (DoubleMorpher) obj; 57 | EqualsBuilder builder = new EqualsBuilder(); 58 | if (isUseDefault() && other.isUseDefault()) { 59 | builder.append(getDefaultValue(), other.getDefaultValue()); 60 | return builder.isEquals(); 61 | } 62 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 63 | } 64 | 65 | /** 66 | * Returns the default value for this Morpher. 67 | */ 68 | public double getDefaultValue() { 69 | return defaultValue; 70 | } 71 | 72 | public int hashCode() { 73 | HashCodeBuilder builder = new HashCodeBuilder(); 74 | if (isUseDefault()) { 75 | builder.append(getDefaultValue()); 76 | } 77 | return builder.toHashCode(); 78 | } 79 | 80 | /** 81 | * Morphs the input object into an output object of the supported type. 82 | * 83 | * @param value The input value to be morphed 84 | * @throws MorphException if conversion cannot be performed successfully 85 | */ 86 | public double morph(Object value) { 87 | if (value == null) { 88 | if (isUseDefault()) { 89 | return defaultValue; 90 | } else { 91 | throw new MorphException("value is null"); 92 | } 93 | } 94 | 95 | if (value instanceof Number) { 96 | return ((Number) value).doubleValue(); 97 | } else { 98 | double i = 0; 99 | try { 100 | i = Double.parseDouble(String.valueOf(value)); 101 | return i; 102 | } catch (NumberFormatException nfe) { 103 | if (isUseDefault()) { 104 | return defaultValue; 105 | } else { 106 | throw new MorphException(nfe); 107 | } 108 | } 109 | } 110 | } 111 | 112 | public Class morphsTo() { 113 | return Double.TYPE; 114 | } 115 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/FloatMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | /** 25 | * Moprhs to a float. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class FloatMorpher extends AbstractDecimalMorpher { 30 | private float defaultValue; 31 | 32 | public FloatMorpher() { 33 | super(); 34 | } 35 | 36 | /** 37 | * @param defaultValue return value if the value to be morphed is null 38 | */ 39 | public FloatMorpher(float defaultValue) { 40 | super(true); 41 | this.defaultValue = defaultValue; 42 | } 43 | 44 | public boolean equals(Object obj) { 45 | if (this == obj) { 46 | return true; 47 | } 48 | if (obj == null) { 49 | return false; 50 | } 51 | 52 | if (!(obj instanceof FloatMorpher)) { 53 | return false; 54 | } 55 | 56 | FloatMorpher other = (FloatMorpher) obj; 57 | EqualsBuilder builder = new EqualsBuilder(); 58 | if (isUseDefault() && other.isUseDefault()) { 59 | builder.append(getDefaultValue(), other.getDefaultValue()); 60 | return builder.isEquals(); 61 | } 62 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 63 | } 64 | 65 | /** 66 | * Returns the default value for this Morpher. 67 | */ 68 | public float getDefaultValue() { 69 | return defaultValue; 70 | } 71 | 72 | public int hashCode() { 73 | HashCodeBuilder builder = new HashCodeBuilder(); 74 | if (isUseDefault()) { 75 | builder.append(getDefaultValue()); 76 | } 77 | return builder.toHashCode(); 78 | } 79 | 80 | /** 81 | * Morphs the input object into an output object of the supported type. 82 | * 83 | * @param value The input value to be morphed 84 | * @throws MorphException if conversion cannot be performed successfully 85 | */ 86 | public float morph(Object value) { 87 | if (value == null) { 88 | if (isUseDefault()) { 89 | return defaultValue; 90 | } else { 91 | throw new MorphException("value is null"); 92 | } 93 | } 94 | 95 | if (value instanceof Number) { 96 | return ((Number) value).floatValue(); 97 | } else { 98 | float i = 0; 99 | try { 100 | i = Float.parseFloat(String.valueOf(value)); 101 | return i; 102 | } catch (NumberFormatException nfe) { 103 | if (isUseDefault()) { 104 | return defaultValue; 105 | } else { 106 | throw new MorphException(nfe); 107 | } 108 | } 109 | } 110 | } 111 | 112 | public Class morphsTo() { 113 | return Float.TYPE; 114 | } 115 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/IntMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | /** 25 | * Morphs to an int. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class IntMorpher extends AbstractIntegerMorpher { 30 | private int defaultValue; 31 | 32 | public IntMorpher() { 33 | super(); 34 | } 35 | 36 | /** 37 | * @param defaultValue return value if the value to be morphed is null 38 | */ 39 | public IntMorpher(int defaultValue) { 40 | super(true); 41 | this.defaultValue = defaultValue; 42 | } 43 | 44 | public boolean equals(Object obj) { 45 | if (this == obj) { 46 | return true; 47 | } 48 | if (obj == null) { 49 | return false; 50 | } 51 | 52 | if (!(obj instanceof IntMorpher)) { 53 | return false; 54 | } 55 | 56 | IntMorpher other = (IntMorpher) obj; 57 | EqualsBuilder builder = new EqualsBuilder(); 58 | if (isUseDefault() && other.isUseDefault()) { 59 | builder.append(getDefaultValue(), other.getDefaultValue()); 60 | return builder.isEquals(); 61 | } 62 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 63 | } 64 | 65 | /** 66 | * Returns the default value for this Morpher. 67 | */ 68 | public int getDefaultValue() { 69 | return defaultValue; 70 | } 71 | 72 | public int hashCode() { 73 | HashCodeBuilder builder = new HashCodeBuilder(); 74 | if (isUseDefault()) { 75 | builder.append(getDefaultValue()); 76 | } 77 | return builder.toHashCode(); 78 | } 79 | 80 | /** 81 | * Morphs the input object into an output object of the supported type. 82 | * 83 | * @param value The input value to be morphed 84 | * @throws MorphException if conversion cannot be performed successfully 85 | */ 86 | public int morph(Object value) { 87 | if (value == null) { 88 | if (isUseDefault()) { 89 | return defaultValue; 90 | } else { 91 | throw new MorphException("value is null"); 92 | } 93 | } 94 | 95 | if (value instanceof Number) { 96 | return ((Number) value).intValue(); 97 | } else { 98 | int i = 0; 99 | try { 100 | i = Integer.parseInt(getIntegerValue(value)); 101 | return i; 102 | } catch (NumberFormatException nfe) { 103 | if (isUseDefault()) { 104 | return defaultValue; 105 | } else { 106 | throw new MorphException(nfe); 107 | } 108 | } 109 | } 110 | } 111 | 112 | public Class morphsTo() { 113 | return Integer.TYPE; 114 | } 115 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/LongMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | /** 25 | * Morphs to a long. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class LongMorpher extends AbstractIntegerMorpher { 30 | private long defaultValue; 31 | 32 | public LongMorpher() { 33 | super(); 34 | } 35 | 36 | /** 37 | * @param defaultValue return value if the value to be morphed is null 38 | */ 39 | public LongMorpher(long defaultValue) { 40 | super(true); 41 | this.defaultValue = defaultValue; 42 | } 43 | 44 | public boolean equals(Object obj) { 45 | if (this == obj) { 46 | return true; 47 | } 48 | if (obj == null) { 49 | return false; 50 | } 51 | 52 | if (!(obj instanceof LongMorpher)) { 53 | return false; 54 | } 55 | 56 | LongMorpher other = (LongMorpher) obj; 57 | EqualsBuilder builder = new EqualsBuilder(); 58 | if (isUseDefault() && other.isUseDefault()) { 59 | builder.append(getDefaultValue(), other.getDefaultValue()); 60 | return builder.isEquals(); 61 | } 62 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 63 | } 64 | 65 | /** 66 | * Returns the default value for this Morpher. 67 | */ 68 | public long getDefaultValue() { 69 | return defaultValue; 70 | } 71 | 72 | public int hashCode() { 73 | HashCodeBuilder builder = new HashCodeBuilder(); 74 | if (isUseDefault()) { 75 | builder.append(getDefaultValue()); 76 | } 77 | return builder.toHashCode(); 78 | } 79 | 80 | /** 81 | * Morphs the input object into an output object of the supported type. 82 | * 83 | * @param value The input value to be morphed 84 | * @throws MorphException if conversion cannot be performed successfully 85 | */ 86 | public long morph(Object value) { 87 | if (value == null) { 88 | if (isUseDefault()) { 89 | return defaultValue; 90 | } else { 91 | throw new MorphException("value is null"); 92 | } 93 | } 94 | 95 | if (value instanceof Number) { 96 | return ((Number) value).longValue(); 97 | } else { 98 | long i = 0; 99 | try { 100 | i = Long.parseLong(getIntegerValue(value)); 101 | return i; 102 | } catch (NumberFormatException nfe) { 103 | if (isUseDefault()) { 104 | return defaultValue; 105 | } else { 106 | throw new MorphException(nfe); 107 | } 108 | } 109 | } 110 | } 111 | 112 | public Class morphsTo() { 113 | return Long.TYPE; 114 | } 115 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/ShortMorpher.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.kordamp.ezmorph.MorphException; 23 | 24 | /** 25 | * Morphs to a short. 26 | * 27 | * @author Andres Almiray 28 | */ 29 | public final class ShortMorpher extends AbstractIntegerMorpher { 30 | private short defaultValue; 31 | 32 | public ShortMorpher() { 33 | super(); 34 | } 35 | 36 | /** 37 | * @param defaultValue return value if the value to be morphed is null 38 | */ 39 | public ShortMorpher(short defaultValue) { 40 | super(true); 41 | this.defaultValue = defaultValue; 42 | } 43 | 44 | public boolean equals(Object obj) { 45 | if (this == obj) { 46 | return true; 47 | } 48 | if (obj == null) { 49 | return false; 50 | } 51 | 52 | if (!(obj instanceof ShortMorpher)) { 53 | return false; 54 | } 55 | 56 | ShortMorpher other = (ShortMorpher) obj; 57 | EqualsBuilder builder = new EqualsBuilder(); 58 | if (isUseDefault() && other.isUseDefault()) { 59 | builder.append(getDefaultValue(), other.getDefaultValue()); 60 | return builder.isEquals(); 61 | } 62 | return !isUseDefault() && !other.isUseDefault() && builder.isEquals(); 63 | } 64 | 65 | /** 66 | * Returns the default value for this Morpher. 67 | */ 68 | public short getDefaultValue() { 69 | return defaultValue; 70 | } 71 | 72 | public int hashCode() { 73 | HashCodeBuilder builder = new HashCodeBuilder(); 74 | if (isUseDefault()) { 75 | builder.append(getDefaultValue()); 76 | } 77 | return builder.toHashCode(); 78 | } 79 | 80 | /** 81 | * Morphs the input object into an output object of the supported type. 82 | * 83 | * @param value The input value to be morphed 84 | * @throws MorphException if conversion cannot be performed successfully 85 | */ 86 | public short morph(Object value) { 87 | if (value == null) { 88 | if (isUseDefault()) { 89 | return defaultValue; 90 | } else { 91 | throw new MorphException("value is null"); 92 | } 93 | } 94 | 95 | if (value instanceof Number) { 96 | return ((Number) value).shortValue(); 97 | } else { 98 | short i = 0; 99 | try { 100 | i = Short.parseShort(getIntegerValue(value)); 101 | return i; 102 | } catch (NumberFormatException nfe) { 103 | if (isUseDefault()) { 104 | return defaultValue; 105 | } else { 106 | throw new MorphException(nfe); 107 | } 108 | } 109 | } 110 | } 111 | 112 | public Class morphsTo() { 113 | return Short.TYPE; 114 | } 115 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/primitive/package.html: -------------------------------------------------------------------------------- 1 | 20 | 21 | 22 | 23 | 24 | 25 |

Morphers for primitive types.

26 | 27 | 28 | -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java/org/kordamp/ezmorph/test/package.html: -------------------------------------------------------------------------------- 1 | 20 | 21 | 22 | 23 | 24 | 25 |

Assertions for testing array equiality.

26 | 27 | 28 | -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/main/java11/module-info.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | * @author Andres Almiray 21 | */ 22 | module org.kordamp.ezmorph { 23 | requires java.desktop; 24 | requires org.apache.commons.lang3; 25 | requires org.slf4j; 26 | 27 | requires transitive commons.beanutils; 28 | requires transitive junit; 29 | 30 | exports org.kordamp.ezmorph; 31 | exports org.kordamp.ezmorph.array; 32 | exports org.kordamp.ezmorph.bean; 33 | exports org.kordamp.ezmorph.object; 34 | exports org.kordamp.ezmorph.primitive; 35 | exports org.kordamp.ezmorph.test; 36 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/array/AbstractArrayMorpherTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestCase; 22 | import junit.framework.TestSuite; 23 | import junit.textui.TestRunner; 24 | import org.kordamp.ezmorph.Morpher; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public abstract class AbstractArrayMorpherTestCase extends TestCase { 30 | public static void main(String[] args) { 31 | TestRunner.run(suite()); 32 | } 33 | 34 | public static Test suite() { 35 | TestSuite suite = new TestSuite(AbstractArrayMorpherTestCase.class); 36 | suite.setName("AbstractArrayMorpher Tests"); 37 | return suite; 38 | } 39 | 40 | public AbstractArrayMorpherTestCase(String name) { 41 | super(name); 42 | } 43 | 44 | // ----------------------------------------------------------------------- 45 | 46 | public void testEquals_another_Morpher() { 47 | assertFalse(getMorpherWithDefaultValue().equals(getAnotherMorpherWithDefaultValue())); 48 | assertTrue(getMorpher().equals(getAnotherMorpher())); 49 | } 50 | 51 | public void testEquals_different_morpher() { 52 | assertFalse(getMorpher().equals(new Morpher() { 53 | public Class morphsTo() { 54 | return null; 55 | } 56 | 57 | public boolean supports(Class clazz) { 58 | return false; 59 | } 60 | })); 61 | } 62 | 63 | public void testEquals_morpher_withDefaultValue() { 64 | assertFalse(getMorpher().equals(getMorpherWithDefaultValue())); 65 | } 66 | 67 | public void testEquals_null() { 68 | assertFalse(getMorpher().equals(null)); 69 | } 70 | 71 | public void testEquals_same_morpher() { 72 | assertTrue(getMorpher().equals(getMorpher())); 73 | assertTrue(getMorpherWithDefaultValue().equals(getMorpherWithDefaultValue())); 74 | } 75 | 76 | public void testHashCode_morpher_withDefaultValue() { 77 | assertTrue(getMorpher().hashCode() != getMorpherWithDefaultValue().hashCode()); 78 | } 79 | 80 | public void testHashCode_same_morpher() { 81 | assertEquals(getMorpher().hashCode(), getMorpher().hashCode()); 82 | assertEquals(getMorpherWithDefaultValue().hashCode(), 83 | getMorpherWithDefaultValue().hashCode()); 84 | } 85 | 86 | public void testMorphsTo() { 87 | assertEquals(getMorphsToClass(), getMorpher().morphsTo()); 88 | } 89 | 90 | // ----------------------------------------------------------------------- 91 | 92 | protected abstract AbstractArrayMorpher getAnotherMorpher(); 93 | 94 | protected abstract AbstractArrayMorpher getAnotherMorpherWithDefaultValue(); 95 | 96 | protected abstract AbstractArrayMorpher getMorpher(); 97 | 98 | protected abstract AbstractArrayMorpher getMorpherWithDefaultValue(); 99 | 100 | protected abstract Class getMorphsToClass(); 101 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/array/IntArrayMorpherTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.array; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestSuite; 22 | import junit.textui.TestRunner; 23 | import org.kordamp.ezmorph.MorphException; 24 | import org.kordamp.ezmorph.test.ArrayAssertions; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public class IntArrayMorpherTest extends AbstractArrayMorpherTestCase { 30 | public static void main(String[] args) { 31 | TestRunner.run(suite()); 32 | } 33 | 34 | public static Test suite() { 35 | TestSuite suite = new TestSuite(IntArrayMorpherTest.class); 36 | suite.setName("IntArrayMorpher Tests"); 37 | return suite; 38 | } 39 | 40 | private IntArrayMorpher anotherMorpher; 41 | private IntArrayMorpher anotherMorpherWithDefaultValue; 42 | private IntArrayMorpher morpher; 43 | private IntArrayMorpher morpherWithDefaultValue; 44 | 45 | public IntArrayMorpherTest(String name) { 46 | super(name); 47 | } 48 | 49 | // ----------------------------------------------------------------------- 50 | 51 | public void testMorph_illegalArgument() { 52 | try { 53 | // argument is not an array 54 | morpher.morph(""); 55 | } catch (MorphException expected) { 56 | // ok 57 | } 58 | } 59 | 60 | public void testMorph_intArray() { 61 | int[] expected = {1, 2, 3}; 62 | int[] actual = (int[]) morpher.morph(expected); 63 | ArrayAssertions.assertEquals(expected, actual); 64 | } 65 | 66 | public void testMorph_intArray_threedims() { 67 | int[][][] expected = {{{1}, {2}}, {{3}, {4}}}; 68 | int[][][] actual = (int[][][]) morpher.morph(expected); 69 | ArrayAssertions.assertEquals(expected, actual); 70 | } 71 | 72 | public void testMorph_intArray_twodims() { 73 | int[][] expected = {{1, 2, 3}, {4, 5, 6}}; 74 | int[][] actual = (int[][]) morpher.morph(expected); 75 | ArrayAssertions.assertEquals(expected, actual); 76 | } 77 | 78 | public void testMorph_null() { 79 | assertNull(morpher.morph(null)); 80 | } 81 | 82 | public void testMorph_strings() { 83 | String[] expected = {"1", "2", "3.3"}; 84 | int[] actual = (int[]) morpher.morph(expected); 85 | ArrayAssertions.assertEquals(new int[]{1, 2, 3}, actual); 86 | } 87 | 88 | public void testMorph_strings_twodims() { 89 | String[][] expected = {{"1", "2", "3.3"}, {"4", "5", "6.6"}}; 90 | int[][] actual = (int[][]) morpher.morph(expected); 91 | ArrayAssertions.assertEquals(new int[][]{{1, 2, 3}, {4, 5, 6}}, actual); 92 | } 93 | 94 | public void testMorph_throwException() { 95 | try { 96 | new IntArrayMorpher().morph(new String[]{null}); 97 | fail("Should have thrown an Exception"); 98 | } catch (MorphException expected) { 99 | // ok 100 | } 101 | } 102 | 103 | protected AbstractArrayMorpher getAnotherMorpher() { 104 | return anotherMorpher; 105 | } 106 | 107 | protected AbstractArrayMorpher getAnotherMorpherWithDefaultValue() { 108 | return anotherMorpherWithDefaultValue; 109 | } 110 | 111 | protected AbstractArrayMorpher getMorpher() { 112 | return morpher; 113 | } 114 | 115 | protected AbstractArrayMorpher getMorpherWithDefaultValue() { 116 | return morpherWithDefaultValue; 117 | } 118 | 119 | protected Class getMorphsToClass() { 120 | return int[].class; 121 | } 122 | 123 | protected void setUp() throws Exception { 124 | morpher = new IntArrayMorpher(); 125 | morpherWithDefaultValue = new IntArrayMorpher(0); 126 | anotherMorpher = new IntArrayMorpher(); 127 | anotherMorpherWithDefaultValue = new IntArrayMorpher(1); 128 | } 129 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/bean/sample/BeanA.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.bean.sample; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.apache.commons.lang3.builder.ToStringBuilder; 23 | import org.apache.commons.lang3.builder.ToStringStyle; 24 | 25 | /** 26 | * @author Andres Almiray 27 | */ 28 | public class BeanA { 29 | private boolean bool = true; 30 | private int integer = 42; 31 | private String string = "morph"; 32 | 33 | public boolean equals(Object obj) { 34 | if (obj == this) { 35 | return true; 36 | } 37 | if (obj == null) { 38 | return false; 39 | } 40 | if (!BeanA.class.isAssignableFrom(obj.getClass())) { 41 | return false; 42 | } 43 | return EqualsBuilder.reflectionEquals(this, obj); 44 | } 45 | 46 | public int getInteger() { 47 | return integer; 48 | } 49 | 50 | public String getString() { 51 | return string; 52 | } 53 | 54 | public int hashCode() { 55 | return HashCodeBuilder.reflectionHashCode(this); 56 | } 57 | 58 | public boolean isBool() { 59 | return bool; 60 | } 61 | 62 | public void setBool(boolean bool) { 63 | this.bool = bool; 64 | } 65 | 66 | public void setInteger(int integer) { 67 | this.integer = integer; 68 | } 69 | 70 | public void setString(String string) { 71 | this.string = string; 72 | } 73 | 74 | public String toString() { 75 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 76 | } 77 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/bean/sample/BeanB.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.bean.sample; 19 | 20 | import org.apache.commons.lang3.builder.ToStringBuilder; 21 | import org.apache.commons.lang3.builder.ToStringStyle; 22 | 23 | /** 24 | * @author Andres Almiray 25 | */ 26 | public class BeanB extends BeanA { 27 | private int[] intarray = new int[]{1, 2, 3}; 28 | 29 | public int[] getIntarray() { 30 | return intarray; 31 | } 32 | 33 | public void setIntarray(int[] intarray) { 34 | this.intarray = intarray; 35 | } 36 | 37 | public String toString() { 38 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 39 | } 40 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/bean/sample/BeanC.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.bean.sample; 19 | 20 | import org.apache.commons.lang3.builder.ToStringBuilder; 21 | import org.apache.commons.lang3.builder.ToStringStyle; 22 | 23 | /** 24 | * @author Andres Almiray 25 | */ 26 | public class BeanC { 27 | private BeanA beanA = new BeanA(); 28 | private BeanB beanB = new BeanB(); 29 | 30 | public BeanA getBeanA() { 31 | return beanA; 32 | } 33 | 34 | public BeanB getBeanB() { 35 | return beanB; 36 | } 37 | 38 | public void setBeanA(BeanA beanA) { 39 | this.beanA = beanA; 40 | } 41 | 42 | public void setBeanB(BeanB beanB) { 43 | this.beanB = beanB; 44 | } 45 | 46 | public String toString() { 47 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 48 | } 49 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/bean/sample/BeanD.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.bean.sample; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.apache.commons.lang3.builder.ToStringBuilder; 23 | import org.apache.commons.lang3.builder.ToStringStyle; 24 | 25 | /** 26 | * @author Andres Almiray 27 | */ 28 | public class BeanD { 29 | private boolean bool = true; 30 | private double decimal = 0d; 31 | private int integer = 42; 32 | 33 | public boolean equals(Object obj) { 34 | if (obj == this) { 35 | return true; 36 | } 37 | if (obj == null) { 38 | return false; 39 | } 40 | if (!BeanD.class.isAssignableFrom(obj.getClass())) { 41 | return false; 42 | } 43 | return EqualsBuilder.reflectionEquals(this, obj); 44 | } 45 | 46 | public double getDecimal() { 47 | return decimal; 48 | } 49 | 50 | public int getInteger() { 51 | return integer; 52 | } 53 | 54 | public int hashCode() { 55 | return HashCodeBuilder.reflectionHashCode(this); 56 | } 57 | 58 | public boolean isBool() { 59 | return bool; 60 | } 61 | 62 | public void setBool(boolean bool) { 63 | this.bool = bool; 64 | } 65 | 66 | public void setDecimal(double decimal) { 67 | this.decimal = decimal; 68 | } 69 | 70 | public void setInteger(int integer) { 71 | this.integer = integer; 72 | } 73 | 74 | public String toString() { 75 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 76 | } 77 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/bean/sample/ObjectBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.bean.sample; 19 | 20 | import org.apache.commons.lang3.builder.ToStringBuilder; 21 | import org.apache.commons.lang3.builder.ToStringStyle; 22 | 23 | /** 24 | * @author Andres Almiray 25 | */ 26 | public class ObjectBean { 27 | private Object parray; 28 | private Object pbean; 29 | private Object pboolean; 30 | private Object pbyte; 31 | private Object pchar; 32 | private Object pclass; 33 | private Object pdouble; 34 | private Object pfloat; 35 | private Object pint; 36 | private Object plist; 37 | private Object plong; 38 | private Object pmap; 39 | private Object pshort; 40 | private Object pstring; 41 | 42 | public Object getParray() { 43 | return parray; 44 | } 45 | 46 | public Object getPbean() { 47 | return pbean; 48 | } 49 | 50 | public Object getPboolean() { 51 | return pboolean; 52 | } 53 | 54 | public Object getPbyte() { 55 | return pbyte; 56 | } 57 | 58 | public Object getPchar() { 59 | return pchar; 60 | } 61 | 62 | public Object getPclass() { 63 | return pclass; 64 | } 65 | 66 | public Object getPdouble() { 67 | return pdouble; 68 | } 69 | 70 | public Object getPfloat() { 71 | return pfloat; 72 | } 73 | 74 | public Object getPint() { 75 | return pint; 76 | } 77 | 78 | public Object getPlist() { 79 | return plist; 80 | } 81 | 82 | public Object getPlong() { 83 | return plong; 84 | } 85 | 86 | public Object getPmap() { 87 | return pmap; 88 | } 89 | 90 | public Object getPshort() { 91 | return pshort; 92 | } 93 | 94 | public Object getPstring() { 95 | return pstring; 96 | } 97 | 98 | public void setParray(Object parray) { 99 | this.parray = parray; 100 | } 101 | 102 | public void setPbean(Object bean) { 103 | this.pbean = bean; 104 | } 105 | 106 | public void setPboolean(Object pboolean) { 107 | this.pboolean = pboolean; 108 | } 109 | 110 | public void setPbyte(Object pbyte) { 111 | this.pbyte = pbyte; 112 | } 113 | 114 | public void setPchar(Object pchar) { 115 | this.pchar = pchar; 116 | } 117 | 118 | public void setPclass(Object pclass) { 119 | this.pclass = pclass; 120 | } 121 | 122 | public void setPdouble(Object pdouble) { 123 | this.pdouble = pdouble; 124 | } 125 | 126 | public void setPfloat(Object pfloat) { 127 | this.pfloat = pfloat; 128 | } 129 | 130 | public void setPint(Object pint) { 131 | this.pint = pint; 132 | } 133 | 134 | public void setPlist(Object plist) { 135 | this.plist = plist; 136 | } 137 | 138 | public void setPlong(Object plong) { 139 | this.plong = plong; 140 | } 141 | 142 | public void setPmap(Object pmap) { 143 | this.pmap = pmap; 144 | } 145 | 146 | public void setPshort(Object pshort) { 147 | this.pshort = pshort; 148 | } 149 | 150 | public void setPstring(Object pstring) { 151 | this.pstring = pstring; 152 | } 153 | 154 | public String toString() { 155 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 156 | } 157 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/bean/sample/PrimitiveBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.bean.sample; 19 | 20 | import org.apache.commons.lang3.builder.ToStringBuilder; 21 | import org.apache.commons.lang3.builder.ToStringStyle; 22 | 23 | import java.util.List; 24 | import java.util.Map; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public class PrimitiveBean { 30 | private int[] parray; 31 | private ObjectBean pbean; 32 | private boolean pboolean; 33 | private byte pbyte; 34 | private char pchar; 35 | private Class pclass; 36 | private double pdouble; 37 | private float pfloat; 38 | private int pint; 39 | private List plist; 40 | private long plong; 41 | private Map pmap; 42 | private short pshort; 43 | private String pstring; 44 | 45 | public int[] getParray() { 46 | return parray; 47 | } 48 | 49 | public ObjectBean getPbean() { 50 | return pbean; 51 | } 52 | 53 | public byte getPbyte() { 54 | return pbyte; 55 | } 56 | 57 | public char getPchar() { 58 | return pchar; 59 | } 60 | 61 | public Class getPclass() { 62 | return pclass; 63 | } 64 | 65 | public double getPdouble() { 66 | return pdouble; 67 | } 68 | 69 | public float getPfloat() { 70 | return pfloat; 71 | } 72 | 73 | public int getPint() { 74 | return pint; 75 | } 76 | 77 | public List getPlist() { 78 | return plist; 79 | } 80 | 81 | public long getPlong() { 82 | return plong; 83 | } 84 | 85 | public Map getPmap() { 86 | return pmap; 87 | } 88 | 89 | public short getPshort() { 90 | return pshort; 91 | } 92 | 93 | public String getPstring() { 94 | return pstring; 95 | } 96 | 97 | public boolean isPboolean() { 98 | return pboolean; 99 | } 100 | 101 | public void setParray(int[] parray) { 102 | this.parray = parray; 103 | } 104 | 105 | public void setPbean(ObjectBean pbean) { 106 | this.pbean = pbean; 107 | } 108 | 109 | public void setPboolean(boolean pboolean) { 110 | this.pboolean = pboolean; 111 | } 112 | 113 | public void setPbyte(byte pbyte) { 114 | this.pbyte = pbyte; 115 | } 116 | 117 | public void setPchar(char pchar) { 118 | this.pchar = pchar; 119 | } 120 | 121 | public void setPclass(Class pclass) { 122 | this.pclass = pclass; 123 | } 124 | 125 | public void setPdouble(double pdouble) { 126 | this.pdouble = pdouble; 127 | } 128 | 129 | public void setPfloat(float pfloat) { 130 | this.pfloat = pfloat; 131 | } 132 | 133 | public void setPint(int pint) { 134 | this.pint = pint; 135 | } 136 | 137 | public void setPlist(List plist) { 138 | this.plist = plist; 139 | } 140 | 141 | public void setPlong(long plong) { 142 | this.plong = plong; 143 | } 144 | 145 | public void setPmap(Map pmap) { 146 | this.pmap = pmap; 147 | } 148 | 149 | public void setPshort(short pshort) { 150 | this.pshort = pshort; 151 | } 152 | 153 | public void setPstring(String pstring) { 154 | this.pstring = pstring; 155 | } 156 | 157 | public String toString() { 158 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 159 | } 160 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/bean/sample/TypedBean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.bean.sample; 19 | 20 | import org.apache.commons.lang3.builder.ToStringBuilder; 21 | import org.apache.commons.lang3.builder.ToStringStyle; 22 | 23 | import java.util.List; 24 | import java.util.Map; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public class TypedBean { 30 | private int[] parray; 31 | private TypedBean pbean; 32 | private Boolean pboolean; 33 | private Byte pbyte; 34 | private Character pchar; 35 | private Class pclass; 36 | private Double pdouble; 37 | private Float pfloat; 38 | private Integer pint; 39 | private List plist; 40 | private Long plong; 41 | private Map pmap; 42 | private Short pshort; 43 | private String pstring; 44 | 45 | public int[] getParray() { 46 | return parray; 47 | } 48 | 49 | public TypedBean getPbean() { 50 | return pbean; 51 | } 52 | 53 | public Boolean getPboolean() { 54 | return pboolean; 55 | } 56 | 57 | public Byte getPbyte() { 58 | return pbyte; 59 | } 60 | 61 | public Character getPchar() { 62 | return pchar; 63 | } 64 | 65 | public Class getPclass() { 66 | return pclass; 67 | } 68 | 69 | public Double getPdouble() { 70 | return pdouble; 71 | } 72 | 73 | public Float getPfloat() { 74 | return pfloat; 75 | } 76 | 77 | public Integer getPint() { 78 | return pint; 79 | } 80 | 81 | public List getPlist() { 82 | return plist; 83 | } 84 | 85 | public Long getPlong() { 86 | return plong; 87 | } 88 | 89 | public Map getPmap() { 90 | return pmap; 91 | } 92 | 93 | public Short getPshort() { 94 | return pshort; 95 | } 96 | 97 | public String getPstring() { 98 | return pstring; 99 | } 100 | 101 | public void setParray(int[] parray) { 102 | this.parray = parray; 103 | } 104 | 105 | public void setPbean(TypedBean pbean) { 106 | this.pbean = pbean; 107 | } 108 | 109 | public void setPboolean(Boolean pboolean) { 110 | this.pboolean = pboolean; 111 | } 112 | 113 | public void setPbyte(Byte pbyte) { 114 | this.pbyte = pbyte; 115 | } 116 | 117 | public void setPchar(Character pchar) { 118 | this.pchar = pchar; 119 | } 120 | 121 | public void setPclass(Class pclass) { 122 | this.pclass = pclass; 123 | } 124 | 125 | public void setPdouble(Double pdouble) { 126 | this.pdouble = pdouble; 127 | } 128 | 129 | public void setPfloat(Float pfloat) { 130 | this.pfloat = pfloat; 131 | } 132 | 133 | public void setPint(Integer pint) { 134 | this.pint = pint; 135 | } 136 | 137 | public void setPlist(List plist) { 138 | this.plist = plist; 139 | } 140 | 141 | public void setPlong(Long plong) { 142 | this.plong = plong; 143 | } 144 | 145 | public void setPmap(Map pmap) { 146 | this.pmap = pmap; 147 | } 148 | 149 | public void setPshort(Short pshort) { 150 | this.pshort = pshort; 151 | } 152 | 153 | public void setPstring(String pstring) { 154 | this.pstring = pstring; 155 | } 156 | 157 | public String toString() { 158 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 159 | } 160 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/AbstractObjectMorpherTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestCase; 22 | import junit.framework.TestSuite; 23 | import junit.textui.TestRunner; 24 | import org.kordamp.ezmorph.Morpher; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public abstract class AbstractObjectMorpherTestCase extends TestCase { 30 | public static void main(String[] args) { 31 | TestRunner.run(suite()); 32 | } 33 | 34 | public static Test suite() { 35 | TestSuite suite = new TestSuite(AbstractObjectMorpherTestCase.class); 36 | suite.setName("AbstractObjectMorpher Tests"); 37 | return suite; 38 | } 39 | 40 | public AbstractObjectMorpherTestCase(String name) { 41 | super(name); 42 | } 43 | 44 | // ----------------------------------------------------------------------- 45 | 46 | public void testEquals_another_Morpher() { 47 | assertFalse(getMorpherWithDefaultValue().equals(getAnotherMorpherWithDefaultValue())); 48 | assertTrue(getMorpher().equals(getAnotherMorpher())); 49 | } 50 | 51 | public void testEquals_different_morpher() { 52 | assertFalse(getMorpher().equals(new Morpher() { 53 | public Class morphsTo() { 54 | return null; 55 | } 56 | 57 | public boolean supports(Class clazz) { 58 | return false; 59 | } 60 | })); 61 | } 62 | 63 | public void testEquals_morpher_withDefaultValue() { 64 | assertFalse(getMorpher().equals(getMorpherWithDefaultValue())); 65 | } 66 | 67 | public void testEquals_null() { 68 | assertFalse(getMorpher().equals(null)); 69 | } 70 | 71 | public void testEquals_same_morpher() { 72 | assertTrue(getMorpher().equals(getMorpher())); 73 | assertTrue(getMorpherWithDefaultValue().equals(getMorpherWithDefaultValue())); 74 | } 75 | 76 | public void testHashCode_morpher_withDefaultValue() { 77 | assertTrue(getMorpher().hashCode() != getMorpherWithDefaultValue().hashCode()); 78 | } 79 | 80 | public void testHashCode_same_morpher() { 81 | assertEquals(getMorpher().hashCode(), getMorpher().hashCode()); 82 | assertEquals(getMorpherWithDefaultValue().hashCode(), 83 | getMorpherWithDefaultValue().hashCode()); 84 | } 85 | 86 | // ----------------------------------------------------------------------- 87 | 88 | protected abstract Morpher getAnotherMorpher(); 89 | 90 | protected abstract Morpher getAnotherMorpherWithDefaultValue(); 91 | 92 | protected abstract Morpher getMorpher(); 93 | 94 | protected abstract Morpher getMorpherWithDefaultValue(); 95 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/BooleanObjectMorpherTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestSuite; 22 | import junit.textui.TestRunner; 23 | import org.kordamp.ezmorph.MorphException; 24 | import org.kordamp.ezmorph.Morpher; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public class BooleanObjectMorpherTest extends AbstractObjectMorpherTestCase { 30 | public static void main(String[] args) { 31 | TestRunner.run(suite()); 32 | } 33 | 34 | public static Test suite() { 35 | TestSuite suite = new TestSuite(BooleanObjectMorpherTest.class); 36 | suite.setName("BooleanObjectMorpher Tests"); 37 | return suite; 38 | } 39 | 40 | private BooleanObjectMorpher anotherMorpher; 41 | private BooleanObjectMorpher anotherMorpherWithDefaultValue; 42 | private BooleanObjectMorpher morpher; 43 | private BooleanObjectMorpher morpherWithDefaultValue; 44 | 45 | public BooleanObjectMorpherTest(String name) { 46 | super(name); 47 | } 48 | 49 | // ----------------------------------------------------------------------- 50 | 51 | public void testBooleanMorph_noConversion() { 52 | Boolean actual = (Boolean) new BooleanObjectMorpher(Boolean.TRUE).morph(Boolean.TRUE); 53 | assertEquals(Boolean.TRUE, actual); 54 | } 55 | 56 | public void testBooleanMorph_throwException() { 57 | try { 58 | new BooleanObjectMorpher().morph("A"); 59 | fail("Should have thrown an Exception"); 60 | } catch (MorphException expected) { 61 | // ok 62 | } 63 | } 64 | 65 | public void testBooleanMorph_throwException_null() { 66 | try { 67 | new BooleanObjectMorpher().morph(null); 68 | fail("Should have thrown an Exception"); 69 | } catch (MorphException expected) { 70 | // ok 71 | } 72 | } 73 | 74 | public void testBooleanMorph_useDefault() { 75 | String expected = String.valueOf("A"); 76 | Boolean actual = (Boolean) new BooleanObjectMorpher(Boolean.TRUE).morph(expected); 77 | assertEquals(Boolean.TRUE, actual); 78 | } 79 | 80 | public void testBooleanMorph_useDefault_null() { 81 | Boolean actual = (Boolean) new BooleanObjectMorpher(Boolean.TRUE).morph(null); 82 | assertEquals(Boolean.TRUE, actual); 83 | } 84 | 85 | public void testBooleanMorphStringValues_false() { 86 | assertEquals(Boolean.FALSE, new BooleanObjectMorpher().morph("false")); 87 | assertEquals(Boolean.FALSE, new BooleanObjectMorpher().morph("no")); 88 | assertEquals(Boolean.FALSE, new BooleanObjectMorpher().morph("off")); 89 | } 90 | 91 | public void testBooleanMorphStringValues_true() { 92 | assertEquals(Boolean.TRUE, new BooleanObjectMorpher().morph("true")); 93 | assertEquals(Boolean.TRUE, new BooleanObjectMorpher().morph("yes")); 94 | assertEquals(Boolean.TRUE, new BooleanObjectMorpher().morph("on")); 95 | } 96 | 97 | protected Morpher getAnotherMorpher() { 98 | return anotherMorpher; 99 | } 100 | 101 | protected Morpher getAnotherMorpherWithDefaultValue() { 102 | return anotherMorpherWithDefaultValue; 103 | } 104 | 105 | protected Morpher getMorpher() { 106 | return morpher; 107 | } 108 | 109 | protected Morpher getMorpherWithDefaultValue() { 110 | return morpherWithDefaultValue; 111 | } 112 | 113 | protected void setUp() throws Exception { 114 | morpher = new BooleanObjectMorpher(); 115 | morpherWithDefaultValue = new BooleanObjectMorpher(Boolean.TRUE); 116 | anotherMorpher = new BooleanObjectMorpher(); 117 | anotherMorpherWithDefaultValue = new BooleanObjectMorpher(Boolean.FALSE); 118 | } 119 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/CharacterObjectMorpherTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestSuite; 22 | import junit.textui.TestRunner; 23 | import org.kordamp.ezmorph.MorphException; 24 | import org.kordamp.ezmorph.Morpher; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public class CharacterObjectMorpherTest extends AbstractObjectMorpherTestCase { 30 | public static void main(String[] args) { 31 | TestRunner.run(suite()); 32 | } 33 | 34 | public static Test suite() { 35 | TestSuite suite = new TestSuite(CharacterObjectMorpherTest.class); 36 | suite.setName("CharacterObjectMorpher Tests"); 37 | return suite; 38 | } 39 | 40 | private CharacterObjectMorpher anotherMorpher; 41 | private CharacterObjectMorpher anotherMorpherWithDefaultValue; 42 | private CharacterObjectMorpher morpher; 43 | private CharacterObjectMorpher morpherWithDefaultValue; 44 | 45 | public CharacterObjectMorpherTest(String name) { 46 | super(name); 47 | } 48 | 49 | // ----------------------------------------------------------------------- 50 | 51 | public void testCharMorph() { 52 | String expected = String.valueOf("A"); 53 | Character actual = (Character) new CharacterObjectMorpher().morph(expected); 54 | assertEquals(new Character('A'), actual); 55 | } 56 | 57 | public void testCharMorph_noConversion() { 58 | Character expected = new Character('A'); 59 | Character actual = (Character) new CharacterObjectMorpher().morph(expected); 60 | assertEquals(expected, actual); 61 | assertSame(expected, actual); 62 | } 63 | 64 | public void testCharMorph_throwException_emptyString() { 65 | try { 66 | new CharacterObjectMorpher().morph(""); 67 | fail("Should have thrown an Exception"); 68 | } catch (MorphException expected) { 69 | // ok 70 | } 71 | } 72 | 73 | public void testCharMorph_throwException_null() { 74 | try { 75 | new CharacterObjectMorpher().morph(null); 76 | fail("Should have thrown an Exception"); 77 | } catch (MorphException expected) { 78 | // ok 79 | } 80 | } 81 | 82 | public void testCharMorph_useDefault() { 83 | String expected = String.valueOf(""); 84 | Character actual = (Character) new CharacterObjectMorpher(new Character('A')).morph(expected); 85 | assertEquals(new Character('A'), actual); 86 | } 87 | 88 | public void testCharMorph_useDefault_null() { 89 | Character actual = (Character) new CharacterObjectMorpher(new Character('A')).morph(null); 90 | assertEquals(new Character('A'), actual); 91 | } 92 | 93 | protected Morpher getAnotherMorpher() { 94 | return anotherMorpher; 95 | } 96 | 97 | protected Morpher getAnotherMorpherWithDefaultValue() { 98 | return anotherMorpherWithDefaultValue; 99 | } 100 | 101 | protected Morpher getMorpher() { 102 | return morpher; 103 | } 104 | 105 | protected Morpher getMorpherWithDefaultValue() { 106 | return morpherWithDefaultValue; 107 | } 108 | 109 | protected void setUp() throws Exception { 110 | morpher = new CharacterObjectMorpher(); 111 | morpherWithDefaultValue = new CharacterObjectMorpher(new Character('A')); 112 | anotherMorpher = new CharacterObjectMorpher(); 113 | anotherMorpherWithDefaultValue = new CharacterObjectMorpher(new Character('B')); 114 | } 115 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/ClassMorpherTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestCase; 22 | import junit.framework.TestSuite; 23 | import junit.textui.TestRunner; 24 | import org.kordamp.ezmorph.MorphException; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public class ClassMorpherTest extends TestCase { 30 | public static void main(String[] args) { 31 | TestRunner.run(suite()); 32 | } 33 | 34 | public static Test suite() { 35 | TestSuite suite = new TestSuite(ClassMorpherTest.class); 36 | suite.setName("ClassMorpher Tests"); 37 | return suite; 38 | } 39 | 40 | private ClassMorpher morpher = ClassMorpher.getInstance(); 41 | 42 | public ClassMorpherTest(String name) { 43 | super(name); 44 | } 45 | 46 | // ----------------------------------------------------------------------- 47 | 48 | public void testEquals() { 49 | assertTrue(ClassMorpher.getInstance() 50 | .equals(ClassMorpher.getInstance())); 51 | assertFalse(ClassMorpher.getInstance() 52 | .equals(StringMorpher.getInstance())); 53 | } 54 | 55 | public void testHashCode() { 56 | assertEquals(ClassMorpher.getInstance() 57 | .hashCode(), ClassMorpher.getInstance() 58 | .hashCode()); 59 | assertTrue(ClassMorpher.getInstance() 60 | .hashCode() != StringMorpher.getInstance() 61 | .hashCode()); 62 | } 63 | 64 | public void testMorph() { 65 | Class expected = Object.class; 66 | Class actual = (Class) morpher.morph("java.lang.Object"); 67 | assertEquals(expected, actual); 68 | } 69 | 70 | public void testMorph_array() { 71 | try { 72 | morpher.morph(new boolean[]{true, false}); 73 | fail("Expected a MorphException"); 74 | } catch (MorphException expected) { 75 | // ok 76 | } 77 | } 78 | 79 | public void testMorph_arrayClass() { 80 | Class expected = int[].class; 81 | Class actual = (Class) morpher.morph("[I"); 82 | assertEquals(expected, actual); 83 | } 84 | 85 | public void testMorph_class() { 86 | Class expected = Object.class; 87 | Class actual = (Class) morpher.morph(Object.class); 88 | assertEquals(expected, actual); 89 | } 90 | 91 | public void testMorph_null() { 92 | assertNull(morpher.morph(null)); 93 | } 94 | 95 | public void testMorph_unknownClassname() { 96 | try { 97 | morpher.morph("bogusClass.I.do.not.exist"); 98 | fail("Expected a MorphException"); 99 | } catch (MorphException expected) { 100 | // ok 101 | } 102 | } 103 | 104 | public void testMorph_withtoString() { 105 | Class expected = MyClass.class; 106 | Class actual = (Class) morpher.morph(new MyClass()); 107 | assertEquals(expected, actual); 108 | } 109 | 110 | public static class MyClass { 111 | public String toString() { 112 | return MyClass.class.getName(); 113 | } 114 | } 115 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/IdentityObjectMorpherTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestCase; 22 | import junit.framework.TestSuite; 23 | import junit.textui.TestRunner; 24 | 25 | /** 26 | * @author Andres Almiray 27 | */ 28 | public class IdentityObjectMorpherTest extends TestCase { 29 | public static void main(String[] args) { 30 | TestRunner.run(suite()); 31 | } 32 | 33 | public static Test suite() { 34 | TestSuite suite = new TestSuite(IdentityObjectMorpherTest.class); 35 | suite.setName("IdentityMorpher Tests"); 36 | return suite; 37 | } 38 | 39 | private IdentityObjectMorpher morpher = IdentityObjectMorpher.getInstance(); 40 | 41 | public IdentityObjectMorpherTest(String name) { 42 | super(name); 43 | } 44 | 45 | // ----------------------------------------------------------------------- 46 | 47 | public void testMorph() { 48 | assertNull(morpher.morph(null)); 49 | Object expected = new Object(); 50 | assertSame(expected, morpher.morph(expected)); 51 | } 52 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/StringMorpherTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestCase; 22 | import junit.framework.TestSuite; 23 | import junit.textui.TestRunner; 24 | import org.kordamp.ezmorph.MorphException; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public class StringMorpherTest extends TestCase { 30 | public static void main(String[] args) { 31 | TestRunner.run(suite()); 32 | } 33 | 34 | public static Test suite() { 35 | TestSuite suite = new TestSuite(StringMorpherTest.class); 36 | suite.setName("StringMorpher Tests"); 37 | return suite; 38 | } 39 | 40 | private StringMorpher morpher = StringMorpher.getInstance(); 41 | 42 | public StringMorpherTest(String name) { 43 | super(name); 44 | } 45 | 46 | // ----------------------------------------------------------------------- 47 | 48 | public void testMorph_array() { 49 | try { 50 | morpher.morph(new boolean[]{true, false}); 51 | fail("Expected a MorphException"); 52 | } catch (MorphException expected) { 53 | // ok 54 | } 55 | 56 | } 57 | 58 | public void testMorph_boolean() { 59 | String expected = "true"; 60 | String actual = (String) morpher.morph(Boolean.TRUE); 61 | assertEquals(expected, actual); 62 | } 63 | 64 | public void testMorph_noConversion() { 65 | String expected = "true"; 66 | String actual = (String) morpher.morph(expected); 67 | assertEquals(expected, actual); 68 | } 69 | 70 | public void testMorph_null() { 71 | assertNull(morpher.morph(null)); 72 | } 73 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/SwitchingMorpherTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestCase; 22 | import junit.framework.TestSuite; 23 | import junit.textui.TestRunner; 24 | import org.kordamp.ezmorph.MorphUtils; 25 | import org.kordamp.ezmorph.MorpherRegistry; 26 | import org.kordamp.ezmorph.bean.BeanMorpher; 27 | import org.kordamp.ezmorph.object.sample.BeanA; 28 | import org.kordamp.ezmorph.object.sample.BeanB; 29 | import org.kordamp.ezmorph.object.sample.WrapperA; 30 | import org.kordamp.ezmorph.object.sample.WrapperB; 31 | 32 | import java.util.HashMap; 33 | import java.util.Map; 34 | 35 | /** 36 | * @author Andres Almiray 37 | */ 38 | public class SwitchingMorpherTest extends TestCase { 39 | public static void main(String[] args) { 40 | TestRunner.run(suite()); 41 | } 42 | 43 | public static Test suite() { 44 | TestSuite suite = new TestSuite(SwitchingMorpherTest.class); 45 | suite.setName("SwitchingMorpher Tests"); 46 | return suite; 47 | } 48 | 49 | private SwitchingMorpher morpher; 50 | 51 | public SwitchingMorpherTest(String name) { 52 | super(name); 53 | } 54 | 55 | // ----------------------------------------------------------------------- 56 | 57 | public void testMorphWrapperAToBeanA() { 58 | WrapperA wrapper = new WrapperA(); 59 | wrapper.setInteger("12"); 60 | BeanA actual = (BeanA) morpher.morph(wrapper); 61 | BeanA expected = new BeanA(); 62 | expected.setInteger(12); 63 | assertEquals(expected, actual); 64 | } 65 | 66 | public void testMorphWrapperBToBeanB() { 67 | WrapperB wrapper = new WrapperB(); 68 | wrapper.setBool("false"); 69 | BeanB actual = (BeanB) morpher.morph(wrapper); 70 | BeanB expected = new BeanB(); 71 | expected.setBool(false); 72 | assertEquals(expected, actual); 73 | } 74 | 75 | public void testMorph_null() { 76 | assertNull(morpher.morph(null)); 77 | } 78 | 79 | protected void setUp() throws Exception { 80 | Map, Class> classMap = new HashMap, Class>(); 81 | classMap.put(WrapperA.class, BeanA.class); 82 | classMap.put(WrapperB.class, BeanB.class); 83 | MorpherRegistry morpherRegistry = new MorpherRegistry(); 84 | MorphUtils.registerStandardMorphers(morpherRegistry); 85 | morpherRegistry.registerMorpher(new BeanMorpher(BeanA.class, morpherRegistry)); 86 | morpherRegistry.registerMorpher(new BeanMorpher(BeanB.class, morpherRegistry)); 87 | morpher = new SwitchingMorpher(classMap, morpherRegistry); 88 | } 89 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/sample/BeanA.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object.sample; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.apache.commons.lang3.builder.ToStringBuilder; 23 | import org.apache.commons.lang3.builder.ToStringStyle; 24 | 25 | /** 26 | * @author Andres Almiray 27 | */ 28 | public class BeanA { 29 | private int integer = 42; 30 | 31 | public boolean equals(Object obj) { 32 | if (obj == this) { 33 | return true; 34 | } 35 | if (obj == null) { 36 | return false; 37 | } 38 | if (!BeanA.class.isAssignableFrom(obj.getClass())) { 39 | return false; 40 | } 41 | return EqualsBuilder.reflectionEquals(this, obj); 42 | } 43 | 44 | public int getInteger() { 45 | return integer; 46 | } 47 | 48 | public int hashCode() { 49 | return HashCodeBuilder.reflectionHashCode(this); 50 | } 51 | 52 | public void setInteger(int integer) { 53 | this.integer = integer; 54 | } 55 | 56 | public String toString() { 57 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 58 | } 59 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/sample/BeanB.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object.sample; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.apache.commons.lang3.builder.ToStringBuilder; 23 | import org.apache.commons.lang3.builder.ToStringStyle; 24 | 25 | /** 26 | * @author Andres Almiray 27 | */ 28 | public class BeanB { 29 | private boolean bool = true; 30 | 31 | public boolean equals(Object obj) { 32 | if (obj == this) { 33 | return true; 34 | } 35 | if (obj == null) { 36 | return false; 37 | } 38 | if (!BeanB.class.isAssignableFrom(obj.getClass())) { 39 | return false; 40 | } 41 | return EqualsBuilder.reflectionEquals(this, obj); 42 | } 43 | 44 | public int hashCode() { 45 | return HashCodeBuilder.reflectionHashCode(this); 46 | } 47 | 48 | public boolean isBool() { 49 | return bool; 50 | } 51 | 52 | public void setBool(boolean bool) { 53 | this.bool = bool; 54 | } 55 | 56 | public String toString() { 57 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 58 | } 59 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/sample/WrapperA.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object.sample; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.apache.commons.lang3.builder.ToStringBuilder; 23 | import org.apache.commons.lang3.builder.ToStringStyle; 24 | 25 | /** 26 | * @author Andres Almiray 27 | */ 28 | public class WrapperA { 29 | private String integer = "42"; 30 | 31 | public boolean equals(Object obj) { 32 | if (obj == this) { 33 | return true; 34 | } 35 | if (obj == null) { 36 | return false; 37 | } 38 | if (!WrapperA.class.isAssignableFrom(obj.getClass())) { 39 | return false; 40 | } 41 | return EqualsBuilder.reflectionEquals(this, obj); 42 | } 43 | 44 | public String getInteger() { 45 | return integer; 46 | } 47 | 48 | public int hashCode() { 49 | return HashCodeBuilder.reflectionHashCode(this); 50 | } 51 | 52 | public void setInteger(String integer) { 53 | this.integer = integer; 54 | } 55 | 56 | public String toString() { 57 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 58 | } 59 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/object/sample/WrapperB.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.object.sample; 19 | 20 | import org.apache.commons.lang3.builder.EqualsBuilder; 21 | import org.apache.commons.lang3.builder.HashCodeBuilder; 22 | import org.apache.commons.lang3.builder.ToStringBuilder; 23 | import org.apache.commons.lang3.builder.ToStringStyle; 24 | 25 | /** 26 | * @author Andres Almiray 27 | */ 28 | public class WrapperB { 29 | private String bool = "true"; 30 | 31 | public boolean equals(Object obj) { 32 | if (obj == this) { 33 | return true; 34 | } 35 | if (obj == null) { 36 | return false; 37 | } 38 | if (!WrapperB.class.isAssignableFrom(obj.getClass())) { 39 | return false; 40 | } 41 | return EqualsBuilder.reflectionEquals(this, obj); 42 | } 43 | 44 | public String getBool() { 45 | return bool; 46 | } 47 | 48 | public int hashCode() { 49 | return HashCodeBuilder.reflectionHashCode(this); 50 | } 51 | 52 | public void setBool(String bool) { 53 | this.bool = bool; 54 | } 55 | 56 | public String toString() { 57 | return ToStringBuilder.reflectionToString(this, ToStringStyle.MULTI_LINE_STYLE); 58 | } 59 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/primitive/AbstractMorpherTestCase.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestCase; 22 | import junit.framework.TestSuite; 23 | import junit.textui.TestRunner; 24 | import org.kordamp.ezmorph.Morpher; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public abstract class AbstractMorpherTestCase extends TestCase { 30 | public static void main(String[] args) { 31 | TestRunner.run(suite()); 32 | } 33 | 34 | public static Test suite() { 35 | TestSuite suite = new TestSuite(AbstractMorpherTestCase.class); 36 | suite.setName("AbstractMorpher Tests"); 37 | return suite; 38 | } 39 | 40 | public AbstractMorpherTestCase(String name) { 41 | super(name); 42 | } 43 | 44 | // ----------------------------------------------------------------------- 45 | 46 | public void testEquals_another_Morpher() { 47 | assertFalse(getMorpherWithDefaultValue().equals(getAnotherMorpherWithDefaultValue())); 48 | assertTrue(getMorpher().equals(getAnotherMorpher())); 49 | } 50 | 51 | public void testEquals_different_morpher() { 52 | assertFalse(getMorpher().equals(new Morpher() { 53 | public Class morphsTo() { 54 | return null; 55 | } 56 | 57 | public boolean supports(Class clazz) { 58 | return false; 59 | } 60 | })); 61 | } 62 | 63 | public void testEquals_morpher_withDefaultValue() { 64 | assertFalse(getMorpher().equals(getMorpherWithDefaultValue())); 65 | } 66 | 67 | public void testEquals_null() { 68 | assertFalse(getMorpher().equals(null)); 69 | } 70 | 71 | public void testEquals_same_morpher() { 72 | assertTrue(getMorpher().equals(getMorpher())); 73 | assertTrue(getMorpherWithDefaultValue().equals(getMorpherWithDefaultValue())); 74 | } 75 | 76 | public void testHashCode_morpher_withDefaultValue() { 77 | assertTrue(getMorpher().hashCode() != getMorpherWithDefaultValue().hashCode()); 78 | } 79 | 80 | public void testHashCode_same_morpher() { 81 | assertEquals(getMorpher().hashCode(), getMorpher().hashCode()); 82 | assertEquals(getMorpherWithDefaultValue().hashCode(), 83 | getMorpherWithDefaultValue().hashCode()); 84 | } 85 | 86 | public void testMorphsTo() { 87 | assertEquals(getMorphsToClass(), getMorpher().morphsTo()); 88 | } 89 | 90 | public void testSupports() { 91 | assertTrue(getMorpher().supports(Object.class)); 92 | assertTrue(getMorpher().supports(Number.class)); 93 | assertTrue(getMorpher().supports(String.class)); 94 | assertFalse(getMorpher().supports(Object[].class)); 95 | } 96 | 97 | // ----------------------------------------------------------------------- 98 | 99 | protected abstract Morpher getAnotherMorpher(); 100 | 101 | protected abstract Morpher getAnotherMorpherWithDefaultValue(); 102 | 103 | protected abstract Morpher getMorpher(); 104 | 105 | protected abstract Morpher getMorpherWithDefaultValue(); 106 | 107 | protected abstract Class getMorphsToClass(); 108 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/primitive/CharMorpherTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.primitive; 19 | 20 | import junit.framework.Test; 21 | import junit.framework.TestSuite; 22 | import junit.textui.TestRunner; 23 | import org.kordamp.ezmorph.MorphException; 24 | import org.kordamp.ezmorph.Morpher; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public class CharMorpherTest extends AbstractMorpherTestCase { 30 | public static void main(String[] args) { 31 | TestRunner.run(suite()); 32 | } 33 | 34 | public static Test suite() { 35 | TestSuite suite = new TestSuite(CharMorpherTest.class); 36 | suite.setName("CharMorpher Tests"); 37 | return suite; 38 | } 39 | 40 | private Morpher anotherMorpher; 41 | private Morpher anotherMorpherWithDefaultValue; 42 | private Morpher morpher; 43 | private Morpher morpherWithDefaultValue; 44 | 45 | public CharMorpherTest(String name) { 46 | super(name); 47 | } 48 | 49 | // ----------------------------------------------------------------------- 50 | 51 | public void testCharMorph() { 52 | String expected = String.valueOf("A"); 53 | char actual = ((CharMorpher) getMorpher()).morph(expected); 54 | assertEquals('A', actual); 55 | } 56 | 57 | public void testCharMorph_throwException_emptyString() { 58 | try { 59 | ((CharMorpher) getMorpher()).morph(""); 60 | fail("Should have thrown an Exception"); 61 | } catch (MorphException expected) { 62 | // ok 63 | } 64 | } 65 | 66 | public void testCharMorph_throwException_null() { 67 | try { 68 | ((CharMorpher) getMorpher()).morph(null); 69 | fail("Should have thrown an Exception"); 70 | } catch (MorphException expected) { 71 | // ok 72 | } 73 | } 74 | 75 | public void testCharMorph_useDefault() { 76 | String expected = String.valueOf(""); 77 | char actual = new CharMorpher('A').morph(expected); 78 | assertEquals('A', actual); 79 | } 80 | 81 | public void testCharMorph_useDefault_null() { 82 | char actual = new CharMorpher('A').morph(null); 83 | assertEquals('A', actual); 84 | } 85 | 86 | protected Morpher getMorpher() { 87 | return morpher; 88 | } 89 | 90 | protected Morpher getMorpherWithDefaultValue() { 91 | return morpherWithDefaultValue; 92 | } 93 | 94 | protected Class getMorphsToClass() { 95 | return Character.TYPE; 96 | } 97 | 98 | protected Morpher getAnotherMorpher() { 99 | return anotherMorpher; 100 | } 101 | 102 | protected Morpher getAnotherMorpherWithDefaultValue() { 103 | return anotherMorpherWithDefaultValue; 104 | } 105 | 106 | protected void setUp() throws Exception { 107 | morpher = new CharMorpher(); 108 | morpherWithDefaultValue = new CharMorpher('A'); 109 | anotherMorpher = new CharMorpher(); 110 | anotherMorpherWithDefaultValue = new CharMorpher('B'); 111 | } 112 | } -------------------------------------------------------------------------------- /subprojects/ezmorph-core/src/test/java/org/kordamp/ezmorph/test/ArrayAssertionsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * SPDX-License-Identifier: Apache-2.0 3 | * 4 | * Copyright 2006-2024 Andres Almiray. 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 | package org.kordamp.ezmorph.test; 19 | 20 | import junit.framework.AssertionFailedError; 21 | import junit.framework.Test; 22 | import junit.framework.TestCase; 23 | import junit.framework.TestSuite; 24 | import junit.textui.TestRunner; 25 | 26 | /** 27 | * @author Andres Almiray 28 | */ 29 | public class ArrayAssertionsTest extends TestCase { 30 | public static void main(String[] args) { 31 | TestRunner.run(suite()); 32 | } 33 | 34 | public static Test suite() { 35 | TestSuite suite = new TestSuite(ArrayAssertionsTest.class); 36 | suite.setName("ArrayAssertions Tests"); 37 | return suite; 38 | } 39 | 40 | public ArrayAssertionsTest(String name) { 41 | super(name); 42 | } 43 | 44 | // ----------------------------------------------------------------------- 45 | 46 | public void testAssertEquals_null_null() { 47 | // assert that original contract is not borken 48 | ArrayAssertions.assertEquals((Object) null, (Object) null); 49 | } 50 | 51 | public void testAssertEquals_actuals_is_null() { 52 | boolean errorThrown = false; 53 | Object[] expecteds = new Object[]{new Object(), new Object()}; 54 | try { 55 | ArrayAssertions.assertEquals(expecteds, (Object[]) null); 56 | } catch (AssertionFailedError expected) { 57 | errorThrown = true; 58 | } 59 | assertTrue("Expected a failure", errorThrown); 60 | } 61 | 62 | public void testAssertEquals_different_length() { 63 | boolean errorThrown = false; 64 | Object[] expecteds = new Object[]{new Object(), new Object()}; 65 | Object[] actuals = new Object[]{new Object()}; 66 | try { 67 | ArrayAssertions.assertEquals(expecteds, actuals); 68 | } catch (AssertionFailedError expected) { 69 | errorThrown = true; 70 | } 71 | assertTrue("Expected a failure", errorThrown); 72 | } 73 | 74 | public void testAssertEquals_expecteds_is_null() { 75 | boolean errorThrown = false; 76 | Object[] actuals = new Object[]{new Object(), new Object()}; 77 | try { 78 | ArrayAssertions.assertEquals((Object[]) null, actuals); 79 | } catch (AssertionFailedError expected) { 80 | errorThrown = true; 81 | } 82 | assertTrue("Expected a failure", errorThrown); 83 | } 84 | 85 | public void testAssertEquals_multi_Object_Object_nulls() { 86 | Object[][] expecteds = new Object[][]{{null}, {null}}; 87 | Object[][] actuals = new Object[][]{{null}, {null}}; 88 | ArrayAssertions.assertEquals(expecteds, actuals); 89 | } 90 | 91 | public void testAssertEquals_null_elements() { 92 | boolean errorThrown = false; 93 | Object[] expecteds = new Object[]{null}; 94 | Object[] actuals = new Object[]{new Object()}; 95 | try { 96 | ArrayAssertions.assertEquals(expecteds, actuals); 97 | } catch (AssertionFailedError expected) { 98 | errorThrown = true; 99 | } 100 | assertTrue("Expected a failure", errorThrown); 101 | } 102 | 103 | public void testAssertEquals_null_elements_2() { 104 | boolean errorThrown = false; 105 | Object[] expecteds = new Object[]{new Object()}; 106 | Object[] actuals = new Object[]{null}; 107 | try { 108 | ArrayAssertions.assertEquals(expecteds, actuals); 109 | } catch (AssertionFailedError expected) { 110 | errorThrown = true; 111 | } 112 | assertTrue("Expected a failure", errorThrown); 113 | } 114 | 115 | public void testAssertEquals_Object_Object_nulls() { 116 | Object[] expecteds = new Object[]{null}; 117 | Object[] actuals = new Object[]{null}; 118 | ArrayAssertions.assertEquals(expecteds, actuals); 119 | } 120 | } --------------------------------------------------------------------------------