@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); }
@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); }
private void serializeLevel(InventoryStructure<?> structure, RelativePath.Extender root, JsonGenerator gen) throws IOException { RelativePath rootPath = root.get(); for (InventoryStructure.EntityType entityType : InventoryStructure.EntityType.values()) { @SuppressWarnings("unchecked") List<? extends Entity.Blueprint> children = getChildren(structure, rootPath, (Class) entityType.elementType); if (!children.isEmpty()) { gen.writeFieldName(entityType.name()); gen.writeStartArray(); for (Entity.Blueprint bl : children) { gen.writeStartObject(); gen.writeObjectField("data", bl); gen.writeFieldName("children"); gen.writeStartObject(); serializeLevel(structure, rootPath.modified().extend(entityType.segmentType, bl.getId()), gen); gen.writeEndObject(); gen.writeEndObject(); } gen.writeEndArray(); } } }
private void deserializeChildren(JsonParser p, AbstractHashTree.Builder<?, ?, T, H> bld, RelativePath.Extender parentPath, DeserializationContext ctx) throws IOException { //make a copy so that we don't modify the parent RelativePath.Extender origParentPath = parentPath; parentPath = origParentPath.get().modified(); RelativePath childPath = null; while (p.nextToken() != null) { switch (p.getCurrentToken()) { case FIELD_NAME: childPath = parentPath.extend(Path.Segment.from(p.getCurrentName())).get(); break; case START_OBJECT: AbstractHashTree.ChildBuilder<?, ?, ?, T, H> childBld = bld.startChild(); deserializeChild(p, childBld, childPath.modified(), ctx); childBld.endChild(); parentPath = origParentPath.get().modified(); break; case END_OBJECT: return; } } }
/** * 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); }
/** * 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); }
/** * 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); }
@SuppressWarnings({"unchecked", "rawtypes"}) default <E extends Entity, B extends Entity.Blueprint> Stream<FullNode> getChildNodes(RelativePath parent, Class<E> childType) { SegmentType childSegmentType = Inventory.types().byElement((Class)childType).getSegmentType(); return getChildren(parent, childType).map(b -> { RelativePath childPath = parent.modified().extend(childSegmentType, b.getId()).get(); return getNode(childPath); }).filter(Objects::nonNull); }
IntermediateHashContext progress(Entity.Blueprint bl) { SegmentType st = Blueprint.getSegmentTypeOf(bl); String id = bl.getId(); return new IntermediateHashContext(origin == null ? null : origin.modified().extend(st, id).get(), root.modified().extend(st, id).get()); } }
@Override public DataEntity.Blueprint<?> getReturnType(RelativePath rootResourceType, OperationType.Blueprint ot) { RelativePath.Extender p = rootResourceType.modified().extend(SegmentType.ot, ot.getId()).get() .slide(1, 0).modified(); return getData(p, returnType); } };
@Override public DataEntity.Blueprint<?> getConnectionConfigurationSchema(ResourceType.Blueprint rt) { RelativePath.Extender p = rt.equals(structure.getRoot()) ? RelativePath.empty() : RelativePath.empty().extend(SegmentType.rt, rt.getId()); return getData(p, connectionConfigurationSchema); }
@Override public DataEntity.Blueprint<?> getConfigurationSchema(ResourceType.Blueprint rt) { RelativePath.Extender p = rt.equals(structure.getRoot()) ? RelativePath.empty() : RelativePath.empty().extend(SegmentType.rt, rt.getId()); return getData(p, configurationSchema); }
@Override public List<Metric.Blueprint> getResourceMetrics(RelativePath rootPath, Resource.Blueprint parentResource) { RelativePath p = rootPath.modified().extend(SegmentType.r, parentResource.getId()).get() .slide(1, 0); try (Stream<Metric.Blueprint> s = structure.getChildren(p, Metric.class)) { return s.collect(toList()); } }
@Override public DataEntity.Blueprint<?> getParameterTypes(RelativePath rootResourceType, OperationType.Blueprint ot) { RelativePath.Extender p = rootResourceType.modified().extend(SegmentType.ot, ot.getId()).get() .slide(1, 0).modified(); return getData(p, parameterTypes); }
@Override public List<Resource.Blueprint> getResources(RelativePath rootPath, Resource.Blueprint parentResource) { RelativePath p = rootPath.modified().extend(SegmentType.r, parentResource.getId()).get() .slide(1, 0); try (Stream<Resource.Blueprint> s = structure.getChildren(p, Resource.class)) { return s.collect(toList()); } }
@Override public DataEntity.Blueprint<?> getConnectionConfiguration(RelativePath root, Resource.Blueprint parentResource) { RelativePath.Extender resourcePath = root.modified().extend(SegmentType.r, parentResource.getId()) .get().slide(1, 0).modified(); return getData(resourcePath, connectionConfiguration); }
@Override public DataEntity.Blueprint<?> getConfiguration(RelativePath rootPath, Resource.Blueprint parentResource) { RelativePath.Extender resourcePath = rootPath.modified() .extend(SegmentType.r, parentResource.getId()).get().slide(1, 0).modified(); return getData(resourcePath, configuration); }
private DataEntity.Blueprint<?> getData(RelativePath.Extender parentPath, DataRole dataRole) { Blueprint b = structure.get(parentPath.extend(SegmentType.d, dataRole.name()).get()); return b == null ? dummyDataBlueprint(dataRole) : (DataEntity.Blueprint<?>) b; }
@SuppressWarnings({"unchecked", "rawtypes"}) default <E extends Entity<? extends B, ?>, B extends Entity.Blueprint> Stream<FullNode> getChildNodes(RelativePath parent, Class<E> childType) { SegmentType childSegmentType = Inventory.types().byElement((Class)childType).getSegmentType(); return getChildren(parent, childType).map(b -> { RelativePath childPath = parent.modified().extend(childSegmentType, b.getId()).get(); return getNode(childPath); }).filter(n -> n != null); }