From 92d6e6604dd40581328edf6973da178f8e5836d7 Mon Sep 17 00:00:00 2001 From: marhali Date: Mon, 11 Apr 2022 19:22:58 +0200 Subject: [PATCH] apply new internal data structure --- .../java/de/marhali/easyi18n/DataBus.java | 2 +- .../de/marhali/easyi18n/InstanceManager.java | 4 +- .../de/marhali/easyi18n/action/AddAction.java | 2 +- .../de/marhali/easyi18n/dialog/AddDialog.java | 8 +- .../marhali/easyi18n/dialog/EditDialog.java | 6 +- .../easyi18n/dialog/TranslationDialog.java | 6 +- .../de/marhali/easyi18n/model/KeyPath.java | 54 ++-- .../marhali/easyi18n/model/Translation.java | 52 +++- .../easyi18n/model/TranslationData.java | 4 +- .../easyi18n/model/TranslationNode.java | 10 +- .../{translation => }/TranslationValue.java | 241 ++++++++++-------- .../model/{ => action}/TranslationCreate.java | 6 +- .../model/{ => action}/TranslationDelete.java | 6 +- .../model/{ => action}/TranslationUpdate.java | 13 +- .../easyi18n/util/KeyPathConverter.java | 15 +- .../easyi18n/KeyPathConverterTest.java | 2 +- .../marhali/easyi18n/TranslationDataTest.java | 200 +++++++-------- 17 files changed, 338 insertions(+), 293 deletions(-) rename src/main/java/de/marhali/easyi18n/model/{translation => }/TranslationValue.java (79%) rename src/main/java/de/marhali/easyi18n/model/{ => action}/TranslationCreate.java (59%) rename src/main/java/de/marhali/easyi18n/model/{ => action}/TranslationDelete.java (60%) rename src/main/java/de/marhali/easyi18n/model/{ => action}/TranslationUpdate.java (69%) diff --git a/src/main/java/de/marhali/easyi18n/DataBus.java b/src/main/java/de/marhali/easyi18n/DataBus.java index d500232..2eb49d0 100644 --- a/src/main/java/de/marhali/easyi18n/DataBus.java +++ b/src/main/java/de/marhali/easyi18n/DataBus.java @@ -1,9 +1,9 @@ package de.marhali.easyi18n; -import de.marhali.easyi18n.model.KeyPath; import de.marhali.easyi18n.model.bus.BusListener; import de.marhali.easyi18n.model.TranslationData; +import de.marhali.easyi18n.model.KeyPath; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/src/main/java/de/marhali/easyi18n/InstanceManager.java b/src/main/java/de/marhali/easyi18n/InstanceManager.java index 1724260..5fe9c55 100644 --- a/src/main/java/de/marhali/easyi18n/InstanceManager.java +++ b/src/main/java/de/marhali/easyi18n/InstanceManager.java @@ -3,7 +3,7 @@ package de.marhali.easyi18n; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; -import de.marhali.easyi18n.model.TranslationUpdate; +import de.marhali.easyi18n.model.action.TranslationUpdate; import org.jetbrains.annotations.NotNull; @@ -67,7 +67,7 @@ public class InstanceManager { } if(!update.isDeletion()) { // Create or re-create translation with changed data - this.store.getData().setTranslation(update.getChange().getKey(), update.getChange().getTranslation()); + this.store.getData().setTranslation(update.getChange().getKey(), update.getChange().getValue()); } this.store.saveToPersistenceLayer(success -> { diff --git a/src/main/java/de/marhali/easyi18n/action/AddAction.java b/src/main/java/de/marhali/easyi18n/action/AddAction.java index 05be4f1..2505a18 100644 --- a/src/main/java/de/marhali/easyi18n/action/AddAction.java +++ b/src/main/java/de/marhali/easyi18n/action/AddAction.java @@ -7,7 +7,7 @@ import com.intellij.openapi.project.Project; import com.intellij.ui.content.Content; import de.marhali.easyi18n.dialog.AddDialog; -import de.marhali.easyi18n.model.translation.KeyPath; +import de.marhali.easyi18n.model.KeyPath; import de.marhali.easyi18n.service.WindowManager; import de.marhali.easyi18n.util.KeyPathConverter; import de.marhali.easyi18n.util.TreeUtil; diff --git a/src/main/java/de/marhali/easyi18n/dialog/AddDialog.java b/src/main/java/de/marhali/easyi18n/dialog/AddDialog.java index 0998611..be14827 100644 --- a/src/main/java/de/marhali/easyi18n/dialog/AddDialog.java +++ b/src/main/java/de/marhali/easyi18n/dialog/AddDialog.java @@ -5,10 +5,10 @@ import com.intellij.openapi.ui.DialogBuilder; import com.intellij.openapi.ui.DialogWrapper; import de.marhali.easyi18n.InstanceManager; -import de.marhali.easyi18n.model.TranslationCreate; -import de.marhali.easyi18n.model.translation.KeyPath; -import de.marhali.easyi18n.model.translation.Translation; -import de.marhali.easyi18n.model.translation.TranslationValue; +import de.marhali.easyi18n.model.action.TranslationCreate; +import de.marhali.easyi18n.model.KeyPath; +import de.marhali.easyi18n.model.Translation; +import de.marhali.easyi18n.model.TranslationValue; import de.marhali.easyi18n.settings.ProjectSettingsService; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/de/marhali/easyi18n/dialog/EditDialog.java b/src/main/java/de/marhali/easyi18n/dialog/EditDialog.java index 97cc5f5..f9ffa69 100644 --- a/src/main/java/de/marhali/easyi18n/dialog/EditDialog.java +++ b/src/main/java/de/marhali/easyi18n/dialog/EditDialog.java @@ -6,9 +6,9 @@ import com.intellij.openapi.ui.DialogWrapper; import de.marhali.easyi18n.InstanceManager; import de.marhali.easyi18n.dialog.descriptor.DeleteActionDescriptor; -import de.marhali.easyi18n.model.TranslationDelete; -import de.marhali.easyi18n.model.TranslationUpdate; -import de.marhali.easyi18n.model.translation.Translation; +import de.marhali.easyi18n.model.action.TranslationDelete; +import de.marhali.easyi18n.model.action.TranslationUpdate; +import de.marhali.easyi18n.model.Translation; import org.jetbrains.annotations.NotNull; diff --git a/src/main/java/de/marhali/easyi18n/dialog/TranslationDialog.java b/src/main/java/de/marhali/easyi18n/dialog/TranslationDialog.java index 01cacd7..d60c74f 100644 --- a/src/main/java/de/marhali/easyi18n/dialog/TranslationDialog.java +++ b/src/main/java/de/marhali/easyi18n/dialog/TranslationDialog.java @@ -7,9 +7,9 @@ import com.intellij.ui.components.JBTextField; import com.intellij.util.ui.FormBuilder; import de.marhali.easyi18n.InstanceManager; -import de.marhali.easyi18n.model.translation.KeyPath; -import de.marhali.easyi18n.model.translation.Translation; -import de.marhali.easyi18n.model.translation.TranslationValue; +import de.marhali.easyi18n.model.KeyPath; +import de.marhali.easyi18n.model.Translation; +import de.marhali.easyi18n.model.TranslationValue; import de.marhali.easyi18n.settings.ProjectSettings; import de.marhali.easyi18n.settings.ProjectSettingsService; import de.marhali.easyi18n.util.KeyPathConverter; diff --git a/src/main/java/de/marhali/easyi18n/model/KeyPath.java b/src/main/java/de/marhali/easyi18n/model/KeyPath.java index 17e4d1a..0ad3089 100644 --- a/src/main/java/de/marhali/easyi18n/model/KeyPath.java +++ b/src/main/java/de/marhali/easyi18n/model/KeyPath.java @@ -1,59 +1,37 @@ package de.marhali.easyi18n.model; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.ArrayList; -import java.util.Collection; import java.util.List; -import java.util.regex.Pattern; /** - * Represents a full translation key with all sections. - * Implementations can use single section or variable section length variants. - * The respective layer (io, presentation) is responsible for using the correct mapping mechanism. + * Represents the absolute key path for a desired translation. + * The key could be based one or many sections. + * Classes implementing this structure need to take care on how to layer translations paths. * @author marhali */ -@Deprecated public class KeyPath extends ArrayList { - public static final String DELIMITER = "."; + public KeyPath() {} - public static KeyPath of(@NotNull String... path) { - return new KeyPath(List.of(path)); + public KeyPath(@Nullable String... path) { + super.addAll(List.of(path)); } - public KeyPath() { - super(); + public KeyPath(@NotNull List path) { + super(path); } - public KeyPath(@NotNull KeyPath path, String... pathToAppend) { + public KeyPath(@NotNull KeyPath path, @Nullable String... pathToAppend) { this(path); - this.addAll(List.of(pathToAppend)); + super.addAll(List.of(pathToAppend)); } - public KeyPath(@NotNull Collection c) { - super(c); + @Override + public String toString() { + // Just a simple array view (e.g. [first, second]) - use KeyPathConverter to properly convert a key path + return super.toString(); } - - public KeyPath(@NotNull String simplePath) { - this(List.of(simplePath.split(Pattern.quote(DELIMITER)))); - } - - /** - * Note: Use {@link KeyPathConverter} if you want to keep hierarchy. - * @return simple path representation by adding delimiter between the secton nodes - */ - public String toSimpleString() { - StringBuilder builder = new StringBuilder(); - - for(String section : this) { - if(builder.length() > 0) { - builder.append(DELIMITER); - } - - builder.append(section); - } - - return builder.toString(); - } -} \ No newline at end of file +} diff --git a/src/main/java/de/marhali/easyi18n/model/Translation.java b/src/main/java/de/marhali/easyi18n/model/Translation.java index cced758..cfff6c8 100644 --- a/src/main/java/de/marhali/easyi18n/model/Translation.java +++ b/src/main/java/de/marhali/easyi18n/model/Translation.java @@ -1,30 +1,54 @@ package de.marhali.easyi18n.model; -import java.util.HashMap; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; /** - * Represents all translations for an element. The assignment to an element is done in the using class. - * This class contains only the translations for this unspecific element. + * Represents a translation with defined key and locale values. + * * @author marhali */ -@Deprecated // Replaced by TranslationValue -public class Translation extends HashMap { - public Translation() { - super(); +public class Translation { + + private final @NotNull KeyPath key; + private @Nullable TranslationValue value; + + /** + * Constructs a new translation instance. + * @param key Absolute key path + * @param value Values to set - nullable to indicate removal + */ + public Translation(@NotNull KeyPath key, @Nullable TranslationValue value) { + this.key = key; + this.value = value; } - public Translation(String locale, String content) { - this(); - super.put(locale, content); + /** + * @return Absolute key path + */ + public @NotNull KeyPath getKey() { + return key; } - public Translation add(String locale, String content) { - super.put(locale, content); - return this; + /** + * @return values - nullable to indicate removal + */ + public @Nullable TranslationValue getValue() { + return value; + } + + /** + * @param value Values to set - nullable to indicate removal + */ + public void setValue(@Nullable TranslationValue value) { + this.value = value; } @Override public String toString() { - return super.toString(); + return "Translation{" + + "key=" + key + + ", value=" + value + + '}'; } } \ No newline at end of file diff --git a/src/main/java/de/marhali/easyi18n/model/TranslationData.java b/src/main/java/de/marhali/easyi18n/model/TranslationData.java index 087048b..33a2946 100644 --- a/src/main/java/de/marhali/easyi18n/model/TranslationData.java +++ b/src/main/java/de/marhali/easyi18n/model/TranslationData.java @@ -93,7 +93,7 @@ public class TranslationData { * @param fullPath Absolute translation key path * @return Found translation. Can be null if path is empty or is not a leaf element */ - public @Nullable Translation getTranslation(@NotNull KeyPath fullPath) { + public @Nullable TranslationValue getTranslation(@NotNull KeyPath fullPath) { TranslationNode node = this.getNode(fullPath); if(node == null || !node.isLeaf()) { @@ -109,7 +109,7 @@ public class TranslationData { * @param fullPath Absolute translation key path * @param translation Translation to set. Can be null to delete the corresponding node */ - public void setTranslation(@NotNull KeyPath fullPath, @Nullable Translation translation) { + public void setTranslation(@NotNull KeyPath fullPath, @Nullable TranslationValue translation) { if(fullPath.isEmpty()) { throw new IllegalArgumentException("Key path cannot be empty"); } diff --git a/src/main/java/de/marhali/easyi18n/model/TranslationNode.java b/src/main/java/de/marhali/easyi18n/model/TranslationNode.java index d85e678..b2ca570 100644 --- a/src/main/java/de/marhali/easyi18n/model/TranslationNode.java +++ b/src/main/java/de/marhali/easyi18n/model/TranslationNode.java @@ -27,7 +27,7 @@ public class TranslationNode { private Map children; @NotNull - private Translation value; + private TranslationValue value; public TranslationNode(boolean sort) { this(sort ? new TreeMap<>() : new LinkedHashMap<>()); @@ -40,7 +40,7 @@ public class TranslationNode { public TranslationNode(@NotNull Map children) { this.parent = null; this.children = children; - this.value = new Translation(); + this.value = new TranslationValue(); } /** @@ -62,11 +62,11 @@ public class TranslationNode { this.parent = parent; } - public @NotNull Translation getValue() { + public @NotNull TranslationValue getValue() { return value; } - public void setValue(@NotNull Translation value) { + public void setValue(@NotNull TranslationValue value) { this.children.clear(); this.value = value; } @@ -93,7 +93,7 @@ public class TranslationNode { } } - public void setChildren(@NotNull String key, @NotNull Translation translation) { + public void setChildren(@NotNull String key, @NotNull TranslationValue translation) { this.setChildren(key).setValue(translation); } diff --git a/src/main/java/de/marhali/easyi18n/model/translation/TranslationValue.java b/src/main/java/de/marhali/easyi18n/model/TranslationValue.java similarity index 79% rename from src/main/java/de/marhali/easyi18n/model/translation/TranslationValue.java rename to src/main/java/de/marhali/easyi18n/model/TranslationValue.java index f3e2688..6abd17b 100644 --- a/src/main/java/de/marhali/easyi18n/model/translation/TranslationValue.java +++ b/src/main/java/de/marhali/easyi18n/model/TranslationValue.java @@ -1,107 +1,134 @@ -package de.marhali.easyi18n.model.translation; - -import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; - -import java.util.HashMap; -import java.util.Map; - -/** - * Represents the set values behind a specific translation. - * @author marhali - */ -public class TranslationValue { - - private @Nullable String description; - private @NotNull Map values; - private @Nullable Object misc; - - public TranslationValue(@Nullable String description, @NotNull Map values, @Nullable Object misc) { - this.description = description; - this.values = values; - this.misc = misc; - } - - public TranslationValue(@NotNull Map values) { - this(null, values, null); - } - - public TranslationValue(@NotNull String locale, @NotNull String value) { - this(Map.of(locale, value)); - } - - public TranslationValue() { - this(null, new HashMap<>(), null); - } - - /** - * Retrieve additional description for this translation - * @return Description - */ - public @Nullable String getDescription() { - return description; - } - - /** - * Override or set description for this translation - * @param description Description - */ - public void setDescription(@Nullable String description) { - this.description = description; - } - - /** - * Set locale specific values. - * @param values New values - */ - public void setValues(@NotNull Map values) { - this.values = values; - } - - /** - * Overrides or sets a value for a specific locale. - * @param locale Locale type - * @param value New value - */ - public void put(@NotNull String locale, @Nullable String value) { - if(value == null) { // Delete operation - values.remove(locale); - } else { - values.put(locale, value); - } - } - - /** - * Retrieves the associated value for a specific locale - * @param locale Locale type - * @return Value or null if missing - */ - public @Nullable String get(@NotNull String locale) { - return values.get(locale); - } - - /** - * I18n support data - * @return Data - */ - public @Nullable Object getMisc() { - return misc; - } - - /** - * Set or update I18n support data - * @param misc New Data - */ - public void setMisc(@Nullable Object misc) { - this.misc = misc; - } - - @Override - public String toString() { - return "TranslationValue{" + - "description='" + description + '\'' + - ", values=" + values + - ", misc=" + misc + - '}'; - } -} +package de.marhali.easyi18n.model; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; + +/** + * Represents the set values behind a specific translation. + * @author marhali + */ +public class TranslationValue { + + private @Nullable String description; + private @NotNull Map values; + private @Nullable Object misc; + + public TranslationValue(@Nullable String description, @NotNull Map values, @Nullable Object misc) { + this.description = description; + this.values = values; + this.misc = misc; + } + + public TranslationValue(@NotNull Map values) { + this(null, values, null); + } + + public TranslationValue(@NotNull String locale, @NotNull String value) { + this(new HashMap<>(Map.of(locale, value))); + } + + public TranslationValue() { + this(null, new HashMap<>(), null); + } + + /** + * Retrieve additional description for this translation + * @return Description + */ + public @Nullable String getDescription() { + return description; + } + + /** + * Override or set description for this translation + * @param description Description + */ + public void setDescription(@Nullable String description) { + this.description = description; + } + + /** + * Set locale specific values. + * @param values New values + */ + public void setValues(@NotNull Map values) { + this.values = values; + } + + public @NotNull Map getValues() { + return values; + } + + /** + * Overrides or sets a value for a specific locale. + * @param locale Locale type + * @param value New value + */ + public void put(@NotNull String locale, @Nullable String value) { + if(value == null) { // Delete operation + values.remove(locale); + } else { + values.put(locale, value); + } + } + + public void remove(String locale) { + values.remove(locale); + } + + /** + * Retrieves the associated value for a specific locale + * @param locale Locale type + * @return Value or null if missing + */ + public @Nullable String get(@NotNull String locale) { + return values.get(locale); + } + + public boolean containsLocale(@Nullable String locale) { + return values.containsKey(locale); + } + + public @NotNull Collection getLocaleValues() { + return values.values(); + } + + public int size() { + return values.size(); + } + + public void clear() { + description = null; + values.clear(); + misc = null; + } + + /** + * I18n support data + * @return Data + */ + public @Nullable Object getMisc() { + return misc; + } + + /** + * Set or update I18n support data + * @param misc New Data + */ + public void setMisc(@Nullable Object misc) { + this.misc = misc; + } + + @Override + public String toString() { + return "TranslationValue{" + + "description='" + description + '\'' + + ", values=" + values + + ", misc=" + misc + + '}'; + } +} diff --git a/src/main/java/de/marhali/easyi18n/model/TranslationCreate.java b/src/main/java/de/marhali/easyi18n/model/action/TranslationCreate.java similarity index 59% rename from src/main/java/de/marhali/easyi18n/model/TranslationCreate.java rename to src/main/java/de/marhali/easyi18n/model/action/TranslationCreate.java index 9c955a9..4edd8f5 100644 --- a/src/main/java/de/marhali/easyi18n/model/TranslationCreate.java +++ b/src/main/java/de/marhali/easyi18n/model/action/TranslationCreate.java @@ -1,4 +1,6 @@ -package de.marhali.easyi18n.model; +package de.marhali.easyi18n.model.action; + +import de.marhali.easyi18n.model.Translation; import org.jetbrains.annotations.NotNull; @@ -7,7 +9,7 @@ import org.jetbrains.annotations.NotNull; * @author marhali */ public class TranslationCreate extends TranslationUpdate { - public TranslationCreate(@NotNull KeyedTranslation translation) { + public TranslationCreate(@NotNull Translation translation) { super(null, translation); } } \ No newline at end of file diff --git a/src/main/java/de/marhali/easyi18n/model/TranslationDelete.java b/src/main/java/de/marhali/easyi18n/model/action/TranslationDelete.java similarity index 60% rename from src/main/java/de/marhali/easyi18n/model/TranslationDelete.java rename to src/main/java/de/marhali/easyi18n/model/action/TranslationDelete.java index 763cb16..6812e86 100644 --- a/src/main/java/de/marhali/easyi18n/model/TranslationDelete.java +++ b/src/main/java/de/marhali/easyi18n/model/action/TranslationDelete.java @@ -1,4 +1,6 @@ -package de.marhali.easyi18n.model; +package de.marhali.easyi18n.model.action; + +import de.marhali.easyi18n.model.Translation; import org.jetbrains.annotations.NotNull; @@ -7,7 +9,7 @@ import org.jetbrains.annotations.NotNull; * @author marhali */ public class TranslationDelete extends TranslationUpdate { - public TranslationDelete(@NotNull KeyedTranslation translation) { + public TranslationDelete(@NotNull Translation translation) { super(translation, null); } } \ No newline at end of file diff --git a/src/main/java/de/marhali/easyi18n/model/TranslationUpdate.java b/src/main/java/de/marhali/easyi18n/model/action/TranslationUpdate.java similarity index 69% rename from src/main/java/de/marhali/easyi18n/model/TranslationUpdate.java rename to src/main/java/de/marhali/easyi18n/model/action/TranslationUpdate.java index 6b9dac8..b1a03b3 100644 --- a/src/main/java/de/marhali/easyi18n/model/TranslationUpdate.java +++ b/src/main/java/de/marhali/easyi18n/model/action/TranslationUpdate.java @@ -1,5 +1,6 @@ -package de.marhali.easyi18n.model; +package de.marhali.easyi18n.model.action; +import de.marhali.easyi18n.model.Translation; import org.jetbrains.annotations.Nullable; /** @@ -10,19 +11,19 @@ import org.jetbrains.annotations.Nullable; */ public class TranslationUpdate { - private final @Nullable KeyedTranslation origin; - private final @Nullable KeyedTranslation change; + private final @Nullable Translation origin; + private final @Nullable Translation change; - public TranslationUpdate(@Nullable KeyedTranslation origin, @Nullable KeyedTranslation change) { + public TranslationUpdate(@Nullable Translation origin, @Nullable Translation change) { this.origin = origin; this.change = change; } - public @Nullable KeyedTranslation getOrigin() { + public @Nullable Translation getOrigin() { return origin; } - public @Nullable KeyedTranslation getChange() { + public @Nullable Translation getChange() { return change; } diff --git a/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java b/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java index cde4cd2..3307d6c 100644 --- a/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java +++ b/src/main/java/de/marhali/easyi18n/util/KeyPathConverter.java @@ -1,7 +1,10 @@ package de.marhali.easyi18n.util; -import de.marhali.easyi18n.model.translation.KeyPath; +import com.intellij.openapi.project.Project; + +import de.marhali.easyi18n.model.KeyPath; import de.marhali.easyi18n.settings.ProjectSettings; +import de.marhali.easyi18n.settings.ProjectSettingsService; import org.jetbrains.annotations.NotNull; @@ -20,10 +23,18 @@ public class KeyPathConverter { * Constructs a new converter instance * @param settings Delimiter configuration */ - public KeyPathConverter(ProjectSettings settings) { + public KeyPathConverter(@NotNull ProjectSettings settings) { this.settings = settings; } + /** + * @see #KeyPathConverter(ProjectSettings) + * @param project Opened project + */ + public KeyPathConverter(@NotNull Project project) { + this(ProjectSettingsService.get(project).getState()); + } + /** * Transform to character literal representation * @param path Absolute key path diff --git a/src/test/java/de/marhali/easyi18n/KeyPathConverterTest.java b/src/test/java/de/marhali/easyi18n/KeyPathConverterTest.java index 9718215..8404ba1 100644 --- a/src/test/java/de/marhali/easyi18n/KeyPathConverterTest.java +++ b/src/test/java/de/marhali/easyi18n/KeyPathConverterTest.java @@ -2,7 +2,7 @@ package de.marhali.easyi18n; import de.marhali.easyi18n.io.parser.ParserStrategyType; import de.marhali.easyi18n.model.FolderStrategyType; -import de.marhali.easyi18n.model.translation.KeyPath; +import de.marhali.easyi18n.model.KeyPath; import de.marhali.easyi18n.settings.ProjectSettings; import de.marhali.easyi18n.util.KeyPathConverter; diff --git a/src/test/java/de/marhali/easyi18n/TranslationDataTest.java b/src/test/java/de/marhali/easyi18n/TranslationDataTest.java index 4e788f0..24f4c0f 100644 --- a/src/test/java/de/marhali/easyi18n/TranslationDataTest.java +++ b/src/test/java/de/marhali/easyi18n/TranslationDataTest.java @@ -1,10 +1,10 @@ package de.marhali.easyi18n; -import de.marhali.easyi18n.model.KeyPath; -import de.marhali.easyi18n.model.Translation; import de.marhali.easyi18n.model.TranslationData; import de.marhali.easyi18n.model.TranslationNode; +import de.marhali.easyi18n.model.KeyPath; +import de.marhali.easyi18n.model.TranslationValue; import org.junit.Assert; import org.junit.Test; @@ -17,27 +17,27 @@ import java.util.*; public class TranslationDataTest { private final int numOfTranslations = 14; - private final Translation translation = new Translation("en", "test"); + private final TranslationValue translation = new TranslationValue("en", "test"); private void addTranslations(TranslationData data) { - data.setTranslation(KeyPath.of("zulu"), translation); - data.setTranslation(KeyPath.of("gamma"), translation); + data.setTranslation(new KeyPath("zulu"), translation); + data.setTranslation(new KeyPath("gamma"), translation); - data.setTranslation(KeyPath.of("foxtrot.super.long.key"), translation); - data.setTranslation(KeyPath.of("foxtrot", "super", "long", "key"), translation); + data.setTranslation(new KeyPath("foxtrot.super.long.key"), translation); + data.setTranslation(new KeyPath("foxtrot", "super", "long", "key"), translation); - data.setTranslation(KeyPath.of("charlie.b", "sub"), translation); - data.setTranslation(KeyPath.of("charlie.a", "sub"), translation); + data.setTranslation(new KeyPath("charlie.b", "sub"), translation); + data.setTranslation(new KeyPath("charlie.a", "sub"), translation); - data.setTranslation(KeyPath.of("bravo.b"), translation); - data.setTranslation(KeyPath.of("bravo.c"), translation); - data.setTranslation(KeyPath.of("bravo.a"), translation); - data.setTranslation(KeyPath.of("bravo.d"), translation); + data.setTranslation(new KeyPath("bravo.b"), translation); + data.setTranslation(new KeyPath("bravo.c"), translation); + data.setTranslation(new KeyPath("bravo.a"), translation); + data.setTranslation(new KeyPath("bravo.d"), translation); - data.setTranslation(KeyPath.of("bravo", "b"), translation); - data.setTranslation(KeyPath.of("bravo", "c"), translation); - data.setTranslation(KeyPath.of("bravo", "a"), translation); - data.setTranslation(KeyPath.of("bravo", "d"), translation); + data.setTranslation(new KeyPath("bravo", "b"), translation); + data.setTranslation(new KeyPath("bravo", "c"), translation); + data.setTranslation(new KeyPath("bravo", "a"), translation); + data.setTranslation(new KeyPath("bravo", "d"), translation); } @Test @@ -46,13 +46,13 @@ public class TranslationDataTest { this.addTranslations(data); Set expectation = new LinkedHashSet<>(Arrays.asList( - KeyPath.of("bravo", "a"), KeyPath.of("bravo", "b"), KeyPath.of("bravo", "c"), KeyPath.of("bravo", "d"), - KeyPath.of("bravo.a"), KeyPath.of("bravo.b"), KeyPath.of("bravo.c"), KeyPath.of("bravo.d"), - KeyPath.of("charlie.a", "sub"), KeyPath.of("charlie.b", "sub"), - KeyPath.of("foxtrot", "super", "long", "key"), - KeyPath.of("foxtrot.super.long.key"), - KeyPath.of("gamma"), - KeyPath.of("zulu") + new KeyPath("bravo", "a"), new KeyPath("bravo", "b"), new KeyPath("bravo", "c"), new KeyPath("bravo", "d"), + new KeyPath("bravo.a"), new KeyPath("bravo.b"), new KeyPath("bravo.c"), new KeyPath("bravo.d"), + new KeyPath("charlie.a", "sub"), new KeyPath("charlie.b", "sub"), + new KeyPath("foxtrot", "super", "long", "key"), + new KeyPath("foxtrot.super.long.key"), + new KeyPath("gamma"), + new KeyPath("zulu") )); Assert.assertEquals(data.getFullKeys(), expectation); @@ -65,13 +65,13 @@ public class TranslationDataTest { this.addTranslations(data); Set expectation = new LinkedHashSet<>(Arrays.asList( - KeyPath.of("zulu"), - KeyPath.of("gamma"), - KeyPath.of("foxtrot.super.long.key"), - KeyPath.of("foxtrot", "super", "long", "key"), - KeyPath.of("charlie.b", "sub"), KeyPath.of("charlie.a", "sub"), - KeyPath.of("bravo.b"), KeyPath.of("bravo.c"), KeyPath.of("bravo.a"), KeyPath.of("bravo.d"), - KeyPath.of("bravo", "b"), KeyPath.of("bravo", "c"), KeyPath.of("bravo", "a"), KeyPath.of("bravo", "d") + new KeyPath("zulu"), + new KeyPath("gamma"), + new KeyPath("foxtrot.super.long.key"), + new KeyPath("foxtrot", "super", "long", "key"), + new KeyPath("charlie.b", "sub"), new KeyPath("charlie.a", "sub"), + new KeyPath("bravo.b"), new KeyPath("bravo.c"), new KeyPath("bravo.a"), new KeyPath("bravo.d"), + new KeyPath("bravo", "b"), new KeyPath("bravo", "c"), new KeyPath("bravo", "a"), new KeyPath("bravo", "d") )); Assert.assertEquals(data.getFullKeys(), expectation); @@ -82,35 +82,35 @@ public class TranslationDataTest { public void testDelete() { TranslationData data = new TranslationData(true); - data.setTranslation(KeyPath.of("alpha"), translation); - data.setTranslation(KeyPath.of("nested.alpha"), translation); - data.setTranslation(KeyPath.of("nested.long.bravo"), translation); + data.setTranslation(new KeyPath("alpha"), translation); + data.setTranslation(new KeyPath("nested.alpha"), translation); + data.setTranslation(new KeyPath("nested.long.bravo"), translation); - data.setTranslation(KeyPath.of("beta"), translation); - data.setTranslation(KeyPath.of("nested", "alpha"), translation); - data.setTranslation(KeyPath.of("nested", "long", "bravo"), translation); + data.setTranslation(new KeyPath("beta"), translation); + data.setTranslation(new KeyPath("nested", "alpha"), translation); + data.setTranslation(new KeyPath("nested", "long", "bravo"), translation); Assert.assertEquals(data.getFullKeys().size(), 6); - data.setTranslation(KeyPath.of("alpha"), null); - data.setTranslation(KeyPath.of("nested.alpha"), null); - data.setTranslation(KeyPath.of("nested.long.bravo"), null); + data.setTranslation(new KeyPath("alpha"), null); + data.setTranslation(new KeyPath("nested.alpha"), null); + data.setTranslation(new KeyPath("nested.long.bravo"), null); Assert.assertEquals(data.getFullKeys().size(), 3); - data.setTranslation(KeyPath.of("beta"), null); - data.setTranslation(KeyPath.of("nested", "alpha"), null); - data.setTranslation(KeyPath.of("nested", "long", "bravo"), null); + data.setTranslation(new KeyPath("beta"), null); + data.setTranslation(new KeyPath("nested", "alpha"), null); + data.setTranslation(new KeyPath("nested", "long", "bravo"), null); Assert.assertEquals(data.getFullKeys().size(), 0); - Assert.assertNull(data.getTranslation(KeyPath.of("alpha"))); - Assert.assertNull(data.getTranslation(KeyPath.of("nested.alpha"))); - Assert.assertNull(data.getTranslation(KeyPath.of("nested.long.bravo"))); + Assert.assertNull(data.getTranslation(new KeyPath("alpha"))); + Assert.assertNull(data.getTranslation(new KeyPath("nested.alpha"))); + Assert.assertNull(data.getTranslation(new KeyPath("nested.long.bravo"))); - Assert.assertNull(data.getTranslation(KeyPath.of("beta"))); - Assert.assertNull(data.getTranslation(KeyPath.of("nested", "alpha"))); - Assert.assertNull(data.getTranslation(KeyPath.of("nested", "long", "bravo"))); + Assert.assertNull(data.getTranslation(new KeyPath("beta"))); + Assert.assertNull(data.getTranslation(new KeyPath("nested", "alpha"))); + Assert.assertNull(data.getTranslation(new KeyPath("nested", "long", "bravo"))); } @Test @@ -118,10 +118,10 @@ public class TranslationDataTest { TranslationData data = new TranslationData(true); this.addTranslations(data); - data.setTranslation(KeyPath.of("foxtrot.super.long.key"), null); - data.setTranslation(KeyPath.of("foxtrot", "super", "long", "key"), null); + data.setTranslation(new KeyPath("foxtrot.super.long.key"), null); + data.setTranslation(new KeyPath("foxtrot", "super", "long", "key"), null); - Assert.assertNull(data.getTranslation(KeyPath.of("foxtrot.super.long.key"))); + Assert.assertNull(data.getTranslation(new KeyPath("foxtrot.super.long.key"))); Assert.assertNull(data.getRootNode().getChildren().get("foxtrot")); Assert.assertEquals(data.getFullKeys().size(), numOfTranslations - 2); } @@ -130,70 +130,70 @@ public class TranslationDataTest { public void testOverwrite() { TranslationData data = new TranslationData(true); - Translation before = new Translation("en", "before"); - Translation after = new Translation("en", "after"); + TranslationValue before = new TranslationValue("en", "before"); + TranslationValue after = new TranslationValue("en", "after"); - data.setTranslation(KeyPath.of("alpha"), before); - data.setTranslation(KeyPath.of("nested.alpha"), before); - data.setTranslation(KeyPath.of("nested.long.bravo"), before); - data.setTranslation(KeyPath.of("beta"), before); - data.setTranslation(KeyPath.of("nested", "alpha"), before); - data.setTranslation(KeyPath.of("nested", "long", "bravo"), before); + data.setTranslation(new KeyPath("alpha"), before); + data.setTranslation(new KeyPath("nested.alpha"), before); + data.setTranslation(new KeyPath("nested.long.bravo"), before); + data.setTranslation(new KeyPath("beta"), before); + data.setTranslation(new KeyPath("nested", "alpha"), before); + data.setTranslation(new KeyPath("nested", "long", "bravo"), before); - Assert.assertEquals(data.getTranslation(KeyPath.of("alpha")), before); - Assert.assertEquals(data.getTranslation(KeyPath.of("nested.alpha")), before); - Assert.assertEquals(data.getTranslation(KeyPath.of("nested.long.bravo")), before); - Assert.assertEquals(data.getTranslation(KeyPath.of("beta")), before); - Assert.assertEquals(data.getTranslation(KeyPath.of("nested", "alpha")), before); - Assert.assertEquals(data.getTranslation(KeyPath.of("nested", "long", "bravo")), before); + Assert.assertEquals(data.getTranslation(new KeyPath("alpha")), before); + Assert.assertEquals(data.getTranslation(new KeyPath("nested.alpha")), before); + Assert.assertEquals(data.getTranslation(new KeyPath("nested.long.bravo")), before); + Assert.assertEquals(data.getTranslation(new KeyPath("beta")), before); + Assert.assertEquals(data.getTranslation(new KeyPath("nested", "alpha")), before); + Assert.assertEquals(data.getTranslation(new KeyPath("nested", "long", "bravo")), before); - data.setTranslation(KeyPath.of("alpha"), after); - data.setTranslation(KeyPath.of("nested.alpha"), after); - data.setTranslation(KeyPath.of("nested.long.bravo"), after); - data.setTranslation(KeyPath.of("beta"), after); - data.setTranslation(KeyPath.of("nested", "alpha"), after); - data.setTranslation(KeyPath.of("nested", "long", "bravo"), after); + data.setTranslation(new KeyPath("alpha"), after); + data.setTranslation(new KeyPath("nested.alpha"), after); + data.setTranslation(new KeyPath("nested.long.bravo"), after); + data.setTranslation(new KeyPath("beta"), after); + data.setTranslation(new KeyPath("nested", "alpha"), after); + data.setTranslation(new KeyPath("nested", "long", "bravo"), after); - Assert.assertEquals(data.getTranslation(KeyPath.of("alpha")), after); - Assert.assertEquals(data.getTranslation(KeyPath.of("nested.alpha")), after); - Assert.assertEquals(data.getTranslation(KeyPath.of("nested.long.bravo")), after); - Assert.assertEquals(data.getTranslation(KeyPath.of("beta")), after); - Assert.assertEquals(data.getTranslation(KeyPath.of("nested", "alpha")), after); - Assert.assertEquals(data.getTranslation(KeyPath.of("nested", "long", "bravo")), after); + Assert.assertEquals(data.getTranslation(new KeyPath("alpha")), after); + Assert.assertEquals(data.getTranslation(new KeyPath("nested.alpha")), after); + Assert.assertEquals(data.getTranslation(new KeyPath("nested.long.bravo")), after); + Assert.assertEquals(data.getTranslation(new KeyPath("beta")), after); + Assert.assertEquals(data.getTranslation(new KeyPath("nested", "alpha")), after); + Assert.assertEquals(data.getTranslation(new KeyPath("nested", "long", "bravo")), after); } @Test public void testTransformRecursively() { TranslationData data = new TranslationData(true); - data.setTranslation(KeyPath.of("alpha.nested.key"), translation); - data.setTranslation(KeyPath.of("alpha.other"), translation); - data.setTranslation(KeyPath.of("bravo"), translation); - data.setTranslation(KeyPath.of("alpha", "nested", "key"), translation); - data.setTranslation(KeyPath.of("alpha", "other"), translation); - data.setTranslation(KeyPath.of("charlie"), translation); + data.setTranslation(new KeyPath("alpha.nested.key"), translation); + data.setTranslation(new KeyPath("alpha.other"), translation); + data.setTranslation(new KeyPath("bravo"), translation); + data.setTranslation(new KeyPath("alpha", "nested", "key"), translation); + data.setTranslation(new KeyPath("alpha", "other"), translation); + data.setTranslation(new KeyPath("charlie"), translation); Assert.assertEquals(6, data.getFullKeys().size()); - data.setTranslation(KeyPath.of("alpha.nested"), translation); - data.setTranslation(KeyPath.of("alpha.other.new"), translation); - data.setTranslation(KeyPath.of("bravo"), null); - data.setTranslation(KeyPath.of("alpha", "nested"), translation); - data.setTranslation(KeyPath.of("alpha", "other", "new"), translation); - data.setTranslation(KeyPath.of("charlie"), null); + data.setTranslation(new KeyPath("alpha.nested"), translation); + data.setTranslation(new KeyPath("alpha.other.new"), translation); + data.setTranslation(new KeyPath("bravo"), null); + data.setTranslation(new KeyPath("alpha", "nested"), translation); + data.setTranslation(new KeyPath("alpha", "other", "new"), translation); + data.setTranslation(new KeyPath("charlie"), null); Assert.assertEquals(6, data.getFullKeys().size()); - Assert.assertNotNull(data.getTranslation(KeyPath.of("alpha.nested.key"))); - Assert.assertNotNull(data.getTranslation(KeyPath.of("alpha.other"))); - Assert.assertNull(data.getTranslation(KeyPath.of("bravo"))); - Assert.assertEquals(data.getTranslation(KeyPath.of("alpha.nested")), translation); - Assert.assertEquals(data.getTranslation(KeyPath.of("alpha.other.new")), translation); + Assert.assertNotNull(data.getTranslation(new KeyPath("alpha.nested.key"))); + Assert.assertNotNull(data.getTranslation(new KeyPath("alpha.other"))); + Assert.assertNull(data.getTranslation(new KeyPath("bravo"))); + Assert.assertEquals(data.getTranslation(new KeyPath("alpha.nested")), translation); + Assert.assertEquals(data.getTranslation(new KeyPath("alpha.other.new")), translation); - Assert.assertNull(data.getTranslation(KeyPath.of("alpha", "nested", "key"))); - Assert.assertNull(data.getTranslation(KeyPath.of("alpha", "other"))); - Assert.assertNull(data.getTranslation(KeyPath.of("charlie"))); - Assert.assertEquals(data.getTranslation(KeyPath.of("alpha", "nested")), translation); - Assert.assertEquals(data.getTranslation(KeyPath.of("alpha", "other", "new")), translation); + Assert.assertNull(data.getTranslation(new KeyPath("alpha", "nested", "key"))); + Assert.assertNull(data.getTranslation(new KeyPath("alpha", "other"))); + Assert.assertNull(data.getTranslation(new KeyPath("charlie"))); + Assert.assertEquals(data.getTranslation(new KeyPath("alpha", "nested")), translation); + Assert.assertEquals(data.getTranslation(new KeyPath("alpha", "other", "new")), translation); } } \ No newline at end of file