|
| 1 | +/* |
| 2 | + * Copyright (C) 2021. Alexander Rogalskiy. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package utils |
| 17 | + |
| 18 | +import java.util.* |
| 19 | + |
| 20 | +object CommonUtils { |
| 21 | + init { |
| 22 | + // To hide annoying warning on Windows |
| 23 | + System.setProperty("idea.use.native.fs.for.win", "false") |
| 24 | + } |
| 25 | + |
| 26 | + /** A state machine for classifying qualified names. */ |
| 27 | + private enum class TyParseState(val isSingleUnit: Boolean) { |
| 28 | + /** The start state. */ |
| 29 | + START(false) { |
| 30 | + override fun next(n: JavaCaseFormat): TyParseState { |
| 31 | + return when (n) { |
| 32 | + JavaCaseFormat.UPPERCASE -> |
| 33 | + // if we see an UpperCamel later, assume this was a class |
| 34 | + // e.g. com.google.FOO.Bar |
| 35 | + AMBIGUOUS |
| 36 | + JavaCaseFormat.LOWER_CAMEL -> REJECT |
| 37 | + JavaCaseFormat.LOWERCASE -> |
| 38 | + // could be a package |
| 39 | + START |
| 40 | + JavaCaseFormat.UPPER_CAMEL -> TYPE |
| 41 | + } |
| 42 | + } |
| 43 | + }, |
| 44 | + |
| 45 | + /** The current prefix is a type. */ |
| 46 | + TYPE(true) { |
| 47 | + override fun next(n: JavaCaseFormat): TyParseState { |
| 48 | + return when (n) { |
| 49 | + JavaCaseFormat.UPPERCASE, JavaCaseFormat.LOWER_CAMEL, JavaCaseFormat.LOWERCASE -> |
| 50 | + FIRST_STATIC_MEMBER |
| 51 | + JavaCaseFormat.UPPER_CAMEL -> TYPE |
| 52 | + } |
| 53 | + } |
| 54 | + }, |
| 55 | + |
| 56 | + /** The current prefix is a type, followed by a single static member access. */ |
| 57 | + FIRST_STATIC_MEMBER(true) { |
| 58 | + override fun next(n: JavaCaseFormat): TyParseState { |
| 59 | + return REJECT |
| 60 | + } |
| 61 | + }, |
| 62 | + |
| 63 | + /** Anything not represented by one of the other states. */ |
| 64 | + REJECT(false) { |
| 65 | + override fun next(n: JavaCaseFormat): TyParseState { |
| 66 | + return REJECT |
| 67 | + } |
| 68 | + }, |
| 69 | + |
| 70 | + /** An ambiguous type prefix. */ |
| 71 | + AMBIGUOUS(false) { |
| 72 | + override fun next(n: JavaCaseFormat): TyParseState { |
| 73 | + return when (n) { |
| 74 | + JavaCaseFormat.UPPERCASE -> AMBIGUOUS |
| 75 | + JavaCaseFormat.LOWER_CAMEL, JavaCaseFormat.LOWERCASE -> REJECT |
| 76 | + JavaCaseFormat.UPPER_CAMEL -> TYPE |
| 77 | + } |
| 78 | + } |
| 79 | + }; |
| 80 | + |
| 81 | + /** Transition function. */ |
| 82 | + abstract fun next(n: JavaCaseFormat): TyParseState |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Returns the end index (inclusive) of the longest prefix that matches the naming conventions of |
| 87 | + * a type or static field access, or -1 if no such prefix was found. |
| 88 | + * |
| 89 | + * Examples: |
| 90 | + * |
| 91 | + * * ClassName |
| 92 | + * * ClassName.staticMemberName |
| 93 | + * * com.google.ClassName.InnerClass.staticMemberName |
| 94 | + */ |
| 95 | + internal fun typePrefixLength(nameParts: List<String>): Optional<Int> { |
| 96 | + var state = TyParseState.START |
| 97 | + var typeLength = Optional.empty<Int>() |
| 98 | + |
| 99 | + for (i in nameParts.indices) { |
| 100 | + state = state.next(JavaCaseFormat.from(nameParts[i])) |
| 101 | + if (state === TyParseState.REJECT) { |
| 102 | + break |
| 103 | + } |
| 104 | + if (state.isSingleUnit) { |
| 105 | + typeLength = Optional.of(i) |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return typeLength |
| 110 | + } |
| 111 | + |
| 112 | + /** Case formats used in Java identifiers. */ |
| 113 | + enum class JavaCaseFormat { |
| 114 | + UPPERCASE, |
| 115 | + LOWERCASE, |
| 116 | + UPPER_CAMEL, |
| 117 | + LOWER_CAMEL; |
| 118 | + |
| 119 | + companion object { |
| 120 | + /** Classifies an identifier's case format. */ |
| 121 | + internal fun from(name: String): JavaCaseFormat { |
| 122 | + var firstUppercase = false |
| 123 | + var hasUppercase = false |
| 124 | + var hasLowercase = false |
| 125 | + var first = true |
| 126 | + |
| 127 | + for (element in name) { |
| 128 | + if (!Character.isAlphabetic(element.toInt())) { |
| 129 | + continue |
| 130 | + } |
| 131 | + if (first) { |
| 132 | + firstUppercase = Character.isUpperCase(element) |
| 133 | + first = false |
| 134 | + } |
| 135 | + hasUppercase = hasUppercase or Character.isUpperCase(element) |
| 136 | + hasLowercase = hasLowercase or Character.isLowerCase(element) |
| 137 | + } |
| 138 | + return if (firstUppercase) { |
| 139 | + if (hasLowercase) UPPER_CAMEL else UPPERCASE |
| 140 | + } else { |
| 141 | + if (hasUppercase) LOWER_CAMEL else LOWERCASE |
| 142 | + } |
| 143 | + } |
| 144 | + } |
| 145 | + } |
| 146 | +} |
0 commit comments