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

nstream.reflect.model.LaneInfo Maven / Gradle / Ivy

The 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.reflect.model;

import java.util.AbstractMap;
import java.util.Iterator;
import java.util.Map;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Kind;
import swim.structure.Record;
import swim.structure.Value;
import swim.system.LaneBinding;
import swim.uri.Uri;

public class LaneInfo {
  public final Uri laneUri;
  public final String type;

  public LaneInfo(Uri laneUri, String type) {
    this.laneUri = laneUri;
    this.type = type;
  }

  public Value toValue() {
    return form().mold(this).toValue();
  }

  public static LaneInfo from(LaneBinding laneBinding) {
    return new LaneInfo(laneBinding.laneUri(), laneBinding.laneType());
  }

  public static Iterator> iterator(Iterator> laneBindings) {
    return new LaneBindingInfoIterator(laneBindings);
  }

  private static Form form;

  @Kind
  public static Form form() {
    if (form == null) {
      form = new LaneInfoForm();
    }
    return form;
  }
}

final class LaneInfoForm extends Form {
  @Override
  public Class type() {
    return LaneInfo.class;
  }

  @Override
  public Item mold(LaneInfo info) {
    if (info != null) {
      final Record record = Record.create(2);
      record.slot("laneUri", info.laneUri.toString());
      record.slot("type", info.type);
      return record;
    } else {
      return Item.extant();
    }
  }

  @Override
  public LaneInfo cast(Item item) {
    final Value value = item.toValue();
    final Uri laneUri = Uri.form().cast(value.get("laneUri"));
    final String type = value.get("type").stringValue(null);
    if (laneUri != null && type != null) {
      return new LaneInfo(laneUri, type);
    }
    return null;
  }
}

final class LaneBindingInfoIterator implements Iterator> {
  final Iterator> laneBindings;

  LaneBindingInfoIterator(Iterator> laneBindings) {
    this.laneBindings = laneBindings;
  }

  @Override
  public boolean hasNext() {
    return laneBindings.hasNext();
  }

  @Override
  public Map.Entry next() {
    final Map.Entry entry = this.laneBindings.next();
    final Uri laneUri = entry.getKey();
    final LaneBinding laneBinding = entry.getValue();
    final LaneInfo laneInfo = LaneInfo.from(laneBinding);
    return new AbstractMap.SimpleImmutableEntry(laneUri, laneInfo);
  }

  @Override
  public void remove() {
    throw new UnsupportedOperationException();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy