Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
12e24f6
Added OWNER
ivan371 Oct 6, 2016
e1af903
Document + Parser
ivan371 Oct 6, 2016
cb7b354
remove
ivan371 Oct 6, 2016
e131560
repair
ivan371 Oct 6, 2016
64e3552
repair
ivan371 Oct 6, 2016
79c6f5f
tabs
ivan371 Oct 6, 2016
2aff30f
tabs
ivan371 Oct 6, 2016
b3255d3
whitespace
ivan371 Oct 6, 2016
f1cfc7d
list
ivan371 Oct 12, 2016
5b1a81c
list
ivan371 Oct 12, 2016
3016df5
next
ivan371 Oct 13, 2016
4f0bfe1
list
ivan371 Oct 13, 2016
86db17c
merge
ivan371 Oct 13, 2016
b21843f
delete errors
ivan371 Oct 15, 2016
2891314
white
ivan371 Oct 15, 2016
44849f8
white
ivan371 Oct 15, 2016
8914837
white
ivan371 Oct 15, 2016
ca42dfa
Parser
ivan371 Oct 15, 2016
c66cd94
maven
ivan371 Oct 19, 2016
c52c6c0
Merge remote-tracking branch 'upstream/master'
ivan371 Oct 19, 2016
4a29ed9
maven
ivan371 Oct 19, 2016
5ef8985
maven
ivan371 Oct 19, 2016
7907bc3
Merge remote-tracking branch 'upstream/master'
ivan371 Nov 3, 2016
752983d
container
ivan371 Nov 3, 2016
a128448
container
ivan371 Nov 3, 2016
9957554
container
ivan371 Nov 3, 2016
cc2e8fa
container with right whitespaces
ivan371 Nov 3, 2016
b5a84a4
bean
ivan371 Nov 3, 2016
8349845
jsonparser
ivan371 Nov 3, 2016
e4169de
} again!
ivan371 Nov 3, 2016
b84c247
test
ivan371 Nov 3, 2016
59aaecb
test
ivan371 Nov 3, 2016
7f69f63
test
ivan371 Nov 3, 2016
b14f8b3
test
ivan371 Nov 3, 2016
4a2901f
test
ivan371 Nov 3, 2016
bd881f1
test
ivan371 Nov 3, 2016
407ca5d
map and id
ivan371 Nov 3, 2016
b23690c
map and id and relax
ivan371 Nov 3, 2016
c9f5274
no self test
ivan371 Nov 3, 2016
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 @@
Nagaiko Ivan
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,11 @@
<artifactId>jackson-databind</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>


</dependencies>
Expand Down
79 changes: 74 additions & 5 deletions src/main/java/track/container/Container.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,103 @@
package track.container;

import java.beans.PropertyEditor;
import java.beans.PropertyEditorManager;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException;
import track.container.config.Bean;
import track.container.config.InvalidConfigurationException;
import track.container.config.Property;
import track.container.config.ValueType;

import static org.apache.log4j.config.PropertyPrinter.capitalize;

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

private Map<String, Bean> beanById;
private Map<String, Bean> beanByClassName;
private Map<Bean, Object> objById;

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

beanById = new HashMap<>();
beanByClassName = new HashMap<>();
objById = new HashMap<>();
for (Bean bean : beans) {
beanById.put(bean.getId(), bean);
beanByClassName.put(bean.getClassName(), bean);
}
}

/**
* Вернуть объект по имени бина из конфига
* Например, Car car = (Car) container.getById("carBean")
*/
public Object getById(String id) {
return null;
public Object getById(String id) throws InvalidConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
Object object = objById.get(id);
if (object == null) {
Bean bean = beanById.get(id);
if (bean != null) {
object = createObject(bean);
} else {
throw new InvalidConfigurationException("Bean without id");
}
}

return object;
}

/**
* Вернуть объект по имени класса
* Например, Car car = (Car) container.getByClass("track.container.beans.Car")
*/
public Object getByClass(String className) {
return null;
public Object getByClass(String className) throws InvalidConfigurationException, ClassNotFoundException, InstantiationException, IllegalAccessException, NoSuchFieldException, NoSuchMethodException, InvocationTargetException {
Object object = objById.get(className);

if (object == null) {
Bean bean = beanByClassName.get(className);
if (bean != null) {
object = createObject(bean);
} else {
throw new InvalidConfigurationException("Bean without class");
}
}

return object;
}

private Object createObject(Bean bean) throws ValueException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
Class clazz = Class.forName(bean.getClassName());
Object object = clazz.newInstance();
objById.put(bean, object);
for (Map.Entry<String, Property> entry : bean.getProperties().entrySet()) {
Property property = entry.getValue();
Field field = clazz.getDeclaredField(property.getName());
Class type = field.getType();
Method set = clazz.getMethod(getSetName(property.getName()), type);
if (property.getType() == ValueType.VAL) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

а если сеттера нет, то не проставим значение у поля?

set.invoke(object, Integer.parseInt(property.getValue()));
} else if (property.getType() == ValueType.REF) {
if (!objById.containsKey(beanById.get(property.getValue()))) {
createObject(beanById.get(property.getValue()));
}
set.invoke(object, objById.get(beanById.get(property.getValue())));
}

}
return object;
}

