Skip to content

Commit 936ed4c

Browse files
committed
Add more documentation
1 parent 410c403 commit 936ed4c

File tree

3 files changed

+65
-2
lines changed

3 files changed

+65
-2
lines changed

core/src/main/java/com/github/fernthedev/fernapi/universal/data/database/RowData.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,21 @@ public abstract class RowData {
3030
private final Map<Field, ColumnData> cachedData = new LinkedHashMap<>();
3131
private final Map<String, ColumnData> cachedDataStr = new LinkedHashMap<>();
3232

33+
/**
34+
* Use to instantiate Row Data with empty data.
35+
*
36+
* It is recommended to call {@link #initiateRowData()} after your values are instantiated.
37+
*/
3338
public RowData() {
3439
validateKeys(getClass());
3540
instance = this;
3641
}
3742

3843
/**
39-
* Must set initiated to true to avoid problems when overriden.
44+
* Must set initiated to true to avoid problems when overridden.
45+
*
46+
* Called when retrieving the data from the SQL table.
47+
*
4048
* @param rowData
4149
*/
4250
@OverridingMethodsMustInvokeSuper
@@ -61,6 +69,12 @@ protected void initiateRowData(DbRow rowData) {
6169
}
6270
}
6371

72+
/**
73+
* Must set initiated to true to avoid problems when overridden.
74+
*
75+
* Called when setting the data of the class manually.
76+
*
77+
*/
6478
protected void initiateRowData() {
6579
if (initiated)
6680
throw new IllegalStateException("Already initiated row data and is immutable.");
@@ -80,6 +94,11 @@ protected void initiateRowData() {
8094
}
8195
}
8296

97+
/**
98+
* Validates that the row data fields are properly configured.
99+
* @param rowData
100+
* @param <T>
101+
*/
83102
public static <T> void validateKeys(Class<T> rowData) {
84103

85104
boolean anyColumns = Arrays.stream(rowData.getDeclaredFields()).anyMatch(field -> field.isAnnotationPresent(Column.class));
@@ -121,13 +140,25 @@ private void addField(Field field, ColumnData columnData, Object value) {
121140
}
122141

123142

143+
/**
144+
* Retrieves the column data
145+
*
146+
* @param sqlName The name of the column in the SQL table
147+
* @return The column data
148+
*/
124149
public ColumnData getColumn(String sqlName) {
125150
if (!initiated)
126151
initiateRowData();
127152

128153
return cachedDataStr.get(sqlName);
129154
}
130155

156+
/**
157+
* Retrieves the column data
158+
*
159+
* @param field The field referencing the SQL data.
160+
* @return The column data
161+
*/
131162
public ColumnData getColumn(Field field) {
132163
if (!initiated)
133164
initiateRowData();

core/src/main/java/com/github/fernthedev/fernapi/universal/data/database/RowDataTemplate.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
import java.util.LinkedHashMap;
1010
import java.util.Map;
1111

12+
/**
13+
* Creates a basic and empty version of the
14+
* RowData class to reference when creating
15+
* or parsing a row from a table.
16+
*
17+
* @param <T>
18+
*/
1219
public class RowDataTemplate<T extends RowData> {
1320

1421
private final Map<Field, ColumnData> cachedData = new LinkedHashMap<>();
@@ -49,6 +56,11 @@ private void addField(Field field, ColumnData columnData) {
4956
cachedDataStr.put(field.getAnnotation(Column.class).value(), columnData);
5057
}
5158

59+
/**
60+
* The SQL column name of the primary key
61+
*
62+
* @return
63+
*/
5264
public String getPrimaryKeySQL() {
5365
return primaryKeyField.getAnnotation(Column.class).value();
5466
}

core/src/main/java/com/github/fernthedev/fernapi/universal/data/database/TableInfo.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ public class TableInfo<T extends RowData> {
2828
@Getter
2929
private final RowDataTemplate<T> rowDataTemplate;
3030

31+
/**
32+
*
33+
* @param tableName The name of the table
34+
* @param rowClass The class of the Row Data
35+
* @param rowSupplier The supplier should return a new and empty instance of the Row Class.
36+
* This instance should not call {@link RowData#initiateRowData()}
37+
* when constructed. Reflection will be used to
38+
* set the field values accordingly.
39+
*/
3140
public TableInfo(String tableName, @NonNull Class<T> rowClass, Supplier<@NonNull T> rowSupplier) {
3241
this.tableName = tableName;
3342
this.rowClass = rowClass;
@@ -51,6 +60,12 @@ public CompletableFuture<TableInfo<T>> loadFromDB(DatabaseListener databaseListe
5160
});
5261
}
5362

63+
/**
64+
* Internal usage. Sets the rows
65+
*
66+
* @param dbRowList
67+
*/
68+
@Deprecated
5469
public void setRows(List<DbRow> dbRowList) {
5570
rowDataList.clear();
5671
dbRowList.forEach(this::addRow);
@@ -69,7 +84,12 @@ private void addRow(DbRow dbRow) {
6984
}
7085

7186

72-
87+
/**
88+
* Retrieves the row based on the primary key.
89+
*
90+
* @param key The value of the key
91+
* @return The row
92+
*/
7393
public T getRow(String key) {
7494
return rowDataList.get(key);
7595
}

0 commit comments

Comments
 (0)