1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-10-31 19:22:31 +00:00
plantuml/src/net/sourceforge/plantuml/cucadiagram/entity/EntityFactory.java

254 lines
6.9 KiB
Java
Raw Normal View History

2013-12-10 19:36:50 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-01-09 12:15:40 +00:00
* (C) Copyright 2009-2017, Arnaud Roques
2013-12-10 19:36:50 +00:00
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2013-12-10 19:36:50 +00:00
*
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*
*
* Original Author: Arnaud Roques
*
*
*/
package net.sourceforge.plantuml.cucadiagram.entity;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sourceforge.plantuml.cucadiagram.Bodier;
import net.sourceforge.plantuml.cucadiagram.Code;
import net.sourceforge.plantuml.cucadiagram.Display;
import net.sourceforge.plantuml.cucadiagram.GroupRoot;
import net.sourceforge.plantuml.cucadiagram.GroupType;
import net.sourceforge.plantuml.cucadiagram.IEntity;
import net.sourceforge.plantuml.cucadiagram.IGroup;
import net.sourceforge.plantuml.cucadiagram.ILeaf;
import net.sourceforge.plantuml.cucadiagram.LeafType;
import net.sourceforge.plantuml.cucadiagram.Link;
2015-04-07 18:18:37 +00:00
import net.sourceforge.plantuml.cucadiagram.LongCode;
2016-03-06 16:47:34 +00:00
import net.sourceforge.plantuml.cucadiagram.Stereotype;
2013-12-10 19:36:50 +00:00
import net.sourceforge.plantuml.skin.VisibilityModifier;
public class EntityFactory {
private final Map<Code, ILeaf> leafs = new Protect<ILeaf>(new LinkedHashMap<Code, ILeaf>());
private final List<Link> links = new ArrayList<Link>();
private final Map<Code, IGroup> groups = new Protect<IGroup>(new LinkedHashMap<Code, IGroup>());
2015-04-07 18:18:37 +00:00
private int rawLayout;
2013-12-10 19:36:50 +00:00
private final IGroup rootGroup = new GroupRoot(this);
2015-04-07 18:18:37 +00:00
private final Set<LeafType> hiddenTypes;
2016-03-06 16:47:34 +00:00
private final Set<String> hiddenStereotype;
2015-04-07 18:18:37 +00:00
2016-03-06 16:47:34 +00:00
public EntityFactory(Set<LeafType> hiddenTypes, Set<String> hiddenStereotype) {
2015-04-07 18:18:37 +00:00
this.hiddenTypes = hiddenTypes;
2016-03-06 16:47:34 +00:00
this.hiddenStereotype = hiddenStereotype;
2015-04-07 18:18:37 +00:00
}
2016-03-06 16:47:34 +00:00
public boolean isHidden(ILeaf leaf) {
if (hiddenTypes.contains(leaf.getEntityType())) {
return true;
}
final Stereotype stereotype = leaf.getStereotype();
if (stereotype != null && hiddenStereotype.contains(stereotype.getLabel(false))) {
return true;
}
return false;
2015-04-07 18:18:37 +00:00
}
2013-12-10 19:36:50 +00:00
public ILeaf createLeaf(Code code, Display display, LeafType entityType, IGroup parentContainer,
2015-04-07 18:18:37 +00:00
Set<VisibilityModifier> hides, String namespaceSeparator) {
2013-12-10 19:36:50 +00:00
if (entityType == null) {
throw new IllegalArgumentException();
}
final Bodier bodier = new Bodier(entityType, hides);
2015-04-07 18:18:37 +00:00
final LongCode longCode = getLongCode(code, namespaceSeparator);
final EntityImpl result = new EntityImpl(this, code, bodier, parentContainer, entityType, longCode,
namespaceSeparator, rawLayout);
2013-12-10 19:36:50 +00:00
result.setDisplay(display);
return result;
}
2015-04-07 18:18:37 +00:00
private LongCode getLongCode(Code code, String namespaceSeparator) {
final LongCode result = LongCode.of(code.getFullName(), namespaceSeparator);
// if (result.toString().equals(code.toString()) == false) {
// System.err.println("result=" + result);
// System.err.println(" code =" + code);
// throw new UnsupportedOperationException();
// }
return result;
}
public IGroup createGroup(Code code, Display display, Code namespace2, GroupType groupType, IGroup parentContainer,
Set<VisibilityModifier> hides, String namespaceSeparator) {
2013-12-10 19:36:50 +00:00
if (groupType == null) {
throw new IllegalArgumentException();
}
final Bodier bodier = new Bodier(null, hides);
2015-04-07 18:18:37 +00:00
final LongCode longCode = getLongCode(code, namespaceSeparator);
final EntityImpl result = new EntityImpl(this, code, bodier, parentContainer, groupType, namespace2, longCode,
namespaceSeparator, rawLayout);
2015-07-11 09:32:49 +00:00
if (Display.isNull(display) == false) {
2013-12-10 19:36:50 +00:00
result.setDisplay(display);
}
return result;
}
public IGroup getRootGroup() {
return rootGroup;
}
public final Map<Code, ILeaf> getLeafs() {
return Collections.unmodifiableMap(leafs);
}
public void addLeaf(ILeaf entity) {
leafs.put(entity.getCode(), entity);
}
2015-04-07 18:18:37 +00:00
public void incRawLayout() {
rawLayout++;
}
2013-12-10 19:36:50 +00:00
void removeLeaf(Code code) {
final IEntity removed = leafs.remove(code);
if (removed == null) {
throw new IllegalArgumentException();
}
}
public void addGroup(IGroup group) {
groups.put(group.getCode(), group);
}
void removeGroup(Code code) {
final IEntity removed = groups.remove(code);
if (removed == null) {
throw new IllegalArgumentException();
}
}
public final Map<Code, IGroup> getGroups() {
return Collections.unmodifiableMap(groups);
}
public final List<Link> getLinks() {
return Collections.unmodifiableList(links);
}
public void addLink(Link link) {
links.add(link);
}
public void removeLink(Link link) {
final boolean ok = links.remove(link);
if (ok == false) {
throw new IllegalArgumentException();
}
}
2015-04-07 18:18:37 +00:00
public IGroup muteToGroup(Code code, Code namespace2, GroupType type, IGroup parent) {
2013-12-10 19:36:50 +00:00
final ILeaf leaf = getLeafs().get(code);
2015-04-07 18:18:37 +00:00
((EntityImpl) leaf).muteToGroup(namespace2, type, parent);
2013-12-10 19:36:50 +00:00
final IGroup result = (IGroup) leaf;
removeLeaf(code);
return result;
}
static class Protect<O extends Object> implements Map<Code, O> {
private final Map<Code, O> m;
public Protect(Map<Code, O> data) {
this.m = data;
}
public O remove(Object key) {
if (key instanceof Code == false) {
throw new IllegalArgumentException();
}
return m.remove(key);
}
public O get(Object key) {
if (key instanceof Code == false) {
throw new IllegalArgumentException();
}
return m.get(key);
}
public Set<Code> keySet() {
return m.keySet();
}
public void putAll(Map<? extends Code, ? extends O> m) {
this.m.putAll(m);
}
public boolean containsKey(Object key) {
if (key instanceof Code == false) {
throw new IllegalArgumentException();
}
return m.containsKey(key);
}
public boolean isEmpty() {
return m.isEmpty();
}
public O put(Code key, O value) {
if (key instanceof Code == false) {
throw new IllegalArgumentException();
}
return m.put(key, value);
}
public boolean containsValue(Object value) {
return m.containsValue(value);
}
public Set<Map.Entry<Code, O>> entrySet() {
return m.entrySet();
}
public Collection<O> values() {
return m.values();
}
public void clear() {
m.clear();
}
public int size() {
return m.size();
}
}
2015-04-07 18:18:37 +00:00
2013-12-10 19:36:50 +00:00
}