Skip to content
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

Adds Visibilty to Attribute, Class and Method #16

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 34 additions & 24 deletions README.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,39 +32,49 @@ Will get :
----
@startuml

interface Vehicule

class Car {
brand : String
model : String
driver : Driver
price : Price
wheels : Collection<Wheel>
+class "Car" {
-ignored : Long
-brand : String
-model : String
-drivers : Set<Driver>
-price : Price
-wheels : Collection<Wheel>
+addDriver(Driver) : Car
+addWheel(Wheel)
+buyBy(Driver, BigDecimal, Devise) : Driver
}

class Driver {
name : String
cars : List<Car>
+enum "Devise" {
+CHF
+EUR
+USD
}

class Price {
amount : BigDecimal
devise : Devise
+class "Driver" {
-name : String
-ignored : Long
-cars : List<Car>
+addCar(Car)
+buy(Car)
}

class Wheel
+class "Price" {
-amount : BigDecimal
-devise : Devise
}

+interface "Vehicule" {
}

enum Devise {
CHF
EUR
USD
+class "Wheel" {
}

Vehicule <|-- Car
Car "*" <-> Driver : driver
Car --> Price : price
Car --> "*" Wheel : wheels
Price --> Devise : devise
"Car" --> "Devise" : use
"Car" "*" <-> "*" "Driver" : drivers/cars
"Car" --> "Price" : price
"Car" --> "*" "Wheel" : wheels
"Price" --> "Devise" : devise
"Vehicule" <|-- "Car"

@enduml
----
Expand Down
Binary file modified images/class-diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 13 additions & 7 deletions src/main/java/ch/ifocusit/plantuml/PlantUmlBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import ch.ifocusit.plantuml.classdiagram.model.Package;
import ch.ifocusit.plantuml.classdiagram.model.attribute.Attribute;
import ch.ifocusit.plantuml.classdiagram.model.clazz.Clazz;
import ch.ifocusit.plantuml.classdiagram.model.clazz.Clazz.Visibilty;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

Expand Down Expand Up @@ -84,7 +86,10 @@ private String color(String color) {
return HASHTAG + color;
}

private PlantUmlBuilder writeClazzDefinition(Clazz clazz) {
private PlantUmlBuilder writeClazzDefinition(Clazz clazz, boolean allowVisibility) {
if(allowVisibility) {
content.append(clazz.getVisibilty());
}
content.append(clazz.getType()).append(SPACE).append(escape(clazz.getName()));
return this;
}
Expand Down Expand Up @@ -123,7 +128,7 @@ public PlantUmlBuilder addPackage(Package aPackage, Clazz... classes) {
content.append(SPACE).append(BRACE_OPEN).append(NEWLINE);
Stream.of(classes).forEach(clazz -> {
clazz.validate();
append(TAB).writeClazzDefinition(clazz).append(NEWLINE);
append(TAB).writeClazzDefinition(clazz, false).append(NEWLINE);
});
content.append(BRACE_CLOSE);
}
Expand All @@ -140,7 +145,7 @@ public PlantUmlBuilder addType(Clazz clazz) {
Validate.notNull(clazz, "No class defined !");
clazz.validate();

writeClazzDefinition(clazz);
writeClazzDefinition(clazz, true);
// stereotype
clazz.getStereotypes().ifPresent(stereotypes -> content
.append(SPACE).append(STEREOTYPE_OPEN)
Expand All @@ -151,13 +156,14 @@ public PlantUmlBuilder addType(Clazz clazz) {
// class color
clazz.getBackgroundColor().ifPresent(color -> content.append(SPACE).append(color(color)));

if (clazz.hasContent()) {
boolean writeBody = clazz.hasContent() || clazz.getVisibilty() != Visibilty.NONE;
if (writeBody) {
content.append(SPACE).append(BRACE_OPEN).append(NEWLINE);
}
// add attributes
for (Attribute attribute : clazz.getAttributes()) {
// name
content.append(TAB).append(attribute.getName());
content.append(TAB).append(attribute.getVisibilty()).append(attribute.getName());
// type
attribute.getTypeName().ifPresent(type -> content.append(SPACE).append(SEMICOLON).append(SPACE).append(type));
// field link
Expand All @@ -167,7 +173,7 @@ public PlantUmlBuilder addType(Clazz clazz) {
// add methods
clazz.getMethods().stream().forEach(method -> {
// name
content.append(TAB).append(method.getName());
content.append(TAB).append(method.getVisibilty()).append(method.getName());
// parameters
method.getParameters().ifPresent(params -> {
content.append(BRACKET_OPEN);
Expand All @@ -182,7 +188,7 @@ public PlantUmlBuilder addType(Clazz clazz) {
method.getLink().ifPresent(link -> content.append(SPACE).append(link.toString()));
content.append(NEWLINE);
});
if (clazz.hasContent()) {
if (writeBody) {
content.append(BRACE_CLOSE);
}
if (!clazz.getAttributes().isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,23 @@ public ClassDiagramBuilder addClasses(Iterable<Class> classes) {
return this;
}

//for backward compatibility
@Deprecated
public ClassDiagramBuilder addClasse(Iterable<Class> classes) {
return addClasses(classes);
}

public ClassDiagramBuilder addClasses(Class... classes) {
this.classesRepository.addAll(Arrays.asList(classes));
return this;
}

//for backward compatibility
@Deprecated
public ClassDiagramBuilder addClasse(Class... classes) {
return addClasses(classes);
}

public ClassDiagramBuilder addPackage(java.lang.Package... packages) {
this.packages.addAll(Arrays.asList(packages));
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package ch.ifocusit.plantuml.classdiagram.model.attribute;

import ch.ifocusit.plantuml.classdiagram.model.Link;
import ch.ifocusit.plantuml.classdiagram.model.clazz.Clazz.Visibilty;

import java.util.Optional;

Expand All @@ -31,6 +32,8 @@
*/
public interface Attribute {

public Visibilty getVisibilty();

public Optional<String> getTypeName();

public String getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import ch.ifocusit.plantuml.classdiagram.model.ClassMember;
import ch.ifocusit.plantuml.classdiagram.model.Link;
import ch.ifocusit.plantuml.classdiagram.model.clazz.Clazz.Visibilty;
import ch.ifocusit.plantuml.utils.ClassUtils;

import java.lang.reflect.Field;
Expand Down Expand Up @@ -56,6 +57,11 @@ public Optional<String> getTypeName() {
return Optional.of(ClassUtils.getSimpleName(field.getGenericType()));
}

@Override
public Visibilty getVisibilty() {
return Visibilty.parseVisibilty(field.getModifiers());
}

@Override
public Class getType() {
return getFieldType();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.ifocusit.plantuml.classdiagram.model.attribute;

import ch.ifocusit.plantuml.classdiagram.model.ClassMember;
import ch.ifocusit.plantuml.classdiagram.model.clazz.Clazz.Visibilty;
import ch.ifocusit.plantuml.utils.ClassUtils;

import java.lang.reflect.Parameter;
Expand All @@ -20,6 +21,11 @@ public Set<Class> getConcernedTypes() {
return ClassUtils.getConcernedTypes(methodParameter);
}

@Override
public Visibilty getVisibilty() {
return Visibilty.NONE;
}

public Class getParameterType() {
return methodParameter.getType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,32 @@

import java.util.Optional;

import ch.ifocusit.plantuml.classdiagram.model.clazz.Clazz.Visibilty;

/**
* @author Julien Boz
*/
public class SimpleAttribute implements Attribute {

private Visibilty visibilty;
private String type;
private String name;

public SimpleAttribute(String name, String type) {
public SimpleAttribute(Visibilty visibilty, String name, String type) {
this.type = type;
this.name = name;
this.visibilty = visibilty;
}

public SimpleAttribute(String name, String type) {
this(Visibilty.NONE, name, type);
}

@Override
public Visibilty getVisibilty() {
return visibilty;
}

@Override
public Optional<String> getTypeName() {
return Optional.ofNullable(type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,27 @@
*/
package ch.ifocusit.plantuml.classdiagram.model.clazz;

import ch.ifocusit.plantuml.classdiagram.model.Link;
import ch.ifocusit.plantuml.classdiagram.model.method.Method;
import ch.ifocusit.plantuml.classdiagram.model.attribute.Attribute;
import org.apache.commons.lang3.Validate;

import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.commons.lang3.Validate;

import ch.ifocusit.plantuml.classdiagram.model.Link;
import ch.ifocusit.plantuml.classdiagram.model.attribute.Attribute;
import ch.ifocusit.plantuml.classdiagram.model.clazz.Clazz.Visibilty;
import ch.ifocusit.plantuml.classdiagram.model.method.Method;

/**
* @author Julien Boz
*/
public interface Clazz extends Comparable<Clazz> {

public String getName();

public Visibilty getVisibilty();

public Type getType();

default public Optional<Link> getLink() {
Expand Down Expand Up @@ -76,6 +81,41 @@ default boolean hasContent() {
return !getAttributes().isEmpty() || !getMethods().isEmpty();
}

public enum Visibilty {
PUBLIC("public", "+"),
PROTECTED("protected", "#"),
PACKAGE("package private", "~"),
PRIVATE("private", "-"),
NONE("", "");

public final String name;
public final String attributePrefix;

private Visibilty(String name, String attributePrefix) {
this.name = name;
this.attributePrefix = attributePrefix;
}

public static Visibilty parseVisibilty(int modifiers) {
if (Modifier.isPrivate(modifiers)) {
return Visibilty.PRIVATE;
}
if (Modifier.isProtected(modifiers)) {
return Visibilty.PROTECTED;
}
if (Modifier.isPublic(modifiers)) {
return Visibilty.PUBLIC;
}
return Visibilty.PACKAGE;
}

@Override
public String toString() {
return attributePrefix;
}

}

enum Type {
INTERFACE("interface"),
ENUM("enum"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public String getName() {
return overridedName.orElse(ClassUtils.getSimpleName(relatedClass));
}

@Override
public Visibilty getVisibilty() {
return Visibilty.parseVisibilty(relatedClass.getModifiers());
}

public Type getType() {
return parseType(relatedClass);
}
Expand Down Expand Up @@ -139,4 +144,5 @@ public static JavaClazz from(Class aClass, ClassAttribute[] attributes, ClassMet
javaClass.addMethods(methods);
return javaClass;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,19 @@ public class SimpleClazz implements Clazz {

private String name;
private Type type;
private Visibilty visibilty;
private List<Attribute> attributes = new ArrayList<>();

@Override
public String getName() {
return name;
}

@Override
public Visibilty getVisibilty() {
return visibilty;
}

@Override
public Type getType() {
return type;
Expand All @@ -52,11 +58,17 @@ public List<Attribute> getAttributes() {
return attributes;
}

public static SimpleClazz create(String name, Type type, Attribute... attributes) {
public static SimpleClazz create(String name, Visibilty visibilty, Type type, Attribute... attributes) {
SimpleClazz c = new SimpleClazz();
c.name = name;
c.visibilty = visibilty;
c.type = type;
Stream.of(attributes).forEach(c.attributes::add);
return c;
}

public static SimpleClazz create(String name, Type type, Attribute... attributes) {
return SimpleClazz.create(name, Visibilty.NONE, type, attributes);
}

}
Loading