/*
 * Copyright (c) 2010-2012 Grid Dynamics Consulting Services, Inc, All Rights Reserved
 * http://www.griddynamics.com
 *
 * This library is free software; you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.griddynamics.jagger.util;

import java.util.Random;

/**
 * UserGroup: dkotlyarov
 */
public final class Parser {
    private Parser() {
    }

    public static long parseTimeMillis(String time) {
        if (time.endsWith("s")) {
            return Long.parseLong(time.split("s")[0]) * 1000L;
        } else if (time.endsWith("m")) {
            return Long.parseLong(time.split("m")[0]) * 1000L * 60L;
        } else if (time.endsWith("h")) {
            return Long.parseLong(time.split("h")[0]) * 1000L * 60L * 60L;
        } else if (time.endsWith("d")) {
            return Long.parseLong(time.split("d")[0]) * 1000L * 60L * 60L * 24L;
        } else if (time.endsWith("w")) {
            return Long.parseLong(time.split("w")[0]) * 1000L * 60L * 60L * 24L * 7L;
        } else {
            return Long.parseLong(time);
        }
    }

    public static long parseTime(String time, Random random) {
        String[] items = time.split("[..]");
        if (items.length == 1) {
            return parseTimeMillis(time);
        } else {
            long min = parseTimeMillis(items[0]);
            long max = parseTimeMillis(items[1]);
            long delta = max - min;
            if (delta < 0) {
                throw new IllegalArgumentException();
            }
            return random.nextLong() % delta + min;
        }
    }

    public static int parseInt(String value, Random random) {
        String[] items = value.split("[..]");
        if (items.length == 1) {
            return Integer.parseInt(value);
        } else {
            int min = Integer.parseInt(items[0]);
            int max = Integer.parseInt(items[1]);
            int delta = max - min;
            if (delta < 0) {
                throw new IllegalArgumentException();
            }
            return random.nextInt() % delta + min;
        }
    }

    public static long parseLong(String value, Random random) {
        String[] items = value.split("[..]");
        if (items.length == 1) {
            return Long.parseLong(value);
        } else {
            long min = Long.parseLong(items[0]);
            long max = Long.parseLong(items[1]);
            long delta = max - min;
            if (delta < 0) {
                throw new IllegalArgumentException();
            }
            return random.nextLong() % delta + min;
        }
    }
}

@@ -1214,16 +1214,48 @@
 .Random;
+%0Aimport java.util.regex.Pattern;
 %0A%0A/**%0A *
@@ -1309,16 +1309,88 @@
 arser %7B%0A
+%0A    private final static Pattern pattern = Pattern.compile(%22%5C%5C.%5C%5C.%22);%0A%0A
     priv
@@ -2204,25 +2204,26 @@
 s = 
-time
+pattern
 .split(
-%22%5B..%5D%22
+time
 );%0A 
@@ -2557,32 +2557,41 @@
          return 
+Math.abs(
 random.nextLong(
@@ -2591,32 +2591,33 @@
 xtLong() %25 delta
+)
  + min;%0A        
@@ -2712,34 +2712,35 @@
 items = 
-value
+pattern
 .split(
-%22%5B..%5D%22
+value
 );%0A     
@@ -3094,25 +3094,22 @@
 nextInt(
-) %25 
 delta
+)
  + min;%0A
@@ -3218,26 +3218,27 @@
 s = 
-value
+pattern
 .split(
-%22%5B..%5D%22
+value
 );%0A 
@@ -3570,32 +3570,41 @@
          return 
+Math.abs(
 random.nextLong(
@@ -3612,16 +3612,17 @@
  %25 delta
+)
  + min;%0A

Fix parser change format to (1..4) from (1.4) //for random value fix calculating value with random (can be less then MIN value)