Git Product home page Git Product logo

touuid's Introduction

Main CI Maven Central

A tiny library for generating UUIDs in automated tests for Java and Kotlin


Table of contents:

Install

Maven

<dependency>
    <groupId>io.github.atomfinger</groupId>
    <artifactId>atomfinger-touuid</artifactId>
    <version>1.0.1</version>
    <scope>test</scope>
</dependency>

Gradle

(Using the Maven Central Repository)

testCompile group: 'io.github.atomfinger', name: 'atomfinger-touuid', version: '1.0.1'

Examples

Java

toUUID is first and foremost a Kotlin project. Still, one of the goals was to keep it free for any unnecessary dependencies, which is why Java users should use the UUIDs class to avoid having to deal with any extra dependencies.

UUID from a single integer:

import static io.github.atomfinger.touuid.UUIDs.*;

UUID uuid = toUUID(1);
System.out.println(uuid.toString());
//Output:
//00000000-0000-0000-0000-000000000001

UUID from a list of integers:

import static io.github.atomfinger.touuid.UUIDs.*;

List<UUID> uuids = toUUIDs(Arrays.asList(1, 2, 3, 4, 5));
uuids.forEach((it) -> System.out.println(it.toString()));
//Output:
//00000000-0000-0000-0000-000000000001
//00000000-0000-0000-0000-000000000002
//00000000-0000-0000-0000-000000000003
//00000000-0000-0000-0000-000000000004
//00000000-0000-0000-0000-000000000005

UUID from varargs:

import static io.github.atomfinger.touuid.UUIDs.*;

List<UUID> uuids = toUUIDs(1, 2, 3, 4, 5);
uuids.forEach((it) -> System.out.println(it.toString()));
//Output:
//00000000-0000-0000-0000-000000000001
//00000000-0000-0000-0000-000000000002
//00000000-0000-0000-0000-000000000003
//00000000-0000-0000-0000-000000000004
//00000000-0000-0000-0000-000000000005

UUID from range of integers:

import static io.github.atomfinger.touuid.UUIDs.*;

List<UUID> uuids = toUUIDsFromRange(1, 5);
uuids.forEach((it) -> System.out.println(it.toString()));
//Output:
//00000000-0000-0000-0000-000000000001
//00000000-0000-0000-0000-000000000002
//00000000-0000-0000-0000-000000000003
//00000000-0000-0000-0000-000000000004
//00000000-0000-0000-0000-000000000005

Kotlin

Kotlin has access to all the feature Java has, but also has the addition of extension functions.

Generate based on an integer:

val uuid = 1.toUuid()
println(uuid.toString())
//Output:
//00000000-0000-0000-0000-000000000001

Generate from a list of integers:

val uuids = listOf(1, 2, 3, 4, 5).toUuids()
uuids.forEach { println(it.toString()) }
//Output:
//00000000-0000-0000-0000-000000000001
//00000000-0000-0000-0000-000000000002
//00000000-0000-0000-0000-000000000003
//00000000-0000-0000-0000-000000000004
//00000000-0000-0000-0000-000000000005

Generate based on a range of integers:

val uuids = (1..5).toUuids()
uuids.forEach { println(it.toString()) }
//Output:
//00000000-0000-0000-0000-000000000001
//00000000-0000-0000-0000-000000000002
//00000000-0000-0000-0000-000000000003
//00000000-0000-0000-0000-000000000004
//00000000-0000-0000-0000-000000000005

Generate based on a sequence of integers:

val uuids = uuids().take(5)
uuids.forEach { println(it.toString()) }
//Output:
//00000000-0000-0000-0000-000000000001
//00000000-0000-0000-0000-000000000002
//00000000-0000-0000-0000-000000000003
//00000000-0000-0000-0000-000000000004
//00000000-0000-0000-0000-000000000005

Why toUUID?

Check out the writeup on why toUUID() is worth it

How toUUID works

toUUID takes the integer and puts it at the back of the UUID. Which is why the number:
1
turns into the UUID:
00000000-0000-0000-0000-000000000001.

Here's some more examples:

Number input UUID output
2 00000000-0000-0000-0000-000000000002
5 00000000-0000-0000-0000-000000000005
10 00000000-0000-0000-0000-000000000010
55 00000000-0000-0000-0000-000000000055
100 00000000-0000-0000-0000-000000000100
100000 00000000-0000-0000-0000-000000100000

⚠️ Never use toUUID in production code ⚠️

toUUID is not a replacement for how one normally generates UUIDs in production code. There are a few versions of UUID which all have different algorithms attached to them, and this library bypasses all that. While toUUID generates technically valid UUIDs it does not create a UUID which is suitable for production (even if a random number is passed).

toUUID is to be used in automated testing or to generate a repeatable set of human-readable UUIDs.

Demo projects

Both Kotlin and Java has demo projects:

How to build

.toUUID is a standard Maven application:

  1. Clone project
  2. Run mvn clean install in the project folder

Requirements:

  • Java 1.8 or higher
  • the Kotlin compiler

Contact

you can contact me through my website or the social links found on said website. Don't hesitate to ask if there are any questions :)

touuid's People

Contributors

atomfinger avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

touuid's Issues

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.