All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.jfachwert.pruefung.AccessValidator.kt Maven / Gradle / Ivy

There is a newer version: 6.0.1
Show newest version
/*
 * Copyright (c) 2017-2020 by Oliver Boehm
 *
 * 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 orimplied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * (c)reated 06.10.17 by oliver ([email protected])
 */
package de.jfachwert.pruefung

import de.jfachwert.pruefung.exception.InvalidValueException
import org.apache.commons.lang3.Range

/**
 * Der AccessValidator ueberprueft den Zugriff auf Arrays, ob er gueltig ist
 * oder mit einem ungueltigen Index erfolgte. Ansonsten wird eine
 * [ValidationException] geworfen.
 *
 * @author oliver ([email protected])
 * @since 0.5
 */
object AccessValidator {

    /**
     * Liefert das n-te Element des uebergebenen Arrays zurueck, falls ein
     * korrekter Index uebergaben wird
     *
     * @param    Typ-Parameter
     * @param array Array, auf das zugegriffen wird
     * @param n     Array-Index, beginnend bei 0
     * @return n -te Element des Arrays
     */
    @JvmStatic
    fun  access(array: Array, n: Int): T {
        val max = array.size - 1
        if (n < 0 || n > max) {
            throw InvalidValueException(n, "n", Range.of(0, max))
        }
        return array[n]
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy