Use new-style for loops specifically for arrays.

As per: http://developer.android.com/training/articles/perf-tips.html
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=117716743
This commit is contained in:
olly 2016-03-21 08:39:23 -07:00 committed by Oliver Woodman
parent 2469f631ae
commit e7a27245e7
12 changed files with 42 additions and 45 deletions

View File

@ -285,8 +285,7 @@ import java.util.concurrent.atomic.AtomicInteger;
boolean allRenderersEnded = true; boolean allRenderersEnded = true;
boolean allRenderersReadyOrEnded = true; boolean allRenderersReadyOrEnded = true;
for (int rendererIndex = 0; rendererIndex < renderers.length; rendererIndex++) { for (TrackRenderer renderer : renderers) {
TrackRenderer renderer = renderers[rendererIndex];
allRenderersEnded = allRenderersEnded && renderer.isEnded(); allRenderersEnded = allRenderersEnded && renderer.isEnded();
allRenderersReadyOrEnded = allRenderersReadyOrEnded && isReadyOrEnded(renderer); allRenderersReadyOrEnded = allRenderersReadyOrEnded && isReadyOrEnded(renderer);
} }

View File

@ -228,8 +228,8 @@ public final class MediaCodecUtil {
} }
CodecProfileLevel[] profileLevels = info.getProfileLevels(); CodecProfileLevel[] profileLevels = info.getProfileLevels();
for (int i = 0; i < profileLevels.length; i++) { for (CodecProfileLevel profileLevel : profileLevels) {
if (profileLevels[i].profile == profile && profileLevels[i].level >= level) { if (profileLevel.profile == profile && profileLevel.level >= level) {
return true; return true;
} }
} }
@ -250,8 +250,7 @@ public final class MediaCodecUtil {
int maxH264DecodableFrameSize = 0; int maxH264DecodableFrameSize = 0;
CodecProfileLevel[] profileLevels = info.getProfileLevels(); CodecProfileLevel[] profileLevels = info.getProfileLevels();
for (int i = 0; i < profileLevels.length; i++) { for (CodecProfileLevel profileLevel : profileLevels) {
CodecProfileLevel profileLevel = profileLevels[i];
maxH264DecodableFrameSize = Math.max(avcLevelToMaxFrameSize(profileLevel.level), maxH264DecodableFrameSize = Math.max(avcLevelToMaxFrameSize(profileLevel.level),
maxH264DecodableFrameSize); maxH264DecodableFrameSize);
} }

View File

@ -266,9 +266,9 @@ public class MediaCodecVideoTrackRenderer extends MediaCodecTrackRenderer {
adaptiveMaxWidth = Format.NO_VALUE; adaptiveMaxWidth = Format.NO_VALUE;
adaptiveMaxHeight = Format.NO_VALUE; adaptiveMaxHeight = Format.NO_VALUE;
if (formats.length > 1) { if (formats.length > 1) {
for (int i = 0; i < formats.length; i++) { for (Format format : formats) {
adaptiveMaxWidth = Math.max(adaptiveMaxWidth, formats[i].width); adaptiveMaxWidth = Math.max(adaptiveMaxWidth, format.width);
adaptiveMaxHeight = Math.max(adaptiveMaxHeight, formats[i].height); adaptiveMaxHeight = Math.max(adaptiveMaxHeight, format.height);
} }
if (adaptiveMaxWidth == Format.NO_VALUE if (adaptiveMaxWidth == Format.NO_VALUE
|| adaptiveMaxHeight == Format.NO_VALUE) { || adaptiveMaxHeight == Format.NO_VALUE) {

View File

@ -43,25 +43,25 @@ public final class MultiSampleSource implements SampleSource {
return true; return true;
} }
boolean sourcesPrepared = true; boolean sourcesPrepared = true;
for (int i = 0; i < sources.length; i++) { for (SampleSource source : sources) {
sourcesPrepared &= sources[i].prepare(positionUs); sourcesPrepared &= source.prepare(positionUs);
} }
if (sourcesPrepared) { if (sourcesPrepared) {
prepared = true; prepared = true;
durationUs = C.UNKNOWN_TIME_US; durationUs = C.UNKNOWN_TIME_US;
int totalTrackGroupCount = 0; int totalTrackGroupCount = 0;
for (int i = 0; i < sources.length; i++) { for (SampleSource source : sources) {
totalTrackGroupCount += sources[i].getTrackGroups().length; totalTrackGroupCount += source.getTrackGroups().length;
if (sources[i].getDurationUs() > durationUs) { if (source.getDurationUs() > durationUs) {
durationUs = sources[i].getDurationUs(); durationUs = source.getDurationUs();
} }
} }
TrackGroup[] trackGroups = new TrackGroup[totalTrackGroupCount]; TrackGroup[] trackGroups = new TrackGroup[totalTrackGroupCount];
int trackGroupIndex = 0; int trackGroupIndex = 0;
for (int i = 0; i < sources.length; i++) { for (SampleSource source : sources) {
int sourceTrackGroupCount = sources[i].getTrackGroups().length; int sourceTrackGroupCount = source.getTrackGroups().length;
for (int j = 0; j < sourceTrackGroupCount; j++) { for (int j = 0; j < sourceTrackGroupCount; j++) {
trackGroups[trackGroupIndex++] = sources[i].getTrackGroups().get(j); trackGroups[trackGroupIndex++] = source.getTrackGroups().get(j);
} }
} }
this.trackGroups = new TrackGroupArray(trackGroups); this.trackGroups = new TrackGroupArray(trackGroups);
@ -91,15 +91,15 @@ public final class MultiSampleSource implements SampleSource {
@Override @Override
public void continueBuffering(long positionUs) { public void continueBuffering(long positionUs) {
for (int i = 0; i < sources.length; i++) { for (SampleSource source : sources) {
sources[i].continueBuffering(positionUs); source.continueBuffering(positionUs);
} }
} }
@Override @Override
public void seekToUs(long positionUs) { public void seekToUs(long positionUs) {
for (int i = 0; i < sources.length; i++) { for (SampleSource source : sources) {
sources[i].seekToUs(positionUs); source.seekToUs(positionUs);
} }
} }
@ -111,8 +111,8 @@ public final class MultiSampleSource implements SampleSource {
@Override @Override
public long getBufferedPositionUs() { public long getBufferedPositionUs() {
long bufferedPositionUs = durationUs != C.UNKNOWN_TIME_US ? durationUs : Long.MAX_VALUE; long bufferedPositionUs = durationUs != C.UNKNOWN_TIME_US ? durationUs : Long.MAX_VALUE;
for (int i = 0; i < sources.length; i++) { for (SampleSource source : sources) {
long rendererBufferedPositionUs = sources[i].getBufferedPositionUs(); long rendererBufferedPositionUs = source.getBufferedPositionUs();
if (rendererBufferedPositionUs == C.UNKNOWN_TIME_US) { if (rendererBufferedPositionUs == C.UNKNOWN_TIME_US) {
return C.UNKNOWN_TIME_US; return C.UNKNOWN_TIME_US;
} else if (rendererBufferedPositionUs == C.END_OF_SOURCE_US) { } else if (rendererBufferedPositionUs == C.END_OF_SOURCE_US) {
@ -126,8 +126,8 @@ public final class MultiSampleSource implements SampleSource {
@Override @Override
public void release() { public void release() {
for (int i = 0; i < sources.length; i++) { for (SampleSource source : sources) {
sources[i].release(); source.release();
} }
prepared = false; prepared = false;
} }

View File

@ -149,14 +149,14 @@ public final class Mp4Extractor implements Extractor, SeekMap {
@Override @Override
public long getPosition(long timeUs) { public long getPosition(long timeUs) {
long earliestSamplePosition = Long.MAX_VALUE; long earliestSamplePosition = Long.MAX_VALUE;
for (int trackIndex = 0; trackIndex < tracks.length; trackIndex++) { for (Mp4Track track : tracks) {
TrackSampleTable sampleTable = tracks[trackIndex].sampleTable; TrackSampleTable sampleTable = track.sampleTable;
int sampleIndex = sampleTable.getIndexOfEarlierOrEqualSynchronizationSample(timeUs); int sampleIndex = sampleTable.getIndexOfEarlierOrEqualSynchronizationSample(timeUs);
if (sampleIndex == TrackSampleTable.NO_SAMPLE) { if (sampleIndex == TrackSampleTable.NO_SAMPLE) {
// Handle the case where the requested time is before the first synchronization sample. // Handle the case where the requested time is before the first synchronization sample.
sampleIndex = sampleTable.getIndexOfLaterOrEqualSynchronizationSample(timeUs); sampleIndex = sampleTable.getIndexOfLaterOrEqualSynchronizationSample(timeUs);
} }
tracks[trackIndex].sampleIndex = sampleIndex; track.sampleIndex = sampleIndex;
long offset = sampleTable.offsets[sampleIndex]; long offset = sampleTable.offsets[sampleIndex];
if (offset < earliestSamplePosition) { if (offset < earliestSamplePosition) {

View File

@ -595,8 +595,8 @@ public class HlsChunkSource {
return false; return false;
} }
String[] codecArray = codecs.split("(\\s*,\\s*)|(\\s*$)"); String[] codecArray = codecs.split("(\\s*,\\s*)|(\\s*$)");
for (int i = 0; i < codecArray.length; i++) { for (String codec : codecArray) {
if (codecArray[i].startsWith(prefix)) { if (codec.startsWith(prefix)) {
return true; return true;
} }
} }

View File

@ -160,8 +160,8 @@ public final class TtmlParser extends SubtitleParser {
TtmlStyle style = parseStyleAttributes(xmlParser, new TtmlStyle()); TtmlStyle style = parseStyleAttributes(xmlParser, new TtmlStyle());
if (parentStyleId != null) { if (parentStyleId != null) {
String[] ids = parseStyleIds(parentStyleId); String[] ids = parseStyleIds(parentStyleId);
for (int i = 0; i < ids.length; i++) { for (String id : ids) {
style.chain(globalStyles.get(ids[i])); style.chain(globalStyles.get(id));
} }
} }
if (style.getId() != null) { if (style.getId() != null) {

View File

@ -46,8 +46,8 @@ import java.util.Map;
} else if (style == null && styleIds.length > 1) { } else if (style == null && styleIds.length > 1) {
// only multiple referential styles present // only multiple referential styles present
TtmlStyle chainedStyle = new TtmlStyle(); TtmlStyle chainedStyle = new TtmlStyle();
for (int i = 0; i < styleIds.length; i++) { for (String styleId : styleIds) {
chainedStyle.chain(globalStyles.get(styleIds[i])); chainedStyle.chain(globalStyles.get(styleId));
} }
return chainedStyle; return chainedStyle;
} else if (style != null && styleIds != null && styleIds.length == 1) { } else if (style != null && styleIds != null && styleIds.length == 1) {
@ -55,8 +55,8 @@ import java.util.Map;
return style.chain(globalStyles.get(styleIds[0])); return style.chain(globalStyles.get(styleIds[0]));
} else if (style != null && styleIds != null && styleIds.length > 1) { } else if (style != null && styleIds != null && styleIds.length > 1) {
// merge multiple referential styles into inline style // merge multiple referential styles into inline style
for (int i = 0; i < styleIds.length; i++) { for (String styleId : styleIds) {
style.chain(globalStyles.get(styleIds[i])); style.chain(globalStyles.get(styleId));
} }
return style; return style;
} }

View File

@ -53,8 +53,8 @@ public final class WebvttParserUtil {
long value = 0; long value = 0;
String[] parts = timestamp.split("\\.", 2); String[] parts = timestamp.split("\\.", 2);
String[] subparts = parts[0].split(":"); String[] subparts = parts[0].split(":");
for (int i = 0; i < subparts.length; i++) { for (String subpart : subparts) {
value = value * 60 + Long.parseLong(subparts[i]); value = value * 60 + Long.parseLong(subpart);
} }
return (value * 1000 + Long.parseLong(parts[1])) * 1000; return (value * 1000 + Long.parseLong(parts[1])) * 1000;
} }

View File

@ -240,8 +240,7 @@ public final class SimpleCache implements Cache {
if (files == null) { if (files == null) {
return; return;
} }
for (int i = 0; i < files.length; i++) { for (File file : files) {
File file = files[i];
if (file.length() == 0) { if (file.length() == 0) {
file.delete(); file.delete();
} else { } else {

View File

@ -182,8 +182,8 @@ public final class Util {
* @return True if the array contains an object equal to the item being searched for. * @return True if the array contains an object equal to the item being searched for.
*/ */
public static boolean contains(Object[] items, Object item) { public static boolean contains(Object[] items, Object item) {
for (int i = 0; i < items.length; i++) { for (Object arrayItem : items) {
if (Util.areEqual(items[i], item)) { if (Util.areEqual(arrayItem, item)) {
return true; return true;
} }
} }

View File

@ -83,8 +83,8 @@ public abstract class SimpleDecoder<I extends InputBuffer, O extends OutputBuffe
*/ */
protected final void setInitialInputBufferSize(int size) { protected final void setInitialInputBufferSize(int size) {
Assertions.checkState(availableInputBufferCount == availableInputBuffers.length); Assertions.checkState(availableInputBufferCount == availableInputBuffers.length);
for (int i = 0; i < availableInputBuffers.length; i++) { for (I inputBuffer : availableInputBuffers) {
availableInputBuffers[i].sampleHolder.ensureSpaceForWrite(size); inputBuffer.sampleHolder.ensureSpaceForWrite(size);
} }
} }