liveBook · Manning

Order property

As explained in Data-Oriented Programming in Java

An ordering is a binary relation that arranges or compares elements according to specified properties such as reflexivity, transitivity, and antisymmetry.

An order makes an implicit comparison explicit. Define what ≤ means for domain values, then use that relationship to express ideas such as customer favorability, capability, classification, or valid state transitions. Ordering is not limited to sorting numbers or dates. section 7.7 section 7.7.1

A useful ordering treatment makes correctness visible through properties:

  • Reflexivity means a ≤ a; antisymmetry means that a ≤ b and b ≤ a imply a = b; transitivity means that a ≤ b and b ≤ c imply a ≤ c. section 7.7.1
  • Compose domain criteria with Java Comparator, using a primary business meaning and a secondary lexicographic tie-breaker. section 7.7
  • Test the laws above the implementation by exercising every relevant state or every permutation and checking for one consistent result. section 7.7.1

Encoding valid task transitions

Model a lifecycle with sealed data: empty, Started, Completed, and Failed. Assign empty the lowest order, Started the next position, and terminal states the highest position. A transition is valid when the destination has a greater order than the current state, preventing self-updates and transitions out of terminal states. Enumerate every pair and verify both the ordering model and the database behavior. section 12.3

Listing 7.42 verifying ordering properties by checking all permutations
static <A> List<List<A>> permutations(A[] items) {      #A
  // ...                                                #A
}                                                       #A
 
@Test
void anExerciseForTheReader() {   
  List<AuditFinding> expectedOrder = List.of(  
    INACCURATE, NO_ISSUE,                  
    BILLING_ERROR, OUT_OF_COMPLIANCE);     
 
assertTrue(
    permutations(AuditFinding.values())    
      .stream()
      .map(x -> x.stream().sorted(comparator).toList())
      .allMatch(sorted -> sorted.equals(expectedOrder))); #B
}

Use the permutation check to verify that every input arrangement produces the same expected sorted order. section 7.7.1

How Data-Oriented Programming in Java covers it

How deep it goes
Defines ordering laws, composes Java comparators, and verifies all states or permutations rather than stopping at sorting implementation.
Examples
Uses customer favorability, policy and audit-finding priorities, lexicographic names, and task lifecycle transitions as concrete cases.
What it assumes
Assumes Java data types and Comparator usage while teaching how to derive ordering laws from domain requirements.
What you can do after
Apply it via Java Comparator and algebraic tests to encode domain priorities, detect invalid transitions, and verify external-system rules.
How it connects
Connects binary relations with Comparator, sorting, algebraic laws, state machines, database constraints, and integration testing.

Also explained in

Compare how 10 books explain Order property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming in Java. The section links point to the parts of the book it draws on.