MATSIM
IntegerCache.java
Go to the documentation of this file.
1 /* *********************************************************************** *
2  * project: org.matsim.*
3  * IntegerCache.java
4  * *
5  * *********************************************************************** *
6  * *
7  * copyright : (C) 2007 by the members listed in the COPYING, *
8  * LICENSE and WARRANTY file. *
9  * email : info at matsim dot org *
10  * *
11  * *********************************************************************** *
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * See also COPYING, LICENSE and WARRANTY file *
18  * *
19  * *********************************************************************** */
20 
21 package org.matsim.core.utils.misc;
22 
23 import org.apache.logging.log4j.LogManager;
24 import org.apache.logging.log4j.Logger;
25 
26 public class IntegerCache {
27  // yyyy not sure what this class is doing. as long as MIN_VALUE=0 (as I am finding it), it just seems to receive an
28  // Integer, and return an Integer of the same value. Maybe there was historically a situation where this did not start
29  // at 0, and then one wanted to get rid of the (un)boxing overhead? kai, jan'16
30 
31  private final static Logger log = LogManager.getLogger(IntegerCache.class);
32 
33 
34  private static final int MIN_VALUE = 0;
35  private static final int MAX_VALUE = 8192;
36 
37  private static boolean initialized = false;
38  private static Integer [] cache;
39 
40  public static Integer getInteger(final int i) {
41 
42  if (!initialized) {
43  init();
44  }
45 
46  if ( i < MAX_VALUE && i >= MIN_VALUE) {
47  return cache[i + MIN_VALUE];
48  }
49 
50  return Integer.valueOf(i);
51  }
52 
53  synchronized private static void init() {
54  if (initialized) {
55  log.warn("IntegerCache has already been initialized.");
56  return;
57  }
58 
59  log.info("Initializing IntegerCache ...");
60  cache = new Integer [MAX_VALUE - MIN_VALUE];
61  for (int i = 0; i < (MAX_VALUE - MIN_VALUE); i++) {
62  cache[i] = Integer.valueOf(i - MIN_VALUE);
63  }
64  log.info("done.");
65  initialized = true;
66  }
67 
68 }
static Integer getInteger(final int i)
synchronized static void init()