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

swim.system.lane.CommandLaneView 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 swim.api.Lane;
import swim.api.Link;
import swim.api.SwimContext;
import swim.api.agent.AgentContext;
import swim.api.lane.CommandLane;
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.OnCommand;
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.system.warp.WarpLaneView;

public class CommandLaneView extends WarpLaneView implements CommandLane {

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

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

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

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

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

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

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

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

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

  @Override
  public  CommandLaneView 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 OnCommand
    return observers;
  }

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

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

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

  @Override
  public CommandLaneView onCommand(OnCommand onCommand) {
    return this.observe(onCommand);
  }

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

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

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

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

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

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

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

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

  public void laneOnCommand(V value) {
    // hook
  }

  @SuppressWarnings("unchecked")
  public boolean dispatchOnCommand(Link link, V value, 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 OnCommand) {
        if (((OnCommand) observers).isPreemptive() == preemptive) {
          try {
            ((OnCommand) observers).onCommand(value);
          } 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 OnCommand) {
            if (((OnCommand) observer).isPreemptive() == preemptive) {
              try {
                ((OnCommand) observer).onCommand(value);
              } 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);
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy