You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by pm...@apache.org on 2017/07/25 20:09:46 UTC

svn commit: r1802992 - /jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java

Author: pmouawad
Date: Tue Jul 25 20:09:46 2017
New Revision: 1802992

URL: http://svn.apache.org/viewvc?rev=1802992&view=rev
Log:
Bug 61341 Add locale parameter to timeShift function
Clarify and simplify code
Bugzilla Id: 61341

Modified:
    jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java

Modified: jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java?rev=1802992&r1=1802991&r2=1802992&view=diff
==============================================================================
--- jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java (original)
+++ jmeter/trunk/src/functions/org/apache/jmeter/functions/TimeShift.java Tue Jul 25 20:09:46 2017
@@ -80,7 +80,7 @@ public class TimeShift extends AbstractF
     private ZoneId systemDefaultZoneID = ZoneId.systemDefault();
 
     
-    class LocaleFormatObject {
+    private static final class LocaleFormatObject {
 
         private String format;
         private Locale locale;
@@ -113,6 +113,14 @@ public class TimeShift extends AbstractF
             return format.equals(otherError.getFormat())
                     && locale.getDisplayName().equals(otherError.getLocale().getDisplayName());
         }
+
+        /**
+         * @see java.lang.Object#toString()
+         */
+        @Override
+        public String toString() {
+            return "LocaleFormatObject [format=" + format + ", locale=" + locale + "]";
+        }
         
         
     }
@@ -127,7 +135,6 @@ public class TimeShift extends AbstractF
     /** {@inheritDoc} */
     @Override
     public String execute(SampleResult previousResult, Sampler currentSampler) throws InvalidVariableException {
-        String dateString;
         String amountToShift = amountToShiftCompound.execute().trim();
         String dateToShift = dateToShiftCompound.execute().trim();
         LocalDateTime localDateTimeToShift = LocalDateTime.now(systemDefaultZoneID);
@@ -152,7 +159,8 @@ public class TimeShift extends AbstractF
                             ZoneId.systemDefault());
                 }
             } catch (DateTimeParseException | NumberFormatException ex) {
-                log.error("Failed to parse the date '{}' to shift", dateToShift, ex); // $NON-NLS-1$
+                log.error("Failed to parse the date '{}' to shift with formatter '{}'", 
+                        dateToShift, formatter, ex); // $NON-NLS-1$
             }
         }
 
@@ -165,7 +173,7 @@ public class TimeShift extends AbstractF
                 log.error("Failed to parse the amount duration '{}' to shift (see https://docs.oracle.com/javase/8/docs/api/java/time/Duration.html#parse-java.lang.CharSequence-) ", amountToShift, ex); // $NON-NLS-1$
             }
         }
-
+        String dateString;
         if (formatter != null) {
             dateString = localDateTimeToShift.format(formatter);
         } else {
@@ -183,7 +191,6 @@ public class TimeShift extends AbstractF
     }
 
     private DateTimeFormatter createFormatter(LocaleFormatObject format) {
-
         log.debug("Create a new instance of DateTimeFormatter for format '{}' in the cache", format);
         return new DateTimeFormatterBuilder().appendPattern(format.getFormat()).parseDefaulting(ChronoField.NANO_OF_SECOND, 0)
                 .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0).parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
@@ -193,12 +200,6 @@ public class TimeShift extends AbstractF
 
     }
 
-    protected static Cache<LocaleFormatObject, DateTimeFormatter> buildCache() {
-        Caffeine<Object, Object> cacheBuilder = Caffeine.newBuilder();
-        cacheBuilder.maximumSize(100);
-        return cacheBuilder.build();
-    }
-
     /** {@inheritDoc} */
     @Override
     public void setParameters(Collection<CompoundVariable> parameters) throws InvalidVariableException {
@@ -213,14 +214,15 @@ public class TimeShift extends AbstractF
             variableName = ((CompoundVariable) values[3]).execute().trim();
         } else {
             String localeAsString = ((CompoundVariable) values[3]).execute().trim();
-            if (!localeAsString.equals("")) {
+            if (!localeAsString.trim().isEmpty()) {
                 locale = LocaleUtils.toLocale(localeAsString);
             }
             variableName = ((CompoundVariable) values[4]).execute().trim();
         }
         // Create the cache
         if (dateTimeFormatterCache == null) {
-            dateTimeFormatterCache = buildCache();
+            dateTimeFormatterCache = Caffeine.newBuilder()
+                    .maximumSize(100).build();
         }
     }