Skip to content

Commit

Permalink
Solution
Browse files Browse the repository at this point in the history
  • Loading branch information
SergeiZhuk committed Jul 23, 2023
1 parent 7c2827f commit b5f8779
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 2 deletions.
3 changes: 1 addition & 2 deletions src/main/java/mate.academy/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
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();
ProductService productService = null;
ProductService productService = (ProductService) injector.getInstance(ProductService.class);
List<Product> products = productService.getAllFromFile("products.txt");
products.forEach(System.out::println);
}
Expand Down
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
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

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 {
@Inject
private ProductParser productParser;
@Inject
private FileReaderService fileReaderService;

@Override
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/mate/academy/lib/Component.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mate.academy.lib;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Component {

}
9 changes: 9 additions & 0 deletions src/main/java/mate/academy/lib/Inject.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mate.academy.lib;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Inject {

}
74 changes: 74 additions & 0 deletions src/main/java/mate/academy/lib/Injector.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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.FileReaderService;
import mate.academy.service.ProductParser;
import mate.academy.service.ProductService;
import mate.academy.service.impl.FileReaderServiceImpl;
import mate.academy.service.impl.ProductParserImpl;
import mate.academy.service.impl.ProductServiceImpl;

public class Injector {
private static final Injector injector = new Injector();
private Map<Class<?>, Object> instances = new HashMap<>();

public static Injector getInjector() {
return injector;
}

public Object getInstance(Class<?> interfaceClazz) {
Object clazzImplementationInstance = null;
Class<?> clazz = findImplementation(interfaceClazz);
Field[] declaredFields = clazz.getDeclaredFields();
if (!clazz.isAnnotationPresent(Component.class)) {
throw new RuntimeException("Injection failed, missing @Component "
+ "annotation on the class "
+ interfaceClazz.getName());
}
for (Field field : declaredFields) {
if (field.isAnnotationPresent(Inject.class)) {
Object fieldInstance = getInstance(field.getType());
clazzImplementationInstance = createNewInstance(clazz);
try {
field.setAccessible(true);
field.set(clazzImplementationInstance, fieldInstance);
} catch (IllegalAccessException e) {
throw new RuntimeException("Can`t initialize field "
+ field.getName() + " in " + clazz.getName(), e);
}
}
}
if (clazzImplementationInstance == null) {
clazzImplementationInstance = createNewInstance(clazz);
}
return clazzImplementationInstance;
}

private Object createNewInstance(Class<?> clazz) {
if (instances.containsKey(clazz)) {
return instances.get(clazz);
}
try {
Constructor<?> constructor = clazz.getConstructor();
Object instance = constructor.newInstance();
instances.put(clazz, instance);
return instance;
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Can`t create new instance of " + clazz.getName(), e);
}
}

private Class<?> findImplementation(Class<?> interfaceClazz) {
Map<Class<?>, Class<?>> interfaceImplementations = new HashMap<>();
interfaceImplementations.put(ProductParser.class, ProductParserImpl.class);
interfaceImplementations.put(FileReaderService.class, FileReaderServiceImpl.class);
interfaceImplementations.put(ProductService.class, ProductServiceImpl.class);
if (interfaceClazz.isInterface()) {
return interfaceImplementations.get(interfaceClazz);
}
return interfaceClazz;
}
}

0 comments on commit b5f8779

Please sign in to comment.