Skip to content
Permalink
Browse files

MINOR: Replace for with foreach loop in common module

Author: Prabhat Kashyap <prabhat.kashyap@knoldus.in>

Reviewers: Ismael Juma <ismael@juma.me.uk>

Closes #2530 from PKOfficial/refactored-code
  • Loading branch information...
PKOfficial authored and ijuma committed Feb 18, 2017
1 parent a515e93 commit d734f4e56d276f84b8c52b602edd67d41cbb6c35
@@ -117,8 +117,8 @@ public boolean shouldRecord(final int configId) {
private void checkForest(Set<Sensor> sensors) {
if (!sensors.add(this))
throw new IllegalArgumentException("Circular dependency in sensors: " + name() + " is its own parent.");
for (int i = 0; i < parents.length; i++)
parents[i].checkForest(sensors);
for (Sensor parent : parents)
parent.checkForest(sensors);
}

/**
@@ -168,12 +168,12 @@ public void record(double value, long timeMs) {
this.lastRecordTime = timeMs;
synchronized (this) {
// increment all the stats
for (int i = 0; i < this.stats.size(); i++)
this.stats.get(i).record(config, value, timeMs);
for (Stat stat : this.stats)
stat.record(config, value, timeMs);
checkQuotas(timeMs);
}
for (int i = 0; i < parents.length; i++)
parents[i].record(value, timeMs);
for (Sensor parent : parents)
parent.record(value, timeMs);
}
}

@@ -185,17 +185,14 @@ public void checkQuotas() {
}

public void checkQuotas(long timeMs) {
for (int i = 0; i < this.metrics.size(); i++) {
KafkaMetric metric = this.metrics.get(i);
for (KafkaMetric metric : this.metrics) {
MetricConfig config = metric.config();
if (config != null) {
Quota quota = config.quota();
if (quota != null) {
double value = metric.value(timeMs);
if (!quota.acceptable(value)) {
throw new QuotaViolationException(
metric.metricName(),
value,
throw new QuotaViolationException(metric.metricName(), value,
quota.bound());
}
}
@@ -34,8 +34,7 @@ protected void update(Sample sample, MetricConfig config, double value, long now
public double combine(List<Sample> samples, MetricConfig config, long now) {
double total = 0.0;
long count = 0;
for (int i = 0; i < samples.size(); i++) {
Sample s = samples.get(i);
for (Sample s : samples) {
total += s.value;
count += s.eventCount;
}
@@ -33,8 +33,8 @@ protected void update(Sample sample, MetricConfig config, double value, long now
@Override
public double combine(List<Sample> samples, MetricConfig config, long now) {
double total = 0.0;
for (int i = 0; i < samples.size(); i++)
total += samples.get(i).value;
for (Sample sample : samples)
total += sample.value;
return total;
}

@@ -33,8 +33,8 @@ protected void update(Sample sample, MetricConfig config, double value, long now
@Override
public double combine(List<Sample> samples, MetricConfig config, long now) {
double max = Double.NEGATIVE_INFINITY;
for (int i = 0; i < samples.size(); i++)
max = Math.max(max, samples.get(i).value);
for (Sample sample : samples)
max = Math.max(max, sample.value);
return max;
}

@@ -33,8 +33,8 @@ protected void update(Sample sample, MetricConfig config, double value, long now
@Override
public double combine(List<Sample> samples, MetricConfig config, long now) {
double min = Double.MAX_VALUE;
for (int i = 0; i < samples.size(); i++)
min = Math.min(min, samples.get(i).value);
for (Sample sample : samples)
min = Math.min(min, sample.value);
return min;
}

@@ -78,8 +78,8 @@ public double value(MetricConfig config, long now, double quantile) {
float sum = 0.0f;
float quant = (float) quantile;
for (int b = 0; b < buckets; b++) {
for (int s = 0; s < this.samples.size(); s++) {
HistogramSample sample = (HistogramSample) this.samples.get(s);
for (Sample s : this.samples) {
HistogramSample sample = (HistogramSample) s;
float[] hist = sample.histogram.counts();
sum += hist[b];
if (sum / count > quant)
@@ -125,8 +125,8 @@ protected void update(Sample sample, MetricConfig config, double value, long tim
@Override
public double combine(List<Sample> samples, MetricConfig config, long now) {
double total = 0.0;
for (int i = 0; i < samples.size(); i++)
total += samples.get(i).value;
for (Sample sample : samples)
total += sample.value;
return total;
}

@@ -96,8 +96,7 @@ public Sample oldest(long now) {
/* Timeout any windows that have expired in the absence of any events */
protected void purgeObsoleteSamples(MetricConfig config, long now) {
long expireAge = config.samples() * config.timeWindowMs();
for (int i = 0; i < samples.size(); i++) {
Sample sample = this.samples.get(i);
for (Sample sample : samples) {
if (now - sample.lastWindowMs >= expireAge)
sample.reset(now);
}
@@ -94,14 +94,14 @@ public MetadataResponse(List<Node> brokers, String clusterId, int controllerId,
public MetadataResponse(Struct struct) {
Map<Integer, Node> brokers = new HashMap<>();
Object[] brokerStructs = (Object[]) struct.get(BROKERS_KEY_NAME);
for (int i = 0; i < brokerStructs.length; i++) {
Struct broker = (Struct) brokerStructs[i];
for (Object brokerStruct : brokerStructs) {
Struct broker = (Struct) brokerStruct;
int nodeId = broker.getInt(NODE_ID_KEY_NAME);
String host = broker.getString(HOST_KEY_NAME);
int port = broker.getInt(PORT_KEY_NAME);
// This field only exists in v1+
// When we can't know if a rack exists in a v0 response we default to null
String rack = broker.hasField(RACK_KEY_NAME) ? broker.getString(RACK_KEY_NAME) : null;
String rack = broker.hasField(RACK_KEY_NAME) ? broker.getString(RACK_KEY_NAME) : null;
brokers.put(nodeId, new Node(nodeId, host, port, rack));
}

@@ -120,8 +120,8 @@ public MetadataResponse(Struct struct) {

List<TopicMetadata> topicMetadata = new ArrayList<>();
Object[] topicInfos = (Object[]) struct.get(TOPIC_METADATA_KEY_NAME);
for (int i = 0; i < topicInfos.length; i++) {
Struct topicInfo = (Struct) topicInfos[i];
for (Object topicInfoObj : topicInfos) {
Struct topicInfo = (Struct) topicInfoObj;
Errors topicError = Errors.forCode(topicInfo.getShort(TOPIC_ERROR_CODE_KEY_NAME));
String topic = topicInfo.getString(TOPIC_KEY_NAME);
// This field only exists in v1+
@@ -131,28 +131,30 @@ public MetadataResponse(Struct struct) {
List<PartitionMetadata> partitionMetadata = new ArrayList<>();

Object[] partitionInfos = (Object[]) topicInfo.get(PARTITION_METADATA_KEY_NAME);
for (int j = 0; j < partitionInfos.length; j++) {
Struct partitionInfo = (Struct) partitionInfos[j];
for (Object partitionInfoObj : partitionInfos) {
Struct partitionInfo = (Struct) partitionInfoObj;
Errors partitionError = Errors.forCode(partitionInfo.getShort(PARTITION_ERROR_CODE_KEY_NAME));
int partition = partitionInfo.getInt(PARTITION_KEY_NAME);
int leader = partitionInfo.getInt(LEADER_KEY_NAME);
Node leaderNode = leader == -1 ? null : brokers.get(leader);
Object[] replicas = (Object[]) partitionInfo.get(REPLICAS_KEY_NAME);

List<Node> replicaNodes = new ArrayList<>(replicas.length);
for (Object replicaNodeId : replicas)
for (Object replicaNodeId : replicas) {
if (brokers.containsKey(replicaNodeId))
replicaNodes.add(brokers.get(replicaNodeId));
else
replicaNodes.add(new Node((int) replicaNodeId, "", -1));
}

Object[] isr = (Object[]) partitionInfo.get(ISR_KEY_NAME);
List<Node> isrNodes = new ArrayList<>(isr.length);
for (Object isrNode : isr)
for (Object isrNode : isr) {
if (brokers.containsKey(isrNode))
isrNodes.add(brokers.get(isrNode));
else
isrNodes.add(new Node((int) isrNode, "", -1));
}

partitionMetadata.add(new PartitionMetadata(partitionError, partition, leaderNode, replicaNodes, isrNodes));
}
@@ -228,9 +228,9 @@ public static int abs(int n) {
*/
public static long min(long first, long ... rest) {
long min = first;
for (int i = 0; i < rest.length; i++) {
if (rest[i] < min)
min = rest[i];
for (long r : rest) {
if (r < min)
min = r;
}
return min;
}

0 comments on commit d734f4e

Please sign in to comment.
You can’t perform that action at this time.