package foundation.capacity; import java.util.Iterator; import foundation.json.IJSONProvider; import foundation.json.IJSONWriter; import foundation.json.JSONReader; import foundation.util.MapList; import foundation.util.Util; public class ActorsRuntime implements Iterable, IJSONProvider { private MapList actors; private CurrentActor current; public ActorsRuntime() { actors = new MapList(); current = new CurrentActor(); } public void loadOne(ActorTarget actor) { //1. 添加 Actor String id = actor.getId(); if (!actors.contains(id)) { actors.add(id, actor); } //2. 设置当前默认 Actor Branch if (current.isEmptyDefault()) { current.setDefault(actor); } } public ActorTarget changeCurrent(String onlineCode, String targetId) { ActorTarget result = actors.get(targetId); current.set(onlineCode, result); return result; } public ActorTarget getCurrent(String onlineCode) { return current.get(onlineCode); } public ActorTarget get(String actorId) { return actors.get(actorId); } public void saveChanges(JSONReader changes) { //1. translate changes to capacity list MapList actors = new MapList(); ActorBucket actorBucket = ActorBucket.getInstance(); for (JSONReader reader: changes) { String id = reader.getString("id"); if (Util.isEmpty(id)) { return; } Actor actor = actorBucket.get(id); if (actor == null) { continue; } actors.add(id, actor); } /* //2. add new for (Actor actor: itemList) { String id = actor.getId(); if (!contains(id)) { add(id, actor); } } //3. delete for (Actor actor: itemList) { String id = actor.getId(); if (!actors.contains(id)) { remove(id); if (isEmpty()) { current = null; } } } */ } public void clear() { actors.clear(); } @Override public Iterator iterator() { return actors.iterator(); } @Override public void writeJSON(IJSONWriter writer) { writer.beginArray(); for (ActorTarget actor: actors) { actor.writeJSON(writer); } writer.endArray(); } }