<commit_before>/*
 * Copyright 2017 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.banes.chris.tivi.tmdb

class TmdbImageUrlProvider(
        private var baseImageUrl: String = TmdbImageSizes.baseImageUrl,
        private var posterSizes: Array<String> = TmdbImageSizes.posterSizes,
        private var backdropSizes: Array<String> = TmdbImageSizes.backdropSizes
) {

    fun getPosterUrl(path: String, imageWidth: Int): String =
            "$baseImageUrl${selectSize(posterSizes, imageWidth)}$path"

    fun getBackdropUrl(path: String, imageWidth: Int): String =
            "$baseImageUrl${selectSize(backdropSizes, imageWidth)}$path"

    private fun selectSize(sizes: Array<String>, imageWidth: Int, forceLarger: Boolean = false): String {
        var previousSize: String? = null
        var previousWidth = 0

        for (i in sizes.indices) {
            val size = sizes[i]
            val sizeWidth = extractWidthAsIntFrom(size) ?: continue

            if (sizeWidth > imageWidth) {
                return if (forceLarger || (previousSize != null && imageWidth > (previousWidth + sizeWidth) / 2)) {
                    size
                } else {
                    previousSize!!
                }
            } else if (i == sizes.size - 1) {
                // If we get here then we're larger than the last bucket
                if (imageWidth < sizeWidth * 2) {
                    return size
                }
            }
<commit_msg>Fix NPE in TmdbImageUrlProvider where previousSize may still be null<commit_after>/*
 * Copyright 2017 Google, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.banes.chris.tivi.tmdb

class TmdbImageUrlProvider(
        private var baseImageUrl: String = TmdbImageSizes.baseImageUrl,
        private var posterSizes: Array<String> = TmdbImageSizes.posterSizes,
        private var backdropSizes: Array<String> = TmdbImageSizes.backdropSizes
) {

    fun getPosterUrl(path: String, imageWidth: Int): String =
            "$baseImageUrl${selectSize(posterSizes, imageWidth)}$path"

    fun getBackdropUrl(path: String, imageWidth: Int): String =
            "$baseImageUrl${selectSize(backdropSizes, imageWidth)}$path"

    private fun selectSize(sizes: Array<String>, imageWidth: Int, forceLarger: Boolean = false): String {
        var previousSize: String? = null
        var previousWidth = 0

        for (i in sizes.indices) {
            val size = sizes[i]
            val sizeWidth = extractWidthAsIntFrom(size) ?: continue

            if (sizeWidth > imageWidth) {
                if (forceLarger || (previousSize != null && imageWidth > (previousWidth + sizeWidth) / 2)) {
                    return size
                } else if (previousSize != null) {
                    return previousSize
                }
            } else if (i == sizes.size - 1) {
                // If we get here then we're larger than the last bucket
                if (imageWidth < sizeWidth * 2) {
                    return size
                }
            }

{'fraction_non_alphanumeric': 0.08516886930983847, 'fraction_numerical': 0.005873715124816446, 'mean_word_length': 2.4373423044575273, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3523341', 'n_tokens_mistral': 1102, 'n_tokens_neox': 1067, 'n_words': 379}