Giter Club home page Giter Club logo

idyll's Introduction

Idyll

Idyll is a lightweight Java library for dealing with object identifiers. This library encourages additional type-safety by making it easy to use Id<T>s rather than ints to uniquely identify other objects.

Why Generic IDs?

Java's type system protects you and your team from an entire class of errors. You can press this advantage by taking type-safety even further. Any non-trivial project will sometimes toss around object IDs. Protect yourself from mixing up these ID types by applying generics.

The Problem

A common pattern in Java is to use ints, or sometimes more complex but still unsafe objects, as unique identifiers. This isn't bad until you start identifying more than one type of object. When more than one type of object starts to rely on ints as identifiers, you open yourself up to the very errors that type-safety is supposed to save you from. For example:

private void showPropertyValue(int userId, int propertyTypeId) {
    ...
}

private void confirmAddress(User user) {
    showPropertyValue(PropertyType.ADDRESS.getId(), user.getId());
    ...

The above code happily compiles. If you're lucky, it'll break in an obvious manner upon execution. In that case, you lose a minute or so with a compile-relaunch-observe cycle. In the worst case, you ship broken code.

The Solution

Use Id<T>s instead of ints! The method signature from above becomes:

private void showPropertyValue(Id<User> userId, Id<PropertyType> propertyTypeId) {
    ...
}

From there, the improper call to showPropertyValue() fails to compile. Your IDE will give you quick feedback if you accidentally juxtapose your parameters, but it may have just suggested the right thing for you in the first place.

Usage

Suggested usage is to have your objects implement the Identifiable interface, which only makes you implement the getId() method. For example:

public class User implements Identifiable<User> {
    private final Id<User> id;
    private final String name;

    public User(Id<User> id, String name) {
        this.id = id;
        this.name = name;
    }
    
    public Id<User> getId() { return id; }
}

IdFactory

To help you generate IDs, there's a thread-safe IdFactory implementation LongIdFactory. Possible usage looks like this:

IdFactory idFactory = new LongIdFactory(); // Generates IDs backed by longs.
User steve = new User(idFactory.generateId(), "Steve");

Implementing your own IdFactory only entails defining a generateId() method.

IdMap

Idyll also comes with the IdMap data structure, which makes it a little simpler to keep a collection of Identifiable objects:

IdMap<User> users = new HashIdMap<User>();
users.add(steve);
users.add(kate);

...

users.get(steveId); // Returns steve.

idyll's People

Contributors

steve-downing avatar

Watchers

 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.