Note that there are some explanatory texts on larger screens.

plurals
  1. POJodaTime Chronology caching
    text
    copied!<p>I have successfully written a new Chronology that represents my company's fiscal calendar, based off of JodaTime. I referred to the JodaTime source code quite a bit, to figure out what I needed to do. One of the things I noticed in the <code>BasicChronology</code> class was the use of the inner class <code>YearInfo</code> to cache the 'firstDayOfYearMillis' - the number of milliseconds since 1970-01-01 (ISO). Figuring that, if it was enough of a performance bottleneck that JodaTime was caching it, I should probably add it to my chronology too.<br> When I did so, though, I made some modifications. Specifically, I moved the <code>getYearInfo</code> method into the <code>YearInfo</code> inner class, as well as making it static. I also moved the array used to store the cached values into the inner class as well. Full definition of the modified class is like this: </p> <pre><code>/** * Caching class for first-day-of-year millis. * */ private static final class YearInfo { /** * Cache setup for first-day-of-year milliseconds. */ private static final int CACHE_SIZE = 1 &lt;&lt; 10; private static final int CACHE_MASK = CACHE_SIZE - 1; private static transient final YearInfo[] YEAR_INFO_CACHE = new YearInfo[CACHE_SIZE]; /** * Storage variables for cache. */ private final int year; private final long firstDayMillis; private final boolean isLeapYear; /** * Create the stored year information. * * @param inYear The year to store info about. */ private YearInfo(final int inYear) { this.firstDayMillis = calculateFirstDayOfYearMillis(inYear); this.isLeapYear = calculateLeapYear(inYear); this.year = inYear; } /** * Get year information. * * @param year The given year. * * @return Year information. */ private static YearInfo getYearInfo(final int year) { YearInfo info = YEAR_INFO_CACHE[year &amp; CACHE_MASK]; if (info == null || info.year != year) { info = new YearInfo(year); YEAR_INFO_CACHE[year &amp; CACHE_MASK] = info; } return info; } } </code></pre> <p>My question is... What are the performance or design implications of my changes? I've already decided that my changes should be thread-safe (given answers about final member variables). But why was the original implementation done the way it was, and not like this? I get why most of the methods that are being used effectively staticly aren't (given subclasses of <code>BasicChronology</code>), but I'll admit that some of my OO design stuff is a little rusty (having spent the last two years using RPG).<br> So... thoughts?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload