Skip to content

Commit

Permalink
Factory provider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
kniazkov committed Nov 12, 2024
1 parent abb55a7 commit a7815be
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 27 deletions.
39 changes: 39 additions & 0 deletions src/main/java/org/cqfn/astranaut/core/base/FactoryProvider.java
Original file line number Diff line number Diff line change
@@ -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);
}
30 changes: 7 additions & 23 deletions src/main/java/org/cqfn/astranaut/core/utils/JsonDeserializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}

/**
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand Down

0 comments on commit a7815be

Please sign in to comment.