Define state

From cdd
Jump to: navigation, search

Let's explain how state can be defined with an example.

Create the class PersonState:

package com.myproject;
 
import net.sf.laja.cdd.annotation.Key;
import net.sf.laja.cdd.annotation.Optional;
 
public class PersonState {
    @Key long ssn;
    String givenName;
    @Optional String middleName;
    String surname;
}


Create the class PersonCreator + generate:

package com.myproject;
 
public class PersonCreator {
    PersonState state;
}


Modify the Main class (from now on, we will only show the contents of the main method):

PersonState person = createPerson()                              // The state creation starts here
        .ssn(197501021234L).givenName("Carl").surname("Carlson") // ...followed by mandatory attributes
        .withMiddleName("Adam")                                  // ...followed by optional attributes
        .asState();                                              // ...and encapsulation.
 
System.out.println(person);

output:

{ssn=197501021234, givenName="Carl", middleName="Adam", surname="Carlson"}


Each attribute can have one or more annotations. None of them are mandatory:

Keyword Example Description

@Id

@Id int id

The surrogate key. Only one attribute should be marked as surrogate key. Should not be combined with @Key.

@Key

@Key long key

The natural key. One or several attributes can be included in the natural key. Should not be combined with @Id.

@Optional

@Optional String middleName

The keywords @Optional and @Id will exclude the attribute from the chained creator allowing them to be uninitialized (null or 0 if a primitive type).

Types

The following types are supported:

Type Description

boolean, Boolean

true / false

byte, Byte

8 bit signed integer, range: -128 to 127

short, Short

16 bit signed integer, range: -32,768 to 32,767

char, Character

16 bit unsigned integer, range: 0 to 65,535

int, Integer

32 bit signed integer, range: -2,147,483,648 to 2,147,483,647

long, Long

64 bit signed integer, range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

float, Float

Single-precision 32-bit IEEE 754 floating point

double, Double

Double-precision 64-bit IEEE 754 floating point

BigInteger

Arbitrary-precision integer (java.math.BigInteger)

BigDecimal

Arbitrary-precision signed decimal number (java.math.BigDecimal)

String

Standard Java string (java.lang.String)

DateTime

Immutable Joda time type org.joda.time.DateTime, alternative to java.util.Date

DateMidnight

Immutable Joda time type org.joda.time.DateMidnight, alternative to java.util.Date

LocalDate

Immutable Joda time type org.joda.time.LocalDate, alternative to java.util.Date

LocalTime

Immutable Joda time type org.joda.time.LocalTime, alternative to java.util.Date

LocalDateTime

Immutable Joda time type org.joda.time.LocalDateTime, alternative to java.util.LocalDateTime

Instant

Immutable Joda time type org.joda.time.Instant

YearMonth

Immutable Joda time type org.joda.time.YearMonth

MonthDay

Immutable Joda time type org.joda.time.MonthDay

Interval

Immutable Joda time type org.joda.time.Interval

Duration

Immutable Joda time type org.joda.time.Duration

Period

Immutable Joda time type org.joda.time.Period

Years

Immutable Joda time type org.joda.time.Years

Months

Immutable Joda time type org.joda.time.Months

Weeks

Immutable Joda time type org.joda.time.Weeks

Days

Immutable Joda time type org.joda.time.Days

Hours

Immutable Joda time type org.joda.time.Hours

Minutes

Immutable Joda time type org.joda.time.Minutes

Seconds

Immutable Joda time type org.joda.time.Seconds

All the supported types are immutable. The type java.util.Date is not supported and replaced by the Joda times equivalents. The type Object is not supported either for the same reason, that it may be mutable.


< Back          Next >