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

nstream.adapter.common.relay.ForEach Maven / Gradle / Ivy

There is a newer version: 4.14.22
Show newest version
// Copyright 2015-2024 Nstream, inc.
//
// Licensed under the Redis Source Available License 2.0 (RSALv2) Agreement;
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://redis.com/legal/rsalv2-agreement/
//
// 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 nstream.adapter.common.relay;

import swim.api.agent.AgentContext;
import swim.structure.Absent;
import swim.structure.Attr;
import swim.structure.Item;
import swim.structure.Record;
import swim.structure.Value;
import swim.util.Log;

public final class ForEach extends Directive {

  private final Directive[] children;

  public ForEach(Directive parent, AgentContext context, Log log, Item scope,
                 Record children) {
    super(parent, context, log, scope);
    if (children.head() instanceof Attr) {
      this.children = new Directive[scope.length()];
      for (int i = 0; i < scope.length(); i++) {
        this.children[i] = DslInterpret.createDirective(this.context, log, children,
            scope.getItem(i), this);
      }
    } else {
      this.children = new Directive[children.length() * scope.length()];
      for (int i = 0, k = 0; i < children.length(); i++) {
        for (int j = 0; j < scope.length(); j++, k++) {
          this.children[k] = DslInterpret.createDirective(this.context, log, (Value) children.getItem(i),
              scope.getItem(j), this);
        }
      }
    }
  }

  static ForEach fromSchema(Directive parent, AgentContext context, Log log, Item scope,
                            Value schema) {
    // FIXME: sanity check
    return new ForEach(parent, context, log, scope, (Record) schema.getItem(1));
  }

  @Override
  public Absent evaluate() throws InterpretException {
    for (Directive child : this.children) {
      child.evaluate();
    }
    return Absent.absent();
  }

  @Override
  public String toString() {
    return "ForEach{"
        + "scope=" + this.scope + ", "
        + "childCount=" + this.children.length
        + '}';
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy