Fix age:4days to parse correctly

We didn't accept an input without a space between the number and
the time unit, making it hard to run a query for age:4days without
writing it as age:"4 days".  Make the whitespace optional.

Bug: issue 689
Change-Id: I272f73fe3a6307f410e587160e5681117a959574
Signed-off-by: Shawn O. Pearce <sop@google.com>
This commit is contained in:
Shawn O. Pearce 2010-08-28 12:20:08 -07:00
parent 7156ac9728
commit b16f19417b
2 changed files with 88 additions and 19 deletions

View File

@ -21,6 +21,7 @@ import com.google.gerrit.reviewdb.AccountGroupName;
import com.google.gerrit.reviewdb.ReviewDb;
import com.google.gwtorm.client.OrmException;
import com.google.gwtorm.client.SchemaFactory;
import org.eclipse.jgit.lib.Config;
import org.slf4j.Logger;
@ -31,6 +32,8 @@ import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ConfigUtil {
/**
@ -226,7 +229,7 @@ public class ConfigUtil {
/**
* Parse a numerical time unit, such as "1 minute", from a string.
*
* @param s the string to parse.
* @param valueString the string to parse.
* @param defaultValue default value to return if no value was set in the
* configuration file.
* @param wantUnit the units of {@code defaultValue} and the return value, as
@ -235,26 +238,16 @@ public class ConfigUtil {
* @return the setting, or {@code defaultValue} if not set, expressed in
* {@code units}.
*/
public static long getTimeUnit(String s, long defaultValue, TimeUnit wantUnit) {
final String valueString = s;
final String unitName;
final int sp = s.indexOf(' ');
if (sp > 0) {
unitName = s.substring(sp + 1).trim();
s = s.substring(0, sp);
} else {
final char last = s.charAt(s.length() - 1);
if ('0' <= last && last <= '9') {
unitName = "";
} else {
unitName = String.valueOf(last);
s = s.substring(0, s.length() - 1).trim();
}
}
if (s.length() == 0) {
public static long getTimeUnit(final String valueString, long defaultValue,
TimeUnit wantUnit) {
Matcher m = Pattern.compile("^([1-9][0-9]*)\\s*(.*)$").matcher(valueString);
if (!m.matches()) {
return defaultValue;
}
String digits = m.group(1);
String unitName = m.group(2).trim();
TimeUnit inputUnit;
int inputMul;
@ -299,7 +292,7 @@ public class ConfigUtil {
}
try {
return wantUnit.convert(Long.parseLong(s) * inputMul, inputUnit);
return wantUnit.convert(Long.parseLong(digits) * inputMul, inputUnit);
} catch (NumberFormatException nfe) {
throw notTimeUnit(valueString);
}

View File

@ -0,0 +1,76 @@
// Copyright (C) 2010 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.config;
import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.*;
import junit.framework.TestCase;
import java.util.concurrent.TimeUnit;
public class ConfigUtilTest extends TestCase {
public void testTimeUnit() {
assertEquals(ms(2, MILLISECONDS), parse("2ms"));
assertEquals(ms(200, MILLISECONDS), parse("200 milliseconds"));
assertEquals(ms(2, SECONDS), parse("2s"));
assertEquals(ms(231, SECONDS), parse("231sec"));
assertEquals(ms(1, SECONDS), parse("1second"));
assertEquals(ms(300, SECONDS), parse("300 seconds"));
assertEquals(ms(2, MINUTES), parse("2m"));
assertEquals(ms(2, MINUTES), parse("2min"));
assertEquals(ms(1, MINUTES), parse("1 minute"));
assertEquals(ms(10, MINUTES), parse("10 minutes"));
assertEquals(ms(5, HOURS), parse("5h"));
assertEquals(ms(5, HOURS), parse("5hr"));
assertEquals(ms(1, HOURS), parse("1hour"));
assertEquals(ms(48, HOURS), parse("48hours"));
assertEquals(ms(5, HOURS), parse("5 h"));
assertEquals(ms(5, HOURS), parse("5 hr"));
assertEquals(ms(1, HOURS), parse("1 hour"));
assertEquals(ms(48, HOURS), parse("48 hours"));
assertEquals(ms(48, HOURS), parse("48 \t \r hours"));
assertEquals(ms(4, DAYS), parse("4d"));
assertEquals(ms(1, DAYS), parse("1day"));
assertEquals(ms(14, DAYS), parse("14days"));
assertEquals(ms(7, DAYS), parse("1w"));
assertEquals(ms(7, DAYS), parse("1week"));
assertEquals(ms(14, DAYS), parse("2w"));
assertEquals(ms(14, DAYS), parse("2weeks"));
assertEquals(ms(30, DAYS), parse("1mon"));
assertEquals(ms(30, DAYS), parse("1month"));
assertEquals(ms(60, DAYS), parse("2mon"));
assertEquals(ms(60, DAYS), parse("2months"));
assertEquals(ms(365, DAYS), parse("1y"));
assertEquals(ms(365, DAYS), parse("1year"));
assertEquals(ms(365 * 2, DAYS), parse("2years"));
}
private static long ms(int cnt, TimeUnit unit) {
return MILLISECONDS.convert(cnt, unit);
}
private static long parse(String string) {
return ConfigUtil.getTimeUnit(string, 1, MILLISECONDS);
}
}