From a7815bee7f25876736d856998a9b347c99aabe20 Mon Sep 17 00:00:00 2001 From: Ivan Kniazkov Date: Tue, 12 Nov 2024 08:27:42 +0300 Subject: [PATCH] Factory provider interface --- .../astranaut/core/base/FactoryProvider.java | 39 +++++++++++++++++++ .../core/utils/JsonDeserializer.java | 30 ++++---------- .../utils/deserializer/TreeDescriptor.java | 8 ++-- 3 files changed, 50 insertions(+), 27 deletions(-) create mode 100644 src/main/java/org/cqfn/astranaut/core/base/FactoryProvider.java diff --git a/src/main/java/org/cqfn/astranaut/core/base/FactoryProvider.java b/src/main/java/org/cqfn/astranaut/core/base/FactoryProvider.java new file mode 100644 index 00000000..589f9ceb --- /dev/null +++ b/src/main/java/org/cqfn/astranaut/core/base/FactoryProvider.java @@ -0,0 +1,39 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2024 Ivan Kniazkov + * + * 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 NON-INFRINGEMENT. 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.cqfn.astranaut.core.base; + +/** + * Provider interface for obtaining {@link Factory} instances based on specific criteria. + * @since 2.0.0 + */ +public interface FactoryProvider { + /** + * Retrieves a {@link Factory} instance associated with the specified language. + * @param language The name of the language for which a {@link Factory} is requested, + * e.g., "Java", "Python", "JavaScript". It should be a non-null and non-empty string. + * @return The {@link Factory} instance associated with the specified language, + * or {@code null} if no matching factory is found + */ + Factory getFactory(String language); +} diff --git a/src/main/java/org/cqfn/astranaut/core/utils/JsonDeserializer.java b/src/main/java/org/cqfn/astranaut/core/utils/JsonDeserializer.java index b547f091..89594f72 100644 --- a/src/main/java/org/cqfn/astranaut/core/utils/JsonDeserializer.java +++ b/src/main/java/org/cqfn/astranaut/core/utils/JsonDeserializer.java @@ -26,7 +26,7 @@ import com.kniazkov.json.Json; import com.kniazkov.json.JsonException; import org.cqfn.astranaut.core.base.EmptyTree; -import org.cqfn.astranaut.core.base.Factory; +import org.cqfn.astranaut.core.base.FactoryProvider; import org.cqfn.astranaut.core.base.Tree; import org.cqfn.astranaut.core.utils.deserializer.TreeDescriptor; @@ -42,18 +42,18 @@ public final class JsonDeserializer { private final String source; /** - * The factory selector. + * The factory provider. */ - private final FactorySelector selector; + private final FactoryProvider provider; /** * Constructor. * @param source String that contains JSON object - * @param selector The factory selector + * @param provider The factory provider */ - public JsonDeserializer(final String source, final FactorySelector selector) { + public JsonDeserializer(final String source, final FactoryProvider provider) { this.source = source; - this.selector = selector; + this.provider = provider; } /** @@ -65,26 +65,10 @@ public Tree convert() { try { final TreeDescriptor tree = Json.parse(this.source, TreeDescriptor.class); if (tree != null) { - result = tree.convert(this.selector); + result = tree.convert(this.provider); } } catch (final JsonException ignored) { } return result; } - - /** - * Interface for factories selection that selects a suitable factory - * for the specified programming language. - * In other words, it's a factory of factories. - * - * @since 1.0.2 - */ - public interface FactorySelector { - /** - * Selects a suitable factory for the specified programming language. - * @param language The language name - * @return A suitable factory - */ - Factory select(String language); - } } diff --git a/src/main/java/org/cqfn/astranaut/core/utils/deserializer/TreeDescriptor.java b/src/main/java/org/cqfn/astranaut/core/utils/deserializer/TreeDescriptor.java index 98106f24..0bc44517 100644 --- a/src/main/java/org/cqfn/astranaut/core/utils/deserializer/TreeDescriptor.java +++ b/src/main/java/org/cqfn/astranaut/core/utils/deserializer/TreeDescriptor.java @@ -30,9 +30,9 @@ import org.cqfn.astranaut.core.base.DiffTree; import org.cqfn.astranaut.core.base.EmptyTree; import org.cqfn.astranaut.core.base.Factory; +import org.cqfn.astranaut.core.base.FactoryProvider; import org.cqfn.astranaut.core.base.Node; import org.cqfn.astranaut.core.base.Tree; -import org.cqfn.astranaut.core.utils.JsonDeserializer; /** * Tree descriptor represented as it is stored in the JSON file. @@ -52,12 +52,12 @@ public class TreeDescriptor { /** * Converts tree into node. - * @param selector The node factory selector + * @param provider The node factory provider * @return A root node */ - public Tree convert(final JsonDeserializer.FactorySelector selector) { + public Tree convert(final FactoryProvider provider) { Tree result = EmptyTree.INSTANCE; - final Factory factory = selector.select(this.language); + final Factory factory = provider.getFactory(this.language); do { if (factory == null) { break;