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

Etn019 container #128

Open
wants to merge 6 commits into
base: master
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
1 change: 1 addition & 0 deletions OWNER.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fakhrnasov Artyom
111 changes: 101 additions & 10 deletions src/main/java/track/container/Container.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,132 @@
package track.container;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import track.container.config.Bean;
import track.container.config.Property;
import track.container.config.ValueType;

import java.lang.reflect.Field;
import java.util.*;

/**
* Основной класс контейнера
* У него определено 2 публичных метода, можете дописывать свои методы и конструкторы
*/
public class Container {

Map<String, Object> map = new HashMap<>();
private Map<String, Bean> map = new HashMap<>();
private Map<String, Object> objById = new HashMap<>();

// Реализуйте этот конструктор, используется в тестах!
public Container(List<Bean> beans) throws Exception {

for (Bean bean : beans) {
map.put(bean.getId(), bean);
}
}

public static void main(String[] args) throws Exception {

}

private Object createObject(Bean bean) throws Exception {
String className = bean.getClassName();
String id = bean.getId();
Map<String, Property> properties = bean.getProperties();
Class cl = Class.forName(className);

Object obj = cl.newInstance();

for (Map.Entry<String, Property> entry : properties.entrySet()) {
Property property = entry.getValue();
Field field = cl.getDeclaredField(property.getName());
field.setAccessible(true);

if (property.getType() == ValueType.VAL) {
setFieldValue(obj, field, property);
} else {
field.set(obj, getById(property.getValue()));
}
}
objById.put(id, obj);
return obj;
}

private void setFieldValue(Object object, Field field, Property property) throws IllegalAccessException {
boolean fieldIsSet = false;

if (field.getType() == Integer.TYPE) {
field.setInt(object, Integer.parseInt(property.getValue()));
fieldIsSet = true;
}
if (field.getType() == Double.TYPE) {
field.setDouble(object, Double.parseDouble(property.getValue()));
fieldIsSet = true;
}
if (field.getType() == Short.TYPE) {
field.setShort(object, Short.parseShort(property.getValue()));
fieldIsSet = true;
}
if (field.getType() == Long.TYPE) {
field.setLong(object, Long.parseLong(property.getValue()));
fieldIsSet = true;
}
if (field.getType() == Boolean.TYPE) {
field.setBoolean(object, Boolean.parseBoolean(property.getValue()));
fieldIsSet = true;
}
if (field.getType() == Float.TYPE) {
field.setFloat(object, Float.parseFloat(property.getValue()));
fieldIsSet = true;
}
if (field.getType() == Byte.TYPE) {
field.setByte(object, Byte.parseByte(property.getValue()));
fieldIsSet = true;
}
if (!fieldIsSet) {
field.set(object, property.getValue());
}
}

/**
* Вернуть объект по имени бина из конфига
* Например, Car car = (Car) container.getById("carBean")
*/
public Object getById(String id) {
return null;
public Object getById(String id) throws Exception {

if (objById.containsKey(id)) {
return objById.get(id);

} else if (map.containsKey(id)) {
return createObject(map.get(id));
}
throw new NoSuchElementException();
}

/**
* Вернуть объект по имени класса
* Например, Car car = (Car) container.getByClass("track.container.beans.Car")
*/
public Object getByClass(String className) {
return null;
public Object getByClass(String className) throws Exception {
String id = null;
Iterator<Bean> beanIterator = map.values().iterator();

while (beanIterator.hasNext()) {
Bean current = beanIterator.next();
if (current.getClassName().equals(className)) {
id = current.getId();
break;
}
}
if (id != null) {
return getById(id);
}
throw new NoSuchElementException();
}

public void printMap() {
Iterator<String> iterator = objById.keySet().iterator();
while (iterator.hasNext()) {
String currentId = iterator.next();
System.out.println(currentId + " : " + objById.get(currentId));
}
}
}
20 changes: 16 additions & 4 deletions src/main/java/track/container/JsonConfigReader.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
package track.container;

import java.io.File;
import java.io.IOException;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import track.container.config.Bean;
import track.container.config.ConfigReader;
import track.container.config.InvalidConfigurationException;
import track.container.config.Root;

/**
* TODO: Реализовать
*/
public class JsonConfigReader implements ConfigReader {

@Override
public List<Bean> parseBeans(File configFile) throws InvalidConfigurationException {
return null;

ObjectMapper mapper = new ObjectMapper();
Root root = null;

try {
root = mapper.readValue(configFile, Root.class);
} catch (IOException e) {
System.out.println("IOException caught");
}
if (root != null) {
return root.getBeans();
}
throw new InvalidConfigurationException("Invalid Config");
}
}
36 changes: 22 additions & 14 deletions src/main/java/track/container/Main.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,34 @@
package track.container;

import track.container.beans.Car;
import track.container.config.ConfigReader;

import java.io.File;

/**
*
*/
public class Main {

public static void main(String[] args) {

/*

ПРИМЕР ИСПОЛЬЗОВАНИЯ

*/

// // При чтении нужно обработать исключение
// ConfigReader reader = new JsonReader();
// List<Bean> beans = reader.parseBeans("config.json");
// Container container = new Container(beans);
//
// Car car = (Car) container.getByClass("track.container.beans.Car");
// car = (Car) container.getById("carBean");
public static class MyClass {
private String theField;
}

public static void main(String[] args) {

ConfigReader reader = new JsonConfigReader();
try {
Container container = new Container(reader.parseBeans(new File("src/main/resources/config.json")));

Car car = (Car) container.getByClass("track.container.beans.Car");
System.out.println(car);
Car car1 = (Car) container.getById("carBean");
System.out.println(car1);
container.printMap();
} catch (Exception e) {
e.printStackTrace();
}
}
}

4 changes: 4 additions & 0 deletions src/main/java/track/container/config/Bean.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public Bean(String id, String className, Map<String, Property> properties) {
this.properties = properties;
}

public Bean() {

}

public Map<String, Property> getProperties() {
return properties;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/track/container/config/Property.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ public Property(String name, String value, ValueType type) {
this.type = type;
}

public Property() {

}

public String getName() {
return name;
}
Expand Down