Id#

The id of a task, link, resource, assignment or calendar. An Id holds either a number or a string. Create one with Id.num or Id.str.

Two ids are equal when their key() values are equal, so Id.num(1) and Id.str("1") are the same id.

Declaration#

public final class Id

Id is immutable and has no public constructor.

Methods#

isNum#

public boolean isNum()

Returns true for a numeric id.

isStr#

public boolean isStr()

Returns true for a string id.

numValue#

public Double numValue()

Returns the number of a numeric id, or null for a string id.

strValue#

public String strValue()

Returns the text of a string id, or null for a numeric id.

key#

public String key()

Returns the id as text. A string id is returned as is. A number is printed the way JavaScript's String(n) prints it:

Number Key
1 "1"
1.5 "1.5"
0 and -0 "0"
1e21 "1e+21"
1e-7 "1e-7"
Double.NaN "NaN"

equals, hashCode and toString all use this key.

Errors: throws IllegalStateException when the id is an infinite number.

norm#

public String norm()

Same as key().

equals#

public boolean equals(Object other)

Returns true when other is an Id with the same key: Id.num(1).equals(Id.str("1")) is true. String keys compare case-sensitively. Every NaN id equals every other NaN id. equals(null) and a non-Id argument return false.

A number beyond double precision is stored rounded, so Id.num(9007199254740993L) equals Id.str("9007199254740992").

Errors: throws IllegalStateException when the key of either id is needed and that id is an infinite number.

hashCode#

public int hashCode()

Hash of the key, so equal ids hash the same and Id works as a HashMap or HashSet key.

Errors: throws IllegalStateException for an infinite number.

toString#

public String toString()

Returns the key.

Static methods#

num#

public static Id num(double n)

Creates a numeric id.

str#

public static Id str(String s)

Creates a string id. The empty string is a valid id.

Errors: throws NullPointerException when s is null.

Validation#

An id in project data must be a finite number or a string. A NaN or infinite numeric id throws InputException with path "id" when the data is loaded.

Example#

import org.dhtmlx.plen.*;
import java.util.HashMap;

public class IdExample {
  public static void main(String[] args) throws Exception {
    Id a = Id.num(1);
    Id b = Id.str("1");
    Id c = Id.num(1.5);

    System.out.println(a.equals(b));    // true
    System.out.println(a.isNum());      // true
    System.out.println(b.strValue());   // 1
    System.out.println(c.key());        // 1.5

    var names = new HashMap<Id, String>();
    names.put(a, "Design");
    System.out.println(names.get(Id.str("1")));  // Design
  }
}