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

swim.streamlet.MapOutlet 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.streamlet;

import java.util.Iterator;
import java.util.Map;
import swim.streamlet.combinator.FilterFieldsCombinator;
import swim.streamlet.combinator.MapFieldValuesCombinator;
import swim.streamlet.combinator.MemoizeMapCombinator;
import swim.streamlet.combinator.ReduceFieldsCombinator;
import swim.streamlet.combinator.WatchFieldsCombinator;
import swim.streamlet.function.FilterFieldsFunction;
import swim.streamlet.function.MapFieldValuesFunction;
import swim.streamlet.function.WatchFieldsFunction;
import swim.util.CombinerFunction;

/**
 * Output connector from a {@link Streamlet} for a key-value map state.
 */
public interface MapOutlet extends Outlet {
  /**
   * Returns {@code true} if the current state of this {@code MapOutlet}
   * contains the given {@code key}; otherwise returns {@code false}.
   */
  boolean containsKey(K key);

  /**
   * Returns the value assocaited with the given {@code key} in the current
   * state of this {@code MapOutlet}, if defined; otherwise returns {@code
   * null}.
   */
  V get(K key);

  /**
   * Returns an {@code Iterator} over the keys in the current state of this
   * {@code MapOutlet}.
   */
  Iterator keyIterator();

  /**
   * Returns an {@code Outlet} that updates when the specified {@code key}
   * updates.
   */
  Outlet outlet(K key);

  /**
   * Marks this {@code MapOutlet} as needing an {@code effect} applied to a
   * given {@code key}.  Invalidating an individual key invalidates the entire
   * state of the {@code Outlet}.  But only the invalidated keys need to be
   * updated in order to reconcile the overall state of the {@code Outlet}.
   */
  void invalidateInputKey(K key, KeyEffect effect);

  /**
   * Reconciles the state of an individual {@code key} in this {@code
   * MapOutlet}, if the version of this {@code MapOutlet}'s state differs from
   * the target {@code version}.  To reconcile the state of a key, the {@code
   * MapOutlet} first invokes {@link Streamlet#reconcile(int)} on its attached
   * streamlets.  Then, for each dependent output, it invokes {@link
   * MapInlet#reconcileOutputKey(Object, int)}, if the dependent output is a
   * {@link MapInlet}, or it invokes {@link Inlet#reconcileOutput(int)}, if the
   * dependent output is not a {@code MapInlet}.
   */
  void reconcileInputKey(K key, int version);

  @Override
  default MapOutlet memoize() {
    final MemoizeMapCombinator combinator = new MemoizeMapCombinator();
    combinator.bindInput(this);
    return combinator;
  }

  default MapOutlet> filter(FilterFieldsFunction func) {
    final FilterFieldsCombinator combinator = new FilterFieldsCombinator(func);
    combinator.bindInput(this);
    return combinator;
  }

  default  MapOutlet> map(MapFieldValuesFunction func) {
    final MapFieldValuesCombinator combinator = new MapFieldValuesCombinator(func);
    combinator.bindInput(this);
    return combinator;
  }

  default  Outlet reduce(U identity, CombinerFunction accumulator, CombinerFunction combiner) {
    final ReduceFieldsCombinator combinator = new ReduceFieldsCombinator(identity, accumulator, combiner);
    combinator.bindInput(this);
    return combinator;
  }

  default MapOutlet watch(WatchFieldsFunction func) {
    final WatchFieldsCombinator combinator = new WatchFieldsCombinator(func);
    combinator.bindInput(this);
    return this;
  }
}