Skip to content

Commit e441fec

Browse files
committed
More java to kt
1 parent 6290f83 commit e441fec

File tree

8 files changed

+221
-266
lines changed

8 files changed

+221
-266
lines changed

play-validations/memory-footprint/src/main/java/com/google/wear/watchface/dfx/memory/InvalidTestRunException.java renamed to play-validations/memory-footprint/src/main/java/com/google/wear/watchface/dfx/memory/InvalidTestRunException.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,11 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
package com.google.wear.watchface.dfx.memory;
16+
package com.google.wear.watchface.dfx.memory
1817

1918
/**
2019
* Exception thrown when the memory footprint test encounters an unexpected issue. The test fails,
2120
* but the watch face should not be rejected nor accepted, but the error should be escalated for
2221
* further analysis.
2322
*/
24-
class InvalidTestRunException extends RuntimeException {
25-
26-
InvalidTestRunException(String message) {
27-
super(message);
28-
}
29-
}
23+
internal class InvalidTestRunException(message: String?) : RuntimeException(message)

play-validations/memory-footprint/src/main/java/com/google/wear/watchface/dfx/memory/SizedIterator.java

Lines changed: 0 additions & 112 deletions
This file was deleted.
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
* Copyright 2023 Google LLC
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 com.google.wear.watchface.dfx.memory
17+
18+
/**
19+
* A lazy iterator, allowing to generate all combinations of a set without getting into stack
20+
* overflows.
21+
*/
22+
internal interface SizedIterator<T> : Iterator<T> {
23+
/** The size of the collection of elements that is generated */
24+
fun getSize(): Long
25+
26+
companion object {
27+
/** Constructs a sized iterator from a plain java iterator and a size. */
28+
fun <T> fromIterator(iterator: Iterator<T>, size: Long): SizedIterator<T> {
29+
return object : SizedIterator<T> {
30+
override fun getSize(): Long {
31+
return size
32+
}
33+
34+
override fun hasNext(): Boolean {
35+
return iterator.hasNext()
36+
}
37+
38+
override fun next(): T {
39+
return iterator.next()
40+
}
41+
}
42+
}
43+
44+
/**
45+
* Lazily combines an existing iterator with a new collection of elements by taking every
46+
* element of the new collection and combining it with every element of the existing
47+
* iterator. The resulting iterator will have a size of iterator.size() * elements.size().
48+
*
49+
* @param iterator the existing iterator
50+
* @param elements the collection providing the new elements that are combined with the
51+
* already generated ones.
52+
* @param combineFn the function used to combine each element of the elements argument with
53+
* each element of the existing iterator.
54+
* @param mapFn the function used to map a single element of the elements collection to
55+
* elements of the iterator, used when the existing iterator is empty and combineFn cannot
56+
* be called.
57+
* @return a new iterator with the combined elements.
58+
* @param <T> the elements of the iterator
59+
* @param <U> the elements of the collection
60+
*/
61+
fun <T, U> combine(
62+
iterator: SizedIterator<T>,
63+
elements: Collection<U>,
64+
combineFn: (T, U) -> T,
65+
mapFn: (U) -> T
66+
): SizedIterator<T> {
67+
// if the current key does not have any configuration values, then ignore it
68+
// and return the next iterator.
69+
if (elements.isEmpty()) {
70+
return iterator
71+
}
72+
73+
// if the rest iterator has no configuration values, then start a new iterator
74+
// from the current keys.
75+
if (!iterator.hasNext()) {
76+
return fromIterator(elements.stream().map(mapFn).iterator(), elements.size.toLong())
77+
}
78+
79+
// lazily consume each partial config from the rest iterator and append each value for
80+
// the
81+
// current key, producing new partial configs.
82+
return object : SizedIterator<T> {
83+
private val _size = elements.size * iterator.getSize()
84+
private var crtValuesIter = elements.iterator()
85+
private var currentFromTail = iterator.next()
86+
override fun getSize(): Long {
87+
return _size
88+
}
89+
90+
override fun hasNext(): Boolean {
91+
return crtValuesIter.hasNext() || iterator.hasNext()
92+
}
93+
94+
override fun next(): T {
95+
if (!crtValuesIter.hasNext()) {
96+
crtValuesIter = elements.iterator()
97+
currentFromTail = iterator.next()
98+
}
99+
return combineFn(currentFromTail, crtValuesIter.next())
100+
}
101+
}
102+
}
103+
}
104+
}

