Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support show time in readable format #31

Merged
merged 2 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
9 changes: 9 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,12 @@ public static long tillNextMillis(long lastTimestamp) {
}
return timestamp;
}

public static String readableTime(long time) {
Duration duration = Duration.ofMillis(time);
Linary marked this conversation as resolved.
Show resolved Hide resolved
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", " "),
toList(StringUtil.split("123", " ")));
Assert.assertEquals(guavaSplit("1 2 3", " "),
toList(StringUtil.split("1 2 3", " ")));
Assert.assertEquals(guavaSplit("1:2:3", ":"),
toList(StringUtil.split("1:2:3", ":")));
Assert.assertEquals(guavaSplit("1::2:3", ":"),
toList(StringUtil.split("1::2:3", ":")));
Assert.assertEquals(guavaSplit("1::2::3", ":"),
toList(StringUtil.split("1::2::3", ":")));
Assert.assertEquals(guavaSplit("1::2::3", "::"),
toList(StringUtil.split("1::2::3", "::")));
Assert.assertEquals(guavaSplit("1:|2|:3", "|"),
toList(StringUtil.split("1:|2|:3", "|")));
Assert.assertEquals(guavaSplit("1\t2\t3", "\t"),
toList(StringUtil.split("1\t2\t3", "\t")));
Assert.assertEquals(guavaSplit("111", "1"),
toList(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> toList(List<CharSequence> chars) {
Linary marked this conversation as resolved.
Show resolved Hide resolved
List<String> results = new ArrayList<>(chars.size());
for (CharSequence seq : chars) {
results.add(seq.toString());
}
return results;
}
}
24 changes: 24 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,28 @@ 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));
Assert.assertEquals("1m 0.1s", TimeUtil.readableTime(60000 + 100));
Assert.assertEquals("1m 1s", TimeUtil.readableTime(60000 + 1000));
Linary marked this conversation as resolved.
Show resolved Hide resolved
Assert.assertEquals("59m", TimeUtil.readableTime(59 * 60 * 1000));
Assert.assertEquals("1h", TimeUtil.readableTime(60 * 60 * 1000));
Linary marked this conversation as resolved.
Show resolved Hide resolved
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 * 1000));
}
}