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

https://github.com/immutables/immutables

Java annotation processor to create immutable objects and builders, for records too. Sweep boilerplate code under the rug!
https://github.com/immutables/immutables

annotation-processor builder gson guava immutable-collections immutable-datastructures immutable-objects immutables jackson java records

Last synced: 13 days ago
JSON representation

Java annotation processor to create immutable objects and builders, for records too. Sweep boilerplate code under the rug!

Awesome Lists containing this project

README

          

![CI](https://github.com/immutables/immutables/workflows/CI/badge.svg) [![Sonatype Central](https://maven-badges.sml.io/sonatype-central/org.immutables/value/badge.svg)](https://maven-badges.sml.io/sonatype-central/org.immutables/value/) [![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)

![imcover.png](imcover.png)

Full documentation at [immutables.org](http://immutables.org)

## Record Builder

```java
@Value.Builder
record Person(String name, int age, String email) {}

// Use the generated builder
Person person = new PersonBuilder()
.name("Alice")
.age(30)
.email("alice@example.com")
.build();
```

More fancy example having copy-with methods generated, and style `withUnaryOperator="with*"`

```java
@Value.Builder
record Person(String name, int age) implements WithPerson {
// Extend the generated PersonBuilder
static class Builder extends PersonBuilder {}
}

// Use your custom builder
var person = new Person.Builder()
.name("Bob")
.age(18)
.build();

person = person.withName("Bobby!")
.withAge(age -> age + 3);
```

## Immutable class

Minimal, classical style

```java
@Value.Immutable
interface Book {
String isbn();
String title();
List authors();
}

ImmutableBook book = ImmutableBook.builder()
.isbn("978-1-56619-909-4")
.title("The Elements of Style")
.addAuthors("William Strunk Jr.", "E.B. White.")
.build();
```

"sandwich" style, with nested builder and extending `With*` interface

```java
// Define abstract value type using interface, abstract class or annotation
@Value.Immutable
public interface ValueObject extends WithValueObject {
// WithValueObject is not yet generated, We extend With* to inherit `with*` method signatures
String name();
List counts();
Optional description();

class Builder extends ImmutableValueObject.Builder {}
// ImmutableValueObject.Builder will be generated and
// our builder will inherit and reexport methods as its own.
// Static nested Builder will inherit all the public method
// signatures of ImmutableValueObject.Builder
}

// Use generated immutable implementation and builder
var value = new ValueObject.Builder()
.name("Nameless")
.description("present")
.addCounts(1)
.addCounts(2)
.build();

value = value.withName("Doe");

//fetch values via accessors
List counts = v.counts();
Optional description = v.description();
```

## Changelog

See [releases](https://github.com/immutables/immutables/releases) tab for release history. Archived [changelog](.archive/CHANGELOG.md) for earlier releases.