-
Notifications
You must be signed in to change notification settings - Fork 44
container #110
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
Open
ivan371
wants to merge
39
commits into
arhangeldim:master
Choose a base branch
from
ivan371:ivan371-container
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
container #110
Changes from all commits
Commits
Show all changes
39 commits
Select commit
Hold shift + click to select a range
12e24f6
Added OWNER
ivan371 e1af903
Document + Parser
ivan371 cb7b354
remove
ivan371 e131560
repair
ivan371 64e3552
repair
ivan371 79c6f5f
tabs
ivan371 2aff30f
tabs
ivan371 b3255d3
whitespace
ivan371 f1cfc7d
list
ivan371 5b1a81c
list
ivan371 3016df5
next
ivan371 4f0bfe1
list
ivan371 86db17c
merge
ivan371 b21843f
delete errors
ivan371 2891314
white
ivan371 44849f8
white
ivan371 8914837
white
ivan371 ca42dfa
Parser
ivan371 c66cd94
maven
ivan371 c52c6c0
Merge remote-tracking branch 'upstream/master'
ivan371 4a29ed9
maven
ivan371 5ef8985
maven
ivan371 7907bc3
Merge remote-tracking branch 'upstream/master'
ivan371 752983d
container
ivan371 a128448
container
ivan371 9957554
container
ivan371 cc2e8fa
container with right whitespaces
ivan371 b5a84a4
bean
ivan371 8349845
jsonparser
ivan371 e4169de
} again!
ivan371 b84c247
test
ivan371 59aaecb
test
ivan371 7f69f63
test
ivan371 b14f8b3
test
ivan371 4a2901f
test
ivan371 bd881f1
test
ivan371 407ca5d
map and id
ivan371 b23690c
map and id and relax
ivan371 c9f5274
no self test
ivan371 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Nagaiko Ivan |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) { | ||
| 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); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } | ||
|
|
||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
а если сеттера нет, то не проставим значение у поля?