Skip to content

Commit 0afbc0b

Browse files
committed
Polish 'Accept Docker progress on numbers >2GB'
Restore `int` returns for existing methods and deprecate them in favor of a new `asPercentage()` method. See gh-43328
1 parent d856518 commit 0afbc0b

File tree

4 files changed

+44
-16
lines changed

4 files changed

+44
-16
lines changed

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/ProgressUpdateEvent.java

+28-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,19 +81,42 @@ public ProgressDetail(Long current, Long total) {
8181
/**
8282
* Return the current progress value.
8383
* @return the current progress
84+
* @deprecated since 3.3.7 for removal in 3.5.0 in favor of
85+
* {@link #asPercentage()}
8486
*/
85-
public long getCurrent() {
86-
return this.current;
87+
@Deprecated(since = "3.3.7", forRemoval = true)
88+
public int getCurrent() {
89+
return (int) Long.min(this.current, Integer.MAX_VALUE);
8790
}
8891

8992
/**
9093
* Return the total progress possible value.
9194
* @return the total progress possible
95+
* @deprecated since 3.3.7 for removal in 3.5.0 in favor of
96+
* {@link #asPercentage()}
9297
*/
93-
public long getTotal() {
94-
return this.total;
98+
@Deprecated(since = "3.3.7", forRemoval = true)
99+
public int getTotal() {
100+
return (int) Long.min(this.total, Integer.MAX_VALUE);
95101
}
96102

103+
/**
104+
* Return the progress as a percentage.
105+
* @return the progress percentage
106+
* @since 3.3.7
107+
*/
108+
public int asPercentage() {
109+
int percentage = (int) ((100.0 / this.total) * this.current);
110+
return (percentage < 0) ? 0 : Math.min(percentage, 100);
111+
}
112+
113+
/**
114+
* Return if the progress detail is considered empty.
115+
* @param progressDetail the progress detail to check
116+
* @return if the progress detail is empty
117+
* @deprecated since 3.3.7 for removal in 3.5.0
118+
*/
119+
@Deprecated(since = "3.3.7", forRemoval = true)
97120
public static boolean isEmpty(ProgressDetail progressDetail) {
98121
return progressDetail == null || progressDetail.current == null || progressDetail.total == null;
99122
}

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/main/java/org/springframework/boot/buildpack/platform/docker/TotalProgressListener.java

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -89,10 +89,7 @@ private void publish(int fallback) {
8989
}
9090

9191
private static int withinPercentageBounds(int value) {
92-
if (value < 0) {
93-
return 0;
94-
}
95-
return Math.min(value, 100);
92+
return (value < 0) ? 0 : Math.min(value, 100);
9693
}
9794

9895
/**
@@ -115,8 +112,7 @@ void update(ImageProgressUpdateEvent event) {
115112
}
116113

117114
private int updateProgress(int current, ProgressDetail detail) {
118-
int result = withinPercentageBounds((int) ((100.0 / detail.getTotal()) * detail.getCurrent()));
119-
return Math.max(result, current);
115+
return Math.max(detail.asPercentage(), current);
120116
}
121117

122118
void finish() {

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/ProgressUpdateEventTests.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,17 +39,21 @@ void getStatusReturnsStatus() {
3939
}
4040

4141
@Test
42+
@SuppressWarnings("removal")
4243
void getProgressDetailsReturnsProgressDetails() {
4344
ProgressUpdateEvent event = createEvent();
4445
assertThat(event.getProgressDetail().getCurrent()).isOne();
4546
assertThat(event.getProgressDetail().getTotal()).isEqualTo(2);
47+
assertThat(event.getProgressDetail().asPercentage()).isEqualTo(50);
4648
}
4749

4850
@Test
51+
@SuppressWarnings("removal")
4952
void getProgressDetailsReturnsProgressDetailsForLongNumbers() {
5053
ProgressUpdateEvent event = createEvent("status", new ProgressDetail(4000000000L, 8000000000L), "progress");
51-
assertThat(event.getProgressDetail().getCurrent()).isEqualTo(4000000000L);
52-
assertThat(event.getProgressDetail().getTotal()).isEqualTo(8000000000L);
54+
assertThat(event.getProgressDetail().getCurrent()).isEqualTo(Integer.MAX_VALUE);
55+
assertThat(event.getProgressDetail().getTotal()).isEqualTo(Integer.MAX_VALUE);
56+
assertThat(event.getProgressDetail().asPercentage()).isEqualTo(50);
5357
}
5458

5559
@Test
@@ -59,18 +63,21 @@ void getProgressReturnsProgress() {
5963
}
6064

6165
@Test
66+
@SuppressWarnings("removal")
6267
void progressDetailIsEmptyWhenCurrentIsNullReturnsTrue() {
6368
ProgressDetail detail = new ProgressDetail(null, 2L);
6469
assertThat(ProgressDetail.isEmpty(detail)).isTrue();
6570
}
6671

6772
@Test
73+
@SuppressWarnings("removal")
6874
void progressDetailIsEmptyWhenTotalIsNullReturnsTrue() {
6975
ProgressDetail detail = new ProgressDetail(1L, null);
7076
assertThat(ProgressDetail.isEmpty(detail)).isTrue();
7177
}
7278

7379
@Test
80+
@SuppressWarnings("removal")
7481
void progressDetailIsEmptyWhenTotalAndCurrentAreNotNullReturnsFalse() {
7582
ProgressDetail detail = new ProgressDetail(1L, 2L);
7683
assertThat(ProgressDetail.isEmpty(detail)).isFalse();

spring-boot-project/spring-boot-tools/spring-boot-buildpack-platform/src/test/java/org/springframework/boot/buildpack/platform/docker/PullUpdateEventTests.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,13 +30,15 @@
3030
class PullUpdateEventTests extends AbstractJsonTests {
3131

3232
@Test
33+
@SuppressWarnings("removal")
3334
void readValueWhenFullDeserializesJson() throws Exception {
3435
PullImageUpdateEvent event = getObjectMapper().readValue(getContent("pull-update-full.json"),
3536
PullImageUpdateEvent.class);
3637
assertThat(event.getId()).isEqualTo("4f4fb700ef54");
3738
assertThat(event.getStatus()).isEqualTo("Extracting");
3839
assertThat(event.getProgressDetail().getCurrent()).isEqualTo(16);
3940
assertThat(event.getProgressDetail().getTotal()).isEqualTo(32);
41+
assertThat(event.getProgressDetail().asPercentage()).isEqualTo(50);
4042
assertThat(event.getProgress()).isEqualTo("[==================================================>] 32B/32B");
4143
}
4244

0 commit comments

Comments
 (0)