Box(BoxStore store, Class<T> entityClass) { this.store = store; this.entityClass = entityClass; idGetter = store.getEntityInfo(entityClass).getIdGetter(); }
/** * Sorts the list by the "natural" ObjectBox order for to-many list (by entity ID). * This will be the order when you get the entities fresh (e.g. initially or after calling {@link #reset()}). * Note that non persisted entities (ID is zero) will be put to the end as they are still to get an ID. */ public void sortById() { ensureEntities(); Collections.sort(entities, new Comparator<TARGET>() { IdGetter<TARGET> idGetter = relationInfo.targetInfo.getIdGetter(); @Override public int compare(TARGET o1, TARGET o2) { long id1 = idGetter.getId(o1); long id2 = idGetter.getId(o2); if (id1 == 0) id1 = Long.MAX_VALUE; if (id2 == 0) id2 = Long.MAX_VALUE; long delta = id1 - id2; // because of long we cannot simply return delta if (delta < 0) return -1; else if (delta > 0) return 1; else return 0; } }); }
/** Removes an object by its entity ID. */ public synchronized TARGET removeById(long id) { ensureEntities(); int size = entities.size(); IdGetter<TARGET> idGetter = relationInfo.targetInfo.getIdGetter(); for (int i = 0; i < size; i++) { TARGET candidate = entities.get(i); if (idGetter.getId(candidate) == id) { TARGET removed = remove(i); if (removed != candidate) { throw new IllegalStateException("Mismatch: " + removed + " vs. " + candidate); } return candidate; } } return null; }
/** Gets an object by its entity ID. */ @Beta public TARGET getById(long id) { ensureEntities(); Object[] objects = entities.toArray(); IdGetter<TARGET> idGetter = relationInfo.targetInfo.getIdGetter(); for (Object target : objects) { TARGET candidate = (TARGET) target; if (idGetter.getId(candidate) == id) { return candidate; } } return null; }
/** Gets the index of the object with the given entity ID. */ @Beta public int indexOfId(long id) { ensureEntities(); Object[] objects = entities.toArray(); IdGetter<TARGET> idGetter = relationInfo.targetInfo.getIdGetter(); int index = 0; for (Object target : objects) { TARGET candidate = (TARGET) target; if (idGetter.getId(candidate) == id) { return index; } index++; } return -1; }
} else { long entityId = relationInfo.sourceInfo.getIdGetter().getId(entity); if (entityId == 0) { throw new IllegalStateException("Source entity has no ID (should have been put before)"); IdGetter<TARGET> idGetter = relationInfo.targetInfo.getIdGetter(); Map<TARGET, Boolean> setAdded = this.entitiesAdded; Map<TARGET, Boolean> setRemoved = this.entitiesRemoved;
IdGetter<TARGET> targetIdGetter = relationInfo.targetInfo.getIdGetter(); synchronized (this) { if (isStandaloneRelation) { long entityId = relationInfo.sourceInfo.getIdGetter().getId(entity); if (entityId == 0) { throw new IllegalStateException("Source entity has no ID (should have been put before)");
long id = relationInfo.sourceInfo.getIdGetter().getId(entity); if (id == 0) { throw new IllegalStateException(
/** * Sets the relation ID in the enclosed entity to the ID of the given target entity. * If the target entity was not put in the DB yet (its ID is 0), it will be put before to get its ID. */ // TODO provide a overload with a ToMany parameter, which also gets updated public void setTarget(@Nullable final TARGET target) { if (target != null) { long targetId = relationInfo.targetInfo.getIdGetter().getId(target); checkIdOfTargetForPut = targetId == 0; setTargetId(targetId); setResolvedTarget(target, targetId); } else { setTargetId(0); clearResolved(); } }
private void ensureEntities() { if (entities == null) { long id = relationInfo.sourceInfo.getIdGetter().getId(entity); if (id == 0) {