All Downloads are FREE. Search and download functionalities are using the official Maven repository.

co.easimart.PointerOrLocalIdEncoder Maven / Gradle / Ivy

package co.easimart;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Encodes {@link EasimartObjects} as pointers. If the object does not have an objectId, uses a
 * local id.
 */
/** package */ class PointerOrLocalIdEncoder extends EasimartEncoder {

  // This class isn't really a Singleton, but since it has no state, it's more efficient to get the
  // default instance.
  private static final PointerOrLocalIdEncoder INSTANCE = new PointerOrLocalIdEncoder();
  public static PointerOrLocalIdEncoder get() {
    return INSTANCE;
  }

  @Override
  public JSONObject encodeRelatedObject(EasimartObject object) {
    JSONObject json = new JSONObject();
    try {
      if (object.getObjectId() != null) {
        json.put("__type", "Pointer");
        json.put("className", object.getClassName());
        json.put("objectId", object.getObjectId());
      } else {
        json.put("__type", "Pointer");
        json.put("className", object.getClassName());
        json.put("localId", object.getOrCreateLocalId());
      }
    } catch (JSONException e) {
      // This should not happen
      throw new RuntimeException(e);
    }
    return json;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy