Skip to content

Commit

Permalink
feat: Add LocaleExtractor.Builder (#18)
Browse files Browse the repository at this point in the history
* Add LocaleExtractor.Builder

* fix build
  • Loading branch information
jpenilla authored Feb 14, 2024
1 parent 1cbfe34 commit ca380a0
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@
<property name="optional" value="true"/>
</module>

<!-- Make the @SuppressWarnings annotations available to Checkstyle -->
<module name="SuppressWarningsHolder" />

</module>

<module name="SuppressWarningsFilter" />

</module>
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
//
package org.incendo.cloud.translations;

import io.leangen.geantyref.TypeToken;
import java.util.Locale;
import java.util.function.Function;
import org.apiguardian.api.API;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
* Extractor that extracts {@link Locale locales} from command senders.
Expand All @@ -42,4 +45,85 @@ public interface LocaleExtractor<C> {
* @return the extracted locale
*/
@NonNull Locale extract(@NonNull C recipient);

/**
* Returns a new {@link Builder}.
*
* @param <C> command sender type
* @return new builder
*/
static <C> Builder<C> builder() {
return new LocaleExtractorBuilderImpl<>();
}

/**
* Builder for a {@link LocaleExtractor} that delegates to functions based on
* command sender type.
*
* @param <C> command sender type
* @see #senderType(TypeToken, Function)
* @see #fallback(LocaleExtractor)
*/
interface Builder<C> {

/**
* Sets the fallback {@link LocaleExtractor} for when none of the configured {@link #senderType(TypeToken, Function)}
* extractors match the sender. Defaults to {@code LocaleExtractor<C> default = recipient -> Locale.getDefault()}.
*
* @param fallback fallback extractor
* @return this builder
*/
@NonNull Builder<C> fallback(@NonNull LocaleExtractor<C> fallback);

/**
* Sets {@link #fallback(LocaleExtractor)} with a constant locale.
*
* @param fallback fallback locale
* @return this builder
*/
default @NonNull Builder<C> fallback(final @NonNull Locale fallback) {
return this.fallback($ -> fallback);
}

/**
* Registers a locale extractor for a specific sender type to this builder.
*
* <p>Extractors are tried in LIFO registration order. Extractors may return {@code null}
* to pass the recipient to the next extractor, even when the type matches.</p>
*
* @param senderType specific command sender type
* @param extractor locale extractor
* @param <S> specific command sender type
* @return this builder
*/
<S extends C> @NonNull Builder<C> senderType(
@NonNull TypeToken<S> senderType,
@NonNull Function<S, @Nullable Locale> extractor
);

/**
* Registers a locale extractor for a specific sender type to this builder.
*
* <p>Extractors are tried in LIFO registration order. Extractors may return {@code null}
* to pass the recipient to the next extractor, even when the type matches.</p>
*
* @param senderType specific command sender type
* @param extractor locale extractor
* @param <S> specific command sender type
* @return this builder
*/
default <S extends C> @NonNull Builder<C> senderType(
final @NonNull Class<S> senderType,
final @NonNull Function<S, @Nullable Locale> extractor
) {
return this.senderType(TypeToken.get(senderType), extractor);
}

/**
* Builds a {@link LocaleExtractor} from the state of this builder.
*
* @return new locale extractor
*/
@NonNull LocaleExtractor<C> build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.translations;

import io.leangen.geantyref.GenericTypeReflector;
import io.leangen.geantyref.TypeToken;
import java.lang.reflect.Type;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.function.Function;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.incendo.cloud.type.tuple.Pair;

final class LocaleExtractorBuilderImpl<C> implements LocaleExtractor.Builder<C> {

private @NonNull LocaleExtractor<C> fallback = $ -> Locale.getDefault();
private final LinkedList<Pair<Type, Function<Object, @Nullable Locale>>> extractors = new LinkedList<>();

@Override
public LocaleExtractor.@NonNull Builder<C> fallback(final @NonNull LocaleExtractor<C> fallback) {
this.fallback = Objects.requireNonNull(fallback);
return this;
}

@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public <S extends C> LocaleExtractor.@NonNull Builder<C> senderType(
final @NonNull TypeToken<S> senderType,
final @NonNull Function<S, @Nullable Locale> extractor
) {
this.extractors.addFirst(Pair.of(senderType.getType(), (Function) extractor));
return this;
}

@Override
public @NonNull LocaleExtractor<C> build() {
final LocaleExtractor<C> fallback = this.fallback;
final List<Pair<Type, Function<Object, Locale>>> extractors = List.copyOf(this.extractors);
return recipient -> {
for (final Pair<Type, Function<Object, Locale>> pair : extractors) {
if (GenericTypeReflector.isSuperType(pair.first(), recipient.getClass())) {
final @Nullable Locale apply = pair.second().apply(recipient);
if (apply != null) {
return apply;
}
}
}
return fallback.extract(recipient);
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
//
// MIT License
//
// Copyright (c) 2024 Incendo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
package org.incendo.cloud.translations;

import java.util.Locale;
import org.junit.jupiter.api.Test;

import static com.google.common.truth.Truth.assertThat;

@SuppressWarnings("checkstyle:VisibilityModifier")
class LocaleExtractorBuilderTest {

@Test
void testSimple() {
final LocaleExtractor<GenericSender> build = LocaleExtractor.<GenericSender>builder()
.fallback(Locale.FRENCH)
.senderType(ChineseSender.class, s -> s.locale)
.senderType(GermanSender.class, s -> s.locale)
.build();

assertThat(build.extract(new GenericSender())).isEqualTo(Locale.FRENCH);
assertThat(build.extract(new ChineseSender())).isEqualTo(Locale.CHINESE);
assertThat(build.extract(new GermanSender())).isEqualTo(Locale.GERMAN);
}

static class GenericSender {

}

static class ChineseSender extends GenericSender {

final Locale locale = Locale.CHINESE;
}

static class GermanSender extends GenericSender {

final Locale locale = Locale.GERMAN;
}
}

0 comments on commit ca380a0

Please sign in to comment.