nstream.reflect.model.PartStats Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nstream-reflect Show documentation
Show all versions of nstream-reflect Show documentation
Web Agent introspection runtime
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 swim.collections.FingerTrieSeq;
import swim.structure.Form;
import swim.structure.Item;
import swim.structure.Kind;
import swim.structure.Record;
import swim.structure.Value;
import swim.util.Builder;
public class PartStats {
final Value partKey;
final LinkStats linkStats;
final FingerTrieSeq hostStats;
public PartStats(Value partKey, LinkStats linkStats, FingerTrieSeq hostStats) {
this.partKey = partKey;
this.linkStats = linkStats;
this.hostStats = hostStats;
}
public Value partKey() {
return this.partKey;
}
public LinkStats linkStats() {
return this.linkStats;
}
public FingerTrieSeq hostStats() {
return this.hostStats;
}
public Value toValue() {
return form().mold(this).toValue();
}
private static Form form;
@Kind
public static Form form() {
if (form == null) {
form = new PartStatsForm();
}
return form;
}
}
final class PartStatsForm extends Form {
@Override
public String tag() {
return "part";
}
@Override
public Class> type() {
return PartStats.class;
}
@Override
public Item mold(PartStats stats) {
if (stats != null) {
final Record record = Record.create();
record.attr(tag(), stats.partKey);
record.addAll((Record) stats.linkStats.toValue());
for (HostStats host : stats.hostStats) {
record.item(host.toValue());
}
return record;
} else {
return Item.extant();
}
}
@Override
public PartStats cast(Item item) {
final Value value = item.toValue();
final Value partKey = value.getAttr(tag());
if (partKey.isDefined()) {
final LinkStats linkStats = LinkStats.form().cast(value);
final Builder> hostStats = FingerTrieSeq.builder();
for (Item member : value) {
final HostStats host = HostStats.form().cast(member);
if (host != null) {
hostStats.add(host);
}
}
return new PartStats(partKey, linkStats, hostStats.bind());
}
return null;
}
}