1 /*
2  * Copyright (C) 2017 The Android Open Source Project
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 
17 package androidx.room.processor
18 
19 import androidx.room.OnConflictStrategy
20 import com.google.auto.common.AnnotationMirrors
21 import javax.lang.model.element.AnnotationMirror
22 
23 /**
24  * Processes on conflict fields in annotations
25  */
26 object OnConflictProcessor {
27     val INVALID_ON_CONFLICT = -1
28 
29     @OnConflictStrategy
extractFromnull30     fun extractFrom(annotation: AnnotationMirror?, fieldName: String = "onConflict"): Int {
31         return if (annotation == null) {
32             INVALID_ON_CONFLICT
33         } else {
34             try {
35                 val onConflictValue = AnnotationMirrors
36                         .getAnnotationValue(annotation, fieldName)
37                         .value
38                 onConflictValue.toString().toInt()
39             } catch (ex: NumberFormatException) {
40                 INVALID_ON_CONFLICT
41             }
42         }
43     }
44 
onConflictTextnull45     fun onConflictText(@OnConflictStrategy onConflict: Int): String {
46         return when (onConflict) {
47             OnConflictStrategy.REPLACE -> "REPLACE"
48             OnConflictStrategy.ABORT -> "ABORT"
49             OnConflictStrategy.FAIL -> "FAIL"
50             OnConflictStrategy.IGNORE -> "IGNORE"
51             OnConflictStrategy.ROLLBACK -> "ROLLBACK"
52             else -> "BAD_CONFLICT_CONSTRAINT"
53         }
54     }
55 }
56