-
Notifications
You must be signed in to change notification settings - Fork 44
Gera container #104
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
LordOfPhys
wants to merge
6
commits into
arhangeldim:master
Choose a base branch
from
LordOfPhys:gera-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
Gera container #104
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9d98cf0
Added
LordOfPhys 523d61e
Merge remote-tracking branch 'upstream/master'
LordOfPhys 97ded7e
Merge remote-tracking branch 'upstream/master'
LordOfPhys 09b89b9
First Attempt
LordOfPhys d8a4650
Fixed style
LordOfPhys 5e9ac43
Again add ALL HOMEWORKS!
LordOfPhys 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 @@ | ||
| Anton Gerasimenko Vyacheslavovich |
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,86 @@ | ||
| package track.container; | ||
|
|
||
| import java.util.List; | ||
| import java.lang.reflect.Field; | ||
| import java.util.*; | ||
|
|
||
| import track.container.config.Bean; | ||
| import track.container.config.InvalidConfigurationException; | ||
| import track.container.config.Property; | ||
| import track.container.config.ValueType; | ||
| import track.container.config.Bean; | ||
| import track.container.config.InvalidConfigurationException; | ||
|
|
||
| /** | ||
| * Основной класс контейнера | ||
| * У него определено 2 публичных метода, можете дописывать свои методы и конструкторы | ||
| */ | ||
| public class Container { | ||
|
|
||
| private Map<String, Bean> beanId; | ||
| private Map<String, Bean> beanClass; | ||
| private Map<String, Object> objByName; | ||
| private Map<String, Object> objByClassName; | ||
|
|
||
| // Реализуйте этот конструктор, используется в тестах! | ||
| public Container(List<Bean> beans) { | ||
| beanId = new HashMap<>(); | ||
| beanClass = new HashMap<>(); | ||
| objByClassName = new HashMap<>(); | ||
| objByName = new HashMap<>(); | ||
| for (Bean bean : beans) { | ||
| beanId.put(bean.getId(), bean); | ||
| beanClass.put(bean.getClassName(), bean); | ||
| } | ||
| } | ||
|
|
||
| public Object getById(String id) throws IllegalAccessException, InstantiationException, NoSuchFieldException, InvalidConfigurationException, ClassNotFoundException { | ||
| if (objByName.containsKey(id)) { | ||
| return objByName.get(id); | ||
| } else { | ||
| if (beanId.get(id) == null) { | ||
| throw new InvalidConfigurationException("No id"); | ||
| } | ||
| Class clazz = Class.forName(beanId.get(id).getClassName()); | ||
| Object obj = clazz.newInstance(); | ||
| for (Map.Entry<String, Property> entry : beanClass.get(id).getProperties().entrySet()) { | ||
| Property prop = entry.getValue(); | ||
| Field field = clazz.getDeclaredField(prop.getName()); | ||
| field.setAccessible(true); | ||
| if (prop.getType().equals(ValueType.VAL)) { | ||
| Object value = Integer.parseInt(prop.getValue()); | ||
| field.set(obj, value); | ||
| } else { | ||
| if (prop.getType().equals(ValueType.REF)) { | ||
| String className = beanId.get(prop.getValue()).getClassName(); | ||
| if (!objByClassName.containsKey(className)) { | ||
| Class classN = Class.forName(className); | ||
| if (isDepend(beanId.get(id).getClassName(), classN.getDeclaredFields())) { | ||
| throw new InvalidConfigurationException("No dependency"); | ||
| } else { | ||
| Object value = getByClass(className); | ||
| field.set(obj, value); | ||
| } | ||
| } else { | ||
| field.set(obj, objByClassName.get(className)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| objByClassName.put(beanId.get(id).getClassName(), obj); | ||
| objByName.put(beanId.get(id).getId(), obj); | ||
| return obj; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Вернуть объект по имени бина из конфига | ||
| * Например, Car car = (Car) container.getById("carBean") | ||
| */ | ||
| public Object getById(String id) { | ||
| return null; | ||
| public boolean isDepend(String classN, Field[] declaredF) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. метод лучше сделать приватным. доступ к нему снаружи не нужен |
||
| for (Field field : declaredF) { | ||
| if (field.getType().getName().equals(classN)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Вернуть объект по имени класса | ||
| * Например, Car car = (Car) container.getByClass("track.container.beans.Car") | ||
| */ | ||
| public Object getByClass(String className) { | ||
| return null; | ||
| public Object getByClass(String className) throws ClassNotFoundException, NoSuchFieldException, InstantiationException, InvalidConfigurationException, IllegalAccessException { | ||
| if (objByClassName.containsKey(className)) { | ||
| return objByClassName.get(className); | ||
| } else { | ||
| return getById(beanClass.get(className).getId()); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. здесь тест падает с NPE. нужна проверка на существование бина |
||
| } | ||
| } | ||
| } | ||
| } | ||
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,50 @@ | ||
| package track.container; | ||
|
|
||
| import java.io.File; | ||
| import java.util.List; | ||
| import java.io.IOException; | ||
| import java.util.*; | ||
|
|
||
| import track.container.config.Bean; | ||
| import track.container.config.ConfigReader; | ||
| import track.container.config.InvalidConfigurationException; | ||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||
| import com.fasterxml.jackson.databind.JsonNode; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import track.container.config.*; | ||
|
|
||
| /** | ||
| * TODO: Реализовать | ||
| */ | ||
| public class JsonConfigReader implements ConfigReader { | ||
|
|
||
| @Override | ||
| public List<Bean> parseBeans(File configFile) throws InvalidConfigurationException { | ||
| return null; | ||
| ObjectMapper mapper = new ObjectMapper(); | ||
| List<Bean> bean = new ArrayList<>(); | ||
| try { | ||
| JsonNode root = mapper.readTree(configFile).path("bean"); | ||
| for (JsonNode indexNode : root) { | ||
| String id = indexNode.path("id").asText(); | ||
| String className = indexNode.path("className").asText(); | ||
| Map<String, Property> properties = createProperty(indexNode.path("properties")); | ||
| bean.add(new Bean(id, className, properties)); | ||
| } | ||
| return bean; | ||
| } catch (IOException e) { | ||
| throw new InvalidConfigurationException("Invalid config"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public Map<String, Property> createProperty(JsonNode jsonNode) throws InvalidConfigurationException { | ||
| Map<String, Property> properties = new HashMap<>(); | ||
| for (JsonNode indexNode : jsonNode) { | ||
| String name = indexNode.path("name").asText(); | ||
| Property property = null; | ||
| if (indexNode.has("ref")) { | ||
| property = new Property(name, indexNode.path("ref").asText(), ValueType.REF); | ||
| } else { | ||
| if (indexNode.has("val")) { | ||
| property = new Property(name, indexNode.path("val").asText(), ValueType.VAL); | ||
| } else { | ||
| throw new InvalidConfigurationException("Not value"); | ||
| } | ||
| } | ||
| properties.put(name, property); | ||
| } | ||
| return properties; | ||
| } | ||
| } |
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,26 +1,26 @@ | ||
| package track.container; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| import track.container.beans.Car; | ||
| import track.container.config.Bean; | ||
| import track.container.config.ConfigReader; | ||
| import track.container.config.InvalidConfigurationException; | ||
| import java.io.File; | ||
| import java.util.List; | ||
|
|
||
| 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"); | ||
|
|
||
|
|
||
| ConfigReader reader = new JsonConfigReader(); | ||
| try { | ||
| File file = new File(JsonConfigReader.class.getClassLoader().getResource("config.json").getFile()); | ||
| List<Bean> bean = reader.parseBeans(file); | ||
| System.out.println(bean); | ||
| Container container = new Container(bean); | ||
| Car car = (Car) container.getByClass("track.container.beans.Car"); | ||
| car = (Car) container.getById("carBean"); | ||
| System.out.println(car); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
| } | ||
| } |
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,23 @@ | ||
| package track.lessons.lesson2; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| import java.util.Arrays; | ||
|
|
||
| public class Document { | ||
| String[] tokens; | ||
|
|
||
| public Document(String[] tokens) { | ||
| this.tokens = tokens; | ||
| } | ||
|
|
||
| String[] getTokens() { | ||
| return null; | ||
| return tokens; | ||
| } | ||
|
|
||
| int getTokenCount() { | ||
| return 0; | ||
| return tokens.length; | ||
| } | ||
|
|
||
| boolean hasToken(String token) { | ||
| return false; | ||
| return Arrays.asList(tokens).contains(token); | ||
| } | ||
| } | ||
| } |
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,7 +1,64 @@ | ||
| package track.lessons.lesson3; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public class DynamicList { | ||
| } | ||
| public class DynamicList extends List { | ||
|
|
||
| private static int defaultsize = 10; | ||
| private int[] array; | ||
| private int size; | ||
|
|
||
| public DynamicList(int size) { | ||
| if (size > 0) { | ||
| array = new int[size]; | ||
| } else { | ||
| array = new int[defaultsize]; | ||
| } | ||
| this.size = 0; | ||
| } | ||
|
|
||
| public DynamicList() { | ||
| array = new int[defaultsize]; | ||
| size = 0; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| void add(int item) { | ||
| if (size == array.length) { | ||
| int[] newArray = new int[array.length * 2]; | ||
| System.arraycopy(array, 0, newArray, 0, size); | ||
| array = newArray; | ||
| } | ||
| array[size++] = item; | ||
| } | ||
|
|
||
| @Override | ||
| int remove(int idx) { | ||
| int value; | ||
| if (!isRange(idx)) { | ||
| value = array[idx]; | ||
| System.arraycopy(array, idx + 1, array, idx, size - idx - 1); | ||
| size--; | ||
| } else { | ||
| value = -1; | ||
| } | ||
| return value; | ||
| } | ||
|
|
||
| private boolean isRange(int idx) { | ||
| return idx < 0 || idx > size - 1; | ||
| } | ||
|
|
||
| @Override | ||
| int get(int idx) { | ||
| if (!isRange(idx)) { | ||
| return array[idx]; | ||
| } else { | ||
| throw new IndexOutOfBoundsException("Index is incorrect"); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| int size() { | ||
| return size; | ||
| } | ||
| } |
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.
В задании указано, что значения полей в объекте выставлять через методы setValue()