-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
81 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
ground/src/main/java/com/google/android/ground/util/FileExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.google.android.ground.util | ||
|
||
import java.io.File | ||
import kotlin.math.ceil | ||
|
||
typealias ByteCount = Int | ||
|
||
typealias MegabyteCount = Float | ||
|
||
/** Return true iff the file is non-null and contains other files. */ | ||
fun File?.isEmpty() = this?.listFiles().isNullOrEmpty() | ||
|
||
/** Deletes the file if itt is non-null and doesn't contain other files. */ | ||
fun File?.deleteIfEmpty() { | ||
if (isEmpty()) this?.delete() | ||
} | ||
|
||
/** Returns the byte count as an equivalent megabyte count. */ | ||
fun ByteCount.toMb(): MegabyteCount = this / 1024f * 1024f | ||
|
||
/** | ||
* Returns the number of megabytes a string, replacing smaller sizes with "<1" and rounding up | ||
* others. | ||
*/ | ||
fun MegabyteCount.toMbString(): String = if (this < 1) "<1" else ceil(this).toString() |
29 changes: 29 additions & 0 deletions
29
ground/src/main/java/com/google/android/ground/util/RangeExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright 2023 Google LLC | ||
* | ||
* Licensed 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 | ||
* | ||
* https://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.google.android.ground.util | ||
|
||
import kotlin.math.max | ||
import kotlin.math.min | ||
|
||
/** | ||
* Returns a range containing the smallest and largest among all values produced by the selector | ||
* function applied to each element in the iterable collection. | ||
*/ | ||
inline fun <T> Iterable<T>.rangeOf(selector: (T) -> Int): IntRange = | ||
map(selector) | ||
.map { IntRange(it, it) } | ||
.reduce { out, el -> IntRange(min(out.first, el.first), max(out.last, el.last)) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters