Interface DerivedValue<T>

Type Parameters:
T - the value type
All Superinterfaces:
Serializable
Functional Interface:
This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.

@FunctionalInterface public interface DerivedValue<T> extends Serializable
Responsible for providing values derived from other attribute values.

Derived attributes compute their values from other attributes within the same entity or from related entities. They provide calculated fields, formatting, concatenation, and other derived values without being stored in the database.

Derived attributes are defined using value providers that receive source values and compute the derived result:

// Computes the invoice line total from the quantity and unit price
class InvoiceLineTotal implements DerivedValue<BigDecimal> {

	@Override
	public BigDecimal from(SourceValues values) {
		Integer quantity = values.get(InvoiceLine.QUANTITY);
		BigDecimal unitPrice = values.get(InvoiceLine.UNITPRICE);
		if (unitPrice == null || quantity == null) {
			return null;
		}

		return unitPrice.multiply(BigDecimal.valueOf(quantity));
	}
}

// In the entity definition
InvoiceLine.TOTAL.as()
				.derived()
				.from(InvoiceLine.QUANTITY, InvoiceLine.UNITPRICE)
				.with(new InvoiceLineTotal())
				.caption("Total");

// Usage
Entity invoiceLine = entities.entity(InvoiceLine.TYPE)
				.with(InvoiceLine.UNITPRICE, BigDecimal.valueOf(0.99))
				.with(InvoiceLine.QUANTITY, 2)
				.build();

// Derived values are computed automatically
BigDecimal total = invoiceLine.get(InvoiceLine.TOTAL); // 1.98
See Also:
  • Method Details

    • from

      Must be total, that is, return a value or null for every combination of source values, including all null, which a new entity presents. A cached derived value is computed for every attribute of an entity being made immutable, not only for the ones a caller happens to read, so a provider throwing on some combination throws from Entity.immutable() rather than from the read it would once have failed.

      Parameters:
      values - the source values, mapped to their respective attributes
      Returns:
      the derived value
    • sourceValues

      static DerivedValue.SourceValues sourceValues(Attribute<?> derivedAttribute, Map<Attribute<?>,Object> values)
      Parameters:
      derivedAttribute - the derived attribute
      values - the values
      Returns:
      a new DerivedValue.SourceValues instance