-
Notifications
You must be signed in to change notification settings - Fork 53
container #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
base: master
Are you sure you want to change the base?
container #38
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Ivanov Mikhail |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,21 @@ | ||
| package arhangel.dim.container; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public class BeanGraph { | ||
|
|
||
| private static Logger log = LoggerFactory.getLogger(BeanGraph.class); | ||
|
|
||
| // Граф представлен в виде списка связности для каждой вершины | ||
| private Map<BeanVertex, List<BeanVertex>> vertices = new HashMap<>(); | ||
|
|
||
|
|
@@ -16,35 +24,71 @@ public class BeanGraph { | |
| * @param value - объект, привязанный к вершине | ||
| */ | ||
| public BeanVertex addVertex(Bean value) { | ||
| return null; | ||
| BeanVertex vertex = new BeanVertex(value); | ||
| vertices.put(vertex, new ArrayList<>()); | ||
| return vertex; | ||
| } | ||
|
|
||
| /** | ||
| * Соединить вершины ребром | ||
| * @param from из какой вершины | ||
| * @param to в какую вершину | ||
| */ | ||
| public void addEdge(BeanVertex from ,BeanVertex to) { | ||
| public void addEdge(BeanVertex from , BeanVertex to) throws Exception { | ||
| if (from != null && to != null && vertices.containsKey(from)) { | ||
| vertices.get(from).add(to); | ||
| } else { | ||
| throw new InvalidConfigurationException("Error in addEdge"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Проверяем, связаны ли вершины | ||
| */ | ||
| public boolean isConnected(BeanVertex v1, BeanVertex v2) { | ||
| return false; | ||
| return getLinked(v1).contains(v2); | ||
| } | ||
|
|
||
| /** | ||
| * Получить список вершин, с которыми связана vertex | ||
| */ | ||
| public List<BeanVertex> getLinked(BeanVertex vertex) { | ||
| return null; | ||
| return vertices.get(vertex); | ||
| } | ||
|
|
||
| /** | ||
| * Количество вершин в графе | ||
| */ | ||
| public int size() { | ||
| return 0; | ||
| return vertices.size(); | ||
| } | ||
|
|
||
| void dfs(BeanVertex current) throws Exception { | ||
|
Owner
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. А что еще может вылететь кроме CycleRefException? |
||
| current.setState(BeanVertex.State.MARKED); | ||
| for (BeanVertex vertex : getLinked(current)) { | ||
| //log.info("ADD SORT LIST " + vertex.getBean().getName()); | ||
| if (vertex.getState() == BeanVertex.State.MARKED) { | ||
| throw new CycleReferenceException("Cycle in your ref dependency"); | ||
| } | ||
| if (vertex.getState() == BeanVertex.State.DEFAULT) { | ||
| dfs(vertex); | ||
| } | ||
| } | ||
| current.setState(BeanVertex.State.VISITED); | ||
| sorted.add(current.getBean()); | ||
| } | ||
|
|
||
| private List<Bean> sorted = new LinkedList<>(); | ||
|
|
||
| public List<Bean> topSort() throws Exception { | ||
| for (BeanVertex vertex : vertices.keySet()) { | ||
| if (vertex.getState() == BeanVertex.State.DEFAULT) { | ||
| //log.info("ADD SORT LIST " + vertex.getBean().getName()); | ||
| dfs(vertex); | ||
| } | ||
| } | ||
|
|
||
| return sorted; | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,28 @@ | ||
| package arhangel.dim.container; | ||
|
|
||
| import java.io.File; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import javax.xml.parsers.DocumentBuilder; | ||
| import javax.xml.parsers.DocumentBuilderFactory; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.w3c.dom.Document; | ||
| import org.w3c.dom.Element; | ||
| import org.w3c.dom.NamedNodeMap; | ||
| import org.w3c.dom.Node; | ||
| import org.w3c.dom.NodeList; | ||
|
|
||
| /** | ||
| * | ||
| */ | ||
| public class BeanXmlReader { | ||
|
|
||
| private static Logger log = LoggerFactory.getLogger(BeanXmlReader.class); | ||
|
|
||
| private static final String TAG_BEAN = "bean"; | ||
| private static final String TAG_PROPERTY = "property"; | ||
| private static final String ATTR_NAME = "name"; | ||
|
|
@@ -14,8 +31,80 @@ public class BeanXmlReader { | |
| private static final String ATTR_BEAN_ID = "id"; | ||
| private static final String ATTR_BEAN_CLASS = "class"; | ||
|
|
||
| public List<Bean> parseBeans(String pathToFile) { | ||
| return null; | ||
| List<Bean> beans = new ArrayList<>(); | ||
|
|
||
| public List<Bean> parseBeans(String pathToFile) throws Exception { | ||
|
Owner
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. Пусть бросает только InvalidConfiguartionExceptino |
||
| Document config = readXml(pathToFile); | ||
| Element root = config.getDocumentElement(); | ||
| NodeList nodes = root.getChildNodes(); | ||
|
|
||
| //проверка на уникальность id | ||
| Set<String> uniqueId = new HashSet<>(); | ||
|
|
||
| for (int i = 0; i < nodes.getLength(); i++) { | ||
| Node node = nodes.item(i); | ||
| if (TAG_BEAN.equals(node.getNodeName())) { | ||
| parseBean(node); | ||
| if (uniqueId.contains(beans.get(beans.size() - 1).getName())) { | ||
| throw new InvalidConfigurationException("Object name is not unique"); | ||
|
Owner
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. Хорошая проверка! |
||
| } | ||
| uniqueId.add(beans.get(beans.size() - 1).getName()); | ||
| } | ||
| } | ||
|
|
||
| return beans; | ||
| } | ||
|
|
||
| private void parseBean(Node bean) throws Exception { | ||
|
Owner
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. Боюсь, я знаю откуда вы скопировали этот код
Owner
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. Ой, извините, этот код действительно есть в репозитории. Абсолютно нормально его использовать. |
||
| NamedNodeMap attr = bean.getAttributes(); | ||
| Node name = attr.getNamedItem(ATTR_BEAN_ID); | ||
| String nameVal = name.getNodeValue(); | ||
| String classVal = attr.getNamedItem(ATTR_BEAN_CLASS).getNodeValue(); | ||
| //log.info("BEAN: [name: {}, class: {}]", nameVal, classVal); | ||
|
|
||
| // ищем все проперти внутри | ||
| NodeList list = bean.getChildNodes(); | ||
| Map<String, Property> properties = new HashMap<>(); | ||
| for (int i = 0; i < list.getLength(); i++) { | ||
| Node node = list.item(i); | ||
| if (TAG_PROPERTY.equals(node.getNodeName())) { | ||
| Property property = parseProperty(node); | ||
| //log.info("\tSET {}", property); | ||
| properties.put(property.getName(), property); | ||
| } | ||
| } | ||
| // | ||
| beans.add(new Bean(nameVal, classVal, properties)); | ||
| } | ||
|
|
||
| private Property parseProperty(Node node) throws Exception { | ||
| NamedNodeMap map = node.getAttributes(); | ||
| String name = map.getNamedItem(ATTR_NAME).getNodeValue(); | ||
| Node val = map.getNamedItem(ATTR_VALUE); | ||
| if (val != null) { | ||
| // если значение примитивного типа | ||
| return new Property(name, val.getNodeValue(), ValueType.VAL); | ||
| } else { | ||
| // если значение ссылочного типа | ||
| val = map.getNamedItem(ATTR_REF); | ||
| if (val != null) { | ||
| return new Property(name, val.getNodeValue(), ValueType.REF); | ||
| } else { | ||
| throw new InvalidConfigurationException("Failed to parse property " + name); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private Document readXml(String path) throws Exception { | ||
| File file = new File(path); | ||
| //log.info("Context configuration xml: " + file.getAbsolutePath()); | ||
| DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); | ||
| DocumentBuilder db = dbf.newDocumentBuilder(); | ||
| return db.parse(file); | ||
| } | ||
|
|
||
| public List<Bean> getBeans() { | ||
| return beans; | ||
| } | ||
|
|
||
| } | ||
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.
Строго говоря, бросать ошибку конфигурации и писать сообщение, что не найдено ребро - нелогично. Имеет смысл пробросить эту ошибку как проверяемое исключение выше по стеку и там отреагировать на него