├── .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