@Override void doReplace(Entity.Blueprint blueprint, Object attachment) { blueprints.put(myPath, new FullNode(blueprint, attachment)); children.remove(myPath); } }
private Offline(Root root, Map<RelativePath, FullNode> entities, Map<RelativePath, Map<EntityType, Set<FullNode>>> children) { this.root = root; this.children = children; this.entities = entities; if (!entities.containsKey(EmptyRelativePath.I)) { entities.put(EmptyRelativePath.I, new FullNode(root, null)); } }
private Offline(Root root, Map<RelativePath, FullNode> entities, Map<RelativePath, Map<EntityType, Set<FullNode>>> children) { this.root = root; this.children = children; this.entities = entities; if (!entities.containsKey(EmptyRelativePath.I)) { entities.put(EmptyRelativePath.I, new FullNode(root, null)); } }
InventoryStructure<B> of(E rootEntity, Inventory inventory) { return new InventoryStructure<B>() { FullNode root = new FullNode(Inventory.asBlueprint(rootEntity), rootEntity); FullNode n = new FullNode(blueprint, e); cache.put(e.getPath(), n); return n; .map(e -> { @SuppressWarnings("unchecked") FullNode n = new FullNode((Entity.Blueprint) Inventory.asBlueprint(e), e); cache.put(e.getPath(), n); return n; FullNode ret = new FullNode(b, entity); cache.put(entity.getPath(), ret);
/** * 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); }
/** * 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); }
@SuppressWarnings("unchecked") private FullNode doGet(RelativePath path) { CanonicalPath pathToElement = rootEntity.getPath().modified().extend(path.getPath()).get(); try { FullNode cached = cache.get(pathToElement); if (cached != null) { return cached == FullNode.EMPTY ? null : cached; } Entity<Entity.Blueprint, ?> entity = (Entity<Entity.Blueprint, ?>) inventory.inspect(pathToElement, ResolvableToSingle.class).entity(); Entity.Blueprint b = Inventory.asBlueprint(entity); FullNode ret = new FullNode(b, entity); cache.put(entity.getPath(), ret); return ret; } catch (EntityNotFoundException e) { cache.put(pathToElement, FullNode.EMPTY); return null; } } };
@Override public Stream<FullNode> getAllChildNodes(RelativePath parent) { CanonicalPath absoluteParent = rootEntity.getPath().modified().extend(parent.getPath()).get(); Query q = Query.path().with(With.path(absoluteParent), Related.by(contains)).get(); @SuppressWarnings("rawtypes") Page<Entity> results = inventory.execute(q, Entity.class, Pager.none()); return StreamSupport.stream(results.spliterator(), false) .map(e -> { @SuppressWarnings("unchecked") FullNode n = new FullNode((Entity.Blueprint) Inventory.asBlueprint(e), e); cache.put(e.getPath(), n); return n; }) .sorted(ENTITY_ORDER) .onClose(results::close); }
@Override void doReplace(Entity.Blueprint blueprint, Object attachment) { blueprints.put(myPath, new FullNode(blueprint, attachment)); children.remove(myPath); } }
private Builder(Root root, Object attachment, RelativePath myPath, Map<RelativePath, FullNode> blueprints, Map<RelativePath, Map<EntityType, Set<FullNode>>> children) { super(myPath, blueprints, children); this.root = root; this.blueprints.put(EmptyRelativePath.I, new FullNode(root, attachment)); }
/** * By providing an empty relative path, one can retrieve the attachment of the root entity. * @param path the path to the entity in the inventory structure * @return the object attached to the entity by the inventory structure builder or null if the path is not known */ default FullNode getNode(RelativePath path) { Entity.Blueprint bl = get(path); return bl == null ? null : new FullNode(bl, null); }
private Builder(Root root, Object attachment, RelativePath myPath, Map<RelativePath, FullNode> blueprints, Map<RelativePath, Map<EntityType, Set<FullNode>>> children) { super(myPath, blueprints, children); this.root = root; this.blueprints.put(EmptyRelativePath.I, new FullNode(root, attachment)); }
@Override void doReplace(Entity.Blueprint blueprint, Object attachment) { Set<FullNode> siblings = getSiblings(); FullNode myBlueprint = blueprints.remove(myPath); siblings.remove(myBlueprint); FullNode myNode = new FullNode(blueprint, attachment); siblings.add(myNode); children.remove(myPath); myPath = parentBuilder.myPath.modified().extend(Blueprint.getSegmentTypeOf(blueprint), blueprint.getId()) .get(); blueprints.put(myPath, myNode); }
/** * By providing an empty relative path, one can retrieve the attachment of the root entity. * @param path the path to the entity in the inventory structure * @return the object attached to the entity by the inventory structure builder or null if the path is not known */ default FullNode getNode(RelativePath path) { Entity.Blueprint bl = get(path); return bl == null ? null : new FullNode(bl, null); }
@Override void doReplace(Entity.Blueprint blueprint, Object attachment) { Set<FullNode> siblings = getSiblings(); FullNode myBlueprint = blueprints.remove(myPath); siblings.remove(myBlueprint); FullNode myNode = new FullNode(blueprint, attachment); siblings.add(myNode); children.remove(myPath); myPath = parentBuilder.myPath.modified().extend(Blueprint.getSegmentTypeOf(blueprint), blueprint.getId()) .get(); blueprints.put(myPath, myNode); }