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

swim.runtime.lane.SpatialLaneView Maven / Gradle / Ivy

There is a newer version: 3.10.0
Show newest version
// Copyright 2015-2019 SWIM.AI 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.runtime.lane;

import java.util.AbstractMap;
import java.util.Iterator;
import java.util.Map;
import swim.api.Link;
import swim.api.SwimContext;
import swim.api.agent.AgentContext;
import swim.api.data.SpatialData;
import swim.api.http.function.DecodeRequestHttp;
import swim.api.http.function.DidRequestHttp;
import swim.api.http.function.DidRespondHttp;
import swim.api.http.function.DoRespondHttp;
import swim.api.http.function.WillRequestHttp;
import swim.api.http.function.WillRespondHttp;
import swim.api.lane.Lane;
import swim.api.lane.SpatialLane;
import swim.api.lane.function.DidCommand;
import swim.api.lane.function.DidEnter;
import swim.api.lane.function.DidLeave;
import swim.api.lane.function.DidUplink;
import swim.api.lane.function.WillCommand;
import swim.api.lane.function.WillEnter;
import swim.api.lane.function.WillLeave;
import swim.api.lane.function.WillUplink;
import swim.concurrent.Conts;
import swim.math.Z2Form;
import swim.observable.function.DidClear;
import swim.observable.function.DidMoveShape;
import swim.observable.function.DidRemoveShape;
import swim.observable.function.DidUpdateShape;
import swim.observable.function.WillClear;
import swim.observable.function.WillMoveShape;
import swim.observable.function.WillRemoveShape;
import swim.observable.function.WillUpdateShape;
import swim.runtime.LaneBinding;
import swim.spatial.SpatialMap;
import swim.structure.Form;

public class SpatialLaneView extends LaneView implements SpatialLane {

  protected final AgentContext agentContext;
  protected Form keyForm;
  protected Z2Form shapeForm;
  protected Form valueForm;

  protected SpatialLaneModel laneBinding;
  protected SpatialData dataView;
  protected int flags;

  SpatialLaneView(AgentContext agentContext, Form keyForm, Z2Form shapeForm, Form valueForm,
                  int flags, Object observers) {
    super(observers);
    this.agentContext = agentContext;
    this.keyForm = keyForm;
    this.shapeForm = shapeForm;
    this.valueForm = valueForm;
    this.flags = flags;
  }

  public SpatialLaneView(AgentContext agentContext, Form keyForm, Z2Form shapeForm, Form valueForm) {
    this(agentContext, keyForm, shapeForm, valueForm, 0, null);
  }

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

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

  public void setLaneBinding(SpatialLaneModel laneBinding) {
    this.laneBinding = laneBinding;
    this.dataView = laneBinding.data.keyForm(keyForm).valueForm(valueForm);
  }

  @Override
  public LaneBinding createLaneBinding() {
    return new SpatialLaneModel(shapeForm, flags);
  }

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

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

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

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

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

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

