/
R.D.X
/
pieplex-api
Обзор
Документация
Войти
/
R.D.X
/
pieplex-api
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
src/main/java/ru/rdx/pieplex/api/data/impl/NodeImpl.java
161 строка
5 KB
a.kiselev
no comment
16 май 2026, 07:33
16 май 2026, 07:33
621410d
Код
Авторство
О чём код?
package ru.rdx.pieplex.api.data.impl; import ru.rdx.pieplex.api.data.Node; import java.util.List; import java.util.Map; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.ArrayList; import java.util.Collections; import java.util.Objects; public class NodeImpl implements Node { private Object value; private Map<String, List<Integer>> nextNodes; private List<String> plainNextNames; private List<Integer> plainNextNodes; private NodeImpl(Object value, Map<String, List<Integer>> nextNodes, List<String> plainNextNames, List<Integer> plainNextNodes) { this.value = value; this.nextNodes = nextNodes; this.plainNextNames = plainNextNames; this.plainNextNodes = plainNextNodes; } public NodeImpl() { this.nextNodes = new LinkedHashMap<>(48); this.plainNextNames = new ArrayList<>(32); this.plainNextNodes = new ArrayList<>(32); } @Override public void link(String name, Integer i) { Map<String, List<Integer>> localNn = this.nextNodes; List<Integer> target = localNn.get(name); if(null == target) { target = new ArrayList<>(32); localNn.put(name, target); } target.add(i); this.plainNextNames.add(name); this.plainNextNodes.add(i); } @Override public Map<String, List<Integer>> getNextNodes() { return this.nextNodes; } @Override public void setValue(Object value) { this.value = value; } @Override public boolean isEmpty() { return this.nextNodes.isEmpty() && null == this.value; } @Override public Object getValue() { return this.value; } @Override public List<String> descendantNames() { return this.plainNextNames; } @Override public List<Integer> descendant() { return this.plainNextNodes; } @Override public List<Integer> descendant(String name) { if(!this.nextNodes.containsKey(name)) { return Collections.emptyList(); } return this.nextNodes.get(name); } // при обращении по полному пути всегда проходим только по первому элементу. // обрабатывать списки надо отдельно. см. each @Override public int next(String name) { List<Integer> nextNode = this.nextNodes.get(name); if(null == nextNode || nextNode.isEmpty()) { return -1; } return nextNode.get(0).intValue(); } @Override public void deconstruct() { for(List<Integer> l : this.nextNodes.values()) { l.clear(); } this.nextNodes.clear(); this.plainNextNodes.clear(); this.plainNextNames.clear(); this.nextNodes = null; this.value = null; this.plainNextNodes = null; this.plainNextNames = null; } @Override public Node reducedCopy() { Map<String, List<Integer>> local = this.nextNodes; if(local.isEmpty()) { return new NodeImpl(this.value, Collections.emptyMap(), Collections.emptyList(), Collections.emptyList()); } Map<String, List<Integer>> tmp = new HashMap<>(local.size()); for(String key: local.keySet()) { tmp.put(key, new ArrayList<Integer>(local.get(key).size())); } return new NodeImpl(this.value, tmp, new ArrayList<String>(this.plainNextNames.size()), new ArrayList<Integer>(this.plainNextNodes.size())); } @Override public Node fullCopy() { Map<String, List<Integer>> local = this.nextNodes; if(local.isEmpty()) { return new NodeImpl(this.value, Collections.emptyMap(), Collections.emptyList(), Collections.emptyList()); } Map<String, List<Integer>> tmp = new HashMap<>(local.size()); for(String key: local.keySet()) { tmp.put(key, copyList(local.get(key))); } return new NodeImpl(this.value, tmp, copyList(this.plainNextNames), copyList(this.plainNextNodes)); } private static <T> List<T> copyList(List<T> src) { int sz = src.size(); List<T> dst = new ArrayList<>(sz); for(int i = 0; i < sz; i++) { dst.add(src.get(i)); } return dst; } @Override public boolean equals(Object other) { if(!(other instanceof NodeImpl)) { return false; } NodeImpl o = (NodeImpl)other; return Objects.equals(this.value, o.value) && this.plainNextNodes.equals(o.plainNextNodes) && this.plainNextNames.equals(o.plainNextNames); } }