From 1805576e8a4df16b0743a6498f0a467eb0d5e446 Mon Sep 17 00:00:00 2001
From: Matthew Swift <matthew.swift@forgerock.com>
Date: Mon, 07 May 2012 14:20:51 +0000
Subject: [PATCH] Remove duplicate code.

---
 opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/GeneralizedTimeSyntaxImpl.java |  146 +++++++++++-------------------------------------
 1 files changed, 35 insertions(+), 111 deletions(-)

diff --git a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/GeneralizedTimeSyntaxImpl.java b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/GeneralizedTimeSyntaxImpl.java
index d72bed1..21d9d8b 100644
--- a/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/GeneralizedTimeSyntaxImpl.java
+++ b/opendj3/opendj-ldap-sdk/src/main/java/org/forgerock/opendj/ldap/schema/GeneralizedTimeSyntaxImpl.java
@@ -22,6 +22,7 @@
  *
  *
  *      Copyright 2009 Sun Microsystems, Inc.
+ *      Portions copyright 2012 ForgeRock AS.
  */
 
 package org.forgerock.opendj.ldap.schema;
@@ -641,24 +642,8 @@
         case 'Z':
             // This is fine only if we are at the end of the value.
             if (length == 11) {
-                try {
-                    final GregorianCalendar calendar = new GregorianCalendar();
-                    calendar.setLenient(false);
-                    calendar.setTimeZone(TIME_ZONE_UTC_OBJ);
-                    calendar.set(year, month, day, hour, minute, second);
-                    calendar.set(Calendar.MILLISECOND, 0);
-                    return calendar.getTimeInMillis();
-                } catch (final Exception e) {
-                    // This should only happen if the provided date wasn't legal
-                    // (e.g., September 31).
-                    final LocalizableMessage message =
-                            WARN_ATTR_SYNTAX_GENERALIZED_TIME_ILLEGAL_TIME.get(valueString, String
-                                    .valueOf(e));
-                    final DecodeException de = DecodeException.error(message, e);
-                    StaticUtils.DEBUG_LOG
-                            .throwing("GeneralizedTimeSyntax", "valueIsAcceptable", de);
-                    throw de;
-                }
+                final TimeZone tz = TIME_ZONE_UTC_OBJ;
+                return createTime(valueString, year, month, day, hour, minute, second, tz);
             } else {
                 final LocalizableMessage message =
                         WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_CHAR.get(valueString, String
@@ -673,25 +658,8 @@
             // These are fine only if there are exactly two or four more
             // digits that specify a valid offset.
             if (length == 13 || length == 15) {
-                try {
-                    final GregorianCalendar calendar = new GregorianCalendar();
-                    calendar.setLenient(false);
-                    calendar.setTimeZone(getTimeZoneForOffset(valueString, 10));
-                    calendar.set(year, month, day, hour, minute, second);
-                    calendar.set(Calendar.MILLISECOND, 0);
-                    return calendar.getTimeInMillis();
-                } catch (final Exception e) {
-
-                    // This should only happen if the provided date wasn't legal
-                    // (e.g., September 31).
-                    final LocalizableMessage message =
-                            WARN_ATTR_SYNTAX_GENERALIZED_TIME_ILLEGAL_TIME.get(valueString, String
-                                    .valueOf(e));
-                    final DecodeException de = DecodeException.error(message, e);
-                    StaticUtils.DEBUG_LOG
-                            .throwing("GeneralizedTimeSyntax", "valueIsAcceptable", de);
-                    throw de;
-                }
+                final TimeZone tz = getTimeZoneForOffset(valueString, 10);
+                return createTime(valueString, year, month, day, hour, minute, second, tz);
             } else {
                 final LocalizableMessage message =
                         WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_CHAR.get(valueString, String
@@ -819,25 +787,8 @@
         case 'Z':
             // This is fine only if we are at the end of the value.
             if (length == 13) {
-                try {
-                    final GregorianCalendar calendar = new GregorianCalendar();
-                    calendar.setLenient(false);
-                    calendar.setTimeZone(TIME_ZONE_UTC_OBJ);
-                    calendar.set(year, month, day, hour, minute, second);
-                    calendar.set(Calendar.MILLISECOND, 0);
-                    return calendar.getTimeInMillis();
-                } catch (final Exception e) {
-
-                    // This should only happen if the provided date wasn't legal
-                    // (e.g., September 31).
-                    final LocalizableMessage message =
-                            WARN_ATTR_SYNTAX_GENERALIZED_TIME_ILLEGAL_TIME.get(valueString, String
-                                    .valueOf(e));
-                    final DecodeException de = DecodeException.error(message, e);
-                    StaticUtils.DEBUG_LOG
-                            .throwing("GeneralizedTimeSyntax", "valueIsAcceptable", de);
-                    throw de;
-                }
+                final TimeZone tz = TIME_ZONE_UTC_OBJ;
+                return createTime(valueString, year, month, day, hour, minute, second, tz);
             } else {
                 final LocalizableMessage message =
                         WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_CHAR.get(valueString, String
@@ -852,25 +803,8 @@
             // These are fine only if there are exactly two or four more
             // digits that specify a valid offset.
             if (length == 15 || length == 17) {
-                try {
-                    final GregorianCalendar calendar = new GregorianCalendar();
-                    calendar.setLenient(false);
-                    calendar.setTimeZone(getTimeZoneForOffset(valueString, 12));
-                    calendar.set(year, month, day, hour, minute, second);
-                    calendar.set(Calendar.MILLISECOND, 0);
-                    return calendar.getTimeInMillis();
-                } catch (final Exception e) {
-
-                    // This should only happen if the provided date wasn't legal
-                    // (e.g., September 31).
-                    final LocalizableMessage message =
-                            WARN_ATTR_SYNTAX_GENERALIZED_TIME_ILLEGAL_TIME.get(valueString, String
-                                    .valueOf(e));
-                    final DecodeException de = DecodeException.error(message, e);
-                    StaticUtils.DEBUG_LOG
-                            .throwing("GeneralizedTimeSyntax", "valueIsAcceptable", de);
-                    throw de;
-                }
+                final TimeZone tz = getTimeZoneForOffset(valueString, 12);
+                return createTime(valueString, year, month, day, hour, minute, second, tz);
             } else {
                 final LocalizableMessage message =
                         WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_CHAR.get(valueString, String
@@ -907,24 +841,8 @@
         case 'Z':
             // This is fine only if we are at the end of the value.
             if (length == 15) {
-                try {
-                    final GregorianCalendar calendar = new GregorianCalendar();
-                    calendar.setLenient(false);
-                    calendar.setTimeZone(TIME_ZONE_UTC_OBJ);
-                    calendar.set(year, month, day, hour, minute, second);
-                    calendar.set(Calendar.MILLISECOND, 0);
-                    return calendar.getTimeInMillis();
-                } catch (final Exception e) {
-                    // This should only happen if the provided date wasn't legal
-                    // (e.g., September 31).
-                    final LocalizableMessage message =
-                            WARN_ATTR_SYNTAX_GENERALIZED_TIME_ILLEGAL_TIME.get(valueString, String
-                                    .valueOf(e));
-                    final DecodeException de = DecodeException.error(message, e);
-                    StaticUtils.DEBUG_LOG
-                            .throwing("GeneralizedTimeSyntax", "valueIsAcceptable", de);
-                    throw de;
-                }
+                final TimeZone tz = TIME_ZONE_UTC_OBJ;
+                return createTime(valueString, year, month, day, hour, minute, second, tz);
             } else {
                 final LocalizableMessage message =
                         WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_CHAR.get(valueString, String
@@ -939,24 +857,8 @@
             // These are fine only if there are exactly two or four more
             // digits that specify a valid offset.
             if (length == 17 || length == 19) {
-                try {
-                    final GregorianCalendar calendar = new GregorianCalendar();
-                    calendar.setLenient(false);
-                    calendar.setTimeZone(getTimeZoneForOffset(valueString, 14));
-                    calendar.set(year, month, day, hour, minute, second);
-                    calendar.set(Calendar.MILLISECOND, 0);
-                    return calendar.getTimeInMillis();
-                } catch (final Exception e) {
-                    // This should only happen if the provided date wasn't legal
-                    // (e.g., September 31).
-                    final LocalizableMessage message =
-                            WARN_ATTR_SYNTAX_GENERALIZED_TIME_ILLEGAL_TIME.get(valueString, String
-                                    .valueOf(e));
-                    final DecodeException de = DecodeException.error(message, e);
-                    StaticUtils.DEBUG_LOG
-                            .throwing("GeneralizedTimeSyntax", "valueIsAcceptable", de);
-                    throw de;
-                }
+                final TimeZone tz = getTimeZoneForOffset(valueString, 14);
+                return createTime(valueString, year, month, day, hour, minute, second, tz);
             } else {
                 final LocalizableMessage message =
                         WARN_ATTR_SYNTAX_GENERALIZED_TIME_INVALID_CHAR.get(valueString, String
@@ -976,6 +878,28 @@
         }
     }
 
+    private static long createTime(final String value, int year, int month, int day, int hour,
+            int minute, int second, final TimeZone tz) throws DecodeException {
+        try {
+            final GregorianCalendar calendar = new GregorianCalendar();
+            calendar.setLenient(false);
+            calendar.setTimeZone(tz);
+            calendar.set(year, month, day, hour, minute, second);
+            calendar.set(Calendar.MILLISECOND, 0);
+            return calendar.getTimeInMillis();
+        } catch (final Exception e) {
+            // This should only happen if the provided date wasn't legal
+            // (e.g., September 31).
+            final LocalizableMessage message =
+                    WARN_ATTR_SYNTAX_GENERALIZED_TIME_ILLEGAL_TIME.get(value, String
+                            .valueOf(e));
+            final DecodeException de = DecodeException.error(message, e);
+            StaticUtils.DEBUG_LOG
+                    .throwing("GeneralizedTimeSyntax", "valueIsAcceptable", de);
+            throw de;
+        }
+    }
+
     /**
      * Completes decoding the generalized time value containing a fractional
      * component. It will also decode the trailing 'Z' or offset.

--
Gitblit v1.10.0