2
2
3
3
import co .aikar .idb .DbRow ;
4
4
import com .google .errorprone .annotations .Immutable ;
5
+ import com .google .errorprone .annotations .OverridingMethodsMustInvokeSuper ;
5
6
import lombok .ToString ;
7
+ import org .panteleyev .mysqlapi .MySqlProxy ;
6
8
import org .panteleyev .mysqlapi .annotations .Column ;
7
9
import org .panteleyev .mysqlapi .annotations .PrimaryKey ;
8
10
9
11
import java .lang .reflect .Field ;
10
12
import java .util .Arrays ;
11
- import java .util .HashMap ;
13
+ import java .util .LinkedHashMap ;
12
14
import java .util .Map ;
13
15
14
16
/**
19
21
@ Immutable
20
22
public abstract class RowData {
21
23
22
- private final Map <Field , ColumnData > cachedData = new HashMap <>();
23
- private final Map <String , ColumnData > cachedDataStr = new HashMap <>();
24
+ private static final MySqlProxy mySqlProxy = new MySqlProxy ();
24
25
25
- public RowData (DbRow rowData ) {
26
+ private boolean initiated = false ;
27
+
28
+ protected final RowData instance ;
29
+
30
+ private final Map <Field , ColumnData > cachedData = new LinkedHashMap <>();
31
+ private final Map <String , ColumnData > cachedDataStr = new LinkedHashMap <>();
32
+
33
+ public RowData () {
26
34
validateKeys (getClass ());
35
+ instance = this ;
36
+ }
27
37
28
- Arrays .stream (getClass ().getDeclaredFields ())
29
- .filter (field -> field .isAnnotationPresent (Column .class ))
30
- .forEach (field -> {
31
- ColumnData columnData ;
32
- try {
33
- Column column = field .getAnnotation (Column .class );
34
- columnData = ColumnData .fromField (field , rowData .get (column .value ()).toString ());
35
- addField (field , columnData );
36
- } catch (IllegalAccessException e ) {
37
- e .printStackTrace ();
38
+ /**
39
+ * Must set initiated to true to avoid problems when overriden.
40
+ * @param rowData
41
+ */
42
+ @ OverridingMethodsMustInvokeSuper
43
+ protected void initiateRowData (DbRow rowData ) {
44
+ if (initiated )
45
+ throw new IllegalStateException ("Already initiated row data and is immutable." );
46
+
47
+ initiated = true ;
48
+
49
+
50
+ for (Field field : getClass ().getDeclaredFields ()) {
51
+ if (field .isAnnotationPresent (Column .class )) {
52
+ ColumnData columnData ;
53
+ try {
54
+ Column column = field .getAnnotation (Column .class );
55
+ columnData = ColumnData .fromField (field , rowData .get (column .value ()).toString ());
56
+ addField (field , columnData , rowData .get (columnData .getColumnName ()));
57
+ } catch (IllegalAccessException e ) {
58
+ e .printStackTrace ();
59
+ }
38
60
}
39
- });
61
+ }
62
+ }
63
+
64
+ protected void initiateRowData () {
65
+ if (initiated )
66
+ throw new IllegalStateException ("Already initiated row data and is immutable." );
67
+
68
+ initiated = true ;
69
+ for (Field field : getClass ().getDeclaredFields ()) {
70
+ if (field .isAnnotationPresent (Column .class )) {
71
+ ColumnData columnData ;
72
+ try {
73
+ field .setAccessible (true );
74
+ columnData = ColumnData .fromField (field , field .get (instance ).toString ());
75
+ addField (field , columnData );
76
+ } catch (IllegalAccessException e ) {
77
+ e .printStackTrace ();
78
+ }
79
+ }
80
+ }
40
81
}
41
82
42
83
public static <T > void validateKeys (Class <T > rowData ) {
43
84
44
- boolean anyColumns = Arrays .stream (rowData .getFields ()).anyMatch (field -> field .isAnnotationPresent (Column .class ));
85
+ boolean anyColumns = Arrays .stream (rowData .getDeclaredFields ()).anyMatch (field -> field .isAnnotationPresent (Column .class ));
45
86
46
87
if (!anyColumns )
47
- throw new IllegalStateException ("No fields have @" + Column .class .toString () + " annotation" );
88
+ throw new IllegalStateException ("No fields have @" + Column .class .getName () + " annotation" );
48
89
49
90
50
- boolean primaryKey = Arrays .stream (rowData .getFields ()).anyMatch (field -> field .isAnnotationPresent (PrimaryKey .class ));
91
+ boolean primaryKey = Arrays .stream (rowData .getDeclaredFields ()).anyMatch (field -> field .isAnnotationPresent (PrimaryKey .class ));
51
92
52
93
if (!primaryKey )
53
- throw new IllegalStateException ("No fields have @" + PrimaryKey .class .toString () + " annotation" );
94
+ throw new IllegalStateException ("No fields have @" + PrimaryKey .class .getName () + " annotation" );
54
95
55
- boolean multiplePrimaryKeys = Arrays .stream (rowData .getFields ()).filter (field -> field .isAnnotationPresent (PrimaryKey .class )).count () > 1 ;
96
+ boolean multiplePrimaryKeys = Arrays .stream (rowData .getDeclaredFields ()).filter (field -> field .isAnnotationPresent (PrimaryKey .class )).count () > 1 ;
56
97
57
98
if (multiplePrimaryKeys )
58
- throw new IllegalStateException ("Too many fields have @" + PrimaryKey .class .toString () + " annotation" );
99
+ throw new IllegalStateException ("Too many fields have @" + PrimaryKey .class .getName () + " annotation" );
59
100
60
101
}
61
102
@@ -64,20 +105,55 @@ private void addField(Field field, ColumnData columnData) {
64
105
cachedDataStr .put (field .getAnnotation (Column .class ).value (), columnData );
65
106
}
66
107
108
+ private void addField (Field field , ColumnData columnData , Object value ) {
109
+ cachedData .put (field , columnData );
110
+ cachedDataStr .put (field .getAnnotation (Column .class ).value (), columnData );
111
+
112
+ try {
113
+ Object val = mySqlProxy .getFieldValue (field .getType (), value );
114
+
115
+ field .setAccessible (true );
116
+
117
+ field .set (this , val );
118
+ } catch (IllegalAccessException e ) {
119
+ e .printStackTrace ();
120
+ }
121
+ }
122
+
67
123
68
124
public ColumnData getColumn (String sqlName ) {
125
+ if (!initiated )
126
+ initiateRowData ();
127
+
69
128
return cachedDataStr .get (sqlName );
70
129
}
71
130
72
131
public ColumnData getColumn (Field field ) {
132
+ if (!initiated )
133
+ initiateRowData ();
134
+
73
135
return cachedData .get (field );
74
136
}
75
137
76
138
/**
77
139
* Immutable
78
140
* @return
79
141
*/
80
- public Map <String , ColumnData > getDataCopy () {
81
- return new HashMap <>(cachedDataStr );
142
+ public Map <String , ColumnData > getDataStrCopy () {
143
+ if (!initiated )
144
+ initiateRowData ();
145
+
146
+ return new LinkedHashMap <>(cachedDataStr );
147
+ }
148
+
149
+ /**
150
+ * Immutable
151
+ * @return
152
+ */
153
+ public Map <Field , ColumnData > getDataCopy () {
154
+ if (!initiated )
155
+ initiateRowData ();
156
+
157
+ return new LinkedHashMap <>(cachedData );
82
158
}
83
159
}
0 commit comments