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

com.zepben.evolve.services.common.extensions.BaseServiceExtensions.kt Maven / Gradle / Ivy

There is a newer version: 0.24.0rc1
Show newest version
/*
 * Copyright 2020 Zeppelin Bend Pty Ltd
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package com.zepben.evolve.services.common.extensions

import com.zepben.evolve.cim.iec61970.base.core.IdentifiedObject
import com.zepben.evolve.cim.iec61970.base.core.NameType
import com.zepben.evolve.database.sqlite.common.MRIDLookupException
import com.zepben.evolve.database.sqlite.common.NameTypeLookupException
import com.zepben.evolve.services.common.BaseService


/**
 * Get an object associated with this service and throw if it is not found.
 *
 * @param T The type of object to look for. If this is a base class it will search all subclasses.
 * @param mRID The mRID of the object to find.
 * @param typeNameAndMRID The description of the item requesting the lookup.
 *
 * @return The object identified by [mRID] as [T].
 * @throws MRIDLookupException if no objects of type [T] are found with the specified [mRID]
 */
@Throws(MRIDLookupException::class)
inline fun  BaseService.getOrThrow(mRID: String?, typeNameAndMRID: String): T {
    return get(mRID)
        ?: throw MRIDLookupException("Failed to find ${T::class.simpleName} with mRID $mRID for $typeNameAndMRID")
}

/**
 * Get a name type associated with this service and throw if it is not found.
 *
 * @param typeName The name of the [NameType] to find.
 *
 * @return The [NameType] identified by [typeName].
 * @throws NameTypeLookupException if no [NameType] is found with the specified [typeName]
 */
@Throws(NameTypeLookupException::class)
fun BaseService.getNameTypeOrThrow(typeName: String): NameType {
    return getNameType(typeName)
        ?: throw NameTypeLookupException("Failed to find ${NameType::class.simpleName} with name $typeName")
}

/**
 * Optionally get an object associated with this service and throw if it is not found.
 *
 * @param T The type of object to look for. If this is a base class it will search all subclasses.
 * @param mRID The mRID of the object to find.
 * @param typeNameAndMRID The description of the item requesting the lookup.
 *
 * @return The object identified by [mRID] as [T] if it was found, or null if no [mRID] was supplied.
 * @throws MRIDLookupException if no objects of type [T] are found with the specified [mRID]
 */
@Throws(MRIDLookupException::class)
inline fun  BaseService.ensureGet(mRID: String?, typeNameAndMRID: String): T? {
    return if (mRID.isNullOrBlank())
        null
    else
        getOrThrow(mRID, typeNameAndMRID)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy