Giter Club home page Giter Club logo

radioactive's Introduction

radioactive

A Java 8 library for building, validating, mutating and mapping beans.

Build Status

In Maven:

<dependency>
    <groupId>com.codepoetics</groupId>
    <artifactId>radioactive</artifactId>
    <version>0.7</version>
</dependency>

An Accessor is a Getter paired with a Setter, like so:

public static final Accessor<Person, String> NAME = Accessor.of(Person::getName, Person::setName);
public static final Accessor<Person, Address> ADDRESS = Accessor.of(Person::getAddress, Person::setAddress);

You can use them to build new objects:

Person person = Person.BUILDER.with(
    Person.NAME.of("Arthur Putey"),
    Person.ADDRESS.of(Address.BUILDER.with(
        Address.FIRST_LINE.of("22 Acacia Avenue"),
        Address.SECOND_LINE.of("Sunderland"),
        Address.POSTCODE.of("VB6 5UX"))))
    .get();

to query and modify existing objects:

assertThat(Person.NAME.get(person), equalTo("Arthur Putey"));

assertThat(Person.ADDRESS.join(Address.SECOND_LINE).get(person),
           equalTo("Sunderland"));
           
Person.NAME.set(person, "Angus Yentob");

assertThat(person.getName(), equalTo("Angus Yentob"));

Person.ADDRESS.join(Address.POSTCODE).set(person, "RA8 81T");

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

to validate objects:

Validator<Person> validator = Validator
        .validating("name", Person.NAME, equalTo("Agnes Phobos"))
        .and("age", Person.AGE, greaterThanOrEqualTo(0));

Valid<Person> validPerson = Valid.validate(new Person("Angus Eros", -2), validator);

assertFalse(validPerson.isValid());
assertFalse(validPerson.get().isPresent());
assertThat(validPerson.validationErrors(), hasItems("name: Angus Eros != Agnes Phobos", "age: -2 < 0"));

and to map from type to type:

Person person1 = new Person();
person1.setName("Antigone");
person1.setAge(30);

Knight bean2 = Mapper.from(Person::getAge).to(Knight::setAge)
        .andFrom(Person::getName).via(String::toUpperCase).to(Knight::setName)
        .andFrom("I seek the grail").to(Knight::setQuest)
        .creatingWith(Knight::new)
        .apply(person1);

assertThat(bean2.getName(), equalTo("ANTIGONE"));
assertThat(bean2.getAge(), equalTo(30));
assertThat(bean2.getQuest(), equalTo("I seek the grail"));

If you've seen any of my other libraries for doing similar things, notably Karg and Octarine, you'll have seen most of this before. Radioactive is a variant specifically for working with old-style "bean" objects with getters and setters, because sometimes you have to and why make it more painful than it needs to be?

radioactive's People

Contributors

poetix avatar dhaase avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.