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

swim.system.reflect.LaneInfo 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.reflect;

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 {

  protected final Uri laneUri;
  protected final String laneType;

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

  public final Uri laneUri() {
    return this.laneUri;
  }

  public final String laneType() {
    return this.laneType;
  }

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

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

  private static Form form;

  @Kind
  public static Form form() {
    if (LaneInfo.form == null) {
      LaneInfo.form = new LaneInfoForm();
    }
    return LaneInfo.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("laneType", info.laneType);
      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 laneType = value.get("laneType").stringValue(null);
    if (laneUri != null && laneType != null) {
      return new LaneInfo(laneUri, laneType);
    }
    return null;
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy