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

swim.runtime.lane.ValueLaneView 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.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.ValueLane;
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.observable.function.DidSet;
import swim.observable.function.WillSet;
import swim.streamlet.Inlet;
import swim.streamlet.Outlet;
import swim.structure.Form;
import swim.util.Cursor;

public class ValueLaneView extends LaneView implements ValueLane {
  protected final AgentContext agentContext;
  protected Form valueForm;

  protected int flags;
  protected ValueLaneModel laneBinding;

  protected Outlet input;
  protected Inlet[] outputs; // TODO: unify with observers
  protected int version;

  ValueLaneView(AgentContext agentContext, Form valueForm, int flags, Object observers) {
    super(observers);
    this.agentContext = agentContext;
    this.valueForm = valueForm;
    this.flags = flags;

    this.input = null;
    this.outputs = null;
    this.version = -1;
  }

  public ValueLaneView(AgentContext agentContext, Form valueForm) {
    this(agentContext, valueForm, RESIDENT, null);
  }

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

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

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

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

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

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

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

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

  protected Object typesafeObservers(Object observers) {
    // TODO: filter out WillSet, DidSet
    return observers;
  }

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

  @Override
  public ValueLaneView isResident(boolean isResident) {
    didSetResident(isResident);

    // note: marked final given access of concurrently accessed volatile objects
    final ValueLaneModel laneBinding = this.laneBinding;

    if (laneBinding != null) {
      laneBinding.isResident(isResident);
    }

    return this;
  }

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

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

  @Override
  public ValueLaneView isTransient(boolean isTransient) {
    didSetTransient(isTransient);

    // note: marked final given access of concurrently accessed volatile objects
    final ValueLaneModel 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 ValueLaneView isSigned(boolean isSigned) {
    didSetSigned(isSigned);

    // note: marked final given access of concurrently accessed volatile objects
    final ValueLaneModel 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 ValueLaneView observe(Object observer) {
    return (ValueLaneView) super.observe(observer);
  }

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

  @Override
  public ValueLaneView willSet(WillSet willSet) {
    return observe(willSet);
  }

  @Override
  public ValueLaneView didSet(DidSet didSet) {
    return observe(didSet);
  }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  @SuppressWarnings("unchecked")
  protected Map.Entry dispatchWillSet(Link link, 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 WillSet) {
        if (((WillSet) observers).isPreemptive() == preemptive) {
          try {
            newValue = ((WillSet) observers).willSet(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 WillSet) {
            if (((WillSet) observer).isPreemptive() == preemptive) {
              try {
                newValue = ((WillSet) observer).willSet(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")
  protected boolean dispatchDidSet(Link link, 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 DidSet) {
        if (((DidSet) observers).isPreemptive() == preemptive) {
          try {
            ((DidSet) observers).didSet(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 DidSet) {
            if (((DidSet) observer).isPreemptive() == preemptive) {
              try {
                ((DidSet) observer).didSet(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);
    }
  }

  public V laneWillSet(V newValue) {
    return newValue;
  }

  public void laneDidSet(V newValue, V oldValue) {
    invalidate();
    reconcile(0); // TODO: debounce and track version
  }

  @Override
  public V get() {
    V state = this.valueForm.cast(this.laneBinding.get());
    if (state == null) {
      state = this.valueForm.unit();
    }
    return state;
  }

  @Override
  public V set(V newValue) {
    return this.laneBinding.set(this, newValue);
  }

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

  @Override
  public void bindInput(Outlet 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 Outlet input = this.input;
    if (input != null) {
      input.unbindOutput(this);
      this.input = null;
      input.disconnectInputs();
    }
  }

  @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 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 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 invalidateOutput() {
    invalidate();
  }

  @Override
  public void invalidateInput() {
    invalidate();
  }

  public void invalidate() {
    if (this.version >= 0) {
      willInvalidate();
      this.version = -1;
      onInvalidate();
      final int n = this.outputs != null ? this.outputs.length : 0;
      for (int i = 0; i < n; i += 1) {
        this.outputs[i].invalidateOutput();
      }
      didInvalidate();
    }
  }

  @Override
  public void reconcileOutput(int version) {
    reconcile(version);
  }

  @Override
  public void reconcileInput(int version) {
    reconcile(version);
  }

  public void reconcile(int version) {
    if (this.version < 0) {
      willReconcile(version);
      this.version = version;
      if (this.input != null) {
        this.input.reconcileInput(version);
      }
      onReconcile(version);
      final int n = this.outputs != null ? this.outputs.length : 0;
      for (int i = 0; i < n; i += 1) {
        this.outputs[i].reconcileOutput(version);
      }
      didReconcile(version);
    }
  }

  protected void willInvalidate() {
    // stub
  }

  protected void onInvalidate() {
    // stub
  }

  protected void didInvalidate() {
    // stub
  }

  protected void willReconcile(int version) {
    // stub
  }

  protected void onReconcile(int version) {
    if (this.input != null) {
      final V value = this.input.get();
      set(value);
    }
  }

  protected void didReconcile(int version) {
    // stub
  }

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