1 package org.djutils.logger;
2
3 import java.util.Random;
4
5 /**
6 * LogCategory for the CategoryLogger.
7 * <p>
8 * Copyright (c) 2018-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
9 * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
10 * distributed under a three-clause BSD-style license, which can be found at
11 * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
12 * </p>
13 */
14 public class LogCategory
15 {
16 /** The category name; can be blank. */
17 private final String name;
18
19 /** Cached hashcode for very quick retrieval. */
20 private final int hashCode;
21
22 /** Random number to generate hashCode for null or "" name. */
23 private static Random random = new Random(1L);
24
25 /** The category to indicate that ALL messages need to be logged. */
26 public static final LogCategory ALL = new LogCategory("ALL");
27
28 /**
29 * @param name can be blank
30 */
31 public LogCategory(final String name)
32 {
33 this.name = name == null ? "" : name;
34 this.hashCode = calcHashCode();
35 }
36
37 @Override
38 public int hashCode()
39 {
40 return this.hashCode;
41 }
42
43 /**
44 * Calculate the hashCode. In case of a blank name, use a reproducible random number (so NOT the memory address of the
45 * LogCategory object)
46 * @return the calculated hash code
47 */
48 private int calcHashCode()
49 {
50 final int prime = 31;
51 int result = 1;
52 result = (this.name.length() == 0) ? random.nextInt() : prime + this.name.hashCode(); // name != null
53 return result;
54 }
55
56 @Override
57 @SuppressWarnings("checkstyle:needbraces")
58 public boolean equals(final Object obj)
59 {
60 if (this == obj)
61 return true;
62 if (obj == null)
63 return false;
64 if (getClass() != obj.getClass())
65 return false;
66 LogCategory other = (LogCategory) obj;
67 if (this.hashCode != other.hashCode)
68 return false;
69 return true;
70 }
71
72 @Override
73 public String toString()
74 {
75 return this.name;
76 }
77
78 }