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 allRenderersReadyOrEnded = true;
for (int rendererIndex = 0; rendererIndex < renderers.length; rendererIndex++) {
TrackRenderer renderer = renderers[rendererIndex];
for (TrackRenderer renderer : renderers) {
allRenderersEnded = allRenderersEnded && renderer.isEnded();
allRenderersReadyOrEnded = allRenderersReadyOrEnded && isReadyOrEnded(renderer);
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -240,8 +240,7 @@ public final class SimpleCache implements Cache {
if (files == null) {
return;
}
for (int i = 0; i < files.length; i++) {
File file = files[i];
for (File file : files) {
if (file.length() == 0) {
file.delete();
} 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.
*/
public static boolean contains(Object[] items, Object item) {
for (int i = 0; i < items.length; i++) {
if (Util.areEqual(items[i], item)) {
for (Object arrayItem : items) {
if (Util.areEqual(arrayItem, item)) {
return true;
}
}

View File

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