Skip to content

steinarb/beans

Repository files navigation

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

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

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

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

Release history

DateVersionComment
<2022-05-29 Sun 08:26>1.2.0Use karaf 4.4.0 and OSGi 8
<2021-06-09 Wed 21:46>1.1.4Avoid dependencyManagement version “leakage” in the BoM
<2021-04-18 Sun 22:47>1.1.2Add a “Bill of Materials” (BoM)
<2021-04-15 Thu 00:43>1.1.1Get common maven dependencies and maven plugin config from parent
<2021-04-11 Sun 21:43>1.1.0Built with karaf 4.3.0 and OSGi 7
<2019-12-31 Tue 00:47>1.0.0Adds 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):

<dependencies>
    <dependency>
        <groupId>no.priv.bang.beans</groupId>
        <artifactId>beans.immutable</artifactId>
        <version>1.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-lang3</artifactId>
        <version>3.9</version>
    </dependency>
</dependencies>

To use the latest version of the OSGi bundle in a maven project, building an OSGi bundle intended for use in 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):
    <dependencies>
        <dependency>
            <groupId>no.priv.bang.beans</groupId>
            <artifactId>beans.immutable</artifactId>
            <version>1.2.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.9</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
        
  2. 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):
    <features>
        <repository>mvn:no.priv.bang.beans/beans/1.2.0/xml/features</repository>
        <feature name="my-feature">
            <feature>beans-immutable</feature>
        </feature>
    </features>
        

License

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

About

Common code for Java beans

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages