protected Entity proxy(Entity target) { return target instanceof AbstractEntity ? ((AbstractEntity)target).getProxyIfAvailable() : target; }
/** * Returns the proxy form (if available) of the entity. If already a proxy, returns unmodified. * * If null is passed in, then null is returned. * * For legacy entities (that did not use {@link EntitySpec} or YAML for creation), the * proxy may not be avilable; in which case the concrete class passed in will be returned. */ @Beta @SuppressWarnings("unchecked") public static <T extends Entity> T proxy(T e) { return (e == null) ? null : e instanceof Proxy ? e : (T) ((AbstractEntity)e).getProxyIfAvailable(); }
public <T> void emitInternal(Sensor<T> sensor, T val) { if (getManagementSupport().isNoLongerManaged()) throw new IllegalStateException("Entity "+AbstractEntity.this+" is no longer managed, when trying to publish "+sensor+" "+val); SubscriptionContext subsContext = subscriptions().getSubscriptionContext(); if (subsContext != null) subsContext.publish(sensor.newEvent(getProxyIfAvailable(), val)); } }
@Override public void clearParent() { if (parent.isNull()) return; Entity oldParent = parent.get(); parent.clear(); if (oldParent != null) { if (!Entities.isNoLongerManaged(oldParent)) oldParent.removeChild(getProxyIfAvailable()); } }
suppliedParent.addChild(getProxyIfAvailable());
/** * Adds the given entity as a child of this parent <em>and</em> sets this entity as the parent of the child; * returns argument passed in, for convenience. * <p> * The child is NOT managed, even if the parent is already managed at this point * (e.g. the child is added *after* the parent's {@link AbstractEntity#init()} is invoked) * and so will need an explicit <code>getEntityManager().manage(childReturnedFromThis)</code> call. * <i>These semantics are currently under review.</i> */ @Override public <T extends Entity> T addChild(T child) { checkNotNull(child, "child must not be null (for entity %s)", this); CatalogUtils.setCatalogItemIdOnAddition(this, child); synchronized (getAttributesSynchObjectInternal()) { // hold synch locks in this order to prevent deadlock synchronized (children) { if (Entities.isAncestor(this, child)) throw new IllegalStateException("loop detected trying to add child "+child+" to "+this+"; it is already an ancestor"); child.setParent(getProxyIfAvailable()); boolean changed = children.add(child); getManagementSupport().getEntityChangeListener().onChildrenChanged(); if (changed) { sensors().emit(AbstractEntity.CHILD_ADDED, child); } } } return child; }
entity.addChild(getProxyIfAvailable()); config().refreshInheritedConfig(); previouslyOwned = true;
/** * Creates an entity using the given spec, and adds it as a child of this entity. * * @see #addChild(Entity) * @see EntityManager#createEntity(EntitySpec) * * @throws IllegalArgumentException If {@code spec.getParent()} is set and is different from this entity */ @Override public <T extends Entity> T addChild(EntitySpec<T> spec) { if (spec.getParent()==null) { spec = EntitySpec.create(spec).parent(getProxyIfAvailable()); } if (!this.equals(spec.getParent())) { throw new IllegalArgumentException("Attempt to create child of "+this+" with entity spec "+spec+ " failed because spec has different parent: "+spec.getParent()); } // The spec now includes this as the parent, so no need to call addChild; // that is done by InternalEntityFactory. return getEntityManager().createEntity(spec); }
if (entity.getParent()!=null) entity.getParent().removeChild(entity.getProxyIfAvailable());
parent = (parent instanceof AbstractEntity) ? ((AbstractEntity)parent).getProxyIfAvailable() : parent; entity.setParent(parent);