Skip to content

Commit acf70b3

Browse files
committed
Convert to Java 5 enhanced loops.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/codec/trunk@1145000 13f79535-47bb-0310-9956-ffa450edef68
1 parent 1440a37 commit acf70b3

19 files changed

Lines changed: 72 additions & 72 deletions

src/java/org/apache/commons/codec/binary/BaseNCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -415,8 +415,8 @@ protected boolean containsAlphabetOrPad(byte[] arrayOctet) {
415415
if (arrayOctet == null) {
416416
return false;
417417
}
418-
for (int i = 0; i < arrayOctet.length; i++) {
419-
if (PAD == arrayOctet[i] || isInAlphabet(arrayOctet[i])) {
418+
for (byte element : arrayOctet) {
419+
if (PAD == element || isInAlphabet(element)) {
420420
return true;
421421
}
422422
}

src/java/org/apache/commons/codec/language/ColognePhonetic.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -272,8 +272,8 @@ public char removeNext() {
272272
* Returns whether the array contains the key, or not.
273273
*/
274274
private static boolean arrayContains(char[] arr, char key) {
275-
for (int i = 0; i < arr.length; i++) {
276-
if (arr[i] == key) {
275+
for (char element : arr) {
276+
if (element == key) {
277277
return true;
278278
}
279279
}
@@ -406,9 +406,9 @@ private String preprocess(String text) {
406406

407407
for (int index = 0; index < chrs.length; index++) {
408408
if (chrs[index] > 'Z') {
409-
for (int replacement = 0; replacement < PREPROCESS_MAP.length; replacement++) {
410-
if (chrs[index] == PREPROCESS_MAP[replacement][0]) {
411-
chrs[index] = PREPROCESS_MAP[replacement][1];
409+
for (char[] element : PREPROCESS_MAP) {
410+
if (chrs[index] == element[0]) {
411+
chrs[index] = element[1];
412412
break;
413413
}
414414
}

src/java/org/apache/commons/codec/language/DoubleMetaphone.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -905,8 +905,8 @@ private boolean isVowel(char ch) {
905905
*/
906906
private boolean isSilentStart(String value) {
907907
boolean result = false;
908-
for (int i = 0; i < SILENT_START.length; i++) {
909-
if (value.startsWith(SILENT_START[i])) {
908+
for (String element : SILENT_START) {
909+
if (value.startsWith(element)) {
910910
result = true;
911911
break;
912912
}
@@ -1013,8 +1013,8 @@ protected static boolean contains(String value, int start, int length,
10131013
if (start >= 0 && start + length <= value.length()) {
10141014
String target = value.substring(start, start + length);
10151015

1016-
for (int i = 0; i < criteria.length; i++) {
1017-
if (target.equals(criteria[i])) {
1016+
for (String element : criteria) {
1017+
if (target.equals(element)) {
10181018
result = true;
10191019
break;
10201020
}

src/java/org/apache/commons/codec/net/QCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,8 @@ protected byte[] doDecoding(byte[] bytes) throws DecoderException {
153153
return null;
154154
}
155155
boolean hasUnderscores = false;
156-
for (int i = 0; i < bytes.length; i++) {
157-
if (bytes[i] == UNDERSCORE) {
156+
for (byte b : bytes) {
157+
if (b == UNDERSCORE) {
158158
hasUnderscores = true;
159159
break;
160160
}

src/java/org/apache/commons/codec/net/QuotedPrintableCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,8 @@ public static final byte[] encodeQuotedPrintable(BitSet printable, byte[] bytes)
145145
printable = PRINTABLE_CHARS;
146146
}
147147
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
148-
for (int i = 0; i < bytes.length; i++) {
149-
int b = bytes[i];
148+
for (byte c : bytes) {
149+
int b = c;
150150
if (b < 0) {
151151
b = 256 + b;
152152
}

src/java/org/apache/commons/codec/net/URLCodec.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,8 @@ public static final byte[] encodeUrl(BitSet urlsafe, byte[] bytes) {
130130
}
131131

132132
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
133-
for (int i = 0; i < bytes.length; i++) {
134-
int b = bytes[i];
133+
for (byte c : bytes) {
134+
int b = c;
135135
if (b < 0) {
136136
b = 256 + b;
137137
}

src/test/org/apache/commons/codec/StringEncoderAbstractTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public void checkEncoding(String expected, String source) throws EncoderExceptio
3636
}
3737

3838
protected void checkEncodings(String[][] data) throws EncoderException {
39-
for (int i = 0; i < data.length; i++) {
40-
this.checkEncoding(data[i][1], data[i][0]);
39+
for (String[] element : data) {
40+
this.checkEncoding(element[1], element[0]);
4141
}
4242
}
4343

4444
protected void checkEncodingVariations(String expected, String data[]) throws EncoderException {
45-
for (int i = 0; i < data.length; i++) {
46-
this.checkEncoding(expected, data[i]);
45+
for (String element : data) {
46+
this.checkEncoding(expected, element);
4747
}
4848
}
4949

@@ -93,16 +93,16 @@ public void testLocaleIndependence() throws Exception {
9393
Locale[] locales = {Locale.ENGLISH, new Locale("tr"), Locale.getDefault()};
9494

9595
try {
96-
for (int i = 0; i < data.length; i++) {
96+
for (String element : data) {
9797
String ref = null;
9898
for (int j = 0; j < locales.length; j++) {
9999
Locale.setDefault(locales[j]);
100100
if (j <= 0) {
101-
ref = encoder.encode(data[i]);
101+
ref = encoder.encode(element);
102102
} else {
103103
String cur = null;
104104
try {
105-
cur = encoder.encode(data[i]);
105+
cur = encoder.encode(element);
106106
} catch (Exception e) {
107107
Assert.fail(Locale.getDefault().toString() + ": " + e.getMessage());
108108
}

src/test/org/apache/commons/codec/binary/Base32OutputStreamTest.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[
230230
// Start with encode.
231231
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
232232
OutputStream out = new Base32OutputStream(byteOut, true, chunkSize, seperator);
233-
for (int i = 0; i < decoded.length; i++) {
234-
out.write(decoded[i]);
233+
for (byte element : decoded) {
234+
out.write(element);
235235
}
236236
out.close();
237237
byte[] output = byteOut.toByteArray();
@@ -240,8 +240,8 @@ private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[
240240
// Now let's try decode.
241241
byteOut = new ByteArrayOutputStream();
242242
out = new Base32OutputStream(byteOut, false);
243-
for (int i = 0; i < encoded.length; i++) {
244-
out.write(encoded[i]);
243+
for (byte element : encoded) {
244+
out.write(element);
245245
}
246246
out.close();
247247
output = byteOut.toByteArray();
@@ -250,8 +250,8 @@ private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[
250250
// Now let's try decode with tonnes of flushes.
251251
byteOut = new ByteArrayOutputStream();
252252
out = new Base32OutputStream(byteOut, false);
253-
for (int i = 0; i < encoded.length; i++) {
254-
out.write(encoded[i]);
253+
for (byte element : encoded) {
254+
out.write(element);
255255
out.flush();
256256
}
257257
out.close();
@@ -265,8 +265,8 @@ private void testByteByByte(byte[] encoded, byte[] decoded, int chunkSize, byte[
265265
out = new Base32OutputStream(out, false);
266266
out = new Base32OutputStream(out, true, chunkSize, seperator);
267267
}
268-
for (int i = 0; i < decoded.length; i++) {
269-
out.write(decoded[i]);
268+
for (byte element : decoded) {
269+
out.write(element);
270270
}
271271
out.close();
272272
output = byteOut.toByteArray();

src/test/org/apache/commons/codec/binary/Base32Test.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,24 +61,24 @@ public class Base32Test {
6161
@Test
6262
public void testBase32Samples() throws Exception {
6363
Base32 codec = new Base32();
64-
for (int i = 0; i < BASE32_TEST_CASES.length; i++) {
65-
assertEquals(BASE32_TEST_CASES[i][1], codec.encodeAsString(BASE32_TEST_CASES[i][0].getBytes("UTF-8")));
64+
for (String[] element : BASE32_TEST_CASES) {
65+
assertEquals(element[1], codec.encodeAsString(element[0].getBytes("UTF-8")));
6666
}
6767
}
6868

6969
@Test
7070
public void testBase32HexSamples() throws Exception {
7171
Base32 codec = new Base32(true);
72-
for (int i = 0; i < BASE32HEX_TEST_CASES.length; i++) {
73-
assertEquals(BASE32HEX_TEST_CASES[i][1], codec.encodeAsString(BASE32HEX_TEST_CASES[i][0].getBytes("UTF-8")));
72+
for (String[] element : BASE32HEX_TEST_CASES) {
73+
assertEquals(element[1], codec.encodeAsString(element[0].getBytes("UTF-8")));
7474
}
7575
}
7676

7777
@Test
7878
public void testBase32Chunked () throws Exception {
7979
Base32 codec = new Base32(20);
80-
for (int i = 0; i < BASE32_TEST_CASES_CHUNKED.length; i++) {
81-
assertEquals(BASE32_TEST_CASES_CHUNKED[i][1], codec.encodeAsString(BASE32_TEST_CASES_CHUNKED[i][0].getBytes("UTF-8")));
80+
for (String[] element : BASE32_TEST_CASES_CHUNKED) {
81+
assertEquals(element[1], codec.encodeAsString(element[0].getBytes("UTF-8")));
8282
}
8383
}
8484

src/test/org/apache/commons/codec/binary/Base32TestData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,8 +116,8 @@ static byte[][] randomData(BaseNCodec codec, int size) {
116116
* @return true if bytes contains c, false otherwise
117117
*/
118118
static boolean bytesContain(byte[] bytes, byte c) {
119-
for (int i = 0; i < bytes.length; i++) {
120-
if (bytes[i] == c) { return true; }
119+
for (byte b : bytes) {
120+
if (b == c) { return true; }
121121
}
122122
return false;
123123
}

0 commit comments

Comments
 (0)