Class DataRepresentation

java.lang.Object
com.commercetools.docs.meta.DataRepresentation

public class DataRepresentation extends Object
Table of content

Representation

The SDK provides types for each of the types described in the documentation of the REST API. Each type is represented by an interface, an implementation class and a builder class.

Interfaces

The interface class describes the JSON structure by providing Getters and Setters. The necessary annotations to de-/serialize the JSON is defined here. Each interface has a factory method to create an instance of the implementation class and to copy an instance.
Customer customer = Customer.of();
Customer newCustomer = Customer.of(customer);

See the test code.

Also a model interface has a factory method to create a builder of a model instance or create a builder with the data of the instance itself. E.g.:
CustomerDraftBuilder builder = CustomerDraft.builder().email("john.doe@example.com");
CustomerDraft customerDraft = builder.build();
CustomerDraft newCustomerDraft = CustomerDraft.builder(customerDraft).build();

See the test code.

And each interface has a method to apply a function to the instance itself. This can be used for custom defined accessors. E.g.:
ProductVariant variant = ProductVariantBuilder.of().id(1L).attributes(Collections.emptyList()).build();
AttributesAccessor attributes = variant.withProductVariant(AttributesAccessor::of);
AttributeLocalizedEnumValue color = attributes.asLocalizedEnum("color");

See the test code.

The model interfaces define the inheritance/composition structure of all documented types.

Polymorphic types

Each polymorphic type interface provides methods for all it's subtype builders
CustomerGroupChangeNameAction action1 = CustomerGroupUpdateAction.changeNameBuilder().name("foo").build();
CustomerGroupSetKeyAction action2 = CustomerGroupUpdateAction.setKeyBuilder().key("foo").build();

See the test code.

This can be used to build a whole update body
CustomerGroupUpdate customerGroupUpdate = CustomerGroupUpdate.builder()
        .version(1L)
        .actions(CustomerGroupUpdateAction.changeNameBuilder().name("foo").build())
        .build();

See the test code.

Builder

For each type a builder class exists. Each property can be set using the method with the property name at the builder instance. E.g.:
CustomerDraft customerDraft = CustomerDraft.builder().email("john.doe@example.com").build();

See the test code.

Object property

If the property is an object type a method with a lambda function parameter is usable. The function is called with a builder instance of the properties type.
CustomerDraft customerDraft = CustomerDraft.builder()
        .email("john.doe@example.com")
        .anonymousCart(cartResourceIdentifierBuilder -> cartResourceIdentifierBuilder.key("cart-key"))
        .build();

See the test code.

Array property

Array properties have additional methods for adding addtional elements and build element instances.
CustomerDraft customerDraft = CustomerDraft.builder()
        .email("john.doe@example.com")
        .addresses(AddressDraft.builder().country("DE").build(), AddressDraft.builder().country("US").build())
        .addresses(Arrays.asList(AddressDraft.builder().country("DE").build(),
            AddressDraft.builder().country("US").build()))
        .build();

CustomerDraft customerDraft2 = CustomerDraft.builder()
        .email("john.doe@example.com")
        .addresses(Arrays.asList(AddressDraft.builder().country("DE").build(),
            AddressDraft.builder().country("US").build()))
        .build();

CustomerDraft customerDraft3 = CustomerDraft.builder()
        .email("john.doe@example.com")
        .addresses(AddressDraft.builder().country("DE").build())
        .plusAddresses(AddressDraft.builder().country("DE").build())
        .build();

CustomerDraft customerDraft4 = CustomerDraft.builder()
        .email("john.doe@example.com")
        .withAddresses(addressBuilder -> addressBuilder.country("DE"))
        .plusAddresses(addressBuilder -> addressBuilder.country("US"))
        .build();

See the test code.

Polymorphic types

Each polymorphic type builder provides methods for all it's subtype builders
CustomerGroupChangeNameAction action1 = CustomerGroupUpdateActionBuilder.of()
        .changeNameBuilder()
        .name("foo")
        .build();
CustomerGroupSetKeyAction action2 = CustomerGroupUpdateActionBuilder.of().setKeyBuilder().key("foo").build();

See the test code.

This can be used to build a whole update body
CustomerGroupUpdate customerGroupUpdate = CustomerGroupUpdate.builder()
        .version(1L)
        .withActions(builder -> builder.changeNameBuilder().name("foo"))
        .plusActions(builder -> builder.setKeyBuilder().key("foo"))
        .build();

See the test code.

Implementation

Each type has an implementation class (POJO). The class implements all properties of all interfaces and it's getters and setters. Also the implementation class implements an equals and hashcode method. The constructor is defined package private as new properties could be added by the API. The method `reflectionString` is available for all implementation classes by the ModelBase interface. This method will return a string representation of the instance.
CustomerDraft customerDraft = CustomerDraft.builder().email("john.doe@example.com").build();

String draft = ModelBase.reflectionString(customerDraft);
String draft2 = ((ModelBase) customerDraft).reflectionString();
String draft3 = customerDraft.withCustomerDraft(ModelBase::reflectionString);

See the test code.

  • Constructor Details

    • DataRepresentation

      public DataRepresentation()