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

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

There is a newer version: 4.13.21
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.reflect.model;

import swim.collections.FingerTrieSeq;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Kind;
import swim.structure.Record;
import swim.structure.Text;
import swim.structure.Value;
import swim.uri.Uri;
import swim.util.Builder;

public class MeshStats {
  final Uri meshUri;
  final LinkStats linkStats;
  final FingerTrieSeq partStats;

  public MeshStats(Uri meshUri, LinkStats linkStats, FingerTrieSeq partStats) {
    this.meshUri = meshUri;
    this.linkStats = linkStats;
    this.partStats = partStats;
  }

  public Uri meshUri() {
    return this.meshUri;
  }

  public LinkStats linkStats() {
    return this.linkStats;
  }

  public FingerTrieSeq partStats() {
    return this.partStats;
  }

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

  private static Form form;

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

final class MeshStatsForm extends Form {
  @Override
  public String tag() {
    return "mesh";
  }

  @Override
  public Class type() {
    return MeshStats.class;
  }

  @Override
  public Item mold(MeshStats stats) {
    if (stats != null) {
      final Value header;
      if (stats.meshUri.isDefined()) {
        header = Text.from(stats.meshUri.toString());
      } else {
        header = Value.extant();
      }
      final Record record = Record.create();
      record.attr(tag(), header);
      record.addAll((Record) stats.linkStats.toValue());
      for (PartStats part : stats.partStats) {
        record.item(part.toValue());
      }
      return record;
    } else {
      return Item.extant();
    }
  }

  @Override
  public MeshStats cast(Item item) {
    final Value value = item.toValue();
    final Value header = value.getAttr(tag());
    if (header.isDefined()) {
      final Uri meshUri = header.coerce(Uri.form());
      final LinkStats linkStats = LinkStats.form().cast(value);
      final Builder> partStats = FingerTrieSeq.builder();
      for (Item member : value) {
        final PartStats part = PartStats.form().cast(member);
        if (part != null) {
          partStats.add(part);
        }
      }
      return new MeshStats(meshUri, linkStats, partStats.bind());
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy