All Downloads are FREE. Search and download functionalities are using the official Maven repository.

swim.system.lane.MapLaneView 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.lane;

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.Lane;
import swim.api.Link;
import swim.api.SwimContext;
import swim.api.agent.AgentContext;
import swim.api.data.MapData;
import swim.api.lane.MapLane;
import swim.api.warp.function.DidCommand;
import swim.api.warp.function.DidEnter;
import swim.api.warp.function.DidLeave;
import swim.api.warp.function.DidUplink;
import swim.api.warp.function.WillCommand;
import swim.api.warp.function.WillEnter;
import swim.api.warp.function.WillLeave;
import swim.api.warp.function.WillUplink;
import swim.collections.HashTrieMap;
import swim.concurrent.Cont;
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.system.warp.WarpLaneView;
import swim.util.Cursor;
import swim.util.OrderedMap;
import swim.util.OrderedMapCursor;

public class MapLaneView extends WarpLaneView implements MapLane {

  protected final AgentContext agentContext;
  protected Form keyForm;
  protected Form valueForm;
  protected MapLaneModel laneBinding;
  protected MapData dataView;
  protected int flags;

  protected MapOutlet> input;
  protected HashTrieMap effects;
  protected HashTrieMap> outlets;
  protected Inlet>[] outputs; // TODO: unify with observers
  protected int version;

  MapLaneView(AgentContext agentContext, Form keyForm, Form valueForm,
              int flags, Object observers) {
    super(observers);
    this.agentContext = agentContext;
    this.keyForm = keyForm;
    this.valueForm = valueForm;
    this.laneBinding = null;
    this.dataView = null;
    this.flags = flags;

    this.input = null;
    this.effects = HashTrieMap.empty();
    this.outlets = HashTrieMap.empty();
    this.outputs = null;
    this.version = -1;
  }

  public MapLaneView(AgentContext agentContext, Form keyForm, Form valueForm) {
    this(agentContext, keyForm, valueForm, 0, null);
  }

  @Override
  public AgentContext agentContext() {
    return this.agentContext;
  }

  @Override
  public MapLaneModel laneBinding() {
    return this.laneBinding;
  }

  void setLaneBinding(MapLaneModel laneBinding) {
    this.laneBinding = laneBinding;
  }

  @Override
  public MapLaneModel createLaneBinding() {
    return new MapLaneModel(this.flags);
  }

  @Override
  public final Form keyForm() {
    return this.keyForm;
  }

  @Override
  public  MapLaneView keyForm(Form keyForm) {
    return new MapLaneView(this.agentContext, keyForm, this.valueForm,
                                  this.flags, this.typesafeObservers(this.observers));
  }

  @Override
  public  MapLaneView keyClass(Class keyClass) {
    return this.keyForm(Form.forClass(keyClass));
  }

  public void setKeyForm(Form keyForm) {
    this.keyForm = keyForm;
  }

  @Override
  public final Form valueForm() {
    return this.valueForm;
  }

  @Override
  public  MapLaneView valueForm(Form valueForm) {
    return new MapLaneView(this.agentContext, this.keyForm, valueForm,
                                  this.flags, this.typesafeObservers(this.observers));
  }

  @Override
  public  MapLaneView valueClass(Class valueClass) {
    return this.valueForm(Form.forClass(valueClass));
  }

  public void setValueForm(Form valueForm) {
    this.valueForm = valueForm;
  }

  protected Object typesafeObservers(Object observers) {
    // TODO: filter out WillUpdateKey, DidUpdateKey, WillRemoveKey, DidRemoveKey,
    //       WillDrop, DidDrop, WillTake, DidTake, WillClear, DidClear
    return observers;
  }

  public final boolean isResident() {
    return (this.flags & MapLaneView.RESIDENT) != 0;
  }

  @Override
  public MapLaneView isResident(boolean isResident) {
    if (isResident) {
      this.flags |= MapLaneView.RESIDENT;
    } else {
      this.flags &= ~MapLaneView.RESIDENT;
    }
    final MapLaneModel laneBinding = this.laneBinding;
    if (laneBinding != null) {
      laneBinding.isResident(isResident);
    }
    return this;
  }

