kafka-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ij...@apache.org
Subject kafka git commit: MINOR: Replace for with foreach loop in common module
Date Sat, 18 Feb 2017 14:36:37 GMT
Repository: kafka
Updated Branches:
  refs/heads/trunk a515e9357 -> d734f4e56


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


Project: http://git-wip-us.apache.org/repos/asf/kafka/repo
Commit: http://git-wip-us.apache.org/repos/asf/kafka/commit/d734f4e5
Tree: http://git-wip-us.apache.org/repos/asf/kafka/tree/d734f4e5
Diff: http://git-wip-us.apache.org/repos/asf/kafka/diff/d734f4e5

Branch: refs/heads/trunk
Commit: d734f4e56d276f84b8c52b602edd67d41cbb6c35
Parents: a515e93
Author: Prabhat Kashyap <prabhat.kashyap@knoldus.in>
Authored: Sat Feb 18 06:22:08 2017 -0800
Committer: Ismael Juma <ismael@juma.me.uk>
Committed: Sat Feb 18 06:31:25 2017 -0800

----------------------------------------------------------------------
 .../org/apache/kafka/common/metrics/Sensor.java | 19 ++++++++-----------
 .../apache/kafka/common/metrics/stats/Avg.java  |  3 +--
 .../kafka/common/metrics/stats/Count.java       |  4 ++--
 .../apache/kafka/common/metrics/stats/Max.java  |  4 ++--
 .../apache/kafka/common/metrics/stats/Min.java  |  4 ++--
 .../kafka/common/metrics/stats/Percentiles.java |  4 ++--
 .../apache/kafka/common/metrics/stats/Rate.java |  4 ++--
 .../kafka/common/metrics/stats/SampledStat.java |  3 +--
 .../kafka/common/requests/MetadataResponse.java | 20 +++++++++++---------
 .../org/apache/kafka/common/utils/Utils.java    |  6 +++---
 10 files changed, 34 insertions(+), 37 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kafka/blob/d734f4e5/clients/src/main/java/org/apache/kafka/common/metrics/Sensor.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/Sensor.java b/clients/src/main/java/org/apache/kafka/common/metrics/Sensor.java
index 4a9b488..e85b559 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/Sensor.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/Sensor.java
@@ -117,8 +117,8 @@ public final class Sensor {
     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 final class Sensor {
             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 final class Sensor {
     }
 
     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());
                     }
                 }

http://git-wip-us.apache.org/repos/asf/kafka/blob/d734f4e5/clients/src/main/java/org/apache/kafka/common/metrics/stats/Avg.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Avg.java b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Avg.java
index 0fe7380..a65dd28 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Avg.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Avg.java
@@ -34,8 +34,7 @@ public class Avg extends SampledStat {
     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;
         }

http://git-wip-us.apache.org/repos/asf/kafka/blob/d734f4e5/clients/src/main/java/org/apache/kafka/common/metrics/stats/Count.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Count.java b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Count.java
index 90c0bf5..9ca3231 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Count.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Count.java
@@ -33,8 +33,8 @@ public class Count extends SampledStat {
     @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;
     }
 

http://git-wip-us.apache.org/repos/asf/kafka/blob/d734f4e5/clients/src/main/java/org/apache/kafka/common/metrics/stats/Max.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Max.java b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Max.java
index 6bbb0a3..f0d741c 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Max.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Max.java
@@ -33,8 +33,8 @@ public final class Max extends SampledStat {
     @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;
     }
 

http://git-wip-us.apache.org/repos/asf/kafka/blob/d734f4e5/clients/src/main/java/org/apache/kafka/common/metrics/stats/Min.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Min.java b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Min.java
index 113d745..5cfb535 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Min.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Min.java
@@ -33,8 +33,8 @@ public class Min extends SampledStat {
     @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;
     }
 

http://git-wip-us.apache.org/repos/asf/kafka/blob/d734f4e5/clients/src/main/java/org/apache/kafka/common/metrics/stats/Percentiles.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Percentiles.java
b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Percentiles.java
index 12064f5..df0ee4a 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Percentiles.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Percentiles.java
@@ -78,8 +78,8 @@ public class Percentiles extends SampledStat implements CompoundStat {
         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)

http://git-wip-us.apache.org/repos/asf/kafka/blob/d734f4e5/clients/src/main/java/org/apache/kafka/common/metrics/stats/Rate.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Rate.java b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Rate.java
index 6e49649..c0dd10e 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/stats/Rate.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/stats/Rate.java
@@ -125,8 +125,8 @@ public class Rate implements MeasurableStat {
         @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;
         }
 

http://git-wip-us.apache.org/repos/asf/kafka/blob/d734f4e5/clients/src/main/java/org/apache/kafka/common/metrics/stats/SampledStat.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/metrics/stats/SampledStat.java
b/clients/src/main/java/org/apache/kafka/common/metrics/stats/SampledStat.java
index 7d52ed3..58ea93f 100644
--- a/clients/src/main/java/org/apache/kafka/common/metrics/stats/SampledStat.java
+++ b/clients/src/main/java/org/apache/kafka/common/metrics/stats/SampledStat.java
@@ -96,8 +96,7 @@ public abstract class SampledStat implements MeasurableStat {
     /* 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);
         }

http://git-wip-us.apache.org/repos/asf/kafka/blob/d734f4e5/clients/src/main/java/org/apache/kafka/common/requests/MetadataResponse.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/requests/MetadataResponse.java
b/clients/src/main/java/org/apache/kafka/common/requests/MetadataResponse.java
index 268bf84..1532f67 100644
--- a/clients/src/main/java/org/apache/kafka/common/requests/MetadataResponse.java
+++ b/clients/src/main/java/org/apache/kafka/common/requests/MetadataResponse.java
@@ -94,14 +94,14 @@ public class MetadataResponse extends AbstractResponse {
     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 class MetadataResponse extends AbstractResponse {
 
         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,8 +131,8 @@ public class MetadataResponse extends AbstractResponse {
             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);
@@ -140,19 +140,21 @@ public class MetadataResponse extends AbstractResponse {
                 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));
             }

http://git-wip-us.apache.org/repos/asf/kafka/blob/d734f4e5/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
----------------------------------------------------------------------
diff --git a/clients/src/main/java/org/apache/kafka/common/utils/Utils.java b/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
index ab89f6b..c8f57bb 100755
--- a/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
+++ b/clients/src/main/java/org/apache/kafka/common/utils/Utils.java
@@ -228,9 +228,9 @@ public class Utils {
      */
     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;
     }


Mime
View raw message