-
Notifications
You must be signed in to change notification settings - Fork 38
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
Lils2013 container #104
Open
Lils2013
wants to merge
16
commits into
tehnotrack:master
Choose a base branch
from
Lils2013:Lils2013-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
Lils2013 container #104
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
6424a37
jhb
Lils2013 5db36c8
Merge remote-tracking branch 'upstream/master'
Lils2013 1b313d3
Added OWNER
Lils2013 f0c9b1e
count words 1st solution
Lils2013 7b09c22
Merge remote-tracking branch 'upstream/master'
Lils2013 c895e7b
added string builder
Lils2013 795a257
fixed int
Lils2013 ba3ea84
Merge remote-tracking branch 'upstream/master'
Lils2013 e44bc89
Merge branch 'master' of https://github.com/Lils2013/track17-spring
Lils2013 198dd22
Merge branch 'Lils2013-countwords'
Lils2013 8485d26
solved task, added tests
Lils2013 9e7e00b
fixed style
Lils2013 4830572
fixed initial array capacity, added resizing, fixed pop/dequeue opera…
Lils2013 84e72a4
first draft
Lils2013 dff8231
solved task container
Lils2013 545b183
fixes
Lils2013 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 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 @@ | ||
Tsoy Alexander Vladimirovich, CA-11 |
This file contains 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,172 @@ | ||
package track.container; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import track.container.config.Bean; | ||
import track.container.config.Property; | ||
|
||
import static track.container.config.ValueType.REF; | ||
import static track.container.config.ValueType.VAL; | ||
|
||
|
||
/** | ||
* Основной класс контейнера | ||
* У него определено 2 публичных метода, можете дописывать свои методы и конструкторы | ||
*/ | ||
public class Container { | ||
|
||
private List<Bean> beans; | ||
private Map<String, Object> objByName; | ||
private Map<String, Object> objByClassName; | ||
|
||
// Реализуйте этот конструктор, используется в тестах! | ||
public Container(List<Bean> beans) { | ||
this.beans = beans; | ||
objByName = new HashMap<>(); | ||
objByClassName = new HashMap<>(); | ||
} | ||
|
||
public Map<String, Object> getMapByName() { | ||
return objByName; | ||
} | ||
|
||
public Map<String, Object> getMapByClass() { | ||
return objByClassName; | ||
} | ||
|
||
/** | ||
* Вернуть объект по имени бина из конфига | ||
* Например, Car car = (Car) container.getById("carBean") | ||
* Вернуть объект по имени бина из конфига | ||
* Например, Car car = (Car) container.getById("carBean") | ||
*/ | ||
public Object getById(String id) { | ||
public Object getById(String id) throws ClassNotFoundException, IllegalAccessException, | ||
InstantiationException, NoSuchMethodException, CyclicReferenceException, InvocationTargetException { | ||
for (Bean bean : beans) { | ||
String objId = bean.getId(); | ||
if (objId.equals(id)) { | ||
Object object = objByName.get(objId); | ||
if (object == null) { | ||
Class<?> cls = Class.forName(bean.getClassName()); | ||
object = (Object) cls.newInstance(); | ||
if (bean.getProperties() != null) { | ||
for (Map.Entry<String, Property> entry : bean.getProperties().entrySet()) { | ||
if (entry.getValue().getType() == VAL) { | ||
parseValue(entry, cls, object); | ||
} else if (entry.getValue().getType() == REF) { | ||
parseRef(bean, entry, cls, object); | ||
} | ||
} | ||
} | ||
objByName.put(id, object); | ||
objByClassName.put(bean.getClassName(), object); | ||
} | ||
return object; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Вернуть объект по имени класса | ||
* Например, Car car = (Car) container.getByClass("track.container.beans.Car") | ||
*/ | ||
public Object getByClass(String className) { | ||
public Object getByClass(String className) throws ClassNotFoundException, IllegalAccessException, | ||
InstantiationException, NoSuchMethodException, CyclicReferenceException, InvocationTargetException { | ||
for (Bean bean : beans) { | ||
String clsName = bean.getClassName(); | ||
if (clsName.equals(className)) { | ||
Object object = objByClassName.get(clsName); | ||
if (object == null) { | ||
Class<?> cls = Class.forName(bean.getClassName()); | ||
object = (Object) cls.newInstance(); | ||
if (bean.getProperties() != null) { | ||
for (Map.Entry<String, Property> entry : bean.getProperties().entrySet()) { | ||
if (entry.getValue().getType() == VAL) { | ||
parseValue(entry, cls, object); | ||
} else if (entry.getValue().getType() == REF) { | ||
parseRef(bean, entry, cls, object); | ||
} | ||
} | ||
} | ||
objByClassName.put(clsName, object); | ||
objByName.put(bean.getId(), object); | ||
} | ||
return object; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
private void parseRef(Bean bean, Map.Entry<String, Property> entry, Class<?> cls, Object object) throws ClassNotFoundException, CyclicReferenceException, InvocationTargetException, NoSuchMethodException, InstantiationException, IllegalAccessException { | ||
Class[] arg = new Class[1]; | ||
Class<?> cls1 = null; | ||
Object object1 = null; | ||
String id1 = entry.getValue().getValue(); | ||
for (Bean bean1 : beans) { | ||
String objId1 = bean1.getId(); | ||
if (objId1.equals(id1)) { | ||
cls1 = Class.forName(bean1.getClassName()); | ||
if (bean.getProperties() != null) { | ||
for (Map.Entry<String, Property> entry1 : bean.getProperties().entrySet()) { | ||
if (entry1.getValue().getType() == REF && | ||
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. Уже выше проверили что REF |
||
entry1.getValue().getValue().equals(bean.getId())) { | ||
throw new CyclicReferenceException(); | ||
} | ||
} | ||
} | ||
object1 = getById(objId1); | ||
} | ||
} | ||
arg[0] = cls1; | ||
Method[] methods = cls.getMethods(); | ||
Method method = null; | ||
for (int i = 0; i < methods.length; i++) { | ||
if (methods[i].getName().equals("set" + | ||
entry.getValue().getName().substring(0, 1).toUpperCase() + | ||
entry.getValue().getName().substring(1))) { | ||
method = methods[i]; | ||
} | ||
} | ||
method.invoke(object, object1); | ||
} | ||
|
||
private void parseValue(Map.Entry<String, Property> entry, Class<?> cls, Object object) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { | ||
Class[] arg = new Class[1]; | ||
Object value; | ||
try { | ||
value = Integer.parseInt(entry.getValue().getValue()); | ||
arg[0] = int.class; | ||
} catch (NumberFormatException e) { | ||
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. Не все типы |
||
try { | ||
value = Double.parseDouble(entry.getValue().getValue()); | ||
arg[0] = double.class; | ||
} catch (NumberFormatException ex) { | ||
value = entry.getValue().getValue(); | ||
arg[0] = String.class; | ||
} | ||
} | ||
Method[] methods = cls.getMethods(); | ||
Method method = null; | ||
for (int i = 0; i < methods.length; i++) { | ||
if (methods[i].getName().equals("set" + | ||
entry.getValue().getName().substring(0, 1).toUpperCase() + | ||
entry.getValue().getName().substring(1))) { | ||
method = methods[i]; | ||
} | ||
} | ||
method.invoke(object, value); | ||
} | ||
} | ||
|
||
class CyclicReferenceException extends Exception { | ||
|
||
public CyclicReferenceException() { | ||
} | ||
|
||
public CyclicReferenceException(String message) { | ||
super(message); | ||
} | ||
} |
This file contains 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 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 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 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 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.
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.
Код дублируетс