All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eventsourcing.StandardEntity Maven / Gradle / Ivy

There is a newer version: 0.4.6
Show newest version
/**
 * Copyright (c) 2016, All Contributors (see CONTRIBUTORS file)
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
package com.eventsourcing;

import com.eventsourcing.hlc.HybridTimestamp;

import java.util.UUID;
import java.util.concurrent.LinkedBlockingDeque;

/**
 * Standard {@link Entity} implementation. Will generate UUID if one is not provided.
 * @param 
 */
public abstract class StandardEntity implements Entity {

    private static LinkedBlockingDeque uuids = new LinkedBlockingDeque<>(10_000);

    static {
        new Thread(() -> {
            while (true) {
                try {
                    uuids.put(UUID.randomUUID());
                } catch (InterruptedException e) {
                }
            }
        }).start();
    }

    private UUID uuid;

    /**
     * Returns entity UUID. Generates one if none assigned.
     *
     * @return Entity UUID
     */
    @Override
    public UUID uuid() {
        while (uuid == null) {
            try {
                uuid = uuids.take();
            } catch (InterruptedException e) {
            }
        }
        return uuid;
    }

    private HybridTimestamp timestamp;

    public HybridTimestamp timestamp() {return this.timestamp;}

    public E uuid(UUID uuid) {
        this.uuid = uuid;
        return (E)this;
    }

    public E timestamp(HybridTimestamp timestamp) {
        this.timestamp = timestamp;
        return (E)this;
    }

    public StandardEntity() {}
    public StandardEntity(HybridTimestamp timestamp) {
        this.timestamp = timestamp;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy