A factory and generators framework for Java 7 and up.

Overview

Factorium-J7

A simple generator and factory framework.

Compatibility

Compatibility is guaranteed for Java 7 and up.

How to use it

The core interfaces of Factorium are Generator, Factory, and Modifier. Generators are the core of this framework, and in fact, factories are also just groups of generators with some sauce on top. Modifiers serve to optionally modify a new object before it is returned.

Generators

A generator's job is to create an object and apply modifiers to it. A new object with random values should be generated at each new call to its methods. To simplify the application of modifiers, the BaseGenerator abstract class is provided.

It is recommended to always extend from BaseGenerator or one of its subclasses when creating a generator as it guarantees protection against possible (unlikely) future API changes.

The FakerGenerator is a BaseGenerator with a static faker instance to simplify the creation of objects with random data. All examples in this readme assume usage of the FakerGenerator.

Simply create a subclass of BaseGenerator (or FakerGenerator) and implement the protected T make() method. Like so:

public class CityGenerator extends FakerGenerator<City> {
    @Override
    protected City make() {
        return new City(faker.name().lastName(), Math.abs(faker.random().nextLong()) + 1);
    }
}

And call it like:

    City foo = new CityGenerator().generate();

But what if I want to have specialized versions of my generator for different scenarios? Simple, just create a new generator. I suggest keeping generators for a same class, in the same class with the outer class being the default generator.

public class CityGenerator extends FakerGenerator<City> {
    /**
     * Suggested key for the default city generator.
     */
    public static String KEY = "default";

    @Override
    protected City make() {
        return new City(faker.name().lastName(), Math.abs(faker.random().nextLong()) + 1);
    }

    public static class CapitalGenerator extends FakerGenerator<City> {
        /**
         * Suggested key for the capital city generator.
         */
        public static String KEY = "capital";

        @Override
        protected City make() {
            return new City(faker.country().capital(), Math.abs(faker.random().nextLong()) + 1);
        }
    }
}

Cool, but I want like, 5 cities. How do I do that? Just give an int as your first argument to the generator you call.

    List<City> cities = new CityGenerator().generate(5);

Modifiers

Modifiers modify a new object before it is returned to the caller. There are two modifiers provided out of the box:

  • FieldMod
  • MethodMod

Note that you may add as many modifiers, one after the other, to a generate() call. If you generate multiple objects at the same time, all the modifiers will be applied to all the objects.

    City city = new CityGenerator().generate(new FieldMod("nCitizens", 5), new MethodMod("setName", "Toronto"));

Note that modifiers are usually applied after the object has been created.

Factories

Factories are groups of generators that produce the same kind of output. They may be used to decorate your generators. For example, the RecordingFactory registers all the objects its generators create, which can be useful if you want to test a DAO.

A Factory is a Generator. To accomplish this contract, it must have a default generator that will be used when no specific generator key is given.

The easiest way to create a Factory is by using the BaseFactory class and implementing it's decorate(T, Modifier[]) method.

You might also like...

Tabletop Games Framework (TAG) - a Java-based benchmark for developing modern board games for AI research

Tabletop Games Framework (TAG) - a Java-based benchmark for developing modern board games for AI research

The Tabletop Games Framework (TAG) is a Java-based benchmark for developing modern board games for AI research

Dec 12, 2022

A simple and scalable Android bot emulation framework, as presented at Black Hat Europe 2021's Arsenal, as well as atHack 2021's Arsenal

m3 A simple and scalable Android bot emulation framework. A detailed explanation can be found here. This project was first published at Black Hat Euro

Aug 20, 2022

A light-weight and dynamic dependency injection framework

⚠️ This project is now part of the EE4J initiative. This repository has been archived as all activities are now happening in the corresponding Eclipse

Dec 23, 2022

A fast, lightweight and more productive microservices framework

A fast, lightweight and cloud-native microservices framework. Stack Overflow | Google Group | Gitter Chat | Subreddit | Youtube Channel | Documentatio

Jan 5, 2023

[WIP] Springram is uber library for working with Telegram using Spring Framework and Spring Boot.

Springram Springram is a library for working with telegram using the spring framework and spring boot. This library gives you the ability to use contr

Nov 1, 2022

This project is a simple messaging application made using React-Native framework, Gifted-Chat library and Firebase database

This project is a simple messaging application made using React-Native framework, Gifted-Chat library and Firebase database. The example that will be shown here focuses on the ability of two people to message each other in a chat room.

Jan 30, 2022

An assistance platform made using Spring framework that analyses your code, and helps you either to start a devops project, or to turn an existing project into a devops project using open source software (Git, Docker, Jenkins..)

An assistance platform made using Spring framework that analyses your code, and helps you either to start a devops project, or to turn an existing project into a devops project using open source software (Git, Docker, Jenkins..)

DevOpsify Description An assistance platform made using Spring framework that analyses your code, and helps you either to start a devops project, or t

Nov 8, 2022

Spring Boot starter module for gRPC framework.

Spring Boot starter module for gRPC framework.

Jan 4, 2023
Owner
Samuel Beausoleil
Samuel Beausoleil
Lattice is a powerful, lightweight business extension invoke framework. By using the Lattice framework, complex business customization can be efficiently organized and managed.

Lattice Framework Introduction Lattice is a powerful, lightweight business extension invoke framework. By using the Lattice framework, complex busines

null 41 Dec 30, 2022
An API Library that provides the functionality to access, manage and store device topologies found in JSON files using Java and Maven Framework

Topology API ?? About An API library which provides the functionality to access, manage and store device topologies. ?? Description Read a topology fr

Abdelrahman Hamdy 2 Aug 4, 2022
An advanced and highly optimized Java library to build framework

An advanced and highly optimized Java library to build frameworks: it's useful for scanning class paths, generating classes at runtime, facilitating the use of reflection, scanning the filesystem, executing stringified source code and much more...

Burningwave 119 Dec 21, 2022
Application for creating blog posts, developed with Java using Spring Framework for backend and Angular along with PrimeNG Library for frontend development.

Application for creating blog posts, developed with Java using Spring Framework for backend and Angular along with PrimeNG Library for frontend development.

Áureo Carmelino 10 Nov 27, 2022
Java framework for making REST clients

Pocolifo's REST Client Framework Java framework for making REST clients Here's how easy it is Here's an example for a GET request in the context of a

null 3 Jun 19, 2022
Java Code Generation Framework

Java::Geci Read "how to contribute". Java::Geci is a library for generating Java code. You can execute code generation programs using Java::Geci to ge

Peter Verhas 113 Dec 4, 2022
RESTX, the lightweight Java REST framework

RESTX - the lightweight Java REST framework RESTX is a full lightweight disrupting stack, which includes Swagger-like ui & considers REST specs tests

null 437 Oct 22, 2022
Cross-platform framework for building truly native mobile apps with Java or Kotlin

Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.

Codename One 1.4k Jan 5, 2023
Simple micro web framework written in Pragmatic Functional Java style

Pragmatica REST Example Example nano web framework written in Pragmatic Functional Java style. Example public class App { public static void main(

Sergiy Yevtushenko 8 Oct 21, 2022
A implementation of shadowsocks that base on java's netty framework

Shadowsocks shadowsocks is a fast tunnel proxy that helps you bypass firewalls. shadowsocks-java is a implementation of shadowsocks protocol that base

bigbyto 36 Oct 17, 2022