@Override public String toString() { if (value != null) return value.toString(); if (entity != null && entity.getDbGeneratedId() != null) return entity.getDbGeneratedId().toString(); else return "?(" + uuid + ")"; } }
/** * @return real ID value or null if it is not assigned yet */ @Nullable public T get() { if (value != null) return value; if (entity == null) return null; return entity.getDbGeneratedId(); }
/** * @return real ID value * @throws IllegalStateException if it is not assigned yet */ public T getNN() { if (value != null) return value; if (entity == null) throw new IllegalStateException("Cannot get primary key value: entity is null"); if (entity.getDbGeneratedId() == null) throw new IllegalStateException("Cannot get primary key value: ID is null in entity " + entity); return entity.getDbGeneratedId(); }
@Override public boolean equals(Object other) { if (this == other) return true; if (other == null || getClass() != other.getClass()) return false; IdProxy that = (IdProxy) other; if (value != null) { if (that.value != null) return value.equals(that.value); if (that.entity != null && that.entity.getDbGeneratedId() != null) return value.equals(that.entity.getDbGeneratedId()); } if (entity != null && entity.getDbGeneratedId() != null) { if (that.entity != null && that.entity.getDbGeneratedId() != null) return entity.getDbGeneratedId().equals(that.entity.getDbGeneratedId()); if (that.value != null) return entity.getDbGeneratedId().equals(that.value); } return Objects.equals(uuid, that.uuid); }
/** * Returns a copy of this IdProxy cleaned from a reference to entity. * @param shared if true, a shared instance of the copy will be returned to avoid creating new object. * DO NOT use shared copies when assigning the same ID to another entity! */ public IdProxy<T> copy(boolean shared) { if (copy == null || !shared || !Objects.equals(value, copy.value) || !Objects.equals(uuid, copy.uuid) || (copy.value == null && entity != null && entity.getDbGeneratedId() != null)) { copy = new IdProxy<>(); if (value != null) copy.value = value; else if (entity != null && entity.getDbGeneratedId() != null) copy.value = entity.getDbGeneratedId(); copy.uuid = uuid; copy.hashCode = hashCode; } return copy; }