|
| 1 | +/** |
| 2 | + |
| 3 | +Copyright 2013 Intel Corporation, All Rights Reserved. |
| 4 | +
|
| 5 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +you may not use this file except in compliance with the License. |
| 7 | +You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package com.intel.cosbench.driver.random; |
| 19 | + |
| 20 | +import java.util.Random; |
| 21 | +import java.util.concurrent.atomic.AtomicLong; |
| 22 | + |
| 23 | +import org.apache.commons.lang.StringUtils; |
| 24 | + |
| 25 | +import com.intel.cosbench.config.ConfigException; |
| 26 | + |
| 27 | +/** |
| 28 | + * This class supplies a thread-save int generator that returns ints from lower to upper, and then restarts at lower |
| 29 | + * again. |
| 30 | + * |
| 31 | + * |
| 32 | + * @author Niklas Goerke niklas974@github |
| 33 | + * |
| 34 | + */ |
| 35 | +class SequentialIntGenerator implements IntGenerator { |
| 36 | + |
| 37 | + private int lower; |
| 38 | + private int range; |
| 39 | + private AtomicLong cursor; |
| 40 | + |
| 41 | + public SequentialIntGenerator(int lower, int upper) { |
| 42 | + if (lower <= 0 || upper <= 0 || lower > upper) |
| 43 | + throw new IllegalArgumentException(); |
| 44 | + this.lower = lower; |
| 45 | + this.range = upper - lower + 1; // plus one, because we want upper and lower |
| 46 | + this.cursor = new AtomicLong(); |
| 47 | + this.cursor.set(-1); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public int next(Random random) { |
| 52 | + return (int) (lower + (cursor.incrementAndGet() % range)); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public int next(Random random, int idx, int all) { |
| 57 | + return next(random); |
| 58 | + |
| 59 | + } |
| 60 | + |
| 61 | + public static SequentialIntGenerator parse(String pattern) { |
| 62 | + if (!StringUtils.startsWith(pattern, "s(")) { |
| 63 | + return null; |
| 64 | + } else { |
| 65 | + try { |
| 66 | + return tryParse(pattern); |
| 67 | + } catch (NullPointerException e) { |
| 68 | + String msg = "illegal iteration pattern: " + pattern; |
| 69 | + throw new ConfigException(msg); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static SequentialIntGenerator tryParse(String pattern) { |
| 75 | + pattern = StringUtils.substringBetween(pattern, "(", ")"); |
| 76 | + String[] args = StringUtils.split(pattern, ","); |
| 77 | + int lower = Integer.parseInt(args[0]); |
| 78 | + int upper = Integer.parseInt(args[1]); |
| 79 | + return new SequentialIntGenerator(lower, upper); |
| 80 | + } |
| 81 | +} |
0 commit comments