Skip to content

Commit 7c89c54

Browse files
committed
Minor code cleanup
1 parent b3b243a commit 7c89c54

File tree

3 files changed

+50
-18
lines changed

3 files changed

+50
-18
lines changed

src/main/java/dev/failsafe/internal/TimedCircuitStats.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ class TimedCircuitStats implements CircuitStats {
3434
private final Stat summary;
3535
volatile int currentIndex;
3636

37+
public TimedCircuitStats(int bucketCount, Duration thresholdingPeriod, Clock clock, CircuitStats oldStats) {
38+
this.clock = clock;
39+
this.buckets = new Bucket[bucketCount];
40+
bucketSizeMillis = thresholdingPeriod.toMillis() / buckets.length;
41+
windowSizeMillis = bucketSizeMillis * buckets.length;
42+
for (int i = 0; i < buckets.length; i++)
43+
buckets[i] = new Bucket();
44+
this.summary = new Stat();
45+
46+
if (oldStats == null) {
47+
buckets[0].startTimeMillis = clock.currentTimeMillis();
48+
} else {
49+
synchronized (oldStats) {
50+
copyStats(oldStats);
51+
}
52+
}
53+
}
54+
3755
static class Clock {
3856
long currentTimeMillis() {
3957
return System.currentTimeMillis();
@@ -85,24 +103,6 @@ public String toString() {
85103
}
86104
}
87105

88-
public TimedCircuitStats(int bucketCount, Duration thresholdingPeriod, Clock clock, CircuitStats oldStats) {
89-
this.clock = clock;
90-
this.buckets = new Bucket[bucketCount];
91-
bucketSizeMillis = thresholdingPeriod.toMillis() / buckets.length;
92-
windowSizeMillis = bucketSizeMillis * buckets.length;
93-
for (int i = 0; i < buckets.length; i++)
94-
buckets[i] = new Bucket();
95-
this.summary = new Stat();
96-
97-
if (oldStats == null) {
98-
buckets[0].startTimeMillis = clock.currentTimeMillis();
99-
} else {
100-
synchronized (oldStats) {
101-
copyStats(oldStats);
102-
}
103-
}
104-
}
105-
106106
/**
107107
* Copies the most recent stats from the {@code oldStats} into this in order from oldest to newest and orders buckets
108108
* from oldest to newest, with uninitialized buckets counting as oldest.

src/main/java/dev/failsafe/internal/util/Lists.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ public final class Lists {
1313
private Lists() {
1414
}
1515

16+
/**
17+
* Returns a list containing the {@code first} element followed by the {@code rest}.
18+
*/
1619
public static <T> List<T> of(T first, T[] rest) {
1720
List<T> result = new ArrayList<>(rest.length + 1);
1821
result.add(first);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
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 dev.failsafe.internal.util;
17+
18+
import org.testng.annotations.Test;
19+
20+
import java.util.Arrays;
21+
22+
import static org.testng.Assert.assertEquals;
23+
24+
@Test
25+
public class ListsTest {
26+
public void testOf() {
27+
assertEquals(Lists.of("a", new String[] { "b", "c" }), Arrays.asList("a", "b", "c"));
28+
}
29+
}

0 commit comments

Comments
 (0)