Skip to content

Commit c42e530

Browse files
committed
Merge PR #3382 by @ar0ne - game details screen
2 parents ef4655b + ca666da commit c42e530

File tree

17 files changed

+1150
-26
lines changed

17 files changed

+1150
-26
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2018 MovingBlocks
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.terasology.utilities.time;
17+
18+
import org.junit.Assert;
19+
import org.junit.Test;
20+
21+
import java.util.Arrays;
22+
23+
public class DateTimeHelperTest {
24+
25+
@Test
26+
public void testGetDeltaBetweenTimestamps() {
27+
Assert.assertEquals("00h 00m 01s", DateTimeHelper.getDeltaBetweenTimestamps(0, 1000));
28+
Assert.assertEquals("00h 00m 10s", DateTimeHelper.getDeltaBetweenTimestamps(0, 10 * 1000));
29+
Assert.assertEquals("00h 01m 00s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000));
30+
Assert.assertEquals("00h 11m 00s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000 * 11));
31+
Assert.assertEquals("01h 00m 00s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000 * 60));
32+
Assert.assertEquals("12h 34m 56s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000 * 60 * 12 + 60 * 1000 * 34 + 56 * 1000));
33+
34+
Assert.assertEquals("1 Days 01h 00m 00s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000 * 60 * 25));
35+
Assert.assertEquals("1 Days 01h 20m 40s", DateTimeHelper.getDeltaBetweenTimestamps(0, 60 * 1000 * 60 * 24 + 60 * 1000 * 60 + 60 * 1000 * 20 + 40 * 1000));
36+
37+
Assert.assertEquals("00h 00m 00s", DateTimeHelper.getDeltaBetweenTimestamps(1000, 1000));
38+
Assert.assertEquals("00h 00m 00s", DateTimeHelper.getDeltaBetweenTimestamps(50000, 50000));
39+
40+
Assert.assertEquals("11h 01m 01s", DateTimeHelper.getDeltaBetweenTimestamps(1000 * 60 * 25, 1000 * 60 * 25 + 60 * 1000 * 60 * 11 + 60 * 1000 + 1000));
41+
}
42+
43+
@Test
44+
public void testGetDeltaBetweenTimestampsWrongData() {
45+
46+
Integer[][] wrongData = {
47+
{-1, 50000},
48+
{123, -1000},
49+
{-667, -1000},
50+
{123, 0},
51+
{12321321, 1231}
52+
};
53+
54+
Arrays.stream(wrongData).forEach(
55+
data -> {
56+
try {
57+
DateTimeHelper.getDeltaBetweenTimestamps(data[0], data[1]);
58+
Assert.fail("Exception should be thrown!");
59+
} catch (IllegalArgumentException ex) {
60+
Assert.assertEquals("Wrong timestamp values: " + data[0] + " or " + data[1], ex.getMessage());
61+
}
62+
}
63+
);
64+
}
65+
66+
}

engine/src/main/java/org/terasology/rendering/nui/AbstractWidget.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import java.util.Collection;
2626
import java.util.List;
27+
import java.util.Optional;
2728

2829
/**
2930
*/
@@ -121,6 +122,11 @@ public final <T extends UIWidget> T find(String targetId, Class<T> type) {
121122
return null;
122123
}
123124

125+
@Override
126+
public <T extends UIWidget> Optional<T> tryFind(String id, Class<T> type) {
127+
return Optional.ofNullable(find(id, type));
128+
}
129+
124130
@Override
125131
public final <T extends UIWidget> Collection<T> findAll(Class<T> type) {
126132
List<T> results = Lists.newArrayList();

engine/src/main/java/org/terasology/rendering/nui/UIWidget.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.terasology.rendering.nui.skin.UISkin;
2525

2626
import java.util.Collection;
27+
import java.util.Optional;
2728

2829
/**
2930
*/
@@ -69,6 +70,15 @@ public interface UIWidget extends Iterable<UIWidget> {
6970
*/
7071
<T extends UIWidget> T find(String id, Class<T> type);
7172

73+
/**
74+
* Try to find a widget with the given id and type, within the current widget and its contents.
75+
*
76+
* @param id of widget to search
77+
* @param <T> type of widget to cast
78+
* @return optional widget with the given id and type
79+
*/
80+
<T extends UIWidget> Optional<T> tryFind(String id, Class<T> type);
81+
7282
<T extends UIWidget> Collection<T> findAll(Class<T> type);
7383

7484
void onDraw(Canvas canvas);

0 commit comments

Comments
 (0)