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

swim.system.lane.DemandLaneView Maven / Gradle / Ivy

// 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 swim.api.Lane;
import swim.api.Link;
import swim.api.SwimContext;
import swim.api.agent.AgentContext;
import swim.api.lane.DemandLane;
import swim.api.lane.function.OnCue;
import swim.api.warp.WarpUplink;
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.concurrent.Cont;
import swim.structure.Form;
import swim.structure.Value;
import swim.system.warp.WarpLaneView;

public class DemandLaneView extends WarpLaneView implements DemandLane {

  protected final AgentContext agentContext;
  protected Form valueForm;
  protected DemandLaneModel laneBinding;

  public DemandLaneView(AgentContext agentContext, Form valueForm, Object observers) {
    super(observers);
    this.agentContext = agentContext;
    this.valueForm = valueForm;
    this.laneBinding = null;
  }

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

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

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

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

  @Override
  public DemandLaneModel createLaneBinding() {
    return new DemandLaneModel();
  }

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

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

  @Override
  public  DemandLaneView 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 OnCue
    return observers;
  }

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

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

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

  @Override
  public DemandLaneView onCue(OnCue onCue) {
    return this.observe(onCue);
  }

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

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

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

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

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

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

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

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

  @SuppressWarnings("unchecked")
  public V dispatchOnCue(WarpUplink uplink) {
    final Lane lane = SwimContext.getLane();
    final Link link = SwimContext.getLink();
    SwimContext.setLane(this);
    SwimContext.setLink(uplink);
    try {
      final Object observers = this.observers;
      if (observers instanceof OnCue) {
        try {
          final V value = ((OnCue) observers).onCue(uplink);
          if (value != null) {
            return value;
          }
        } catch (Throwable error) {
          if (Cont.isNonFatal(error)) {
            this.laneDidFail(error);
          }
          throw error;
        }
      } 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 OnCue) {
            try {
              final V value = ((OnCue) observer).onCue(uplink);
              if (value != null) {
                return value;
              }
            } catch (Throwable error) {
              if (Cont.isNonFatal(error)) {
                this.laneDidFail(error);
              }
              throw error;
            }
          }
        }
      }
      return null;
    } finally {
      SwimContext.setLink(link);
      SwimContext.setLane(lane);
    }
  }

  Value nextDownCue(WarpUplink uplink) {
    final V object = this.dispatchOnCue(uplink);
    if (object != null) {
      return this.valueForm.mold(object).toValue();
    } else {
      return null;
    }
  }

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

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy