Skip to content

Commit

Permalink
#28 Adapt Chronograph for Passage Operator
Browse files Browse the repository at this point in the history
Make Brick domain-aware

Signed-off-by: Alexander Fedorov <[email protected]>
  • Loading branch information
ruspl-afed committed May 17, 2020
1 parent 068d7de commit 089f00e
Show file tree
Hide file tree
Showing 32 changed files with 215 additions and 200 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public interface Chronograph {

/**
* Structure data according to the given type sequence: from section to brick
* Structure data according to the given type sequence
*
* @param types the types to group the input
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ public interface Access<I> {
<D> Function<D, String> identification(Class<D> type);

/**
* "map" function for the given type to define groups
* "adapt" function for the given type to define groups
*
* @param <G>
* @param grouping
* @return
*/
<G> Function<I, Optional<G>> map(Class<G> grouping);
<G> Function<I, Optional<G>> adapt(Class<G> grouping);

/**
* "grouping" function to collect by groups
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* The Brick describes the drawing element on the stage
*
*/
public interface Brick {
public interface Brick<D> {

/**
*
Expand All @@ -30,4 +30,11 @@ public interface Brick {
* @return original Brick position
*/
Position position();

/**
* Associated domain object
*
* @return domain object
*/
D data();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
* Container for Brick typed elements
*
*/
public interface BrickContainer {
public interface BrickContainer<D> {

/**
*
* @return stored Brick elements in List data structure
*/
List<? extends Brick> bricks();
List<? extends Brick<D>> bricks();
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* The Group describes typed container with sequence Brick items
*
*/
public interface Group extends BrickContainer, GroupContainer {
public interface Group<D> extends BrickContainer<D>, GroupContainer<D> {

/**
*
Expand All @@ -29,6 +29,6 @@ public interface Group extends BrickContainer, GroupContainer {
*
* @return GroupContainer as a parent element
*/
GroupContainer container();
GroupContainer<D> container();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@
* Container for Group typed elements
*
*/
public interface GroupContainer {
public interface GroupContainer<D> {

/**
*
* @return stored Group elements in List data structure
*/
List<? extends Group> groups();
List<Group<D>> groups();
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* The Section describes typed container with sequence of Group and Brick items
*
*/
public interface Section extends GroupContainer, BrickContainer {
public interface Section<D> extends GroupContainer<D>, BrickContainer<D> {

/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@
* Encapsulates the storage of objects to be displayed
*
*/
public interface Storage {
public interface Storage<D> {

/**
*
* @param predicate the predicate to filter the bricks
* @return
*/
// FIXME: not sure that we should use interface from "graphics" here
List<Brick> query(Predicate<Brick> predicate);
List<Brick<D>> query(Predicate<Brick<D>> predicate);

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import org.eclipse.chronograph.internal.api.graphics.Brick;
import org.eclipse.chronograph.internal.api.graphics.Position;

public class BrickImpl implements Brick {
public class BrickImpl<D> implements Brick<D> {

private final D data;
private final String id;
private final Position position;

public BrickImpl(String id, int start, int end) {
// FIXME: should we pass function to resolve everything interesting here
public BrickImpl(String id, int start, int end, D data) {
this.data = data;
this.id = id;
this.position = new PositionImpl(start, end);
}
Expand All @@ -36,4 +39,9 @@ public Position position() {
return position;
}

@Override
public D data() {
return data;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,25 @@
* Implementation of {@link Group} interface
*
*/
public class GroupImpl implements Group {
public class GroupImpl<D> implements Group<D> {

private final String id;
private final GroupContainer parent;
private final List<Brick> bricks;
private final GroupContainer<D> parent;
private final List<Brick<D>> bricks;

public GroupImpl(String id, GroupContainer parent) {
public GroupImpl(String id, GroupContainer<D> parent) {
this.id = id;
this.parent = parent;
this.bricks = new ArrayList<Brick>();
this.bricks = new ArrayList<>();
}

public GroupImpl(String id, List<Brick> bricks) {
public GroupImpl(String id, List<Brick<D>> bricks) {
this.id = id;
this.parent = null;
this.bricks = bricks;
}

public GroupImpl(String id, GroupContainer parent, List<Brick> bricks) {
public GroupImpl(String id, GroupContainer<D> parent, List<Brick<D>> bricks) {
this.id = id;
this.parent = parent;
this.bricks = bricks;
Expand All @@ -55,17 +55,17 @@ public String id() {
}

@Override
public List<? extends Brick> bricks() {
public List<? extends Brick<D>> bricks() {
return bricks;
}

@Override
public GroupContainer container() {
public GroupContainer<D> container() {
return parent;
}

@Override
public List<? extends Group> groups() {
public List<Group<D>> groups() {
return parent.groups();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,40 @@
* Class intended to aggregate data
*
*/
public class PlainData<I> implements Storage {
public class PlainData<D> implements Storage<D> {

private final Access<I> access;
private final Access<D> access;
private final List<Class<?>> structure;
private final Map<String, Section> sectionsById = new HashMap<>();
private final Map<String, List<Group>> groupsBySection = new HashMap<>();
private final Map<Group, List<Group>> subGroupsBygroup = new HashMap<>();
private final Map<Group, List<Brick>> bricksBySubgroup = new HashMap<>();
private final Map<String, Section<D>> sectionsById = new HashMap<>();
private final Map<String, List<Group<D>>> groupsBySection = new HashMap<>();
private final Map<Group<D>, List<Group<D>>> subGroupsBygroup = new HashMap<>();
private final Map<Group<D>, List<Brick<D>>> bricksBySubgroup = new HashMap<>();

public PlainData(Access<I> access) {
public PlainData(Access<D> access) {
this.access = access;
this.structure = new ArrayList<>();
}

public List<Section> getSections() {
return new ArrayList<Section>(sectionsById.values());
public List<Section<D>> getSections() {
return new ArrayList<>(sectionsById.values());
}

public List<Group> getGroupBySection(Section section) {
public List<Group<D>> getGroupBySection(Section<D> section) {
return groupsBySection.getOrDefault(section.id(), Collections.emptyList());
}

public List<Group> getSubGroupByGroupSection(Group group) {
public List<Group<D>> getSubGroupByGroupSection(Group<D> group) {
return subGroupsBygroup.getOrDefault(group, Collections.emptyList());
}

public List<Brick> getBrickBySubgroup(String subgroupId, String groupId, String sectionId) {
public List<Brick<D>> getBrickBySubgroup(String subgroupId, String groupId, String sectionId) {
if (!sectionId.isEmpty()) {
Section section = sectionsById.get(sectionId);
List<Group> groups = groupsBySection.get(section.id());
for (Group group : groups) {
Section<D> section = sectionsById.get(sectionId);
List<Group<D>> groups = groupsBySection.get(section.id());
for (Group<D> group : groups) {
if (group.id().equals(groupId)) {
List<Group> subGroups = subGroupsBygroup.get(group);
for (Group subGroup : subGroups) {
List<Group<D>> subGroups = subGroupsBygroup.get(group);
for (Group<D> subGroup : subGroups) {
return bricksBySubgroup.get(subGroup);
}
}
Expand All @@ -76,7 +76,7 @@ public List<Brick> getBrickBySubgroup(String subgroupId, String groupId, String
}

@Override
public List<Brick> query(Predicate<Brick> predicate) {
public List<Brick<D>> query(Predicate<Brick<D>> predicate) {
return bricksBySubgroup.values().stream() //
.flatMap(List::stream) //
.filter(predicate) //
Expand All @@ -91,52 +91,52 @@ public void restructure(List<Class<?>> types) {
return;
}
structure.addAll(types);
Predicate<I> filter = (Predicate<I>) t -> true; // FIXME: support filters
List<I> input = access.input().apply(filter);
Predicate<D> filter = (Predicate<D>) t -> true; // FIXME: support filters
List<D> input = access.input().apply(filter);
@SuppressWarnings("unchecked")
Class<Object> type0 = (Class<Object>) types.get(0);
@SuppressWarnings("unchecked")
Class<Object> type1 = (Class<Object>) types.get(1);
@SuppressWarnings("unchecked")
Class<Object> type2 = (Class<Object>) types.get(2);
Map<String, List<I>> grouping0 = input.stream().collect(Collectors.groupingBy(access.grouping(type0)));
Map<String, List<I>> grouping1 = input.stream().collect(Collectors.groupingBy(access.grouping(type1)));
Map<String, List<I>> grouping2 = input.stream().collect(Collectors.groupingBy(access.grouping(type2)));
List<Section> sections = input.stream().map(access.map(type0))//
Map<String, List<D>> grouping0 = input.stream().collect(Collectors.groupingBy(access.grouping(type0)));
Map<String, List<D>> grouping1 = input.stream().collect(Collectors.groupingBy(access.grouping(type1)));
Map<String, List<D>> grouping2 = input.stream().collect(Collectors.groupingBy(access.grouping(type2)));
List<Section<D>> sections = input.stream().map(access.adapt(type0))//
.filter(Optional::isPresent)//
.map(Optional::get)//
.distinct()//
.map(access.identification(type0))//
.map(id -> new SectionImpl(id))//
.map(id -> new SectionImpl<D>(id))//
.collect(Collectors.toList());
for (Section section : sections) {
List<I> g0 = grouping0.getOrDefault(section.id(), Collections.emptyList());
for (Section<D> section : sections) {
List<D> g0 = grouping0.getOrDefault(section.id(), Collections.emptyList());
sectionsById.put(section.id(), section);
List<Group> groups = input.stream().map(access.map(type1))//
List<Group<D>> groups = input.stream().map(access.adapt(type1))//
.filter(Optional::isPresent)//
.map(Optional::get)//
.distinct()//
.map(access.identification(type1))//
.map(id -> new GroupImpl(id, section))//
.map(id -> new GroupImpl<D>(id, section))//
.collect(Collectors.toList());
groupsBySection.put(section.id(), groups);
for (Group group : groups) {
for (Group<D> group : groups) {
String id1 = group.id();
List<I> g1 = grouping1.getOrDefault(id1, Collections.emptyList());
List<Group> subGroups = input.stream().map(access.map(type2))//
List<D> g1 = grouping1.getOrDefault(id1, Collections.emptyList());
List<Group<D>> subGroups = input.stream().map(access.adapt(type2))//
.filter(Optional::isPresent)//
.map(Optional::get)//
.distinct()//
.map(access.identification(type2))//
.map(id -> new GroupImpl(id, group))//
.map(id -> new GroupImpl<D>(id, group))//
.collect(Collectors.toList());
subGroupsBygroup.put(group, subGroups);
for (Group subGroup : subGroups) {
List<Brick> bricks = grouping2.getOrDefault(subGroup.id(), Collections.emptyList()).stream()//
for (Group<D> subGroup : subGroups) {
List<Brick<D>> bricks = grouping2.getOrDefault(subGroup.id(), Collections.emptyList()).stream()//
.filter(g0::contains)//
.filter(g1::contains)//
.map(i -> new BrickImpl(access.identification(access.type()).apply(i),
access.start().apply(i), access.end().apply(i)))//
.map(i -> new BrickImpl<>(access.identification(access.type()).apply(i),
access.start().apply(i), access.end().apply(i), i))//
.collect(Collectors.toList());
bricksBySubgroup.put(subGroup, bricks);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
* Implementation of {@link Section}
*
*/
public class SectionImpl implements Section {
public class SectionImpl<D> implements Section<D> {

private final String id;
private final List<Group> groups;
private final List<Group<D>> groups;

public SectionImpl(String id) {
this.id = id;
this.groups = new ArrayList<>();
}

public SectionImpl(String id, List<Group> groups) {
public SectionImpl(String id, List<Group<D>> groups) {
this.id = id;
this.groups = groups;
}
Expand All @@ -46,14 +46,14 @@ public String id() {
}

@Override
public List<Group> groups() {
public List<Group<D>> groups() {
return groups;
}

@Override
public List<Brick> bricks() {
List<Brick> bricks = new ArrayList<>();
for (Group gr : groups) {
public List<Brick<D>> bricks() {
List<Brick<D>> bricks = new ArrayList<>();
for (Group<D> gr : groups) {
bricks.addAll(gr.bricks());
}
return bricks;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public <D> Function<D, String> identification(Class<D> domain) {
}

@Override
public <G> Function<I, Optional<G>> map(Class<G> group) {
public <G> Function<I, Optional<G>> adapt(Class<G> group) {
return p -> Optional.empty();
}

Expand Down
Loading

0 comments on commit 089f00e

Please sign in to comment.