Common code for Java beans

Steinar Bang 141cd100c0 Upgrade parent to use maven-compiler-plugin 3.12.1, maven-javadoc-plugin 3.6.3, maven-surefire-plugin 3.2.3, jacoco-maven-plugin 0.8.11, maven-clean-plugin 3.3.2 3 months ago
.github d64f0148d0 Replace java 11 with java 17 in github actions CI build 9 months ago
beans 3ca8740159 Exclude karaf feature file generation from eclipse m2e lifecycle handling 1 year ago
beans-bom 17101f89f4 [maven-release-plugin] prepare for next development iteration 1 year ago
jacoco-coverage-report 17101f89f4 [maven-release-plugin] prepare for next development iteration 1 year ago
.editorconfig 77de8b5900 Add immutable bean base class implementing equals() and hashCode() for immutable beans 4 years ago
.gitignore 77de8b5900 Add immutable bean base class implementing equals() and hashCode() for immutable beans 4 years ago
LICENSE 77de8b5900 Add immutable bean base class implementing equals() and hashCode() for immutable beans 4 years ago
README.org 6788e6c60f Replace travis-ci CI build with github actions CI build 1 year ago
pom.xml 141cd100c0 Upgrade parent to use maven-compiler-plugin 3.12.1, maven-javadoc-plugin 3.6.3, maven-surefire-plugin 3.2.3, jacoco-maven-plugin 0.8.11, maven-clean-plugin 3.3.2 3 months ago

README.org

Java bean support code

Common code for Java beans, used in my other projects. Having this code in a library cuts down on boilerplate.

Status of the project

file:https://github.com/steinarb/beans/actions/workflows/beans-maven-ci-build.yml/badge.svg file:https://coveralls.io/repos/github/steinarb/beans/badge.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=alert_status#.svg file:https://maven-badges.herokuapp.com/maven-central/no.priv.bang.beans/beans/badge.svg file:https://www.javadoc.io/badge/no.priv.bang.beans/beans.svg

file:https://sonarcloud.io/images/project_badges/sonarcloud-white.svg

file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=sqale_index#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=coverage#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=ncloc#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=code_smells#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=sqale_rating#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=security_rating#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=bugs#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=vulnerabilities#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=duplicated_lines_density#.svg file:https://sonarcloud.io/api/project_badges/measure?project=steinarb_beans&metric=reliability_rating#.svg

Release history

Date Version Comment
<2022-05-29 Sun 08:26> 1.2.0 Use karaf 4.4.0 and OSGi 8
<2021-06-09 Wed 21:46> 1.1.4 Avoid dependencyManagement version "leakage" in the BoM
<2021-04-18 Sun 22:47> 1.1.2 Add a "Bill of Materials" (BoM)
<2021-04-15 Thu 00:43> 1.1.1 Get common maven dependencies and maven plugin config from parent
<2021-04-11 Sun 21:43> 1.1.0 Built with karaf 4.3.0 and OSGi 7
<2019-12-31 Tue 00:47> 1.0.0 Adds bean base class Immutable implementing hashCode() and equals()

Overview of the project

Immutable

This is an OSGi bundle that contains the class Immutable which serves as a base class for immutable Java beans and provides the beans with implementations of hashCode() and equals().

The Object.hashCode() and Object.equals() methods must be overriden from their default Object implmentations in data objects, if those data objects are to work properly in maps and sets. However, writing hashCode() and equals() methods manually is boring. It consists of creating a lot of boilerplate code, and that boilerplate needs to have tests written if I don't wish to have sonar complain about test coverage, increasing the code that has to be modified when the bean changes.

IDEs like eclipse and IntelliJ can create hashCode() and equals() methods, but the auto-generated code may need a little tinkering, and the tests needs to be handwritten, or your code coverage will become low enough for sonar to complain. And if the beans change the IDE can recreate the methods, but the tests still needs to be changed manually

The apache commons-lang3 HashCodeBuilder has methods to create hashCode() using reflection, but using reflections means there is a performance cost. However, since the beans used in my jackson/jersey REST APIs are invariably immutable, it is possible to create the hashCode() lazily and then cache it. And that's what the Immutable bean base class found in this library does.

The equals() method also depends on the hashCode(), so it may not be entirely comparison proof, but for the first version I'm trying it out.

To use the latest version of the OSGi bundle in a maven project, or just use it as a plain JAR file, add the following dependencies (commons-lang3 is a provided dependency in the beans.immutable pom, so won't become a transitive dependency and must be added explicitly to a project using beans.immutable, even if the project is just using beans.immutable as a plain JAR file):

no.priv.bang.beans beans.immutable 1.2.0 org.apache.commons commons-lang3 3.9

    To use the latest version of the OSGi bundle in a maven project, building an OSGi bundle intended for use in [[https://karaf.apache.org][apache karaf]]:
  1. Add a provided dependency to beans.immutable (to make things compile) and a test dependency to commons-lang3 (to prevent tests from failing in startup. If you already have a dependency to commons-lang3 you don't need this test dependency):
  2. #+begin_src xml no.priv.bang.beans beans.immutable 1.2.0 provided org.apache.commons commons-lang3 3.9 test #+end_src
  3. Add a load of the feature repository containing beans.immutable and depend on the beans-immutable feature from your projects template feature.xml (src/main/feature/feature.xml):
  4. #+begin_src xml mvn:no.priv.bang.beans/beans/1.2.0/xml/features beans-immutable #+end_src

License

This code is licensed under the Apache license v. 2. See the LICENSE file for details.