structParent.startChild(Inventory.asBlueprint(e)); currentStructRow.add(childStructBuilder);
/** * Replaces the blueprint on this position in the structure with another. The blueprint must have the same type * as the original one. * * @param blueprint the blueprint to replace the current with * @param attachment the object to attach to the entity blueprint * @return this builder */ public This replace(Entity.Blueprint blueprint, Object attachment) { removeAllChildren(); FullNode myBl = blueprints.get(myPath); if (!myBl.getEntity().getClass().equals(blueprint.getClass())) { throw new IllegalArgumentException("Blueprint " + blueprint + " not of the same type as " + myBl.getEntity()); } doReplace(blueprint, attachment); return castThis(); }
type.blueprintType); InventoryStructure.ChildBuilder<?> childBld = bld.startChild(bl);
@SuppressWarnings("unchecked") private InventoryStructure.Builder<B> mergeDeepTree(InventoryStructure<?> currentTree, RelativePath treePath, InventoryStructure.AbstractBuilder<?> newTree, Set<SegmentType> mergedTypes) { DBG.debugf("Starting to merge shallow tree. Loading children under [%s]", treePath); try (Stream<InventoryStructure.FullNode> currentChildren = currentTree.getAllChildNodes(treePath)) { DBG.debugf("Done loading the children under [%s]", treePath); Set<Path.Segment> newChildPaths = newTree.getChildrenPaths(); currentChildren.forEach(currentChild -> { SegmentType childType = Inventory.types().byBlueprint(currentChild.getEntity().getClass()).getSegmentType(); Path.Segment childPathSegment = new Path.Segment(childType, currentChild.getEntity().getId()); if (newChildPaths.contains(childPathSegment)) { mergeDeepTree(currentTree, treePath.modified().extend(childPathSegment).get(), newTree.getChild(childPathSegment), mergedTypes); } else if (!mergedTypes.contains(childType)) { newTree.addChild(currentChild.getEntity(), currentChild.getAttachment()); mergeDeepTree(currentTree, treePath.modified().extend(childPathSegment).get(), newTree.getChild(childPathSegment), mergedTypes); } }); DBG.debugf("Done merging shallow tree of [%s]", treePath); if (newTree instanceof InventoryStructure.Builder) { return (InventoryStructure.Builder<B>) newTree; } else { return null; } } }
@SuppressWarnings("unchecked") private InventoryStructure.Builder<B> mergeShallowTree(InventoryStructure<?> currentTree, RelativePath treePath, InventoryStructure.AbstractBuilder<?> newTree, Set<SegmentType> mergedTypes) { DBG.debugf("Starting to merge shallow tree. Loading children under [%s]", treePath); try (Stream<InventoryStructure.FullNode> currentChildren = currentTree.getAllChildNodes(treePath)) { DBG.debugf("Done loading the children under [%s]", treePath); Set<Path.Segment> newChildPaths = newTree.getChildrenPaths(); currentChildren.forEach(currentChild -> { SegmentType childType = Inventory.types().byBlueprint(currentChild.getEntity().getClass()).getSegmentType(); Path.Segment childPathSegment = new Path.Segment(childType, currentChild.getEntity().getId()); if (newChildPaths.contains(childPathSegment)) { mergeShallowTree(currentTree, treePath.modified().extend(childPathSegment).get(), newTree.getChild(childPathSegment), mergedTypes); } else if (!mergedTypes.contains(childType)) { newTree.addChild(currentChild.getEntity(), currentChild.getAttachment()); } }); DBG.debugf("Done merging shallow tree of [%s]", treePath); if (newTree instanceof InventoryStructure.Builder) { return (InventoryStructure.Builder<B>) newTree; } else { return null; } } }
/** * Starts building a new child of the currently built entity. * * @param child the child entity blueprint * @param childAttachment the attachment to store along with the child * @return a new child builder * @throws IllegalArgumentException if the provided child cannot be contained in the currently built entity * (i.e. a resource type cannot be contained in a resource for example). */ public ChildBuilder<This> startChild(Entity.Blueprint child, Object childAttachment) { RelativePath.Extender extender = myPath.modified(); Class<? extends AbstractElement> childType = Blueprint.getEntityTypeOf(child); SegmentType childSeg = Blueprint.getSegmentTypeOf(child); if (!extender.canExtendTo(childSeg)) { throw new IllegalArgumentException("Cannot extend path " + myPath + " with child of type " + childType); } RelativePath childPath = extender.extend(childSeg, child.getId()).get(); Set<FullNode> bls = getChildrenOfType(EntityType.of(childType)); FullNode node = new FullNode(child, childAttachment); bls.add(node); blueprints.put(childPath, node); return new ChildBuilder<>(castThis(), childPath, blueprints, children); }
/** * Returns a child builder of a pre-existing child. * @param childPath the path to the child * @return the child builder or null */ public ChildBuilder<This> getChild(Path.Segment childPath) { Map<EntityType, Set<FullNode>> myChildren = children.get(myPath); if (myChildren == null) { return null; } EntityType childType = EntityType.of(childPath.getElementType()); Set<FullNode> childrenOfType = myChildren.get(childType); return childrenOfType.stream().filter(child -> child.getEntity().getId().equals(childPath.getElementId())) .findAny().map(child -> { RelativePath rp = myPath.modified().extend(childPath).get(); return new ChildBuilder<>(castThis(), rp, blueprints, children); }).orElse(null); }
/** * Starts building a new child of the currently built entity. * * @param child the child entity blueprint * @param childAttachment the attachment to store along with the child * @return a new child builder * @throws IllegalArgumentException if the provided child cannot be contained in the currently built entity * (i.e. a resource type cannot be contained in a resource for example). */ public ChildBuilder<This> startChild(Entity.Blueprint child, Object childAttachment) { RelativePath.Extender extender = myPath.modified(); Class<? extends AbstractElement<?, ?>> childType = Blueprint.getEntityTypeOf(child); SegmentType childSeg = Blueprint.getSegmentTypeOf(child); if (!extender.canExtendTo(childSeg)) { throw new IllegalArgumentException("Cannot extend path " + myPath + " with child of type " + childType); } RelativePath childPath = extender.extend(childSeg, child.getId()).get(); Set<FullNode> bls = getChildrenOfType(EntityType.of(childType)); FullNode node = new FullNode(child, childAttachment); bls.add(node); blueprints.put(childPath, node); return new ChildBuilder<>(castThis(), childPath, blueprints, children); }
/** * Returns a child builder of a pre-existing child. * @param childPath the path to the child * @return the child builder or null */ public ChildBuilder<This> getChild(Path.Segment childPath) { Map<EntityType, Set<FullNode>> myChildren = children.get(myPath); if (myChildren == null) { return null; } EntityType childType = EntityType.of(childPath.getElementType()); Set<FullNode> childrenOfType = myChildren.get(childType); return childrenOfType.stream().filter(child -> child.getEntity().getId().equals(childPath.getElementId())) .findAny().map(child -> { RelativePath rp = myPath.modified().extend(childPath).get(); return new ChildBuilder<>(castThis(), rp, blueprints, children); }).orElse(null); }
public This addChild(InventoryStructure<?> structure, boolean overwrite) { return addChild(Offline.copy(structure).asBuilder(), overwrite); }
public This replace(Entity.Blueprint blueprint) { return replace(blueprint, null); }
/** * Replaces the blueprint on this position in the structure with another. The blueprint must have the same type * as the original one. * * @param blueprint the blueprint to replace the current with * @param attachment the object to attach to the entity blueprint * @return this builder */ public This replace(Entity.Blueprint blueprint, Object attachment) { removeAllChildren(); FullNode myBl = blueprints.get(myPath); if (!myBl.getEntity().getClass().equals(blueprint.getClass())) { throw new IllegalArgumentException("Blueprint " + blueprint + " not of the same type as " + myBl.getEntity()); } doReplace(blueprint, attachment); return castThis(); }
public This replace(Entity.Blueprint blueprint) { return replace(blueprint, null); }
public This addChild(Entity.Blueprint child, Object attachment) { startChild(child, attachment).end(); return castThis(); } public This addChild(InventoryStructure<?> structure, boolean overwrite) {
/** * Equivalent to calling {@code startChilld(child, null)}. * @see #startChild(Entity.Blueprint, Object) */ public ChildBuilder<This> startChild(Entity.Blueprint child) { return startChild(child, null); }
/** * Adds a new child to this entity without the possibility to add further grand-children to the child. * * @param child the child to add to this entity * @return this builder * @throws IllegalArgumentException if the provided child cannot be contained in the currently built entity * (i.e. a resource type cannot be contained in a resource for example). */ public This addChild(Entity.Blueprint child) { return addChild(child, null); }
public This addChild(Entity.Blueprint child, Object attachment) { startChild(child, attachment).end(); return castThis(); }
public This removeAllChildren() { getChildrenPaths().forEach(this::removeChild); children.remove(myPath); return castThis(); }