private String getSetName(String line) {
return "set" + Character.toUpperCase(line.charAt(0)) + line.substring(1);
}
}
49 changes: 45 additions & 4 deletions src/main/java/track/container/JsonConfigReader.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,60 @@
package track.container;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

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

private Property parseProperty(JsonNode property) throws InvalidConfigurationException {
String name = property.get("name").asText();
ValueType type;
String value = property.get("value").asText();
if (property.get("type").asText().equals("REF")) {
type = ValueType.REF;
} else {
type = ValueType.VAL;
}

return new Property(name, value, type);
}

private Bean parseBean(JsonNode bean) throws InvalidConfigurationException {
String id = bean.get("id").asText();
String className = bean.get("className").asText();
Map<String, Property> properties = new HashMap();
for (JsonNode property: bean.get("properties")) {
Property parsedProperty = parseProperty(property);
properties.put(parsedProperty.getName(), parsedProperty);
}

return new Bean(id, className, properties);
}

@Override
public List<Bean> parseBeans(File configFile) throws InvalidConfigurationException {
return null;
ObjectMapper objectMapper = new ObjectMapper();
List<Bean> beans = new ArrayList<>();
try {
JsonNode root = objectMapper.readTree(configFile);
for (JsonNode bean : root.get("beans")) {
beans.add(parseBean(bean));
}
} catch (InvalidConfigurationException | IOException e) {
e.printStackTrace();
}
return beans;
}
}


35 changes: 28 additions & 7 deletions src/main/java/track/container/Main.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
package track.container;

import jdk.nashorn.internal.runtime.regexp.joni.exception.ValueException;
import org.junit.Assert;
import track.container.beans.Car;
import track.container.beans.Engine;
import track.container.beans.Gear;
import track.container.config.Bean;
import track.container.config.ConfigReader;
import track.container.config.InvalidConfigurationException;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.lang.String;

import java.io.File;

import static org.apache.log4j.helpers.Loader.getResource;

/**
*
*/
public class Main {

public static void main(String[] args) {
public static void main(String[] args) throws ValueException, NoSuchFieldException, NoSuchMethodException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException, InvalidConfigurationException {

/*

Expand All @@ -14,12 +31,16 @@ 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");
File file = new File(getResource("config.json").getFile());
ConfigReader reader = new JsonConfigReader();
try {
List<Bean> beans = reader.parseBeans(file);
System.out.print(beans.toString());

} catch (InvalidConfigurationException exception) {
System.out.println(exception.getMessage());
}



}
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/track/lessons/lesson2/Document.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
package track.lessons.lesson2;

/**
*
*/
public class Document {
String[] tokens;

Document(String[] token) {
this.tokens = token;
}

String[] getTokens() {
return null;
return tokens;
}

int getTokenCount() {
return 0;
return tokens.length;
}

boolean hasToken(String token) {
for (int i = 0; i < tokens.length; i++) {
if (token.equals(tokens[i])) {
return true;
}
}
return false;
}
}
26 changes: 26 additions & 0 deletions src/main/java/track/lessons/lesson2/Document.java~
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package track.lessons.lesson2;

public class Document {
String[] tokens;

Document(String[] token) {
this.tokens = token;
}

String[] getTokens() {
return tokens;
}

int getTokenCount() {
return tokens.length;
}

boolean hasToken(String token) {
for(int i = 0; i < tokens.length; i++) {
if (token.equals(tokens[i])) {
return true;
}
}
return false;
}
}
26 changes: 14 additions & 12 deletions src/main/java/track/lessons/lesson2/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,27 @@
import java.io.BufferedReader;
import java.io.FileReader;

/**
*
*/
public class Parser {

Document parse(String data) {
return null;
if (data == null) {
return null;
}
String[] tokens = data.split("[ ]");
return new Document(tokens);
}

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

String path = "path/to/file";
BufferedReader reader = new BufferedReader(new FileReader(path));
// reader умеет читать по строкам с помощью метода readLine()

// Создайте объект Parser

// Получите объект Document, реализовав метод parse()


StringBuilder builder = new StringBuilder();
Parser pars = new Parser();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
builder.append(" ");
}
String res = builder.toString();
Document doc = pars.parse(res);
}
}
28 changes: 28 additions & 0 deletions src/main/java/track/lessons/lesson2/Parser.java~
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package track.lessons.lesson2;

import java.io.BufferedReader;
import java.io.FileReader;

public class Parser {
Document parse(String data) {
String[] tokens = data.split("[ ]");
if (tokens == null) {
return null;
}
return new Document(tokens);
}

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

String path = "path/to/file";
BufferedReader reader = new BufferedReader(new FileReader(path));
StringBuilder builder = new StringBuilder();
Parser pars = new Parser();
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
String res = builder.toString();
Document doc = pars.parse(res);
}
}
Loading