play-validations/memory-footprint/src/main/java/com/google/wear/watchface/dfx/memory/TestFailedException.java renamed to play-validations/memory-footprint/src/main/java/com/google/wear/watchface/dfx/memory/TestFailedException.kt

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,10 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
17-
package com.google.wear.watchface.dfx.memory;
16+
package com.google.wear.watchface.dfx.memory
1817

1918
/**
2019
* Exception thrown when the memory footprint test fails for an expected reason and the watch face
2120
* must be rejected from the store.
2221
*/
23-
public class TestFailedException extends RuntimeException {
24-
25-
public TestFailedException(String message) {
26-
super(message);
27-
}
28-
}
22+
class TestFailedException(message: String?) : RuntimeException(message)

play-validations/memory-footprint/src/main/java/com/google/wear/watchface/dfx/memory/TestResultFormatter.java renamed to play-validations/memory-footprint/src/main/java/com/google/wear/watchface/dfx/memory/TestResultFormatter.kt

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
/*
22
* Copyright 2023 Google LLC
3-
*
3+
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
66
* You may obtain a copy of the License at
7-
*
7+
*
88
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
9+
*
1010
* Unless required by applicable law or agreed to in writing, software
1111
* distributed under the License is distributed on an "AS IS" BASIS,
1212
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
1616

17-
package com.google.wear.watchface.dfx.memory;
17+
package com.google.wear.watchface.dfx.memory
1818

1919
/** Annotates test results to make it easier for humans to read them. */
20-
class TestResultFormatter {
21-
22-
static String formatSuccess(String successMessage) {
23-
return String.format("[MEMORY_FOOTPRINT]: ✅PASS✅ %s ✅ ", successMessage);
20+
internal object TestResultFormatter {
21+
@JvmStatic
22+
fun formatSuccess(successMessage: String): String {
23+
return "[MEMORY_FOOTPRINT]: ✅PASS✅ $successMessage"
2424
}
2525

26-
static String formatFailure(String failedMessage) {
27-
return String.format("[MEMORY_FOOTPRINT]: ❌FAIL❌ %s ❌ ", failedMessage);
26+
@JvmStatic
27+
fun formatFailure(failedMessage: String): String {
28+
return "[MEMORY_FOOTPRINT]: ❌FAIL❌ $failedMessage"
2829
}
2930

30-
static String formatException(String exceptionMessage) {
31-
return String.format(
32-
"%s\n%s",
33-
"❗❗❗❗ Something went wrong. Please retry or seek assistance.❗❗❗❗",
34-
exceptionMessage);
31+
@JvmStatic
32+
fun formatException(exceptionMessage: String): String {
33+
return "❗❗❗❗ Something went wrong. Please retry or seek assistance.❗❗❗❗\n$exceptionMessage"
3534
}
3635
}

play-validations/memory-footprint/src/main/java/com/google/wear/watchface/dfx/memory/UserConfigKey.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ static SizedIterator<UserConfigSet> buildConfigSets(Iterable<UserConfigKey> conf
148148

149149
private static SizedIterator<UserConfigSet> buildConfigSets(Iterator<UserConfigKey> configs) {
150150
if (!configs.hasNext()) {
151-
return SizedIterator.fromIterator(emptyIterator(), 0);
151+
return SizedIterator.Companion.fromIterator(emptyIterator(), 0);
152152
}
153153
UserConfigKey head = configs.next();
154154
SizedIterator<UserConfigSet> tailExpanded = buildConfigSets(configs);
155-
return SizedIterator.combine(
155+
return SizedIterator.Companion.combine(
156156
tailExpanded,
157157
head.getConfigurationValues(),
158158
(configSet, configValue) -> configSet.plus(head, configValue),

0 commit comments

Comments
 (0)