  void didSetResident(boolean isResident) {
    if (isResident) {
      this.flags |= MapLaneView.RESIDENT;
    } else {
      this.flags &= ~MapLaneView.RESIDENT;
    }
  }

  public final boolean isTransient() {
    return (this.flags & MapLaneView.TRANSIENT) != 0;
  }

  @Override
  public MapLaneView isTransient(boolean isTransient) {
    if (isTransient) {
      this.flags |= MapLaneView.TRANSIENT;
    } else {
      this.flags &= ~MapLaneView.TRANSIENT;
    }
    final MapLaneModel laneBinding = this.laneBinding;
    if (laneBinding != null) {
      laneBinding.isTransient(isTransient);
    }
    return this;
  }

  void didSetTransient(boolean isTransient) {
    if (isTransient) {
      this.flags |= MapLaneView.TRANSIENT;
    } else {
      this.flags &= ~MapLaneView.TRANSIENT;
    }
  }

  @Override
  protected void willLoad() {
    this.dataView = this.laneBinding.data.keyForm(this.keyForm).valueForm(this.valueForm);
    super.willLoad();
  }

  @Override
  public void close() {
    this.laneBinding.closeLaneView(this);
  }

  @Override
  public MapLaneView observe(Object observer) {
    super.observe(observer);
    return this;
  }

  @Override
  public MapLaneView unobserve(Object observer) {
    super.unobserve(observer);
    return this;
  }

  @Override
  public MapLaneView willUpdate(WillUpdateKey willUpdate) {
    return this.observe(willUpdate);
  }

  @Override
  public MapLaneView didUpdate(DidUpdateKey didUpdate) {
    return this.observe(didUpdate);
  }

  @Override
  public MapLaneView willRemove(WillRemoveKey willRemove) {
    return this.observe(willRemove);
  }

  @Override
  public MapLaneView didRemove(DidRemoveKey didRemove) {
    return this.observe(didRemove);
  }

  @Override
  public MapLaneView willDrop(WillDrop willDrop) {
    return this.observe(willDrop);
  }

  @Override
  public MapLaneView didDrop(DidDrop didDrop) {
    return this.observe(didDrop);
  }

  @Override
  public MapLaneView willTake(WillTake willTake) {
    return this.observe(willTake);
  }

  @Override
  public MapLaneView didTake(DidTake didTake) {
    return this.observe(didTake);
  }

  @Override
  public MapLaneView willClear(WillClear willClear) {
    return this.observe(willClear);
  }

  @Override
  public MapLaneView didClear(DidClear didClear) {
    return this.observe(didClear);
  }

  @Override
  public MapLaneView willCommand(WillCommand willCommand) {
    return this.observe(willCommand);
  }

  @Override
  public MapLaneView didCommand(DidCommand didCommand) {
    return this.observe(didCommand);
  }

  @Override
  public MapLaneView willUplink(WillUplink willUplink) {
    return this.observe(willUplink);
  }

  @Override
  public MapLaneView didUplink(DidUplink didUplink) {
    return this.observe(didUplink);
  }

  @Override
  public MapLaneView willEnter(WillEnter willEnter) {
    return this.observe(willEnter);
  }

  @Override
  public MapLaneView didEnter(DidEnter didEnter) {
    return this.observe(didEnter);
  }

  @Override
  public MapLaneView willLeave(WillLeave willLeave) {
    return this.observe(willLeave);
  }

  @Override
  public MapLaneView didLeave(DidLeave didLeave) {
    return this.observe(didLeave);
  }

