Skip to content

Commit 65abccd

Browse files
authored
Merge pull request #39 from cryptomator/feature/display-name
Feature: Add interface for display name
2 parents 4bfb945 + 9508ee5 commit 65abccd

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.cryptomator.integrations.common;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
5+
import java.lang.annotation.Documented;
6+
import java.lang.annotation.ElementType;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.RetentionPolicy;
9+
import java.lang.annotation.Target;
10+
11+
/**
12+
* A humanreadable name of the annotated class.
13+
* <p>
14+
* Checked in the default implementation of the {@link NamedServiceProvider#getName()} with lower priority.
15+
*
16+
* @see NamedServiceProvider
17+
* @see LocalizedDisplayName
18+
*/
19+
@Documented
20+
@Retention(RetentionPolicy.RUNTIME)
21+
@Target(ElementType.TYPE)
22+
@ApiStatus.Experimental
23+
public @interface DisplayName {
24+
String value();
25+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.cryptomator.integrations.common;
2+
3+
import org.jetbrains.annotations.ApiStatus;
4+
5+
import java.lang.annotation.Documented;
6+
import java.lang.annotation.ElementType;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.RetentionPolicy;
9+
import java.lang.annotation.Target;
10+
11+
/**
12+
* A humanreadable, localized name of the annotated class.
13+
* <p>
14+
* Checked in the default implementation of the {@link NamedServiceProvider#getName()} with highest priority.
15+
*
16+
* @see NamedServiceProvider
17+
* @see DisplayName
18+
*/
19+
@Documented
20+
@Retention(RetentionPolicy.RUNTIME)
21+
@Target(ElementType.TYPE)
22+
@ApiStatus.Experimental
23+
public @interface LocalizedDisplayName {
24+
25+
/**
26+
* Name of the localization bundle, where the display name is loaded from.
27+
*
28+
* @return Name of the localization bundle
29+
*/
30+
String bundle();
31+
32+
/**
33+
* The localization key containing the display name.
34+
*
35+
* @return Localization key to use
36+
*/
37+
String key();
38+
39+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.cryptomator.integrations.common;
2+
3+
import org.slf4j.LoggerFactory;
4+
5+
import java.util.MissingResourceException;
6+
import java.util.ResourceBundle;
7+
8+
/**
9+
* A service provider with a human-readable, possibly localized name.
10+
*/
11+
public interface NamedServiceProvider {
12+
13+
/**
14+
* Get the name of this service provider.
15+
*
16+
* @return The name of the service provider
17+
* @implNote The default implementation looks first for a {@link LocalizedDisplayName} and loads the name from the specified resource bundle/key. If the annotation is not present or loading the resource throws an exception, the code looks for {@link DisplayName} and uses its value. If none of the former annotations are present, it falls back to the qualified class name.
18+
* @see DisplayName
19+
* @see LocalizedDisplayName
20+
*/
21+
default String getName() {
22+
var localizedDisplayName = this.getClass().getAnnotation(LocalizedDisplayName.class);
23+
if (localizedDisplayName != null) {
24+
try {
25+
return ResourceBundle.getBundle(localizedDisplayName.bundle()) //
26+
.getString(localizedDisplayName.key());
27+
} catch (MissingResourceException e) {
28+
var clazz = this.getClass();
29+
var logger = LoggerFactory.getLogger(clazz);
30+
logger.warn("Failed to load localized display name for {}. Falling back to not-localized display name/class name.", clazz.getName(), e);
31+
}
32+
}
33+
34+
var displayName = this.getClass().getAnnotation(DisplayName.class);
35+
if (displayName != null) {
36+
return displayName.value();
37+
} else {
38+
return this.getClass().getName();
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)