-
Notifications
You must be signed in to change notification settings - Fork 44
Egiby container #108
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
egiby
wants to merge
16
commits into
arhangeldim:master
Choose a base branch
from
egiby:egiby-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
Egiby container #108
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
35f05b4
first solution
f7b6e85
test added
424b7c5
style
9a7d8aa
owner added
c9e5579
small fix
1ea3b0c
no changes
93ec606
Merge remote-tracking branch 'upstream/master'
5e2ac58
list developed
610c763
style fix
faaeaf5
fixed some bugs
4482e6a
init
17d56bc
Merge remote-tracking branch 'upstream/master'
51d0c14
Merge branch 'egiby-container'
f7149f5
first working version
ef2bc7c
merge conflicts fixed
fdb014f
parsing of primitive types added
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 |
|---|---|---|
|
|
@@ -2,3 +2,4 @@ target/ | |
| classes/ | ||
| *.iml | ||
| .idea | ||
| test | ||
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 @@ | ||
| Andrey Upshinskiy |
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,106 @@ | ||
| package track.container; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import track.container.config.Bean; | ||
| import track.container.config.InvalidConfigurationException; | ||
| import track.container.config.Property; | ||
|
|
||
| import java.beans.PropertyEditor; | ||
| import java.beans.PropertyEditorManager; | ||
| import java.io.File; | ||
| import java.lang.reflect.Field; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * Основной класс контейнера | ||
| * У него определено 2 публичных метода, можете дописывать свои методы и конструкторы | ||
| */ | ||
| public class Container { | ||
| private Map<String, Bean> beansById = new HashMap(); | ||
| private Map<String, Bean> beansByClassName = new HashMap(); | ||
| private Map<Bean, Object> objByBean = new HashMap(); | ||
|
|
||
|
|
||
| // Реализуйте этот конструктор, используется в тестах! | ||
| public Container(List<Bean> beans) { | ||
| for (Bean bean: beans) { | ||
| this.beansById.put(bean.getId(), bean); | ||
| this.beansByClassName.put(bean.getClassName(), bean); | ||
| } | ||
| } | ||
|
|
||
| public Container(String configFileName) throws InvalidConfigurationException { | ||
| this((new JsonConfigReader()).parseBeans(new File(Main.class.getClassLoader() | ||
| .getResource(configFileName).getFile()))); | ||
| } | ||
|
|
||
| private String getSetMethodName(String fieldName) { | ||
| return "set" + Character.toUpperCase(fieldName.charAt(0)) + fieldName.substring(1); | ||
| } | ||
|
|
||
| private void createObject(Bean bean) throws ClassNotFoundException, | ||
| IllegalAccessException, InstantiationException, NoSuchFieldException, | ||
| NoSuchMethodException, InvocationTargetException { | ||
| Class clazz = Class.forName(bean.getClassName()); | ||
| Object object = clazz.newInstance(); | ||
| objByBean.put(bean, object); | ||
|
|
||
| for (Property property: bean.getProperties().values()) { | ||
| Field field = clazz.getDeclaredField(property.getName()); | ||
| Class type = field.getType(); | ||
| Method set = clazz.getMethod(getSetMethodName(property.getName()), type); | ||
|
|
||
| switch (property.getType()) { | ||
| case VAL: | ||
| PropertyEditor editor = PropertyEditorManager.findEditor(type); | ||
| editor.setAsText(property.getValue()); | ||
| set.invoke(object, editor.getValue()); | ||
| break; | ||
| case REF: | ||
| if (!objByBean.containsKey(beansById.get(property.getValue()))) { | ||
| createObject(beansById.get(property.getValue())); | ||
| } | ||
| set.invoke(object, objByBean.get(beansById.get(property.getValue()))); | ||
| break; | ||
| default: | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Object getByBean(Bean bean) throws ClassNotFoundException, | ||
| InstantiationException, IllegalAccessException, NoSuchMethodException, | ||
| NoSuchFieldException, InvocationTargetException { | ||
| if (bean == null) { | ||
| return null; | ||
| } | ||
|
|
||
| if (!objByBean.containsKey(bean)) { | ||
| createObject(bean); | ||
| } | ||
|
|
||
| return objByBean.get(bean); | ||
| } | ||
|
|
||
| /** | ||
| * Вернуть объект по имени бина из конфига | ||
| * Например, Car car = (Car) container.getById("carBean") | ||
| */ | ||
| public Object getById(String id) { | ||
| return null; | ||
| public Object getById(String id) throws ClassNotFoundException, | ||
| IllegalAccessException, InstantiationException, NoSuchMethodException, | ||
| NoSuchFieldException, InvocationTargetException { | ||
| return getByBean(beansById.get(id)); | ||
| } | ||
|
|
||
| /** | ||
| * Вернуть объект по имени класса | ||
| * Например, Car car = (Car) container.getByClass("track.container.beans.Car") | ||
| */ | ||
| public Object getByClass(String className) { | ||
| return null; | ||
| public Object getByClass(String className) throws ClassNotFoundException, | ||
| IllegalAccessException, InstantiationException, NoSuchMethodException, | ||
| NoSuchFieldException, InvocationTargetException { | ||
|
|
||
| return getByBean(beansByClassName.get(className)); | ||
| } | ||
| } | ||
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,89 @@ | ||
| package track.container; | ||
|
|
||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import track.container.config.*; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
|
|
||
| import track.container.config.Bean; | ||
| import track.container.config.ConfigReader; | ||
| import track.container.config.InvalidConfigurationException; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * TODO: Реализовать | ||
| */ | ||
| public class JsonConfigReader implements ConfigReader { | ||
|
|
||
| private Property parseProperty(JsonNode property) throws InvalidConfigurationException { | ||
| if (!property.has("name")) { | ||
| throw new InvalidConfigurationException("There is no field \"name\" in property" + | ||
| ", but it must be here"); | ||
| } | ||
| if (!(property.has("type"))) { | ||
| throw new InvalidConfigurationException("There is no field \"type\" in property" + | ||
| ", but it must be here"); | ||
| } | ||
| if (!(property.has("value"))) { | ||
| throw new InvalidConfigurationException("There is no field \"type\" in property" + | ||
| ", but it must be here"); | ||
| } | ||
|
|
||
| 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 { | ||
| if (!bean.has("id")) { | ||
| throw new InvalidConfigurationException("There is no field \"id\", but it must be here"); | ||
| } | ||
| if (!bean.has("className")) { | ||
| throw new InvalidConfigurationException("There is no field \"className\", but it must be here"); | ||
| } | ||
| if (!bean.has("properties")) { | ||
| throw new InvalidConfigurationException("There is no field \"properties\", but it must be here"); | ||
| } | ||
|
|
||
| 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(); | ||
| JsonNode root = null; | ||
| try { | ||
| root = objectMapper.readTree(configFile); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| if (!root.has("beans")) { | ||
| throw new InvalidConfigurationException("There is no field \"beans\", but it must be here"); | ||
| } | ||
| List<Bean> beans = new ArrayList(); | ||
|
|
||
| for (JsonNode bean: root.get("beans")) { | ||
| beans.add(parseBean(bean)); | ||
| } | ||
|
|
||
| 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; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public class Document { | ||
| String[] tokens; | ||
|
|
||
| Document(String[] newTokens) { | ||
| tokens = newTokens; | ||
| } | ||
|
|
||
| String[] getTokens() { | ||
| return null; | ||
| return tokens; | ||
| } | ||
|
|
||
| int getTokenCount() { | ||
| return 0; | ||
| return tokens.length; | ||
| } | ||
|
|
||
| boolean hasToken(String token) { | ||
| return false; | ||
| return Arrays.stream(tokens).anyMatch(token::equals); | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
если у поля не будет сеттера мы не проставим поле?
можно через рефлекшен напрямую выставлять значение поля