001package com.box.sdk; 002 003import com.eclipsesource.json.JsonObject; 004 005/** 006 * The abstract base class for all resource types (files, folders, comments, collaborations, etc.) used by the API. 007 * 008 * <p>Every API resource has an ID and a {@link BoxAPIConnection} that it uses to communicate with the API. Some 009 * resources also have an associated {@link Info} class that contains information about the resource.</p> 010 */ 011public abstract class BoxResource { 012 private final BoxAPIConnection api; 013 private final String id; 014 015 /** 016 * Constructs a BoxResource for a resource with a given ID. 017 * @param api the API connection to be used by the resource. 018 * @param id the ID of the resource. 019 */ 020 public BoxResource(BoxAPIConnection api, String id) { 021 this.api = api; 022 this.id = id; 023 } 024 025 static BoxResource.Info parseInfo(BoxAPIConnection api, JsonObject jsonObject) { 026 String type = jsonObject.get("type").asString(); 027 String id = jsonObject.get("id").asString(); 028 029 if (type.equals("folder")) { 030 BoxFolder folder = new BoxFolder(api, id); 031 return folder.new Info(jsonObject); 032 } else if (type.equals("file")) { 033 BoxFile file = new BoxFile(api, id); 034 return file.new Info(jsonObject); 035 } else if (type.equals("comment")) { 036 BoxComment comment = new BoxComment(api, id); 037 return comment.new Info(jsonObject); 038 } else if (type.equals("collaboration")) { 039 BoxCollaboration collaboration = new BoxCollaboration(api, id); 040 return collaboration.new Info(jsonObject); 041 } else if (type.equals("user")) { 042 BoxUser user = new BoxUser(api, id); 043 return user.new Info(jsonObject); 044 } else if (type.equals("group")) { 045 BoxGroup group = new BoxGroup(api, id); 046 return group.new Info(jsonObject); 047 } else { 048 return null; 049 } 050 } 051 052 /** 053 * Gets the API connection used by this resource. 054 * @return the API connection used by this resource. 055 */ 056 public BoxAPIConnection getAPI() { 057 return this.api; 058 } 059 060 /** 061 * Gets the ID of this resource. 062 * @return the ID of this resource. 063 */ 064 public String getID() { 065 return this.id; 066 } 067 068 /** 069 * Indicates whether this BoxResource is equal to another BoxResource. Two BoxResources are equal if they have the 070 * same type and ID. 071 * @param other the other BoxResource to compare. 072 * @return true if the type and IDs of the two resources are equal; otherwise false. 073 */ 074 @Override 075 public boolean equals(Object other) { 076 if (other == null) { 077 return false; 078 } 079 080 if (this.getClass().equals(other.getClass())) { 081 BoxResource otherResource = (BoxResource) other; 082 return this.getID().equals(otherResource.getID()); 083 } 084 085 return false; 086 } 087 088 /** 089 * Returns a hash code value for this BoxResource. 090 * @return a hash code value for this BoxResource. 091 */ 092 @Override 093 public int hashCode() { 094 return this.getID().hashCode(); 095 } 096 097 /** 098 * Contains information about a BoxResource. 099 */ 100 public abstract class Info extends BoxJSONObject { 101 /** 102 * Constructs an empty Info object. 103 */ 104 public Info() { 105 super(); 106 } 107 108 /** 109 * Constructs an Info object by parsing information from a JSON string. 110 * @param json the JSON string to parse. 111 */ 112 public Info(String json) { 113 super(json); 114 } 115 116 /** 117 * Constructs an Info object using an already parsed JSON object. 118 * @param jsonObject the parsed JSON object. 119 */ 120 Info(JsonObject jsonObject) { 121 super(jsonObject); 122 } 123 124 /** 125 * Gets the ID of the resource associated with this Info. 126 * @return the ID of the associated resource. 127 */ 128 public String getID() { 129 return BoxResource.this.getID(); 130 } 131 132 /** 133 * Gets the resource associated with this Info. 134 * @return the associated resource. 135 */ 136 public abstract BoxResource getResource(); 137 } 138}