I have an in memory database class that can be used to store any java objects. When the…
- User
I have an in memory database class that can be used to store any java objects. When the class is created, it is told what classes are stored in the db. Like so:
public class RelatableAutoGraph implements AutoGraph {
private final Map<Class> tables; private final Map<SelectionKey, AutographTable<?>> selectorsToTables;
public RelatableAutoGraph(Class entityType : entityTypes) { tables.put(entityType, new AutographTable<>(this, entityType)); } }
objects can be added with an addAll(Collection<?>) method or add(Object o) method. These methods look like:
@Override public void addAll(Collection table = null; Class<?> prev = null; for (Object o : objects) { if (prev == null || !prev.equals(o.getClass())) { table = findTableFor(o.getClass()); prev = o.getClass(); } table.addObject(o); } }
public void add(Object o) { AutographTable<?> table = findTableFor(o.getClass()); if (table != null) { table.addObject(o); } }
private AutographTable clzz) { AutographTable intface : clzz.getInterfaces()) { t = findTableFor(intface); if (t != null) { tables.put(clzz, t); return t; } } t = findTableFor(clzz.getSuperclass()); if (t != null) { tables.put(clzz, t); return t; } } return null; }
As you can see, the class has to do some work to find the right table object to hold each incoming object. Now, in addition, incoming objects might have references to other objects we want stored in the database, so consider this db:
AutoGraph db = new RelatableAutoGraph(CompanyEntity.class, EmployeeEntity.class, Address.class, City.class);
will hold employee, company, address, and city objects. These classes look like:
public record EmployeeEntity(ID id, String name, Address addr, String industry, String ceoName) public record Address(ID<?> id, int number, String street, int zip, City city, String state) public record City(String cityName, String description, int population)
So, when someone adds a new Employee, that employee references a CompanyEntity, which references an Address, which references a City. Upon adding the employee to the db, the db should hold all those objects in the proper tables, which means we have to inspect the incoming object for getters and recordComponent Accessors for possible objects to be added, and then do that recursively.
Also, since we want it to be performant, we should cache the information necessary for every type of object so we don't have to do the reflection again and again.
And lastly, if we find objects that are assignable from classes we're storing, we want to store them too, so we can't just ask if the class of an object equals a class we're interested in. Please make methods that will find all sub-objects of interest to be added to the db.
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.