  @SuppressWarnings("unchecked")
  public Entry dispatchWillUpdate(Link link, K key, V newValue, boolean preemptive) {
    final Lane oldLane = SwimContext.getLane();
    final Link oldLink = SwimContext.getLink();
    try {
      SwimContext.setLane(this);
      SwimContext.setLink(link);
      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.laneDidFail(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.laneDidFail(error);
                }
                throw error;
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return new AbstractMap.SimpleImmutableEntry(complete, newValue);
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  @SuppressWarnings("unchecked")
  public boolean dispatchDidUpdate(Link link, K key, V newValue, V oldValue, boolean preemptive) {
    final Lane oldLane = SwimContext.getLane();
    final Link oldLink = SwimContext.getLink();
    try {
      SwimContext.setLane(this);
      SwimContext.setLink(link);
      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.laneDidFail(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.laneDidFail(error);
                }
                throw error;
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  @SuppressWarnings("unchecked")
  public boolean dispatchWillRemove(Link link, K key, boolean preemptive) {
    final Lane oldLane = SwimContext.getLane();
    final Link oldLink = SwimContext.getLink();
    try {
      SwimContext.setLane(this);
      SwimContext.setLink(link);
      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.laneDidFail(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.laneDidFail(error);
                }
                throw error;
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  @SuppressWarnings("unchecked")
  public boolean dispatchDidRemove(Link link, K key, V oldValue, boolean preemptive) {
    final Lane oldLane = SwimContext.getLane();
    final Link oldLink = SwimContext.getLink();
    try {
      SwimContext.setLane(this);
      SwimContext.setLink(link);
      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.laneDidFail(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.laneDidFail(error);
                }
                throw error;
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  public boolean dispatchWillDrop(Link link, int lower, boolean preemptive) {
    final Lane oldLane = SwimContext.getLane();
    final Link oldLink = SwimContext.getLink();
    try {
      SwimContext.setLane(this);
      SwimContext.setLink(link);
      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.laneDidFail(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.laneDidFail(error);
                }
                throw error;
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  public boolean dispatchDidDrop(Link link, int lower, boolean preemptive) {
    final Lane oldLane = SwimContext.getLane();
    final Link oldLink = SwimContext.getLink();
    try {
      SwimContext.setLane(this);
      SwimContext.setLink(link);
      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.laneDidFail(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.laneDidFail(error);
                }
                throw error;
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  public boolean dispatchWillTake(Link link, int upper, boolean preemptive) {
    final Lane oldLane = SwimContext.getLane();
    final Link oldLink = SwimContext.getLink();
    try {
      SwimContext.setLane(this);
      SwimContext.setLink(link);
      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.laneDidFail(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.laneDidFail(error);
                }
                throw error;
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  public boolean dispatchDidTake(Link link, int upper, boolean preemptive) {
    final Lane oldLane = SwimContext.getLane();
    final Link oldLink = SwimContext.getLink();
    try {
      SwimContext.setLane(this);
      SwimContext.setLink(link);
      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.laneDidFail(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.laneDidFail(error);
                }
                throw error;
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  public boolean dispatchWillClear(Link link, boolean preemptive) {
    final Lane oldLane = SwimContext.getLane();
    final Link oldLink = SwimContext.getLink();
    try {
      SwimContext.setLane(this);
      SwimContext.setLink(link);
      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.laneDidFail(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.laneDidFail(error);
                }
                throw error;
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  public boolean dispatchDidClear(Link link, boolean preemptive) {
    final Lane oldLane = SwimContext.getLane();
    final Link oldLink = SwimContext.getLink();
    try {
      SwimContext.setLane(this);
      SwimContext.setLink(link);
      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.laneDidFail(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.laneDidFail(error);
                }
                throw error;
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  public V laneWillUpdate(K key, V newValue) {
    return newValue;
  }

  public void laneDidUpdate(K key, V newValue, V oldValue) {
    this.decohereInputKey(key, KeyEffect.UPDATE);
    this.recohereInputKey(key, 0); // TODO: debounce and track version
  }

  public void laneWillRemove(K key) {
    // hook
  }

  public void laneDidRemove(K key, V oldValue) {
    this.decohereInputKey(key, KeyEffect.REMOVE);
    this.recohereInputKey(key, 0); // TODO: debounce and track version
  }

  public void laneWillDrop(int lower) {
    // hook
  }

  public void laneDidDrop(int lower) {
    // hook
  }

  public void laneWillTake(int upper) {
    // hook
  }

  public void laneDidTake(int upper) {
    // hook
  }

  public void laneWillClear() {
    // hook
  }

  public void laneDidClear() {
    // hook
  }

  @Override
  public boolean isEmpty() {
    return this.dataView.isEmpty();
  }

  @Override
  public int size() {
    return this.dataView.size();
  }

  @Override
  public boolean containsKey(Object key) {
    return this.dataView.containsKey(key);
  }

  @Override
  public boolean containsValue(Object value) {
    return this.dataView.containsValue(value);
  }

  @Override
  public int indexOf(Object key) {
    return this.dataView.indexOf(key);
  }

  @Override
  public V get(Object key) {
    return this.dataView.get(key);
  }

  @Override
  public Entry getEntry(Object key) {
    return this.dataView.getEntry(key);
  }

  @Override
  public Entry getIndex(int index) {
    return this.dataView.getIndex(index);
  }

  @Override
  public Entry firstEntry() {
    return this.dataView.firstEntry();
  }

  @Override
  public K firstKey() {
    return this.dataView.firstKey();
  }

  @Override
  public V firstValue() {
    return this.dataView.firstValue();
  }

  @Override
  public Entry lastEntry() {
    return this.dataView.lastEntry();
  }

  @Override
  public K lastKey() {
    return this.dataView.lastKey();
  }

  @Override
  public V lastValue() {
    return this.dataView.lastValue();
  }

  @Override
  public Entry nextEntry(K key) {
    return this.dataView.nextEntry(key);
  }

  @Override
  public K nextKey(K key) {
    return this.dataView.nextKey(key);
  }

  @Override
  public V nextValue(K key) {
    return this.dataView.nextValue(key);
  }

  @Override
  public Entry previousEntry(K key) {
    return this.dataView.previousEntry(key);
  }

  @Override
  public K previousKey(K key) {
    return this.dataView.previousKey(key);
  }

  @Override
  public V previousValue(K key) {
    return this.dataView.previousValue(key);
  }

  @Override
  public V put(K key, V value) {
    return this.laneBinding.put(this, key, value);
  }

  @Override
  public void putAll(Map map) {
    for (Entry entry : map.entrySet()) {
      this.laneBinding.put(this, entry.getKey(), entry.getValue());
    }
  }

  @SuppressWarnings("unchecked")
  @Override
  public V remove(Object key) {
    final Class keyType = this.keyForm.type();
    if (keyType == null || keyType.isInstance(key)) {
      return this.laneBinding.remove(this, (K) key);
    }
    return this.valueForm.unit();
  }

  @Override
  public void drop(int lower) {
    this.laneBinding.drop(this, lower);
  }

  @Override
  public void take(int upper) {
    this.laneBinding.take(this, upper);
  }

  @Override
  public void clear() {
    this.laneBinding.clear(this);
  }

  @Override
  public OrderedMap headMap(K toKey) {
    return this.dataView.headMap(toKey);
  }

  @Override
  public OrderedMap tailMap(K fromKey) {
    return this.dataView.tailMap(fromKey);
  }

  @Override
  public OrderedMap subMap(K fromKey, K toKey) {
    return this.dataView.subMap(fromKey, toKey);
  }

  @Override
  public Set> entrySet() {
    return this.dataView.entrySet();
  }

  @Override
  public Set keySet() {
    return this.dataView.keySet();
  }

  @Override
  public Collection values() {
    return this.dataView.values();
  }

  @Override
  public OrderedMapCursor iterator() {
    return this.dataView.iterator();
  }

  @Override
  public Cursor keyIterator() {
    return this.dataView.keyIterator();
  }

  @Override
  public Cursor valueIterator() {
    return this.dataView.valueIterator();
  }

  @Override
  public OrderedMap snapshot() {
    return this.dataView.snapshot();
  }

  @Override
  public Comparator comparator() {
    return this.dataView.comparator();
  }

  @Override
  public MapLane get() {
    return this;
  }

  @Override
  public MapOutlet> input() {
    return this.input;
  }

  @SuppressWarnings("unchecked")
  @Override
  public void bindInput(Outlet> 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> output) {
    final Inlet>[] oldOutputs = this.outputs;
    final int n = oldOutputs != null ? oldOutputs.length : 0;
    final Inlet>[] newOutputs = (Inlet>[]) 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> output) {
    final Inlet>[] 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>[] newOutputs = (Inlet>[]) 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>[] outputs = this.outputs;
    if (outputs != null) {
      this.outputs = null;
      for (int i = 0, n = outputs.length; i < n; i += 1) {
        final Inlet> 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>[] outputs = this.outputs;
    if (outputs != null) {
      this.outputs = null;
      for (int i = 0, n = outputs.length; i < n; i += 1) {
        final Inlet> 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
  }

  static final int RESIDENT = 1 << 0;
  static final int TRANSIENT = 1 << 1;

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy