An open API service indexing awesome lists of open source software.

https://github.com/fillmore-labs/value-comparison

Comparison of value objects in Java
https://github.com/fillmore-labs/value-comparison

autovalue bazel builder-pattern code-generation domain-driven-design freebuilder immutables java java-records java17 lombok scala value-object value-semantics

Last synced: 9 days ago
JSON representation

Comparison of value objects in Java

Awesome Lists containing this project

README

          

= Value Objects in Java
:Author: Oliver Eikemeier
:Email:
:Date: 2021-12
:Revision: v0.1.1
:toc: macro
ifdef::env-github[]
:note-caption: :information_source:
:tip-caption: :bulb:
endif::[]

image:https://badge.buildkite.com/13a9640ff1e11890cfcfc26bde3dcc40ffd6617f062e67a35e.svg?branch=main[title="Buildkite build status",link=https://buildkite.com/fillmore-labs/value-objects-in-java]
image:https://codecov.io/gh/fillmore-labs/value-comparison/branch/main/graph/badge.svg?token=4KRPCM9FRT[title="Codecov test coverage",link=https://codecov.io/gh/fillmore-labs/value-comparison]
image:https://api.codeclimate.com/v1/badges/d2d37684cb129ef18713/maintainability[title="Code Climate maintainability",link=https://codeclimate.com/github/fillmore-labs/value-comparison/maintainability]

Demo code about value objects and how to create them in Java.

toc::[]

== Purpose

This source demonstrates how to create value objects in Java using various annotation processors.

== Evaluated Libraries

- https://github.com/google/auto/blob/main/value/userguide/index.md[AutoValue]
- https://freebuilder.inferred.org[FreeBuilder]
- https://immutables.github.io[Immutables]
- https://projectlombok.org[Project Lombok]

Also, simple https://docs.oracle.com/en/java/javase/17/language/records.html[record classes],
https://docs.scala-lang.org/tour/case-classes.html[Scala case class] amd
https://kotlinlang.org/docs/data-classes.html[Kotlin data class] examples are included for
demonstration purposes.

== Structure

In `+com.fillmore_labs.talk.value.simple.*+` we have simple classes, one `+EmailAddress+` which
demonstrates the power of being able to check value objects for consistency during construction and
`+Person+`, which show how different libraries handle optional parameters and documentation
propagation.

`+com.fillmore_labs.talk.value.inheritance.*+` and `+com.fillmore_labs.talk.value.composition.*+` demonstrates
the https://en.wikipedia.org/wiki/Composition_over_inheritance[composition over inheritance]
principle and why value objects can not meaningfully extend each other.

== Running

=== Prerequisites

You need https://github.com/bazelbuild/bazelisk[Bazelisk] installed, see also
https://docs.bazel.build/versions/main/install-bazelisk.html[Installing Bazel using Bazelisk].

==== macOS

Using https://brew.sh[HomeBrew] enter

[source,shell]
brew install bazelisk

==== Windows

Using https://chocolatey.org[Chocolatey] enter

[source,shell]
choco install bazelisk

Enable developer mode:

. Open Windows settings
. Go to “Update & security”, then “For developers”
. Under “Developer Mode” section enable “Install apps from any source, including loose files”.

or run with administrator privileges.

=== Main App

The main app simply demonstrates the `toString()` method with a simple structure.
Run it with

[source,shell]
----
bazelisk run //:value-comparison
----

=== Tests

To run all tests, use

[source,shell]
----
bazelisk test //src/test/...
----

[bibliography]
== References

* Joshua Bloch.
https://www.pearson.com/us/higher-education/program/Bloch-Effective-Java-3rd-Edition/PGM1763855.html[Effective Java, 3rd Edition].
Addison-Wesley Professional. 2018.

* Martin Odersky, Lex Spoon, Bill Venners. https://www.artima.com/lejava/articles/equality.html[How to Write an Equality Method in Java]. 2009

* Dan Bergh Johnsson.
https://www.infoq.com/presentations/Value-Objects-Dan-Bergh-Johnsson/[Power of Value - Power Use of
Value Objects in Domain Driven Design]. QCon London. 2009.

* Brian Goetz. https://cr.openjdk.java.net/~briangoetz/amber/datum.html[Data Classes and Sealed Types for Java]. 2019.

* JEP 395. https://openjdk.java.net/jeps/395[Records]. 2020.