Objectos 0.2.0 released: introduces Objectos Lang

Marcio Endo
June 13, 2022

Hi, I am happy to announce the release of Objectos 0.2.0!

This is the second public release of the Objectos suite of Java libraries in its new form.

It still does not do much. In fact, it does very little. In this initial phase my focus is more on the release process itself and less on the artifact and features released. In particular I am investing more on:

But I digress, let me show you what is new in this release.

Objectos Lang

This release introduces Objectos Lang. It is a core library on top of which the rest of the Objectos libraries will be built upon. Let's take a look on the features it introduces.

Runtime checks with the Check class

The Check class provides static methods for verifying:

The following example shows all of the provided methods in action:

public void start(String name, int count) {
  Check.notNull(name, "name == null");
  Check.argument(count > 0, "count must be positive");
  Check.state(this.state == State.NEW, "cannot start: already started");
}

Think of it as the start method of a hypothetical service.

If any of the check conditions does not evaluate to true then the check fails with a specific RuntimeException sub-type.

Utilities for overriding java.lang.Object methods

Objectos Lang provides three utilities each for overriding one of the following java.lang.Object method: equals, hashCode and toString.

Suppose the following class representing a (poorly) modeled edition of a book:

class Edition {
  Book book;
  Publisher publisher;
  LocalDate date;
}

You can use the Equals class to override the equals method:

@Override
public boolean equals(Object o) {
  return o == this || o instanceof Edition that
    && Equals.of(
      book, that.book,
      publisher, that.publisher,
      date, that.date
    );
}

And the HashCode class to implement the hashCode method:

@Override
public int hashCode() {
  return HashCode.of(book, publisher, date);
}

Finally, you can use the ToString and the ToString.Formattable types for producing a standard, human-readable toString method return value.

class Edition implements ToString.Formattable {
  (...)

  @Override
  public void formatToString(StringBuilder sb, int level) {
    ToString.format(
      sb, level, this,
      "book", book,
      "publisher", publisher,
      "date", date
    );
  }

  @Override
  public String toString() {
    return ToString.of(this);
  }
}

Assuming both Book and Publisher are also instances of ToString.Formattable then the following could be an example of the toString method output:

Edition [
  book = Book [
    title = The Posthumous Memoirs of Brás Cubas
    author = Joaquim Maria Machado de Assis
  ]
  publisher = Publisher [
    name = The publisher's name
    country = The publisher's country
  ]
  date = 2020-06-02
]

Which is meant to be human-readable. It is useful during a debugging session either using a debugger or through System.out.println statements.

The note sink API

Objectos Lang provides an API for defining and sending notes about events taking place during an application execution. Use it for logging or debugging.

import objectos.lang.*;

public class Service {
  static final Note1<String> SAY_HELLO = Note1.info();

  private final NoteSink sink;

  Service(NoteSink sink) { this.sink = sink; }

  public void sayHelloWorld() {
    sink.send(SAY_HELLO, "world!");
  }
}

Built for JDK 17

Objectos requires JDK 17 or higher.

Alpha release

With the zero-based version numbers we want to convey the idea that the API is of alpha quality and, therefore, subject to change.

What's next

You can find the full documentation here. It includes the full release notes as well.