  @Override
  public  SpatialLane valueClass(Class valueClass) {
    return 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 & RESIDENT) != 0;
  }

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

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

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

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

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

  @Override
  public final boolean isSigned() {
    return (this.flags & SIGNED) != 0;
  }

  @Override
  public SpatialLane isSigned(boolean isSigned) {
    didSetSigned(isSigned);
    final SpatialLaneModel laneBinding = this.laneBinding;
    if (laneBinding != null) {
      laneBinding.isSigned(isSigned);
    }
    return this;
  }

  void didSetSigned(boolean isSigned) {
    if (isSigned) {
      this.flags |= SIGNED;
    } else {
      this.flags &= ~SIGNED;
    }
  }

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

  @SuppressWarnings("unchecked")
  @Override
  public SpatialLaneView observe(Object observer) {
    return (SpatialLaneView) super.observe(observer);
  }

  @SuppressWarnings("unchecked")
  @Override
  public SpatialLaneView unobserve(Object observer) {
    return (SpatialLaneView) super.unobserve(observer);
  }

  @Override
  public SpatialLane willUpdate(WillUpdateShape willUpdate) {
    return observe(willUpdate);
  }

  @Override
  public SpatialLane didUpdate(DidUpdateShape didUpdate) {
    return observe(didUpdate);
  }

  @Override
  public SpatialLane willMove(WillMoveShape willMove) {
    return observe(willMove);
  }

  @Override
  public SpatialLane didMove(DidMoveShape didMove) {
    return observe(didMove);
  }

  @Override
  public SpatialLane willRemove(WillRemoveShape willRemove) {
    return observe(willRemove);
  }

  @Override
  public SpatialLane didRemove(DidRemoveShape didRemove) {
    return observe(didRemove);
  }

  @Override
  public SpatialLane willClear(WillClear willClear) {
    return observe(willClear);
  }

  @Override
  public SpatialLane didClear(DidClear didClear) {
    return observe(didClear);
  }

  @Override
  public SpatialLaneView willCommand(WillCommand willCommand) {
    return observe(willCommand);
  }

  @Override
  public SpatialLaneView didCommand(DidCommand didCommand) {
    return observe(didCommand);
  }

  @Override
  public SpatialLaneView willUplink(WillUplink willUplink) {
    return observe(willUplink);
  }

  @Override
  public SpatialLaneView didUplink(DidUplink didUplink) {
    return observe(didUplink);
  }

  @Override
  public SpatialLaneView willEnter(WillEnter willEnter) {
    return observe(willEnter);
  }

  @Override
  public SpatialLaneView didEnter(DidEnter didEnter) {
    return observe(didEnter);
  }

  @Override
  public SpatialLaneView willLeave(WillLeave willLeave) {
    return observe(willLeave);
  }

  @Override
  public SpatialLaneView didLeave(DidLeave didLeave) {
    return observe(didLeave);
  }

  @Override
  public SpatialLaneView decodeRequest(DecodeRequestHttp decodeRequest) {
    return observe(decodeRequest);
  }

  @Override
  public SpatialLaneView willRequest(WillRequestHttp willRequest) {
    return observe(willRequest);
  }

  @Override
  public SpatialLaneView didRequest(DidRequestHttp didRequest) {
    return observe(didRequest);
  }

  @Override
  public SpatialLaneView doRespond(DoRespondHttp doRespond) {
    return observe(doRespond);
  }

  @Override
  public SpatialLaneView willRespond(WillRespondHttp willRespond) {
    return observe(willRespond);
  }

  @Override
  public SpatialLaneView didRespond(DidRespondHttp didRespond) {
    return observe(didRespond);
  }

  @SuppressWarnings("unchecked")
  public Map.Entry dispatchWillUpdate(Link link, K key, S shape, 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 WillUpdateShape) {
        if (((WillUpdateShape) observers).isPreemptive() == preemptive) {
          try {
            newValue = ((WillUpdateShape) observers).willUpdate(key, shape, newValue);
          } catch (Throwable error) {
            if (Conts.isNonFatal(error)) {
              laneDidFail(error);
            } else {
              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 WillUpdateShape) {
            if (((WillUpdateShape) observer).isPreemptive() == preemptive) {
              try {
                newValue = ((WillUpdateShape) observer).willUpdate(key, shape, newValue);
              } catch (Throwable error) {
                if (Conts.isNonFatal(error)) {
                  laneDidFail(error);
                } else {
                  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, S shape, 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 DidUpdateShape) {
        if (((DidUpdateShape) observers).isPreemptive() == preemptive) {
          try {
            ((DidUpdateShape) observers).didUpdate(key, shape, newValue, oldValue);
          } catch (Throwable error) {
            if (Conts.isNonFatal(error)) {
              laneDidFail(error);
            } else {
              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 DidUpdateShape) {
            if (((DidUpdateShape) observer).isPreemptive() == preemptive) {
              try {
                ((DidUpdateShape) observer).didUpdate(key, shape, newValue, oldValue);
              } catch (Throwable error) {
                if (Conts.isNonFatal(error)) {
                  laneDidFail(error);
                } else {
                  throw error;
                }
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  @SuppressWarnings("unchecked")
  public Map.Entry dispatchWillMove(Link link, K key, S newShape, V newValue, S oldShape,
                                                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 WillMoveShape) {
        if (((WillMoveShape) observers).isPreemptive() == preemptive) {
          try {
            newValue = ((WillMoveShape) observers).willMove(key, newShape, newValue, oldShape);
          } catch (Throwable error) {
            if (Conts.isNonFatal(error)) {
              laneDidFail(error);
            } else {
              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 WillMoveShape) {
            if (((WillMoveShape) observer).isPreemptive() == preemptive) {
              try {
                newValue = ((WillMoveShape) observer).willMove(key, newShape, newValue, oldShape);
              } catch (Throwable error) {
                if (Conts.isNonFatal(error)) {
                  laneDidFail(error);
                } else {
                  throw error;
                }
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return new AbstractMap.SimpleImmutableEntry(complete, newValue);
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  @SuppressWarnings("unchecked")
  public boolean dispatchDidMove(Link link, K key, S newShape, V newValue, S oldShape, 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 WillMoveShape) {
        if (((DidMoveShape) observers).isPreemptive() == preemptive) {
          try {
            ((DidMoveShape) observers).didMove(key, newShape, newValue, oldShape, oldValue);
          } catch (Throwable error) {
            if (Conts.isNonFatal(error)) {
              laneDidFail(error);
            } else {
              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 WillMoveShape) {
            if (((DidMoveShape) observer).isPreemptive() == preemptive) {
              try {
                ((DidMoveShape) observer).didMove(key, newShape, newValue, oldShape, oldValue);
              } catch (Throwable error) {
                if (Conts.isNonFatal(error)) {
                  laneDidFail(error);
                } else {
                  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, S shape, 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 WillMoveShape) {
        if (((WillRemoveShape) observers).isPreemptive() == preemptive) {
          try {
            ((WillRemoveShape) observers).willRemove(key, shape);
          } catch (Throwable error) {
            if (Conts.isNonFatal(error)) {
              laneDidFail(error);
            } else {
              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 WillMoveShape) {
            if (((WillRemoveShape) observers).isPreemptive() == preemptive) {
              try {
                ((WillRemoveShape) observers).willRemove(key, shape);
              } catch (Throwable error) {
                if (Conts.isNonFatal(error)) {
                  laneDidFail(error);
                } else {
                  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, S shape, 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 WillMoveShape) {
        if (((DidRemoveShape) observers).isPreemptive() == preemptive) {
          try {
            ((DidRemoveShape) observers).didRemove(key, shape, oldValue);
          } catch (Throwable error) {
            if (Conts.isNonFatal(error)) {
              laneDidFail(error);
            } else {
              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 WillMoveShape) {
            if (((DidRemoveShape) observers).isPreemptive() == preemptive) {
              try {
                ((DidRemoveShape) observers).didRemove(key, shape, oldValue);
              } catch (Throwable error) {
                if (Conts.isNonFatal(error)) {
                  laneDidFail(error);
                } else {
                  throw error;
                }
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  protected 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 (Conts.isNonFatal(error)) {
              laneDidFail(error);
            } else {
              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 (Conts.isNonFatal(error)) {
                  laneDidFail(error);
                } else {
                  throw error;
                }
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

  protected 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 (Conts.isNonFatal(error)) {
              laneDidFail(error);
            } else {
              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 (Conts.isNonFatal(error)) {
                  laneDidFail(error);
                } else {
                  throw error;
                }
              }
            } else if (preemptive) {
              complete = false;
            }
          }
        }
      }
      return complete;
    } finally {
      SwimContext.setLink(oldLink);
      SwimContext.setLane(oldLane);
    }
  }

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

  public void laneDidUpdate(K key, S shape, V newValue, V oldValue) {

  }

  public V laneWillMove(K key, S newShape, V newValue, S oldShape) {
    return newValue;
  }

  public V laneDidMove(K key, S newShape, V newValue, S oldShape, V oldValue) {
    return newValue;
  }

  public void laneWillRemove(K key, S shape) {

  }

  public void laneDidRemove(K key, S shape) {

  }

  public void laneWillClear() {

  }

  public void laneDidClear() {

  }

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

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

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

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

  @Override
  public boolean containsKey(K key, S shape) {
    return this.dataView.containsKey(key, shape);
  }

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

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

  @Override
  public V get(K key, S shape) {
    return this.dataView.get(key, shape);
  }

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

  @Override
  public V put(K key, S shape, V newValue) {
    return this.dataView.put(key, shape, newValue);
  }

  @Override
  public V move(K key, S oldShape, S newShape, V newValue) {
    return this.dataView.move(key, oldShape, newShape, newValue);
  }

  @Override
  public V remove(K key, S shape) {
    return this.dataView.remove(key, shape);
  }

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

  @Override
  public Iterator> iterator(S shape) {
    return this.dataView.iterator(shape);
  }

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

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

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

}