Skip to content

Commit

Permalink
Fix warnings and redundant mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
minborg committed Nov 23, 2015
1 parent 41cedbe commit 475045c
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 36 deletions.
Expand Up @@ -95,7 +95,7 @@ public Boolean isExpanded() {
public Stream<? extends Parent<?>> ancestors() {
return asChild()
.flatMap(c -> c.getParent())
.map(p -> (Parent<?>) p)
//.map(p -> (Parent<?>) p)
.map(parent -> Trees.walkOptional(
parent, (Parent<?> p) -> p.asChild()
.flatMap(c -> c.getParent())
Expand All @@ -117,7 +117,7 @@ public <E extends Node> Optional<E> ancestor(final Class<E> clazz) {
public <T extends Parent<?>> String getRelativeName(final Class<T> from, Function<String, String> nameMapper) {
Objects.requireNonNull(from);
final StringJoiner sj = new StringJoiner(".", "", ".").setEmptyValue("");
final List<Parent<?>> ancestors = ancestors().map(p -> (Parent<?>) p).collect(toList());
final List<Parent<?>> ancestors = ancestors()/*.map(p -> (Parent<?>) p)*/.collect(toList());
boolean add = false;
for (final Parent<?> parent : ancestors) {
if (from.isAssignableFrom(parent.getClass())) {
Expand Down
Expand Up @@ -69,7 +69,7 @@ public Optional<Project> getParent() {
}

@Override
public ChildHolder getChildren() {
public ChildHolder<Child<PluginData>> getChildren() {
throw new IllegalStateException(PluginData.class.getSimpleName() + " does not have a known child type at this point.");
}

Expand Down
Expand Up @@ -84,7 +84,7 @@ public void setExpanded(Boolean expanded) {
public final Stream<? extends Parent<?>> ancestors() {
return asChild()
.flatMap(c -> c.getParent())
.map(p -> (Parent<?>) p)
//.map(p -> (Parent<?>) p)
.map(parent -> Trees.walkOptional(
parent, (Parent<?> p) -> p.asChild()
.flatMap(c -> c.getParent())
Expand All @@ -106,7 +106,7 @@ public final <E extends Node> Optional<E> ancestor(final Class<E> clazz) {
public final <T extends Parent<?>> String getRelativeName(final Class<T> from, Function<String, String> nameMapper) {
Objects.requireNonNull(from);
final StringJoiner sj = new StringJoiner(".", "", ".").setEmptyValue("");
final List<Parent<?>> ancestors = ancestors().map(p -> (Parent<?>) p).collect(toList());
final List<Parent<?>> ancestors = ancestors()/*.map(p -> (Parent<?>) p)*/.collect(toList());
boolean add = false;
for (final Parent<?> parent : ancestors) {
if (from.isAssignableFrom(parent.getClass())) {
Expand Down
Expand Up @@ -58,7 +58,9 @@ public static <T extends Child<?>> ChildHolder<T> of(T child) {

public static <T extends Child<?>> ChildHolder<T> of(Class<T> childClass, Collection<T> childs) {
if (childs.isEmpty()) {
return (ChildHolder<T>) ofNone();
@SuppressWarnings("unchecked")
final ChildHolder<T> result = (ChildHolder<T>) ofNone();
return result;
}
if (childs.size() == 1) {
return of(childs.stream().findAny().get());
Expand Down Expand Up @@ -133,7 +135,9 @@ public Child<?> find(String name) throws SpeedmentException {

@Override
public Class<Child<?>> getChildClass() {
return (Class<Child<?>>) (Class) Child.class;
@SuppressWarnings("unchecked")
final Class<Child<?>> result = (Class<Child<?>>) (Class) Child.class;
return result;
}
}

Expand Down
Expand Up @@ -41,20 +41,24 @@ public final class ImmutablePluginData extends ImmutableAbstractNamedConfigEntit
public ImmutablePluginData(Project parent, PluginData prototype) {
super(requireNonNull(prototype).getName(), prototype.isExpanded(), prototype.isEnabled());
requireNonNull(parent);

// Members
this.speedment = parent.getSpeedment();
this.parent = Optional.of(parent);

// Children
switch (prototype.count()) {
case 0 :
this.children = (ChildHolder<Child<PluginData>>) ImmutableChildHolder.ofNone(); break;
case 1 :
case 0: {
@SuppressWarnings("unchecked")
ChildHolder<Child<PluginData>> c = (ChildHolder<Child<PluginData>>) ImmutableChildHolder.ofNone();
this.children = c;
break;
}
case 1:
final Child<PluginData> child = prototype.stream().findAny().get();
this.children = ImmutableChildHolder.of(child);
break;
default :
default:
throw new SpeedmentException(getClass().getSimpleName() + " should only contain one child node.");
}
}
Expand All @@ -70,7 +74,7 @@ public Optional<Project> getParent() {
}

@Override
public ChildHolder getChildren() {
public ChildHolder<Child<PluginData>> getChildren() {
return children;
}

Expand All @@ -83,4 +87,4 @@ public Speedment getSpeedment() {
public Child<PluginData> metadata(Closure<?> c) {
return throwNewUnsupportedOperationExceptionImmutable();
}
}
}
Expand Up @@ -59,29 +59,38 @@ public ImmutableProject(Project project) {

public ImmutableProject(ProjectManager parent, Project project) {
super(requireNonNull(project).getName(), project.isExpanded(), project.isEnabled());

// Menbers
this.speedment = project.getSpeedment();
this.packageName = project.getPackageName();
this.packageLocation = project.getPackageLocation();
this.configPath = project.getConfigPath();
this.parent = Optional.ofNullable(parent);

// Children
dbmsChildren = (ChildHolder<Dbms>) (ChildHolder) childHolderOf(
(Class<Child<Project>>) (Class) Dbms.class,
project.streamOf(Dbms.class).map(p -> new ImmutableDbms(this, p)));

pluginDataChildren = (ChildHolder<PluginData>) (ChildHolder) childHolderOf(
(Class<Child<Project>>) (Class) PluginData.class,
project.streamOf(PluginData.class).map(p -> new ImmutablePluginData(this, p)));

@SuppressWarnings("unchecked")
final Class<Child<Project>> dbmsClass = (Class<Child<Project>>) (Class) Dbms.class;
@SuppressWarnings("unchecked")
final ChildHolder<Dbms> dbmsCh = (ChildHolder<Dbms>) (ChildHolder) childHolderOf(
dbmsClass,
project.streamOf(Dbms.class).map(p -> new ImmutableDbms(this, p)));
dbmsChildren = dbmsCh;

@SuppressWarnings("unchecked")
final Class<Child<Project>> pluginClass = (Class<Child<Project>>) (Class) PluginData.class;
@SuppressWarnings("unchecked")
final ChildHolder<PluginData> pluginDataCh = (ChildHolder<PluginData>) (ChildHolder) childHolderOf(
pluginClass,
project.streamOf(PluginData.class).map(p -> new ImmutablePluginData(this, p)));

pluginDataChildren = pluginDataCh;

// Special
this.tableNameMap = new HashMap<>();
traverseOver(Table.class).forEach(t -> {
tableNameMap.put(t.getRelativeName(Dbms.class), t);
});

// resolve all dependencies that needs to be done post tree construction
traverse().forEach(node -> {
if (node instanceof ImmutableAbstractConfigEntity) {
Expand Down Expand Up @@ -139,13 +148,13 @@ public void setConfigPath(Path configPath) {
public Speedment getSpeedment() {
return speedment;
}

@Override
public Stream<? extends Child<Project>> stream() {
return Stream.of(dbmsChildren, pluginDataChildren)
.flatMap(ChildHolder::stream);
.flatMap(ChildHolder::stream);
}

@Override
public <T extends Child<Project>> Stream<T> streamOf(Class<T> childClass) {
if (Dbms.class.equals(childClass)) {
Expand All @@ -158,13 +167,13 @@ public <T extends Child<Project>> Stream<T> streamOf(Class<T> childClass) {
return result;
} else {
throw new SpeedmentException(
"'" + childClass.getName() +
"' is not a child to '" +
getClass().getSimpleName() + "'."
"'" + childClass.getName()
+ "' is not a child to '"
+ getClass().getSimpleName() + "'."
);
}
}

@Override
public <T extends Child<Project>> T find(Class<T> childClass, String name) throws SpeedmentException {
if (Dbms.class.equals(childClass)) {
Expand All @@ -175,9 +184,11 @@ public <T extends Child<Project>> T find(Class<T> childClass, String name) throw
@SuppressWarnings("unchecked")
final T result = (T) pluginDataChildren.find(name);
return result;
} else throw thereIsNo(childClass, this.getClass(), name).get();
} else {
throw thereIsNo(childClass, this.getClass(), name).get();
}
}

@Override
public Table findTableByName(String fullName) {
final Table table = tableNameMap.get(fullName);
Expand All @@ -191,7 +202,7 @@ public Table findTableByName(String fullName) {
public Dbms dbms(Closure<?> c) {
return throwNewUnsupportedOperationExceptionImmutable();
}

@Override
public PluginData pluginData(Closure<?> c) {
return throwNewUnsupportedOperationExceptionImmutable();
Expand Down

0 comments on commit 475045c

Please sign in to comment.