|
| 1 | +// Copyright 2026 DeepL SE (https://www.deepl.com) |
| 2 | +// Use of this source code is governed by an MIT |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | +package com.deepl.api; |
| 5 | + |
| 6 | +import java.util.ArrayList; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Map; |
| 9 | +import java.util.function.Function; |
| 10 | +import org.jetbrains.annotations.Nullable; |
| 11 | + |
| 12 | +/** |
| 13 | + * Options for {@link DeepLClient#createStyleProfile(String, CreateStyleProfileOptions)}, also the |
| 14 | + * base of {@link UpdateStyleProfileOptions}. |
| 15 | + * |
| 16 | + * <p>A field left <code>null</code> is not sent, so an update keeps its current value. An empty |
| 17 | + * string clears the description or industry; an empty list unlinks all components of that kind. A |
| 18 | + * list that is sent replaces the linked list in full. |
| 19 | + */ |
| 20 | +public class CreateStyleProfileOptions extends BaseRequestOptions { |
| 21 | + private @Nullable String description; |
| 22 | + private @Nullable String industry; |
| 23 | + private @Nullable List<String> glossaryIds; |
| 24 | + private @Nullable List<String> styleRuleIds; |
| 25 | + private @Nullable List<String> translationMemoryIds; |
| 26 | + private @Nullable Boolean glossaryPriorityOrderEnabled; |
| 27 | + |
| 28 | + /** Sets the description of the style profile; an empty string clears it. */ |
| 29 | + public CreateStyleProfileOptions setDescription(@Nullable String description) { |
| 30 | + this.description = description; |
| 31 | + return this; |
| 32 | + } |
| 33 | + |
| 34 | + /** Sets the industry of the style profile, for example "Legal"; an empty string clears it. */ |
| 35 | + public CreateStyleProfileOptions setIndustry(@Nullable String industry) { |
| 36 | + this.industry = industry; |
| 37 | + return this; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * Sets the IDs of the glossaries to link, in priority order; an empty list unlinks all |
| 42 | + * glossaries. |
| 43 | + */ |
| 44 | + public CreateStyleProfileOptions setGlossaryIds(@Nullable List<String> glossaryIds) { |
| 45 | + this.glossaryIds = glossaryIds; |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Sets the glossaries to link, in priority order, as {@link GlossaryInfo} or {@link |
| 51 | + * MultilingualGlossaryInfo} objects; their IDs are sent. |
| 52 | + */ |
| 53 | + public CreateStyleProfileOptions setGlossaryIds(IGlossary first, IGlossary... more) { |
| 54 | + this.glossaryIds = idsOf(first, more, IGlossary::getGlossaryId, "glossaries"); |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + /** Sets the IDs of the style rules to link; an empty list unlinks all style rules. */ |
| 59 | + public CreateStyleProfileOptions setStyleRuleIds(@Nullable List<String> styleRuleIds) { |
| 60 | + this.styleRuleIds = styleRuleIds; |
| 61 | + return this; |
| 62 | + } |
| 63 | + |
| 64 | + /** Sets the style rules to link as {@link StyleRuleInfo} objects; their IDs are sent. */ |
| 65 | + public CreateStyleProfileOptions setStyleRuleIds(StyleRuleInfo first, StyleRuleInfo... more) { |
| 66 | + this.styleRuleIds = idsOf(first, more, StyleRuleInfo::getStyleId, "style rules"); |
| 67 | + return this; |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Sets the IDs of the translation memories to link; an empty list unlinks all translation |
| 72 | + * memories. |
| 73 | + */ |
| 74 | + public CreateStyleProfileOptions setTranslationMemoryIds( |
| 75 | + @Nullable List<String> translationMemoryIds) { |
| 76 | + this.translationMemoryIds = translationMemoryIds; |
| 77 | + return this; |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Sets the translation memories to link as {@link TranslationMemoryInfo} objects; their IDs are |
| 82 | + * sent. |
| 83 | + */ |
| 84 | + public CreateStyleProfileOptions setTranslationMemoryIds( |
| 85 | + TranslationMemoryInfo first, TranslationMemoryInfo... more) { |
| 86 | + this.translationMemoryIds = |
| 87 | + idsOf(first, more, TranslationMemoryInfo::getTranslationMemoryId, "translation memories"); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + /** Sets whether the linked glossaries are applied in the order of the glossary IDs. */ |
| 92 | + public CreateStyleProfileOptions setGlossaryPriorityOrderEnabled( |
| 93 | + @Nullable Boolean glossaryPriorityOrderEnabled) { |
| 94 | + this.glossaryPriorityOrderEnabled = glossaryPriorityOrderEnabled; |
| 95 | + return this; |
| 96 | + } |
| 97 | + |
| 98 | + /** {@inheritDoc} */ |
| 99 | + @Override |
| 100 | + public CreateStyleProfileOptions setExtraBodyParameters(Map<String, String> extraBodyParameters) { |
| 101 | + super.setExtraBodyParameters(extraBodyParameters); |
| 102 | + return this; |
| 103 | + } |
| 104 | + |
| 105 | + /** Gets the current description. */ |
| 106 | + public @Nullable String getDescription() { |
| 107 | + return description; |
| 108 | + } |
| 109 | + |
| 110 | + /** Gets the current industry. */ |
| 111 | + public @Nullable String getIndustry() { |
| 112 | + return industry; |
| 113 | + } |
| 114 | + |
| 115 | + /** Gets the current glossary IDs. */ |
| 116 | + public @Nullable List<String> getGlossaryIds() { |
| 117 | + return glossaryIds; |
| 118 | + } |
| 119 | + |
| 120 | + /** Gets the current style rule IDs. */ |
| 121 | + public @Nullable List<String> getStyleRuleIds() { |
| 122 | + return styleRuleIds; |
| 123 | + } |
| 124 | + |
| 125 | + /** Gets the current translation memory IDs. */ |
| 126 | + public @Nullable List<String> getTranslationMemoryIds() { |
| 127 | + return translationMemoryIds; |
| 128 | + } |
| 129 | + |
| 130 | + /** Gets the current glossary priority order setting. */ |
| 131 | + public @Nullable Boolean getGlossaryPriorityOrderEnabled() { |
| 132 | + return glossaryPriorityOrderEnabled; |
| 133 | + } |
| 134 | + |
| 135 | + // The separate first parameter keeps a zero-argument call off the object overloads. |
| 136 | + private static <T> List<String> idsOf( |
| 137 | + T first, T[] more, Function<T, String> getId, String description) { |
| 138 | + List<String> ids = new ArrayList<>(); |
| 139 | + ids.add(idOf(first, getId, description)); |
| 140 | + for (T item : more) { |
| 141 | + ids.add(idOf(item, getId, description)); |
| 142 | + } |
| 143 | + return ids; |
| 144 | + } |
| 145 | + |
| 146 | + private static <T> String idOf(@Nullable T item, Function<T, String> getId, String description) { |
| 147 | + if (item == null) { |
| 148 | + throw new IllegalArgumentException(description + " must not contain null"); |
| 149 | + } |
| 150 | + return getId.apply(item); |
| 151 | + } |
| 152 | +} |
0 commit comments