Syed Sayem blog

Immutables Library Examples

Some examples demonstrating how to use the Immutables Library to create immutable classes in Java.

import org.immutables.value.Value;

import java.util.List;
import java.util.Optional;

public interface Book {
    String title();

    Optional<String> excerpt();

    Float price();

    List<String> tags();

import org.immutables.value.Value;

import java.util.List;

public interface Library {
    String name();

    List<Book> books();

    boolean opened();
public class AppTest {

    public static void main(final String[] args) {
        Book book1 = ImmutableBook.builder()
                .title("One first book")
                .excerpt("Lorem ipsum dolor sit.")
                .addTags("foo", "bar", "baz")

        Book book2 = ImmutableBook.builder()
                .title("Another book")
                .addTags("xoxo", "trololol")

        Library library = ImmutableLibrary.builder()
                .name("My first library")
                .addBooks(book1, book2)


You may need to configure annotation processing in your IDE. You can follow this link to configure annotation processing in IntelliJ

comments powered by Disqus