Skip to content

Commit

Permalink
Support show time in readable format (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Linary authored and zhoney committed Jul 1, 2019
1 parent b525aef commit 88d8838
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 5 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.baidu.hugegraph</groupId>
<artifactId>hugegraph-common</artifactId>
<version>1.6.7</version>
<version>1.6.8</version>

<name>hugegraph-common</name>
<url>https://github.com/hugegraph/hugegraph-common</url>
Expand Down Expand Up @@ -212,7 +212,7 @@
<manifestEntries>
<!-- Must be on one line, otherwise the automatic
upgrade script cannot replace the version number -->
<Implementation-Version>1.6.7.0</Implementation-Version>
<Implementation-Version>1.6.8.0</Implementation-Version>
</manifestEntries>
</archive>
</configuration>
Expand Down
40 changes: 40 additions & 0 deletions src/main/java/com/baidu/hugegraph/util/StringUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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.baidu.hugegraph.util;

import java.nio.CharBuffer;
import java.util.ArrayList;
import java.util.List;

public final class StringUtil {

public static List<CharSequence> split(String line, String delimiter) {
E.checkArgument(delimiter.length() > 0,
"The delimiter can't be empty");
List<CharSequence> results = new ArrayList<>();
int from = 0;
for (int to; (to = line.indexOf(delimiter, from)) >= 0;
from = to + delimiter.length()) {
results.add(CharBuffer.wrap(line, from, to));
}
results.add(CharBuffer.wrap(line, from, line.length()));
return results;
}
}
13 changes: 13 additions & 0 deletions src/main/java/com/baidu/hugegraph/util/TimeUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package com.baidu.hugegraph.util;

import java.time.Duration;
import java.util.Date;

public final class TimeUtil {
Expand All @@ -45,4 +46,16 @@ public static long tillNextMillis(long lastTimestamp) {
}
return timestamp;
}

public static String readableTime(long time) {
if (time > 60 * 1000) {
// Remove the milliseconds part
time = time / 1000 * 1000;
}
Duration duration = Duration.ofMillis(time);
return duration.toString()
.substring(2)
.replaceAll("(\\d[HMS])(?!$)", "$1 ")
.toLowerCase();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ public class CommonVersion {

// The second parameter of Version.of() is for all-in-one JAR
public static final Version VERSION = Version.of(CommonVersion.class,
"1.6.7");
"1.6.8");
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public void testClasses() throws IOException {
@SuppressWarnings("unchecked")
List<ClassInfo> classes = IteratorUtils.toList(ReflectionUtil.classes(
"com.baidu.hugegraph.util"));
Assert.assertEquals(14, classes.size());
Assert.assertEquals(15, classes.size());
classes.sort((c1, c2) -> c1.getName().compareTo(c2.getName()));
Assert.assertEquals("com.baidu.hugegraph.util.Bytes",
classes.get(0).getName());
Expand All @@ -100,7 +100,7 @@ public void testClasses() throws IOException {
Assert.assertEquals("com.baidu.hugegraph.util.CollectionUtil",
classes.get(2).getName());
Assert.assertEquals("com.baidu.hugegraph.util.VersionUtil",
classes.get(13).getName());
classes.get(14).getName());
}

@Test
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/com/baidu/hugegraph/unit/util/StringUtilTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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.baidu.hugegraph.unit.util;

import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import com.baidu.hugegraph.testutil.Assert;
import com.baidu.hugegraph.util.StringUtil;
import com.google.common.base.Splitter;

public class StringUtilTest {

@Test
public void testSplit() {
Assert.assertEquals(guavaSplit("123", " "),
toStringList(StringUtil.split("123", " ")));
Assert.assertEquals(guavaSplit("1 2 3", " "),
toStringList(StringUtil.split("1 2 3", " ")));
Assert.assertEquals(guavaSplit("1:2:3", ":"),
toStringList(StringUtil.split("1:2:3", ":")));
Assert.assertEquals(guavaSplit("1::2:3", ":"),
toStringList(StringUtil.split("1::2:3", ":")));
Assert.assertEquals(guavaSplit("1::2::3", ":"),
toStringList(StringUtil.split("1::2::3", ":")));
Assert.assertEquals(guavaSplit("1::2::3", "::"),
toStringList(StringUtil.split("1::2::3", "::")));
Assert.assertEquals(guavaSplit("1:|2|:3", "|"),
toStringList(StringUtil.split("1:|2|:3", "|")));
Assert.assertEquals(guavaSplit("1\t2\t3", "\t"),
toStringList(StringUtil.split("1\t2\t3", "\t")));
Assert.assertEquals(guavaSplit("111", "1"),
toStringList(StringUtil.split("111", "1")));

Assert.assertThrows(IllegalArgumentException.class, () -> {
StringUtil.split("123", "");
});
}

private static List<String> guavaSplit(String line, String delimiter) {
return Splitter.on(delimiter).splitToList(line);
}

private static List<String> toStringList(List<CharSequence> chars) {
List<String> results = new ArrayList<>(chars.size());
for (CharSequence seq : chars) {
results.add(seq.toString());
}
return results;
}
}
28 changes: 28 additions & 0 deletions src/test/java/com/baidu/hugegraph/unit/util/TimeUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,32 @@ public void testTillNextMillis() {
Assert.assertNotEquals(lastTimestamp, time);
}
}

@Test
public void testReadableTime() {
Assert.assertEquals("0.001s", TimeUtil.readableTime(1));
Assert.assertEquals("0.999s", TimeUtil.readableTime(999));
Assert.assertEquals("1s", TimeUtil.readableTime(1000));
Assert.assertEquals("1.5s", TimeUtil.readableTime(1500));
Assert.assertEquals("15s", TimeUtil.readableTime(15 * 1000));
Assert.assertEquals("59.99s", TimeUtil.readableTime(59990));
Assert.assertEquals("1m", TimeUtil.readableTime(60000));
// Ignore milliseconds part
Assert.assertEquals("1m", TimeUtil.readableTime(60000 + 100));
Assert.assertEquals("1m 1s", TimeUtil.readableTime(60000 + 1000));
Assert.assertEquals("1m 1s", TimeUtil.readableTime(60000 + 1200));
Assert.assertEquals("59m", TimeUtil.readableTime(59 * 60 * 1000));
Assert.assertEquals("1h", TimeUtil.readableTime(60 * 60 * 1000));
Assert.assertEquals("1h 1m", TimeUtil.readableTime(60 * 60 * 1000 +
60 * 1000));
Assert.assertEquals("23h 59m 59s", TimeUtil.readableTime(
23 * 60 * 60 * 1000 +
59 * 60 * 1000 +
59 * 1000));
Assert.assertEquals("24h", TimeUtil.readableTime(24 * 60 * 60 * 1000));
Assert.assertEquals("25h 1m 1s", TimeUtil.readableTime(
25 * 60 * 60 * 1000 +
1 * 60 * 1000 +
1 * 1200));
}
}

0 comments on commit 88d8838

Please sign in to comment.