Skip to content

Commit

Permalink
Make BoxResource.Info a subclass of BoxJSONObject
Browse files Browse the repository at this point in the history
  • Loading branch information
gcurtis committed Sep 26, 2014
1 parent 988d443 commit 6c75538
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 72 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/box/sdk/BoxFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void updateInfo(BoxFile.Info info) {
request.setBody(info.getPendingChanges());
BoxJSONResponse response = (BoxJSONResponse) request.send();
JsonObject jsonObject = JsonObject.readFrom(response.getJSON());
info.updateFromJSON(jsonObject);
info.update(jsonObject);
}

public Collection<BoxFileVersion> getVersions() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/box/sdk/BoxFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void updateInfo(BoxFolder.Info info) {
request.setBody(info.getPendingChanges());
BoxJSONResponse response = (BoxJSONResponse) request.send();
JsonObject jsonObject = JsonObject.readFrom(response.getJSON());
info.updateFromJSON(jsonObject);
info.update(jsonObject);
}

public BoxFolder.Info copy(BoxFolder destination) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/box/sdk/BoxItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public String getName() {

public void setName(String name) {
this.name = name;
this.addPendingChange("name", JsonValue.valueOf(name));
this.addPendingChange("name", name);
}

public Date getCreatedAt() {
Expand All @@ -72,7 +72,7 @@ public String getDescription() {

public void setDescription(String description) {
this.description = description;
this.addPendingChange("description", JsonValue.valueOf(description));
this.addPendingChange("description", description);
}

public long getSize() {
Expand Down
72 changes: 4 additions & 68 deletions src/main/java/com/box/sdk/BoxResource.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package com.box.sdk;

import java.util.List;

import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;

/**
* The abstract base class for all resource types (files, folders, comments, collaborations, etc.) used by the API.
Expand Down Expand Up @@ -79,30 +76,28 @@ public int hashCode() {
*
* @param <T> the type of the resource associated with this info.
*/
public abstract class Info<T extends BoxResource> {
private JsonObject pendingChanges;

public abstract class Info<T extends BoxResource> extends BoxJSONObject {
/**
* Constructs an empty Info object.
*/
public Info() {
this.pendingChanges = new JsonObject();
super();
}

/**
* Constructs an Info object by parsing information from a JSON string.
* @param json the JSON string to parse.
*/
public Info(String json) {
this(JsonObject.readFrom(json));
super(json);
}

/**
* Constructs an Info object using an already parsed JSON object.
* @param jsonObject the parsed JSON object.
*/
protected Info(JsonObject jsonObject) {
this.updateFromJSON(jsonObject);
super(jsonObject);
}

/**
Expand All @@ -113,69 +108,10 @@ public String getID() {
return BoxResource.this.getID();
}

/**
* Gets a list of fields that have pending changes that haven't been sent to the API yet.
* @return a list of changed fields with pending changes.
*/
public List<String> getChangedFields() {
return this.pendingChanges.names();
}

/**
* Gets a JSON object of any pending changes.
* @return a JSON object containing the pending changes.
*/
public String getPendingChanges() {
return this.pendingChanges.toString();
}

/**
* Gets the resource associated with this Info.
* @return the associated resource.
*/
public abstract T getResource();

/**
* Adds a pending field change that needs to be sent to the API. It will be included in the JSON string the next
* time {@link #getPendingChanges} is called.
* @param key the name of the field.
* @param value the new value of the field.
*/
protected void addPendingChange(String key, JsonValue value) {
this.pendingChanges.set(key, value);
}

/**
* Clears all pending changes.
*/
protected void clearPendingChanges() {
this.pendingChanges = new JsonObject();
}

/**
* Updates this Info object using the information in a JSON object.
* @param jsonObject the JSON object containing updated information.
*/
protected void updateFromJSON(JsonObject jsonObject) {
for (JsonObject.Member member : jsonObject) {
if (member.getValue().isNull()) {
continue;
}

this.parseJSONMember(member);
}

this.clearPendingChanges();
}

/**
* Invoked with a JSON member whenever this Info object is updated or created from a JSON object.
*
* <p>Subclasses should override this method in order to parse any JSON members it knows about. This method is a
* no-op by default.</p>
*
* @param member the JSON member to be parsed.
*/
protected void parseJSONMember(JsonObject.Member member) { }
}
}

0 comments on commit 6c75538

Please sign in to comment.