Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

First solution, basic tasks solved #1014

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/java/mate.academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ public class Main {
public static void main(String[] args) {
// Please test your Injector here. Feel free to push this class as a part of your solution
Injector injector = Injector.getInjector();
noiseintheattic marked this conversation as resolved.
Show resolved Hide resolved
ProductService productService = null;
ProductService productService = (ProductService) injector.getInstance(ProductService.class);
noiseintheattic marked this conversation as resolved.
Show resolved Hide resolved

// Checking the singleton pattern (on AI mentor request)
Injector injectorSecond = Injector.getInjector();
if (!injector.equals(injectorSecond)) {
throw new RuntimeException("The singleton pattern doesn't work correctly");
}
Comment on lines 11 to +19

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not necessary to test the singleton pattern functionality within the main method of the application. This should be covered by dedicated unit tests.


List<Product> products = productService.getAllFromFile("products.txt");
products.forEach(System.out::println);
}
Expand Down
8 changes: 7 additions & 1 deletion src/main/java/mate.academy/lib/Component.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package mate.academy.lib;

public @interface Component {
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Component {
noiseintheattic marked this conversation as resolved.
Show resolved Hide resolved
}
5 changes: 4 additions & 1 deletion src/main/java/mate.academy/lib/Inject.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package mate.academy.lib;

public @interface Inject {
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {
}
56 changes: 55 additions & 1 deletion src/main/java/mate.academy/lib/Injector.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,67 @@
package mate.academy.lib;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import mate.academy.service.impl.ImplementationFinder;

public class Injector {
private static final Injector injector = new Injector();
private final Map<Class<?>, Object> instances = new HashMap<>();
noiseintheattic marked this conversation as resolved.
Show resolved Hide resolved

public static Injector getInjector() {
return injector;
}

public Object getInstance(Class<?> interfaceClazz) {
return null;
if (interfaceClazz.isAnnotationPresent(Component.class)) {
Object clazzImplementationInstance = null;
Class<?> clazz = findImplementation(interfaceClazz);
Field[] declaredFields = clazz.getDeclaredFields();
if (clazzImplementationInstance == null) {
clazzImplementationInstance = createNewInstance(clazz);
}
clazzImplementationInstance = createNewInstance(clazz);
for (Field field : declaredFields) {
if (field.isAnnotationPresent(Inject.class)) {
Object fieldInstance = getInstance(field.getType());
field.setAccessible(true);
try {
field.set(clazzImplementationInstance, fieldInstance);
} catch (IllegalAccessException e) {
throw new RuntimeException("Can't initialize field value. Class: "
+ clazz.getName() + ". Field: " + field.getName(), e);
}
}
}
return clazzImplementationInstance;
} else {
throw new RuntimeException("This instance of "
+ interfaceClazz + " class, cannot be created, because "
+ "class has no annotation '@Component'");
noiseintheattic marked this conversation as resolved.
Show resolved Hide resolved
}
}

private Object createNewInstance(Class<?> clazz) {
noiseintheattic marked this conversation as resolved.
Show resolved Hide resolved
if (instances.containsKey(clazz)) {
return instances.get(clazz);
}
Constructor<?> constructor = null;
try {
constructor = clazz.getConstructor();
Object object = constructor.newInstance();
instances.put(clazz, object);
return object;
} catch (ReflectiveOperationException e) {
noiseintheattic marked this conversation as resolved.
Show resolved Hide resolved
throw new RuntimeException("Can't create new instance of: " + clazz.getName(), e);
}
}

private Class<?> findImplementation(Class<?> interfaceClazz) {
if (interfaceClazz.isInterface()) {
return ImplementationFinder.getImplementation(interfaceClazz);
}
return interfaceClazz;
}
}
2 changes: 2 additions & 0 deletions src/main/java/mate.academy/service/FileReaderService.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package mate.academy.service;

import java.util.List;
import mate.academy.lib.Component;

@Component
public interface FileReaderService {
List<String> readFile(String fileName);
}
2 changes: 2 additions & 0 deletions src/main/java/mate.academy/service/ProductParser.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package mate.academy.service;

import mate.academy.lib.Component;
import mate.academy.model.Product;

@Component
public interface ProductParser {
/**
* Parse the productInfo and return created {@link Product} object.
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/mate.academy/service/ProductService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package mate.academy.service;

import java.util.List;
import mate.academy.lib.Component;
import mate.academy.model.Product;

@Component
public interface ProductService {
List<Product> getAllFromFile(String filePath);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
import java.io.IOException;
import java.nio.file.Files;
import java.util.List;
import mate.academy.lib.Component;
import mate.academy.service.FileReaderService;

@Component
public class FileReaderServiceImpl implements FileReaderService {
@Override
public List<String> readFile(String fileName) {
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/mate.academy/service/impl/ImplementationFinder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package mate.academy.service.impl;

import java.util.Map;
import mate.academy.service.FileReaderService;
import mate.academy.service.ProductParser;
import mate.academy.service.ProductService;

public class ImplementationFinder {
private static final Map<Class<?>, Class<?>> IMPLEMENTATION_MAP = Map.of(
FileReaderService.class, FileReaderServiceImpl.class,
ProductParser.class, ProductParserImpl.class,
ProductService.class, ProductServiceImpl.class
);

public static Class<?> getImplementation(Class<?> clazz) {
return IMPLEMENTATION_MAP.get(clazz);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package mate.academy.service.impl;

import java.math.BigDecimal;
import mate.academy.lib.Component;
import mate.academy.model.Product;
import mate.academy.service.ProductParser;

@Component
public class ProductParserImpl implements ProductParser {
public static final int ID_POSITION = 0;
public static final int NAME_POSITION = 1;
Expand Down
14 changes: 10 additions & 4 deletions src/main/java/mate.academy/service/impl/ProductServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,26 @@

import java.util.List;
import java.util.stream.Collectors;
import mate.academy.lib.Component;
import mate.academy.lib.Inject;
import mate.academy.model.Product;
import mate.academy.service.FileReaderService;
import mate.academy.service.ProductParser;
import mate.academy.service.ProductService;

@Component
public class ProductServiceImpl implements ProductService {
private ProductParser productParser;
private FileReaderService fileReaderService;
@Inject
private ProductParser txtProductParser;

@Inject
private FileReaderService txtFileReaderService;

@Override
public List<Product> getAllFromFile(String filePath) {
return fileReaderService.readFile(filePath)
return txtFileReaderService.readFile(filePath)
.stream()
.map(productParser::parse)
.map(txtProductParser::parse)
.collect(Collectors.toList());
}
}
Loading