This is an automated email from the ASF dual-hosted git repository.
ahuber pushed a commit to branch v4
in repository https://gitbox.apache.org/repos/asf/causeway.git
The following commit(s) were added to refs/heads/v4 by this push:
new 95015d24cae CAUSEWAY-2297: adds CommonMimeType#valueOf(MimeType)
(quality of life)
95015d24cae is described below
commit 95015d24caebac4f2d012669f23d36168fa62436
Author: Andi Huber <[email protected]>
AuthorDate: Wed Aug 20 09:11:32 2025 +0200
CAUSEWAY-2297: adds CommonMimeType#valueOf(MimeType) (quality of life)
---
.../causeway/applib/value/NamedWithMimeType.java | 34 ++++++++++++----------
1 file changed, 19 insertions(+), 15 deletions(-)
diff --git
a/api/applib/src/main/java/org/apache/causeway/applib/value/NamedWithMimeType.java
b/api/applib/src/main/java/org/apache/causeway/applib/value/NamedWithMimeType.java
index 30429b7982e..08c6f713cb5 100644
---
a/api/applib/src/main/java/org/apache/causeway/applib/value/NamedWithMimeType.java
+++
b/api/applib/src/main/java/org/apache/causeway/applib/value/NamedWithMimeType.java
@@ -52,9 +52,7 @@ default int compareTo(final NamedWithMimeType o) {
o!=null
? o.name()
: null);
- if(c!=0) {
- return c;
- }
+ if(c!=0) return c;
return _Strings.compareNullsFirst(
this.mimeType().getBaseType(),
@@ -212,13 +210,22 @@ public boolean matches(final MimeType otherMimeType) {
return mimeType.match(otherMimeType);
}
+ /**
+ * Tries to match mimeType with any {@link CommonMimeType}.
+ */
+ public static Optional<CommonMimeType> valueOf(final @Nullable
MimeType mimeType) {
+ if(mimeType==null) return Optional.empty();
+ return Stream.of(CommonMimeType.values())
+ .filter(mime->mime.matches(mimeType))
+ .findFirst();
+ }
+
/**
* Tries to match fileExt with any {@link CommonMimeType}.
*/
public static Optional<CommonMimeType> valueOfFileExtension(final
@Nullable String fileExt) {
- if(_Strings.isNullOrEmpty(fileExt)) {
- return Optional.empty();
- }
+ if(_Strings.isNullOrEmpty(fileExt)) return Optional.empty();
+
var fileExtLower = fileExt.toLowerCase();
return Stream.of(CommonMimeType.values())
.filter(mime->mime.proposedFileExtensions().contains(fileExtLower))
@@ -229,17 +236,14 @@ public static Optional<CommonMimeType>
valueOfFileExtension(final @Nullable Stri
* Parses fileName for its extension and tries to match with any
{@link CommonMimeType}.
*/
public static Optional<CommonMimeType> valueOfFileName(final @Nullable
String fileName) {
- if(_Strings.isNullOrEmpty(fileName)) {
- return Optional.empty();
- }
+ if(_Strings.isNullOrEmpty(fileName)) return Optional.empty();
+
final int p = fileName.lastIndexOf('.');
- if(p<0) {
- return Optional.empty();
- }
+ if(p<0) return Optional.empty();
+
final int beginIndex = p + 1;
- if ((fileName.length() - beginIndex) < 0) {
- return Optional.empty();
- }
+ if ((fileName.length() - beginIndex) < 0) return Optional.empty();
+
return valueOfFileExtension(fileName.substring(beginIndex));
}