View Javadoc
1   package org.opentrafficsim.base.logger;
2   
3   import java.util.Random;
4   
5   /**
6    * LogCategory for the CategoryLogger. <br>
7    * <br>
8    * Copyright (c) 2003-2018 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
9    * for project information <a href="https://www.simulation.tudelft.nl/" target="_blank">www.simulation.tudelft.nl</a>. The
10   * source code and binary code of this software is proprietary information of Delft University of Technology.
11   * @author <a href="https://www.tudelft.nl/averbraeck" target="_blank">Alexander Verbraeck</a>
12   */
13  public class LogCategory
14  {
15      /** The category name; can be blank. */
16      private final String name;
17  
18      /** Cached hashcode for very quick retrieval. */
19      private final int hashCode;
20  
21      /** Random number to generate fast hashCode. */
22      private static Random random = new Random(1L);
23  
24      /** The category to indicate that ALL messages need to be logged. */
25      public static final LogCategory ALL = new LogCategory("ALL");
26      
27      /**
28       * @param name the category name; can be blank
29       */
30      public LogCategory(final String name)
31      {
32          this.name = name == null ? "" : name;
33          this.hashCode = calcHashCode();
34      }
35  
36      /** {@inheritDoc} */
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 == null || this.name.length() == 0) ? random.nextInt() : prime + this.name.hashCode();
53          return result;
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      @SuppressWarnings("checkstyle:needbraces")
59      public boolean equals(final Object obj)
60      {
61          if (this == obj)
62              return true;
63          if (obj == null)
64              return false;
65          if (getClass() != obj.getClass())
66              return false;
67          LogCategory other = (LogCategory) obj;
68          if (this.hashCode != other.hashCode)
69              return false;
70          return true;
71      }
72  
73  }