mirror of
https://github.com/collinsmith/riiablo.git
synced 2025-07-07 00:39:13 +07:00
Implemented Table interface as abstract class
This commit is contained in:
@ -6,6 +6,8 @@ import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import com.riiablo.table.Table;
|
||||
|
||||
/**
|
||||
* Indicates that the specified type is a record schema.
|
||||
*/
|
||||
@ -23,4 +25,19 @@ public @interface Schema {
|
||||
* will override any set {@link PrimaryKey primary key}.
|
||||
*/
|
||||
boolean indexed() default false;
|
||||
|
||||
/**
|
||||
* Number of expected records of this type.
|
||||
*
|
||||
* @see #loadFactor()
|
||||
* @see <a href="https://planetmath.org/goodhashtableprimes">good hash table primes</a>
|
||||
*/
|
||||
int initialCapacity() default Table.DEFAULT_INITIAL_CAPACITY;
|
||||
|
||||
/**
|
||||
* Percentage factor when the table should be resized.
|
||||
*
|
||||
* @see #initialCapacity()
|
||||
*/
|
||||
float loadFactor() default Table.DEFAULT_LOAD_FACTOR;
|
||||
}
|
||||
|
@ -1,2 +1,3 @@
|
||||
dependencies {
|
||||
api "com.badlogicgames.gdx:gdx:$gdxVersion"
|
||||
}
|
||||
|
@ -1,9 +1,79 @@
|
||||
package com.riiablo.table;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
import com.badlogic.gdx.utils.Array;
|
||||
import com.badlogic.gdx.utils.IntMap;
|
||||
import com.badlogic.gdx.utils.ObjectIntMap;
|
||||
|
||||
/**
|
||||
* Stores a set of records.
|
||||
* Stores a table of records.
|
||||
*
|
||||
* @param <R> record type
|
||||
*/
|
||||
public interface Table<R> extends Iterable<R> {
|
||||
public abstract class Table<R> implements Iterable<R> {
|
||||
public static final int DEFAULT_INITIAL_CAPACITY = 53;
|
||||
public static final float DEFAULT_LOAD_FACTOR = 0.8f;
|
||||
|
||||
protected final Class<R> recordClass;
|
||||
protected ObjectIntMap<String> lookup;
|
||||
protected IntMap<R> records;
|
||||
protected Array<R> ordered;
|
||||
|
||||
protected Table(Class<R> recordClass) {
|
||||
this(recordClass, DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
|
||||
}
|
||||
|
||||
protected Table(Class<R> recordClass, int initialCapacity) {
|
||||
this(recordClass, initialCapacity, DEFAULT_LOAD_FACTOR);
|
||||
}
|
||||
|
||||
protected Table(Class<R> recordClass, int initialCapacity, float loadFactor) {
|
||||
this.recordClass = recordClass;
|
||||
records = new IntMap<>(initialCapacity, loadFactor);
|
||||
ordered = new Array<>(true, (int) (initialCapacity * loadFactor), recordClass);
|
||||
lookup = null;
|
||||
}
|
||||
|
||||
protected abstract R newRecord();
|
||||
|
||||
public Class<R> recordClass() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<R> iterator() {
|
||||
return ordered.iterator();
|
||||
}
|
||||
|
||||
protected void initialize() {}
|
||||
|
||||
protected void put(int id, R record) {
|
||||
records.put(id, record);
|
||||
ordered.add(record);
|
||||
}
|
||||
|
||||
protected int offset() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
protected String primaryKey() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public R get(int id) {
|
||||
return records.get(id);
|
||||
}
|
||||
|
||||
public int index(String id) {
|
||||
return lookup == null ? -1 : lookup.get(id, -1);
|
||||
}
|
||||
|
||||
public R get(String id) {
|
||||
return lookup == null ? null : get(lookup.get(id, -1));
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return records.size;
|
||||
}
|
||||
}
|
||||
|
@ -2,5 +2,13 @@ package com.riiablo.table.schema;
|
||||
|
||||
import com.riiablo.table.Table;
|
||||
|
||||
public abstract class MonStatsTableImpl implements Table<MonStats> {
|
||||
public abstract class MonStatsTableImpl extends Table<MonStats> {
|
||||
public MonStatsTableImpl() {
|
||||
super(MonStats.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MonStats newRecord() {
|
||||
return new MonStats();
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user