be.yildizgames.common.model.EntityId Maven / Gradle / Ivy
Show all versions of common-model Show documentation
/*
* This file is part of the Yildiz-Engine project, licenced under the MIT License (MIT)
*
* Copyright (c) 2019 Grégory Van den Borre
*
* More infos available: https://engine.yildiz-games.be
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial
* portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package be.yildizgames.common.model;
import java.util.HashMap;
import java.util.Map;
/**
* An id represent an unique instance of a class of object. The same id can be shared between different class but two objects of the same class cannot have the same id.
*
* Immutable class.
*
* @author Grégory Van den Borre
*/
public final class EntityId {
/**
* Constant value for the world.
*/
private static final long WORLD_VALUE = 0L;
/**
* List of all registered id with their value as key.
*/
private static final Map LIST = new HashMap<>();
/**
* Constant id for the world.
*/
public static final EntityId WORLD = new EntityId(EntityId.WORLD_VALUE);
/**
* Id value, it is immutable.
*/
public final long value;
/**
* Precomputed hashcode.
*/
private final int hashCode;
/**
* Full constructor, private to prevent use, to create an id, retrieve it from Id.get.
*
* @param idValue Initialize the wrapped value.
*/
private EntityId(final long idValue) {
super();
this.value = idValue;
EntityId.LIST.putIfAbsent(this.value, this);
this.hashCode = Long.valueOf(this.value).hashCode();
}
/**
* Retrieve an Id from a value.
*
* @param value Id value to get, positive and negative values are allowed, -1 is WORLD.
* @return The Id with the internal value correspond to the parameter.
*/
public static EntityId valueOf(final long value) {
if (!EntityId.LIST.containsKey(value)) {
EntityId.LIST.put(value, new EntityId(value));
}
return EntityId.LIST.get(value);
}
/**
* Check if an id is world.
*
* @param id Id to check.
* @return true
if Id matches world Id.
*/
public static boolean isWorld(final EntityId id) {
return id.value == EntityId.WORLD_VALUE;
}
/**
* Check if an id is world.
*
* @param id Internal value to check.
* @return true
if the internal value matches world internal value.
*/
public static boolean isWorld(final long id) {
return id == EntityId.WORLD_VALUE;
}
/**
* @return true
if id value is lower than 0.
*/
public final boolean isNegative() {
return this.value < 0;
}
/**
* Check if this is world.
*
* @return true
if this id is world.
*/
public final boolean isWorld() {
return this.value == EntityId.WORLD_VALUE;
}
@Override
public final boolean equals(final Object obj) {
if (!(obj instanceof EntityId)) {
throw new IllegalArgumentException("wrong type");
}
return this == obj;
}
@Override
public final int hashCode() {
return this.hashCode;
}
/**
* @return Id value as a String.
*/
@Override
public final String toString() {
return String.valueOf(this.value);
}
}