How to Ensure There is One Row With a Value

The Problem

Let’s imagine a business requirement that there should be only one active row in a table. Active means that the column ‘is_active’ is set to true. There can be many rows with false or null values. The row with only one value can be only one.

The simple table could look like this:

CREATE TABLE test(
    id SERIAL PRIMARY KEY,
    is_active BOOLEAN NOT NULL
);

Continue reading

Operator Overloading in Java

C++ has got operator overloading. Java does NOT have. Java folks just say that it is bad, and provides bad abstraction and leads to very unreliable and difficult to manage code. That’s why Java doesn’t have operators overloading.

This is why I cannot check if BigDecimal is less than zero in the normal way:

public something(BigDecimal price) {
    if (price < 0) {
        throw new IllegalArgumentException("Price cannot be less than 0");
    }
}

Instead of the above simple code I just have to write this:

public something(BigDecimal price) {
    if (price.compareTo(BigDecimal.ZERO) == -1) {
        throw new IllegalArgumentException("Price cannot be less than 0");
    }
}

This is a kind of a nightmare, why use the compareTo function which returns that cryptic -1, 0, 1 values? I really want to have operator overloading in Java. The code will be much simpler.

Btw: PostgreSQL has operator overloading.