com.codepoetics:phantom-pojo

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/

Categories

Categories

Ant Build Tools
GroupId

GroupId

com.codepoetics
ArtifactId

ArtifactId

phantom-pojo
Last Version

Last Version

0.7
Release Date

Release Date

Type

Type

jar
Description

Description

Sonatype helps open source projects to set up Maven repositories on https://oss.sonatype.org/
Source Code Management

Source Code Management

http://github.com/poetix/phantom-pojos

Download phantom-pojo

How to add to project

<!-- https://jarcasting.com/artifacts/com.codepoetics/phantom-pojo/ -->
<dependency>
    <groupId>com.codepoetics</groupId>
    <artifactId>phantom-pojo</artifactId>
    <version>0.7</version>
</dependency>
// https://jarcasting.com/artifacts/com.codepoetics/phantom-pojo/
implementation 'com.codepoetics:phantom-pojo:0.7'
// https://jarcasting.com/artifacts/com.codepoetics/phantom-pojo/
implementation ("com.codepoetics:phantom-pojo:0.7")
'com.codepoetics:phantom-pojo:jar:0.7'
<dependency org="com.codepoetics" name="phantom-pojo" rev="0.7">
  <artifact name="phantom-pojo" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.codepoetics', module='phantom-pojo', version='0.7')
)
libraryDependencies += "com.codepoetics" % "phantom-pojo" % "0.7"
[com.codepoetics/phantom-pojo "0.7"]

Dependencies

compile (2)

Group / Artifact Type Version
com.codepoetics : navn jar 0.3
org.databene : contiperf jar 2.2.0

test (3)

Group / Artifact Type Version
junit : junit jar 4.11
org.hamcrest : hamcrest-library jar 1.3
com.fasterxml.jackson.core : jackson-databind jar 2.4.2

Project Modules

There are no modules declared in this project.

phantom-pojos

Build Status

Immutable value types and builders implemented with dynamic proxies.

An PhantomPojo is a bean-like object defined using two interfaces:

public interface Person extends PhantomPojo<Person.Builder> {

    interface Builder extends Supplier<Person> {
        Builder withName(String name);
        Builder withAge(int age);
        Builder withFriends(Builder...friendBuilders);
        Builder withAddress(Address.Builder addressBuilder);
    }

    static Builder builder() {
        return PhantomBuilder.building(Person.class);
    }

    String getName();
    int getAge();
    List<Person> getFriends();
    Address getAddress();
}

Implementations of these interfaces are automatically created by PhantomBuilder (using dynamic proxies). They implement equals, hashCode and toString.

You create an instance of a PhantomPojo with its Builder, like so:

Person henry = Person.builder()
                .withName("Henry")
                .withAge(42)
                .withFriends(Person.builder()
                    .withName("Jerry")
                    .withAge(33)
                    .withFriends())
                .get();

The created object has getter methods for each of its properties:

assertThat(henry.getName(), equalTo("Henry"));
assertThat(henry.getFriends().get(0).getAge(), equalTo(33));

Once it has been created it cannot be changed, but a modified copy can be created using the update method:

Person henrietta = henry.update().withName("Henrietta").get();

A PhantomPojo wraps an array of property values, and can be created directly out of a map of property names to property values:

Map<String, Object> properties = new HashMap<>();
properties.put("name", "Angie");
properties.put("age", 63);
Person angie = PhantomPojo.wrapping(properties).with(Person.class);

assertThat(angie.getAge(), equalTo(63));

You can always retrieve this map of property values from the PhantomPojo via its properties method:

assertThat(angie.properties().get("name"), equalTo("Angie"));

Nested maps are automatically promoted to PhantomPojos:

Map<String, Object> addressProperties = new HashMap<>();
addressProperties.put("addressLines", Arrays.asList("67 Penguin Street", "Cinderford"));
addressProperties.put("postcode", "RA8 81T");

Map<String, Object> personProperties = new HashMap<>();
personProperties.put("name", "Harry");
personProperties.put("age", 37);
personProperties.put("address", addressProperties);

Person person = PhantomPojo.wrapping(personProperties).with(Person.class);

assertThat(person.getAddress().getPostcode(), equalTo("RA8 81T"));

Versions

Version
0.7
0.6
0.5
0.4
0.3
0.2
0.1