YDLPreconditions.kt 6.85 KB
Newer Older
1
package com.ydl.ydlcommon.utils
konghaorui committed
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

/**
 * Created by haorui on 2019-08-22 .
 * Des: 前置条件判断工具类
 */
class YDLPreconditions private constructor() {

    init {
        throw IllegalStateException("you can't instantiate me!")
    }

    companion object {

        fun checkArgument(expression: Boolean) {
            if (!expression) {
                throw IllegalArgumentException()
            }
        }

        fun checkArgument(expression: Boolean, errorMessage: Any?) {
            if (!expression) {
                throw IllegalArgumentException(errorMessage.toString())
            }
        }

        fun checkArgument(expression: Boolean, errorMessageTemplate: String, vararg errorMessageArgs: Any) {
            if (!expression) {
29 30 31 32 33 34
                throw IllegalArgumentException(
                    format(
                        errorMessageTemplate,
                        *errorMessageArgs
                    )
                )
konghaorui committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
            }
        }

        fun checkState(expression: Boolean) {
            if (!expression) {
                throw IllegalStateException()
            }
        }

        fun checkState(expression: Boolean, errorMessage: Any?) {
            if (!expression) {
                throw IllegalStateException(errorMessage.toString())
            }
        }

        fun checkState(expression: Boolean, errorMessageTemplate: String, vararg errorMessageArgs: Any) {
            if (!expression) {
52 53 54 55 56 57
                throw IllegalStateException(
                    format(
                        errorMessageTemplate,
                        *errorMessageArgs
                    )
                )
konghaorui committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
            }
        }

        fun <T> checkNotNull(reference: T?): T {
            return if (reference == null) {
                throw NullPointerException()
            } else {
                reference
            }
        }

        fun <T> checkNotNull(reference: T?, errorMessage: Any?): T {
            return reference ?: throw NullPointerException(errorMessage.toString())
        }

        fun <T> checkNotNull(reference: T?, errorMessageTemplate: String, vararg errorMessageArgs: Any): T {
74 75 76 77 78 79
            return reference ?: throw NullPointerException(
                format(
                    errorMessageTemplate,
                    *errorMessageArgs
                )
            )
konghaorui committed
80 81 82
        }

        fun checkEmpty(reference: String?, errorMessageTemplate: String, vararg errorMessageArgs: Any){
83 84 85 86 87 88
            reference?.isEmpty() ?: throw NullPointerException(
                format(
                    errorMessageTemplate,
                    *errorMessageArgs
                )
            )
konghaorui committed
89 90 91 92 93 94 95
        }

        @JvmOverloads
        fun checkElementIndex(index: Int, size: Int, desc: String = "index"): Int {
            return if (index >= 0 && index < size) {
                index
            } else {
96 97 98 99 100 101 102
                throw IndexOutOfBoundsException(
                    badElementIndex(
                        index,
                        size,
                        desc
                    )
                )
konghaorui committed
103 104 105 106 107
            }
        }

        private fun badElementIndex(index: Int, size: Int, desc: String): String {
            return if (index < 0) {
108 109 110 111
                format(
                    "%s (%s) must not be negative",
                    *arrayOf<Any>(desc, Integer.valueOf(index))
                )
konghaorui committed
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            } else if (size < 0) {
                throw IllegalArgumentException(StringBuilder(26).append("negative size: ").append(size).toString())
            } else {
                format(
                    "%s (%s) must be less than size (%s)",
                    *arrayOf<Any>(desc, Integer.valueOf(index), Integer.valueOf(size))
                )
            }
        }

        @JvmOverloads
        fun checkPositionIndex(index: Int, size: Int, desc: String = "index"): Int {
            return if (index >= 0 && index <= size) {
                index
            } else {
127 128 129 130 131 132 133
                throw IndexOutOfBoundsException(
                    badPositionIndex(
                        index,
                        size,
                        desc
                    )
                )
konghaorui committed
134 135 136 137 138
            }
        }

        private fun badPositionIndex(index: Int, size: Int, desc: String): String {
            return if (index < 0) {
139 140 141 142
                format(
                    "%s (%s) must not be negative",
                    *arrayOf<Any>(desc, Integer.valueOf(index))
                )
konghaorui committed
143 144 145 146 147 148 149 150 151 152 153 154
            } else if (size < 0) {
                throw IllegalArgumentException(StringBuilder(26).append("negative size: ").append(size).toString())
            } else {
                format(
                    "%s (%s) must not be greater than size (%s)",
                    *arrayOf<Any>(desc, Integer.valueOf(index), Integer.valueOf(size))
                )
            }
        }

        fun checkPositionIndexes(start: Int, end: Int, size: Int) {
            if (start < 0 || end < start || end > size) {
155 156 157 158 159 160 161
                throw IndexOutOfBoundsException(
                    badPositionIndexes(
                        start,
                        end,
                        size
                    )
                )
konghaorui committed
162 163 164 165 166 167 168
            }
        }

        private fun badPositionIndexes(start: Int, end: Int, size: Int): String {
            return if (start >= 0 && start <= size) if (end >= 0 && end <= size) format(
                "end index (%s) must not be less than start index (%s)",
                *arrayOf<Any>(Integer.valueOf(end), Integer.valueOf(start))
169 170 171 172 173 174 175 176 177
            ) else badPositionIndex(
                end,
                size,
                "end index"
            ) else badPositionIndex(
                start,
                size,
                "start index"
            )
konghaorui committed
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        }

        internal fun format(template: String, vararg args: Any): String {
            var template = template
            template = template.toString()
            val builder = StringBuilder(template.length + 16 * args.size)
            var templateStart = 0

            var i: Int
            var placeholderStart: Int
            i = 0
            while (i < args.size) {
                placeholderStart = template.indexOf("%s", templateStart)
                if (placeholderStart == -1) {
                    break
                }

                builder.append(template.substring(templateStart, placeholderStart))
                builder.append(args[i++])
                templateStart = placeholderStart + 2
            }

            builder.append(template.substring(templateStart))
            if (i < args.size) {
                builder.append(" [")
                builder.append(args[i++])

                while (i < args.size) {
                    builder.append(", ")
                    builder.append(args[i++])
                }

                builder.append(']')
            }

            return builder.toString()
        }
    }
}