public final void initialize(@NonNull final Realm realm) {
    Contracts.requireNonNull(realm, "realm == null");

    synchronized (_lock$maximumKeys) {
      if (_initialized) {
        throw new IllegalStateException(RealmIdGenerator.class + " is already initialized.");
      }

      final val maximumKeys = getMaximumKeys();

      final val configuration = realm.getConfiguration();
      final val realmSchema = realm.getSchema();
      for (final val modelClass : configuration.getRealmObjectClasses()) {
        final val objectSchema = realmSchema.get(modelClass.getSimpleName());
        if (objectSchema.hasPrimaryKey()) {
          final val primaryKeyField = objectSchema.getPrimaryKey();
          final val primaryKey = realm.where(modelClass).max(primaryKeyField);

          final long primaryKeyValue;
          if (primaryKey != null) {
            primaryKeyValue = primaryKey.longValue();
          } else {
            primaryKeyValue = 0L;
          }

          maximumKeys.put(modelClass, new AtomicLong(primaryKeyValue));
        }
      }

      _initialized = true;
    }
  }
 /**
  * Sets a reference to another object on the given field.
  *
  * @param fieldName the field name.
  * @param value the object to link to.
  * @throws IllegalArgumentException if field name doesn't exists, it doesn't link to other Realm
  *     objects, or the type of DynamicRealmObject doesn't match.
  */
 public void setObject(String fieldName, DynamicRealmObject value) {
   long columnIndex = row.getColumnIndex(fieldName);
   if (value == null) {
     row.nullifyLink(columnIndex);
   } else {
     if (value.realm == null || value.row == null) {
       throw new IllegalArgumentException(
           "Cannot link to objects that are not part of the Realm.");
     }
     if (!realm.getConfiguration().equals(value.realm.getConfiguration())) {
       throw new IllegalArgumentException("Cannot add an object from another Realm");
     }
     Table table = row.getTable();
     Table inputTable = value.row.getTable();
     if (!table.hasSameSchema(inputTable)) {
       throw new IllegalArgumentException(
           String.format(
               "Type of object is wrong. Was %s, expected %s",
               inputTable.getName(), table.getName()));
     }
     row.setLink(columnIndex, value.row.getIndex());
   }
 }