Skip to content

Commit c57a18b

Browse files
committed
refactor: properly encapsulate the ResourceBundle into a Reference
1 parent 418accf commit c57a18b

File tree

2 files changed

+17
-16
lines changed

2 files changed

+17
-16
lines changed

src/main/java/com/github/lppedd/cc/configuration/CCMainConfigurableGui.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ private void finishUpComponents(
233233
@SuppressWarnings("ConstantExpression")
234234
@Nullable
235235
private HyperlinkLabel buildTranslatorLabel() {
236-
final var name = CCBundle.getWithDefault("cc.translation.translator.name", "");
236+
final var name = CCBundle.getOrDefault("cc.translation.translator.name", "");
237237

238238
if (name.isEmpty()) {
239239
return null;
@@ -243,7 +243,7 @@ private HyperlinkLabel buildTranslatorLabel() {
243243
label.setForeground(UIUtil.getContextHelpForeground());
244244
label.setFontSize(FontSize.SMALL);
245245

246-
final var url = CCBundle.getWithDefault("cc.translation.translator.url", "");
246+
final var url = CCBundle.getOrDefault("cc.translation.translator.url", "");
247247

248248
if (url.isEmpty()) {
249249
label.setText(CCBundle.get("cc.translation.text") + " " + name);

src/main/kotlin/com/github/lppedd/cc/CCBundle.kt

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.intellij.reference.SoftReference
55
import org.jetbrains.annotations.PropertyKey
66
import java.lang.ref.Reference
77
import java.util.*
8+
import java.lang.ref.SoftReference as JavaSoftReference
89

910
/**
1011
* @author Edoardo Luppi
@@ -13,25 +14,25 @@ public object CCBundle {
1314
private const val BUNDLE = "messages.ConventionalCommitBundle"
1415

1516
private var bundleReference: Reference<ResourceBundle>? = null
16-
private val bundle: ResourceBundle by lazy {
17-
SoftReference.dereference(bundleReference)
18-
?: ResourceBundle.getBundle(BUNDLE).also {
19-
bundleReference = java.lang.ref.SoftReference(it)
20-
}
21-
}
17+
private val bundle: ResourceBundle
18+
get() = derefBundle()
2219

2320
@JvmStatic
2421
public operator fun get(@PropertyKey(resourceBundle = BUNDLE) key: String, vararg params: Any): String =
25-
/* We cannot use anything else but BundleBase to be compatible
26-
* with all IDE versions and to avoid deprecated methods */
2722
BundleBase.message(bundle, key, *params)
2823

2924
@JvmStatic
30-
@Suppress("SameParameterValue")
31-
public fun getWithDefault(
32-
@PropertyKey(resourceBundle = BUNDLE) key: String,
33-
defaultValue: String? = null,
34-
vararg params: Any,
35-
): String =
25+
public fun getOrDefault(@PropertyKey(resourceBundle = BUNDLE) key: String, defaultValue: String, vararg params: Any): String =
3626
BundleBase.messageOrDefault(bundle, key, defaultValue, *params)
27+
28+
private fun derefBundle(): ResourceBundle {
29+
var ref = SoftReference.dereference(bundleReference)
30+
31+
if (ref == null) {
32+
ref = ResourceBundle.getBundle(BUNDLE)
33+
bundleReference = JavaSoftReference(ref)
34+
}
35+
36+
return ref
37+
}
3738
}

0 commit comments

Comments
 (0)