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

nstream.reflect.model.NodeInfo 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.NodeBinding;
import swim.uri.Uri;

public class NodeInfo {
  public final Uri nodeUri;
  public final long created;

  public NodeInfo(Uri nodeUri, long created) {
    this.nodeUri = nodeUri;
    this.created = created;
  }

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

  public static NodeInfo from(NodeBinding nodeBinding) {
    return new NodeInfo(nodeBinding.nodeUri(), nodeBinding.createdTime());
  }

  public static Iterator> iterator(Iterator> nodeBindings) {
    return new NodeBindingInfoIterator(nodeBindings);
  }

  private static Form form;

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

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

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

  @Override
  public NodeInfo cast(Item item) {
    final Value value = item.toValue();
    final Uri nodeUri = Uri.form().cast(value.get("nodeUri"));
    if (nodeUri != null) {
      final long created = value.get("created").longValue(0L);
      return new NodeInfo(nodeUri, created);
    }
    return null;
  }
}

final class NodeBindingInfoIterator implements Iterator> {
  final Iterator> nodeBindings;

  NodeBindingInfoIterator(Iterator> nodeBindings) {
    this.nodeBindings = nodeBindings;
  }

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

  @Override
  public Map.Entry next() {
    final Map.Entry entry = this.nodeBindings.next();
    final Uri nodeUri = entry.getKey();
    final NodeBinding nodeBinding = entry.getValue();
    final NodeInfo nodeInfo = NodeInfo.from(nodeBinding);
    return new AbstractMap.SimpleImmutableEntry(nodeUri, nodeInfo);
  }

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy