From 5dc4592d6eaa3d27041a9a6b2645493ce0e0d301 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Thu, 04 Apr 2013 08:29:12 +0000
Subject: [PATCH] Minor optimizations: avoid unnecessary Calendar clone in toString() and avoid possible additional volatile read.

---
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/GeneralizedTime.java |   44 ++++++++++++++++++++++++++++++++------------
 1 files changed, 32 insertions(+), 12 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/GeneralizedTime.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/GeneralizedTime.java
index 8f0b9ba..aaa871d 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/GeneralizedTime.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/GeneralizedTime.java
@@ -21,11 +21,23 @@
  * CDDL HEADER END
  *
  *
- *      Copyright 2012 ForgeRock AS.
+ *      Copyright 2012-2013 ForgeRock AS.
  */
 package org.forgerock.opendj.ldap;
 
-import static org.forgerock.opendj.ldap.CoreMessages.*;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_EMPTY_FRACTION;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_ILLEGAL_FRACTION_CHAR;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_ILLEGAL_TIME;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_CHAR;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_DAY;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_HOUR;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_MINUTE;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_MONTH;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_OFFSET;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_SECOND;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_YEAR;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_NO_TIME_ZONE_INFO;
+import static org.forgerock.opendj.ldap.CoreMessages.WARN_ATTR_SYNTAX_GENERALIZED_TIME_TOO_SHORT;
 
 import java.util.Calendar;
 import java.util.Date;
@@ -1179,6 +1191,7 @@
     /**
      * {@inheritDoc}
      */
+    @Override
     public int compareTo(final GeneralizedTime o) {
         final Long timeMS1 = getTimeInMillis();
         final Long timeMS2 = o.getTimeInMillis();
@@ -1188,6 +1201,7 @@
     /**
      * {@inheritDoc}
      */
+    @Override
     public boolean equals(final Object obj) {
         if (this == obj) {
             return true;
@@ -1221,6 +1235,7 @@
     /**
      * {@inheritDoc}
      */
+    @Override
     public int hashCode() {
         return ((Long) getTimeInMillis()).hashCode();
     }
@@ -1234,14 +1249,7 @@
      * @return A {@code Calendar} representation of this generalized time.
      */
     public Calendar toCalendar() {
-        Calendar tmpCalendar = calendar;
-        if (tmpCalendar == null) {
-            tmpCalendar = new GregorianCalendar(TIME_ZONE_UTC_OBJ);
-            tmpCalendar.setLenient(false);
-            tmpCalendar.setTimeInMillis(getTimeInMillis());
-            calendar = tmpCalendar;
-        }
-        return (Calendar) tmpCalendar.clone();
+        return (Calendar) getCalendar().clone();
     }
 
     /**
@@ -1264,13 +1272,14 @@
     /**
      * {@inheritDoc}
      */
+    @Override
     public String toString() {
         String tmpString = stringValue;
         if (tmpString == null) {
             // Do this in a thread-safe non-synchronized fashion.
             // (Simple)DateFormat is neither fast nor thread-safe.
             final StringBuilder sb = new StringBuilder(19);
-            final Calendar tmpCalendar = toCalendar();
+            final Calendar tmpCalendar = getCalendar();
 
             // Format the year yyyy.
             int n = tmpCalendar.get(Calendar.YEAR);
@@ -1360,6 +1369,17 @@
             tmpString = sb.toString();
             stringValue = tmpString;
         }
-        return stringValue;
+        return tmpString;
+    }
+
+    private Calendar getCalendar() {
+        Calendar tmpCalendar = calendar;
+        if (tmpCalendar == null) {
+            tmpCalendar = new GregorianCalendar(TIME_ZONE_UTC_OBJ);
+            tmpCalendar.setLenient(false);
+            tmpCalendar.setTimeInMillis(getTimeInMillis());
+            calendar = tmpCalendar;
+        }
+        return tmpCalendar;
     }
 }

--
Gitblit v1.10.0