swim.system.downlink.MapDownlinkView Maven / Gradle / Ivy
The newest version!
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package swim.system.downlink;
import java.util.AbstractMap;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import swim.api.DownlinkException;
import swim.api.Link;
import swim.api.SwimContext;
import swim.api.downlink.MapDownlink;
import swim.api.function.DidClose;
import swim.api.function.DidConnect;
import swim.api.function.DidDisconnect;
import swim.api.function.DidFail;
import swim.api.warp.function.DidLink;
import swim.api.warp.function.DidReceive;
import swim.api.warp.function.DidSync;
import swim.api.warp.function.DidUnlink;
import swim.api.warp.function.WillCommand;
import swim.api.warp.function.WillLink;
import swim.api.warp.function.WillReceive;
import swim.api.warp.function.WillSync;
import swim.api.warp.function.WillUnlink;
import swim.collections.HashTrieMap;
import swim.concurrent.Cont;
import swim.concurrent.Stage;
import swim.observable.function.DidClear;
import swim.observable.function.DidDrop;
import swim.observable.function.DidRemoveKey;
import swim.observable.function.DidTake;
import swim.observable.function.DidUpdateKey;
import swim.observable.function.WillClear;
import swim.observable.function.WillDrop;
import swim.observable.function.WillRemoveKey;
import swim.observable.function.WillTake;
import swim.observable.function.WillUpdateKey;
import swim.streamlet.Inlet;
import swim.streamlet.KeyEffect;
import swim.streamlet.KeyOutlet;
import swim.streamlet.MapInlet;
import swim.streamlet.MapOutlet;
import swim.streamlet.Outlet;
import swim.structure.Form;
import swim.structure.Value;
import swim.structure.collections.ValueCollection;
import swim.structure.collections.ValueCursor;
import swim.structure.collections.ValueEntry;
import swim.structure.collections.ValueMapEntrySet;
import swim.structure.collections.ValueOrderedMap;
import swim.structure.collections.ValueOrderedMapCursor;
import swim.structure.collections.ValueSet;
import swim.system.CellContext;
import swim.system.LinkBinding;
import swim.system.warp.WarpDownlinkView;
import swim.uri.Uri;
import swim.util.Cursor;
import swim.util.OrderedMap;
import swim.util.OrderedMapCursor;
public class MapDownlinkView extends WarpDownlinkView implements MapDownlink {
protected final Form keyForm;
protected final Form valueForm;
protected MapDownlinkModel model;
protected MapOutlet> input;
protected HashTrieMap effects;
protected HashTrieMap> outlets;
protected Inlet super MapDownlink>[] outputs; // TODO: unify with observers
protected int version;
public MapDownlinkView(CellContext cellContext, Stage stage, Uri meshUri,
Uri hostUri, Uri nodeUri, Uri laneUri, float prio,
float rate, Value body, int flags, Form keyForm,
Form valueForm, Object observers) {
super(cellContext, stage, meshUri, hostUri, nodeUri, laneUri, prio, rate, body, flags, observers);
this.keyForm = keyForm;
this.valueForm = valueForm;
this.model = null;
this.input = null;
this.effects = HashTrieMap.empty();
this.outlets = HashTrieMap.empty();
this.outputs = null;
this.version = -1;
}
public MapDownlinkView(CellContext cellContext, Stage stage, Uri meshUri,
Uri hostUri, Uri nodeUri, Uri laneUri, float prio,
float rate, Value body, Form keyForm, Form valueForm) {
this(cellContext, stage, meshUri, hostUri, nodeUri, laneUri, prio, rate, body,
WarpDownlinkView.KEEP_LINKED | WarpDownlinkView.KEEP_SYNCED | MapDownlinkView.STATEFUL,
keyForm, valueForm, null);
}
@Override
public MapDownlinkModel downlinkModel() {
return this.model;
}
@Override
public MapDownlinkView hostUri(Uri hostUri) {
return new MapDownlinkView(this.cellContext, this.stage, this.meshUri,
hostUri, this.nodeUri, this.laneUri,
this.prio, this.rate, this.body, this.flags,
this.keyForm, this.valueForm, this.observers);
}
@Override
public MapDownlinkView hostUri(String hostUri) {
return this.hostUri(Uri.parse(hostUri));
}
@Override
public MapDownlinkView nodeUri(Uri nodeUri) {
return new MapDownlinkView(this.cellContext, this.stage, this.meshUri,
this.hostUri, nodeUri, this.laneUri,
this.prio, this.rate, this.body, this.flags,
this.keyForm, this.valueForm, this.observers);
}
@Override
public MapDownlinkView nodeUri(String nodeUri) {
return this.nodeUri(Uri.parse(nodeUri));
}
@Override
public MapDownlinkView laneUri(Uri laneUri) {
return new MapDownlinkView(this.cellContext, this.stage, this.meshUri,
this.hostUri, this.nodeUri, laneUri,
this.prio, this.rate, this.body, this.flags,
this.keyForm, this.valueForm, this.observers);
}
@Override
public MapDownlinkView laneUri(String laneUri) {
return this.laneUri(Uri.parse(laneUri));
}
@Override
public MapDownlinkView prio(float prio) {
return new MapDownlinkView(this.cellContext, this.stage, this.meshUri,
this.hostUri, this.nodeUri, this.laneUri,
prio, this.rate, this.body, this.flags,
this.keyForm, this.valueForm, this.observers);
}
@Override
public MapDownlinkView rate(float rate) {
return new MapDownlinkView(this.cellContext, this.stage, this.meshUri,
this.hostUri, this.nodeUri, this.laneUri,
this.prio, rate, this.body, this.flags,
this.keyForm, this.valueForm, this.observers);
}
@Override
public MapDownlinkView body(Value body) {
return new MapDownlinkView(this.cellContext, this.stage, this.meshUri,
this.hostUri, this.nodeUri, this.laneUri,
this.prio, this.rate, body, this.flags,
this.keyForm, this.valueForm, this.observers);
}
@Override
public MapDownlinkView keepLinked(boolean keepLinked) {
if (keepLinked) {
this.flags |= WarpDownlinkView.KEEP_LINKED;
} else {
this.flags &= ~WarpDownlinkView.KEEP_LINKED;
}
return this;
}
@Override
public MapDownlinkView keepSynced(boolean keepSynced) {
if (keepSynced) {
this.flags |= WarpDownlinkView.KEEP_SYNCED;
} else {
this.flags &= ~WarpDownlinkView.KEEP_SYNCED;
}
return this;
}
@Override
public final boolean isStateful() {
return (this.flags & MapDownlinkView.STATEFUL) != 0;
}
@Override
public MapDownlinkView isStateful(boolean isStateful) {
if (isStateful) {
this.flags |= MapDownlinkView.STATEFUL;
} else {
this.flags &= ~MapDownlinkView.STATEFUL;
}
final MapDownlinkModel model = this.model;
if (model != null) {
model.isStateful(isStateful);
}
return this;
}
void didSetStateful(boolean isStateful) {
if (isStateful) {
this.flags |= MapDownlinkView.STATEFUL;
} else {
this.flags &= ~MapDownlinkView.STATEFUL;
}
}
@Override
public final Form keyForm() {
return this.keyForm;
}
@Override
public MapDownlinkView keyForm(Form keyForm) {
return new MapDownlinkView(this.cellContext, this.stage, this.meshUri,
this.hostUri, this.nodeUri, this.laneUri,
this.prio, this.rate, this.body, this.flags,
keyForm, this.valueForm, this.typesafeObservers(this.observers));
}
@Override
public MapDownlinkView keyClass(Class keyClass) {
return this.keyForm(Form.forClass(keyClass));
}
@Override
public final Form valueForm() {
return this.valueForm;
}
@Override
public MapDownlinkView valueForm(Form valueForm) {
return new MapDownlinkView(this.cellContext, this.stage, this.meshUri,
this.hostUri, this.nodeUri, this.laneUri,
this.prio, this.rate, this.body, this.flags,
this.keyForm, valueForm, this.typesafeObservers(this.observers));
}
@Override
public MapDownlinkView valueClass(Class valueClass) {
return this.valueForm(Form.forClass(valueClass));
}
protected Object typesafeObservers(Object observers) {
// TODO: filter out WillUpdateKey, DidUpdateKey, WillRemoveKey, DidRemoveKey,
// WillDrop, DidDrop, WillTake, DidTake, WillClear, DidClear
return observers;
}
@SuppressWarnings("unchecked")
@Override
public MapDownlinkView observe(Object observer) {
return (MapDownlinkView) super.observe(observer);
}
@SuppressWarnings("unchecked")
@Override
public MapDownlinkView unobserve(Object observer) {
return (MapDownlinkView) super.unobserve(observer);
}
@Override
public MapDownlinkView willUpdate(WillUpdateKey willUpdate) {
return this.observe(willUpdate);
}
@Override
public MapDownlinkView didUpdate(DidUpdateKey didUpdate) {
return this.observe(didUpdate);
}
@Override
public MapDownlinkView willRemove(WillRemoveKey willRemove) {
return this.observe(willRemove);
}
@Override
public MapDownlinkView didRemove(DidRemoveKey didRemove) {
return this.observe(didRemove);
}
@Override
public MapDownlink willDrop(WillDrop willDrop) {
return this.observe(willDrop);
}
@Override
public MapDownlink didDrop(DidDrop didDrop) {
return this.observe(didDrop);
}
@Override
public MapDownlink willTake(WillTake willTake) {
return this.observe(willTake);
}
@Override
public MapDownlink didTake(DidTake didTake) {
return this.observe(didTake);
}
@Override
public MapDownlinkView willClear(WillClear willClear) {
return this.observe(willClear);
}
@Override
public MapDownlinkView didClear(DidClear didClear) {
return this.observe(didClear);
}
@Override
public MapDownlinkView willReceive(WillReceive willReceive) {
return this.observe(willReceive);
}
@Override
public MapDownlinkView didReceive(DidReceive didReceive) {
return this.observe(didReceive);
}
@Override
public MapDownlinkView willCommand(WillCommand willCommand) {
return this.observe(willCommand);
}
@Override
public MapDownlinkView willLink(WillLink willLink) {
return this.observe(willLink);
}
@Override
public MapDownlinkView didLink(DidLink didLink) {
return this.observe(didLink);
}
@Override
public MapDownlinkView willSync(WillSync willSync) {
return this.observe(willSync);
}
@Override
public MapDownlinkView didSync(DidSync didSync) {
return this.observe(didSync);
}
@Override
public MapDownlinkView willUnlink(WillUnlink willUnlink) {
return this.observe(willUnlink);
}
@Override
public MapDownlinkView didUnlink(DidUnlink didUnlink) {
return this.observe(didUnlink);
}
@Override
public MapDownlinkView didConnect(DidConnect didConnect) {
return this.observe(didConnect);
}
@Override
public MapDownlinkView didDisconnect(DidDisconnect didDisconnect) {
return this.observe(didDisconnect);
}
@Override
public MapDownlinkView didClose(DidClose didClose) {
return this.observe(didClose);
}
@Override
public MapDownlinkView didFail(DidFail didFail) {
return this.observe(didFail);
}
public Value downlinkWillUpdateValue(Value key, Value newValue) {
return newValue;
}
public void downlinkDidUpdateValue(Value key, Value newValue, Value oldValue) {
// hook
}
public V downlinkWillUpdate(K key, V newValue) {
return newValue;
}
public void downlinkDidUpdate(K key, V newValue, V oldValue) {
this.decohereInputKey(key, KeyEffect.UPDATE);
this.recohereInput(0); // TODO: debounce and track version
}
public void downlinkWillRemoveValue(Value key) {
// hook
}
public void downlinkDidRemoveValue(Value key, Value oldValue) {
// hook
}
public void downlinkWillRemove(K key) {
// hook
}
public void downlinkDidRemove(K key, V oldValue) {
this.decohereInputKey(key, KeyEffect.REMOVE);
this.recohereInput(0); // TODO: debounce and track version
}
public void downlinkWillDrop(int lower) {
// hook
}
public void downlinkDidDrop(int lower) {
// hook
}
public void downlinkWillTake(int upper) {
// hook
}
public void downlinkDidTake(int upper) {
// hook
}
public void downlinkWillClear() {
// hook
}
public void downlinkDidClear() {
// hook
}
@SuppressWarnings("unchecked")
public Entry dispatchWillUpdate(K key, V newValue, boolean preemptive) {
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLink(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof WillUpdateKey, ?>) {
if (((WillUpdateKey, ?>) observers).isPreemptive() == preemptive) {
try {
newValue = ((WillUpdateKey) observers).willUpdate(key, newValue);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof WillUpdateKey, ?>) {
if (((WillUpdateKey, ?>) observer).isPreemptive() == preemptive) {
try {
newValue = ((WillUpdateKey) observer).willUpdate(key, newValue);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
}
}
}
return new AbstractMap.SimpleImmutableEntry(complete, newValue);
} finally {
SwimContext.setLink(oldLink);
}
}
@SuppressWarnings("unchecked")
public boolean dispatchDidUpdate(K key, V newValue, V oldValue, boolean preemptive) {
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLink(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof DidUpdateKey, ?>) {
if (((DidUpdateKey, ?>) observers).isPreemptive() == preemptive) {
try {
((DidUpdateKey) observers).didUpdate(key, newValue, oldValue);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof DidUpdateKey, ?>) {
if (((DidUpdateKey, ?>) observer).isPreemptive() == preemptive) {
try {
((DidUpdateKey) observer).didUpdate(key, newValue, oldValue);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
}
}
@SuppressWarnings("unchecked")
public boolean dispatchWillRemove(K key, boolean preemptive) {
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLink(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof WillRemoveKey>) {
if (((WillRemoveKey>) observers).isPreemptive() == preemptive) {
try {
((WillRemoveKey) observers).willRemove(key);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof WillRemoveKey>) {
if (((WillRemoveKey>) observer).isPreemptive() == preemptive) {
try {
((WillRemoveKey) observer).willRemove(key);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
}
}
@SuppressWarnings("unchecked")
public boolean dispatchDidRemove(K key, V oldValue, boolean preemptive) {
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLink(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof DidRemoveKey, ?>) {
if (((DidRemoveKey, ?>) observers).isPreemptive() == preemptive) {
try {
((DidRemoveKey) observers).didRemove(key, oldValue);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof DidRemoveKey, ?>) {
if (((DidRemoveKey, ?>) observer).isPreemptive() == preemptive) {
try {
((DidRemoveKey) observer).didRemove(key, oldValue);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
}
}
public boolean dispatchWillDrop(int lower, boolean preemptive) {
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLink(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof WillDrop) {
if (((WillDrop) observers).isPreemptive() == preemptive) {
try {
((WillDrop) observers).willDrop(lower);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof WillDrop) {
if (((WillDrop) observer).isPreemptive() == preemptive) {
try {
((WillDrop) observer).willDrop(lower);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
}
}
public boolean dispatchDidDrop(int lower, boolean preemptive) {
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLink(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof DidDrop) {
if (((DidDrop) observers).isPreemptive() == preemptive) {
try {
((DidDrop) observers).didDrop(lower);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof DidDrop) {
if (((DidDrop) observer).isPreemptive() == preemptive) {
try {
((DidDrop) observer).didDrop(lower);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
}
}
public boolean dispatchWillTake(int upper, boolean preemptive) {
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLink(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof WillTake) {
if (((WillTake) observers).isPreemptive() == preemptive) {
try {
((WillTake) observers).willTake(upper);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof WillTake) {
if (((WillTake) observer).isPreemptive() == preemptive) {
try {
((WillTake) observer).willTake(upper);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
}
}
public boolean dispatchDidTake(int upper, boolean preemptive) {
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLink(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof DidTake) {
if (((DidTake) observers).isPreemptive() == preemptive) {
try {
((DidTake) observers).didTake(upper);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof DidTake) {
if (((DidTake) observer).isPreemptive() == preemptive) {
try {
((DidTake) observer).didTake(upper);
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
}
}
public boolean dispatchWillClear(boolean preemptive) {
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLink(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof WillClear) {
if (((WillClear) observers).isPreemptive() == preemptive) {
try {
((WillClear) observers).willClear();
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof WillClear) {
if (((WillClear) observer).isPreemptive() == preemptive) {
try {
((WillClear) observer).willClear();
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
}
}
public boolean dispatchDidClear(boolean preemptive) {
final Link oldLink = SwimContext.getLink();
try {
SwimContext.setLink(this);
final Object observers = this.observers;
boolean complete = true;
if (observers instanceof DidClear) {
if (((DidClear) observers).isPreemptive() == preemptive) {
try {
((DidClear) observers).didClear();
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
} else if (observers instanceof Object[]) {
final Object[] array = (Object[]) observers;
for (int i = 0, n = array.length; i < n; i += 1) {
final Object observer = array[i];
if (observer instanceof DidClear) {
if (((DidClear) observer).isPreemptive() == preemptive) {
try {
((DidClear) observer).didClear();
} catch (Throwable error) {
if (Cont.isNonFatal(error)) {
this.downlinkDidFail(error);
}
throw error;
}
} else if (preemptive) {
complete = false;
}
}
}
}
return complete;
} finally {
SwimContext.setLink(oldLink);
}
}
@Override
public MapDownlinkModel createDownlinkModel() {
return new MapDownlinkModel(this.meshUri, this.hostUri, this.nodeUri,
this.laneUri, this.prio, this.rate, this.body);
}
@Override
public MapDownlinkView open() {
if (this.model == null) {
final LinkBinding linkBinding = this.cellContext.bindDownlink(this);
if (linkBinding instanceof MapDownlinkModel) {
this.model = (MapDownlinkModel) linkBinding;
this.model.addDownlink(this);
} else {
throw new DownlinkException("downlink type mismatch");
}
}
return this;
}
@Override
public void close() {
super.close();
this.model = null;
}
@Override
public boolean isEmpty() {
return this.model.isEmpty();
}
@Override
public int size() {
return this.model.size();
}
@SuppressWarnings("unchecked")
@Override
public boolean containsKey(Object keyObject) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
return this.model.containsKey(key);
}
return false;
}
@SuppressWarnings("unchecked")
@Override
public boolean containsValue(Object valueObject) {
final Class> valueType = this.valueForm.type();
if (valueType == null || valueType.isInstance(valueObject)) {
final Value value = this.valueForm.mold((V) valueObject).toValue();
return this.model.containsValue(value);
}
return false;
}
@SuppressWarnings("unchecked")
@Override
public int indexOf(Object keyObject) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
return this.model.indexOf(key);
}
throw new IllegalArgumentException(keyObject.toString());
}
@SuppressWarnings("unchecked")
@Override
public V get(Object keyObject) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value value = this.model.get(key);
final V valueObject = this.valueForm.cast(value);
if (valueObject != null) {
return valueObject;
}
}
return this.valueForm.unit();
}
@SuppressWarnings("unchecked")
@Override
public Entry getEntry(Object keyObject) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Entry entry = this.model.getEntry(key);
if (entry != null) {
return new ValueEntry(entry, this.keyForm, this.valueForm);
}
}
return null;
}
@Override
public Entry getIndex(int index) {
final Entry entry = this.model.getIndex(index);
if (entry != null) {
return new ValueEntry(entry, this.keyForm, this.valueForm);
}
return null;
}
@Override
public Entry firstEntry() {
final Entry entry = this.model.firstEntry();
if (entry != null) {
return new ValueEntry(entry, this.keyForm, this.valueForm);
}
return null;
}
@Override
public K firstKey() {
final Value key = this.model.firstKey();
final K keyObject = this.keyForm.cast(key);
if (keyObject != null) {
return keyObject;
}
return this.keyForm.unit();
}
@Override
public V firstValue() {
final Value value = this.model.firstValue();
final V object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@Override
public Entry lastEntry() {
final Entry entry = this.model.lastEntry();
if (entry != null) {
return new ValueEntry(entry, this.keyForm, this.valueForm);
}
return null;
}
@Override
public K lastKey() {
final Value key = this.model.lastKey();
final K keyObject = this.keyForm.cast(key);
if (keyObject != null) {
return keyObject;
}
return this.keyForm.unit();
}
@Override
public V lastValue() {
final Value value = this.model.lastValue();
final V object = this.valueForm.cast(value);
if (object != null) {
return object;
}
return this.valueForm.unit();
}
@SuppressWarnings("unchecked")
@Override
public Entry nextEntry(Object keyObject) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Entry entry = this.model.nextEntry(key);
if (entry != null) {
return new ValueEntry(entry, this.keyForm, this.valueForm);
}
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public K nextKey(Object keyObject) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value nextKey = this.model.nextKey(key);
final K nextKeyObject = this.keyForm.cast(nextKey);
if (nextKeyObject != null) {
return nextKeyObject;
}
return this.keyForm.unit();
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public V nextValue(Object keyObject) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value nextValue = this.model.nextValue(key);
final V nextObject = this.valueForm.cast(nextValue);
if (nextObject != null) {
return nextObject;
}
return this.valueForm.unit();
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public Entry previousEntry(Object keyObject) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Entry entry = this.model.previousEntry(key);
if (entry != null) {
return new ValueEntry(entry, this.keyForm, this.valueForm);
}
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public K previousKey(Object keyObject) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value previousKey = this.model.previousKey(key);
final K previousKeyObject = this.keyForm.cast(previousKey);
if (previousKeyObject != null) {
return previousKeyObject;
}
return this.keyForm.unit();
}
return null;
}
@SuppressWarnings("unchecked")
@Override
public V previousValue(Object keyObject) {
final Class> keyType = this.keyForm.type();
if (keyType == null || keyType.isInstance(keyObject)) {
final Value key = this.keyForm.mold((K) keyObject).toValue();
final Value previousValue = this.model.previousValue(key);
final V previousObject = this.valueForm.cast(previousValue);
if (previousObject != null) {
return previousObject;
}
return this.valueForm.unit();
}
return null;
}
@Override
public V put(K key, V value) {
return this.model.put(this, key, value);
}
@Override
public void putAll(Map extends K, ? extends V> map) {
for (Entry extends K, ? extends V> entry : map.entrySet()) {
this.model.put(this, entry.getKey(), entry.getValue());
}
}
@SuppressWarnings("unchecked")
@Override
public V remove(Object key) {
return this.model.remove(this, (K) key);
}
@Override
public void drop(int lower) {
this.model.drop(this, lower);
}
@Override
public void take(int upper) {
this.model.take(this, upper);
}
@Override
public void clear() {
this.model.clear(this);
}
@SuppressWarnings("unchecked")
@Override
public OrderedMap headMap(K toKeyObject) {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
final Value toKey = this.keyForm.mold(toKeyObject).toValue();
return new ValueOrderedMap(this.model.headMap(toKey), this.keyForm, this.valueForm);
} else {
return (OrderedMap) (OrderedMap, ?>) this.model.headMap((Value) toKeyObject);
}
}
@SuppressWarnings("unchecked")
@Override
public OrderedMap tailMap(K fromKeyObject) {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
final Value fromKey = this.keyForm.mold(fromKeyObject).toValue();
return new ValueOrderedMap(this.model.tailMap(fromKey), this.keyForm, this.valueForm);
} else {
return (OrderedMap) (OrderedMap, ?>) this.model.tailMap((Value) fromKeyObject);
}
}
@SuppressWarnings("unchecked")
@Override
public OrderedMap subMap(K fromKeyObject, K toKeyObject) {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
final Value fromKey = this.keyForm.mold(fromKeyObject).toValue();
final Value toKey = this.keyForm.mold(toKeyObject).toValue();
return new ValueOrderedMap(this.model.subMap(fromKey, toKey), this.keyForm, this.valueForm);
} else {
return (OrderedMap) (OrderedMap, ?>) this.model.subMap((Value) fromKeyObject, (Value) toKeyObject);
}
}
@SuppressWarnings("unchecked")
@Override
public Set> entrySet() {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
return new ValueMapEntrySet(this.model.state, this.keyForm, this.valueForm);
} else {
return (Set>) (Set>) this.model.entrySet();
}
}
@SuppressWarnings("unchecked")
@Override
public Set keySet() {
if (this.keyForm != Form.forValue()) {
return new ValueSet(this.model.keySet(), this.keyForm);
} else {
return (Set) this.model.keySet();
}
}
@SuppressWarnings("unchecked")
@Override
public Collection values() {
if (this.valueForm != Form.forValue()) {
return new ValueCollection(this.model.values(), this.valueForm);
} else {
return (Collection) this.model.values();
}
}
@SuppressWarnings("unchecked")
@Override
public OrderedMapCursor iterator() {
if (this.keyForm != Form.forValue() || this.valueForm != Form.forValue()) {
return new ValueOrderedMapCursor(this.model.iterator(), this.keyForm, this.valueForm);
} else {
return (OrderedMapCursor) (OrderedMapCursor, ?>) this.model.iterator();
}
}
@SuppressWarnings("unchecked")
public Cursor keyIterator() {
if (this.keyForm != Form.forValue()) {
return new ValueCursor(this.model.keyIterator(), this.keyForm);
} else {
return (Cursor) this.model.keyIterator();
}
}
@SuppressWarnings("unchecked")
public Cursor valueIterator() {
if (this.valueForm != Form.forValue()) {
return new ValueCursor(this.model.valueIterator(), this.valueForm);
} else {
return (Cursor) this.model.valueIterator();
}
}
@Override
public Comparator super K> comparator() {
return null;
}
@Override
public MapDownlink get() {
return this;
}
@Override
public MapOutlet> input() {
return this.input;
}
@SuppressWarnings("unchecked")
@Override
public void bindInput(Outlet extends Map> input) {
if (input instanceof MapOutlet, ?, ?>) {
this.bindInput((MapOutlet>) input);
} else {
throw new IllegalArgumentException(input.toString());
}
}
public void bindInput(MapOutlet> input) {
if (this.input != null) {
this.input.unbindOutput(this);
}
this.input = input;
if (this.input != null) {
this.input.bindOutput(this);
}
}
@Override
public void unbindInput() {
if (this.input != null) {
this.input.unbindOutput(this);
}
this.input = null;
}
@Override
public void disconnectInputs() {
final MapOutlet> input = this.input;
if (input != null) {
input.unbindOutput(this);
this.input = null;
input.disconnectInputs();
}
}
@Override
public Outlet outlet(K key) {
KeyOutlet outlet = this.outlets.get(key);
if (outlet == null) {
outlet = new KeyOutlet(this, key);
this.outlets = this.outlets.updated(key, outlet);
}
return outlet;
}
@Override
public Iterator>> outputIterator() {
return this.outputs != null ? Cursor.array(this.outputs) : Cursor.empty();
}
@SuppressWarnings("unchecked")
@Override
public void bindOutput(Inlet super MapDownlink> output) {
final Inlet super MapDownlink>[] oldOutputs = this.outputs;
final int n = oldOutputs != null ? oldOutputs.length : 0;
final Inlet super MapDownlink>[] newOutputs = (Inlet super MapDownlink>[]) new Inlet>[n + 1];
if (n > 0) {
System.arraycopy(oldOutputs, 0, newOutputs, 0, n);
}
newOutputs[n] = output;
this.outputs = newOutputs;
}
@SuppressWarnings("unchecked")
@Override
public void unbindOutput(Inlet super MapDownlink> output) {
final Inlet super MapDownlink>[] oldOutputs = this.outputs;
final int n = oldOutputs != null ? oldOutputs.length : 0;
for (int i = 0; i < n; i += 1) {
if (oldOutputs[i] == output) {
if (n > 1) {
final Inlet super MapDownlink>[] newOutputs = (Inlet super MapDownlink>[]) new Inlet>[n - 1];
System.arraycopy(oldOutputs, 0, newOutputs, 0, i);
System.arraycopy(oldOutputs, i + 1, newOutputs, i, (n - 1) - i);
this.outputs = newOutputs;
} else {
this.outputs = null;
}
break;
}
}
}
@Override
public void unbindOutputs() {
final HashTrieMap> outlets = this.outlets;
if (!outlets.isEmpty()) {
this.outlets = HashTrieMap.empty();
final Iterator> keyOutlets = outlets.valueIterator();
while (keyOutlets.hasNext()) {
final KeyOutlet keyOutlet = keyOutlets.next();
keyOutlet.unbindOutputs();
}
}
final Inlet super MapDownlink>[] outputs = this.outputs;
if (outputs != null) {
this.outputs = null;
for (int i = 0, n = outputs.length; i < n; i += 1) {
final Inlet super MapDownlink> output = outputs[i];
output.unbindInput();
}
}
}
@Override
public void disconnectOutputs() {
final HashTrieMap> outlets = this.outlets;
if (!outlets.isEmpty()) {
this.outlets = HashTrieMap.empty();
final Iterator> keyOutlets = outlets.valueIterator();
while (keyOutlets.hasNext()) {
final KeyOutlet keyOutlet = keyOutlets.next();
keyOutlet.disconnectOutputs();
}
}
final Inlet super MapDownlink>[] outputs = this.outputs;
if (outputs != null) {
this.outputs = null;
for (int i = 0, n = outputs.length; i < n; i += 1) {
final Inlet super MapDownlink> output = outputs[i];
output.unbindInput();
output.disconnectOutputs();
}
}
}
@Override
public void decohereOutputKey(K key, KeyEffect effect) {
this.decohereKey(key, effect);
}
@Override
public void decohereInputKey(K key, KeyEffect effect) {
this.decohereKey(key, effect);
}
@SuppressWarnings("unchecked")
public void decohereKey(K key, KeyEffect effect) {
final HashTrieMap oldEffects = this.effects;
if (oldEffects.get(key) != effect) {
this.willDecohereKey(key, effect);
this.effects = oldEffects.updated(key, effect);
this.version = -1;
this.onDecohereKey(key, effect);
final int n = this.outputs != null ? this.outputs.length : 0;
for (int i = 0; i < n; i += 1) {
final Inlet> output = this.outputs[i];
if (output instanceof MapInlet, ?, ?>) {
((MapInlet>) output).decohereOutputKey(key, effect);
} else {
output.decohereOutput();
}
}
final KeyOutlet outlet = this.outlets.get(key);
if (outlet != null) {
outlet.decohereInput();
}
this.didDecohereKey(key, effect);
}
}
@Override
public void decohereOutput() {
this.decohere();
}
@Override
public void decohereInput() {
this.decohere();
}
public void decohere() {
if (this.version >= 0) {
this.willDecohere();
this.version = -1;
this.onDecohere();
final int n = this.outputs != null ? this.outputs.length : 0;
for (int i = 0; i < n; i += 1) {
this.outputs[i].decohereOutput();
}
final Iterator> outlets = this.outlets.valueIterator();
while (outlets.hasNext()) {
outlets.next().decohereInput();
}
this.didDecohere();
}
}
@Override
public void recohereOutputKey(K key, int version) {
this.recohereKey(key, version);
}
@Override
public void recohereInputKey(K key, int version) {
this.recohereKey(key, version);
}
@SuppressWarnings("unchecked")
public void recohereKey(K key, int version) {
if (this.version < 0) {
final HashTrieMap oldEffects = this.effects;
final KeyEffect effect = oldEffects.get(key);
if (effect != null) {
this.willRecohereKey(key, effect, version);
this.effects = oldEffects.removed(key);
if (this.input != null) {
this.input.recohereInputKey(key, version);
}
this.onRecohereKey(key, effect, version);
for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) {
final Inlet> output = this.outputs[i];
if (output instanceof MapInlet, ?, ?>) {
((MapInlet>) output).recohereOutputKey(key, version);
}
}
final KeyOutlet outlet = this.outlets.get(key);
if (outlet != null) {
outlet.recohereInput(version);
}
this.didRecohereKey(key, effect, version);
}
}
}
@Override
public void recohereOutput(int version) {
this.recohere(version);
}
@Override
public void recohereInput(int version) {
this.recohere(version);
}
public void recohere(int version) {
if (this.version < 0) {
this.willRecohere(version);
final Iterator keys = this.effects.keyIterator();
while (keys.hasNext()) {
this.recohereKey(keys.next(), version);
}
this.version = version;
this.onRecohere(version);
for (int i = 0, n = this.outputs != null ? this.outputs.length : 0; i < n; i += 1) {
this.outputs[i].recohereOutput(version);
}
this.didRecohere(version);
}
}
protected void willDecohereKey(K key, KeyEffect effect) {
// hook
}
protected void onDecohereKey(K key, KeyEffect effect) {
// hook
}
protected void didDecohereKey(K key, KeyEffect effect) {
// hook
}
protected void willDecohere() {
// hook
}
protected void onDecohere() {
// hook
}
protected void didDecohere() {
// hook
}
protected void willRecohereKey(K key, KeyEffect effect, int version) {
// hook
}
protected void onRecohereKey(K key, KeyEffect effect, int version) {
if (effect == KeyEffect.UPDATE) {
if (this.input != null) {
final V value = this.input.get(key);
if (value != null) {
this.put(key, value);
} else {
this.remove(key);
}
}
} else if (effect == KeyEffect.REMOVE) {
if (this.containsKey(key)) {
this.remove(key);
}
}
}
protected void didRecohereKey(K key, KeyEffect effect, int version) {
// hook
}
protected void willRecohere(int version) {
// hook
}
protected void onRecohere(int version) {
// hook
}
protected void didRecohere(int version) {
// hook
}
protected static final int STATEFUL = 1 << 2;
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy