Skip to content

Commit

Permalink
8065: Remove socket read illegal value
Browse files Browse the repository at this point in the history
Reviewed-by: hirt
  • Loading branch information
bobpengxie authored and RealCLanger committed Apr 26, 2023
1 parent 06bbd5f commit 58c9ede
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,27 @@ private static class SumConsumer extends QuantityConsumer<SumConsumer> {
double sum = 0.0;
IUnit unit = null;

Predicate predicate;

SumConsumer(IMemberAccessor<? extends IQuantity, IItem> accessor) {
super(accessor);
}

SumConsumer(IMemberAccessor<? extends IQuantity, IItem> accessor, Predicate predicate) {
this(accessor);
this.predicate = predicate;
}

@Override
public void consume(IItem item) {
IQuantity fieldValue = accessor.getMember(item);
if (unit == null) {
unit = fieldValue.getUnit();
}
sum += fieldValue.doubleValueIn(unit);
double value = fieldValue.doubleValueIn(unit);
if ((predicate != null && predicate.test(value)) || (predicate == null)) {
sum += value;
}
}

@Override
Expand Down Expand Up @@ -757,6 +767,31 @@ protected IMemberAccessor<IQuantity, IItem> doGetAccessor(IType<IItem> type) {
throw new IllegalArgumentException("Can only use LinearKindOfQuantity"); //$NON-NLS-1$
}

public static IAggregator<IQuantity, ?> sum(
String name, String description, final String typeId, final IAttribute<IQuantity> attribute,
Predicate<Double> predicate) {
ContentType<IQuantity> contentType = attribute.getContentType();
if (contentType instanceof LinearKindOfQuantity) {
return new Sum(name, description, (LinearKindOfQuantity) contentType) {

@Override
public SumConsumer newItemConsumer(IType<IItem> type) {
return new SumConsumer(getAccessor(type), predicate);
}

@Override
protected IMemberAccessor<IQuantity, IItem> doGetAccessor(IType<IItem> type) {
if (type.getIdentifier().equals(typeId)) {
return attribute.getAccessor(type);
}
return null;
}

};
}
throw new IllegalArgumentException("Can only use LinearKindOfQuantity"); //$NON-NLS-1$
}

public static IAggregator<IQuantity, ?> sum(
String name, String description, LinearKindOfQuantity ct, final IAccessorFactory<IQuantity> af) {
return new Sum(name, description, ct) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ public final class JdkAggregators {
Messages.getString(Messages.AGGR_SOCKET_WRITE_SIZE_DESC), SOCKET_WRITE, IO_SOCKET_BYTES_WRITTEN);
public static final IAggregator<IQuantity, ?> SOCKET_READ_SIZE = Aggregators.sum(
Messages.getString(Messages.AGGR_SOCKET_READ_SIZE), Messages.getString(Messages.AGGR_SOCKET_READ_SIZE_DESC),
SOCKET_READ, IO_SOCKET_BYTES_READ);
SOCKET_READ, IO_SOCKET_BYTES_READ, (value -> value >= 0));
public static final IAggregator<IQuantity, ?> SOCKET_WRITE_COUNT = Aggregators.count(
Messages.getString(Messages.AGGR_SOCKET_WRITE_COUNT),
Messages.getString(Messages.AGGR_SOCKET_WRITE_COUNT_DESC), JdkFilters.SOCKET_WRITE);
Expand Down

0 comments on commit 58c9ede

Please sign in to comment.