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        switch (type) {
030            case "folder":
031                BoxFolder folder = new BoxFolder(api, id);
032                return folder.new Info(jsonObject);
033            case "file":
034                BoxFile file = new BoxFile(api, id);
035                return file.new Info(jsonObject);
036            case "comment":
037                BoxComment comment = new BoxComment(api, id);
038                return comment.new Info(jsonObject);
039            case "collaboration":
040                BoxCollaboration collaboration = new BoxCollaboration(api, id);
041                return collaboration.new Info(jsonObject);
042            case "user":
043                BoxUser user = new BoxUser(api, id);
044                return user.new Info(jsonObject);
045            case "group":
046                BoxGroup group = new BoxGroup(api, id);
047                return group.new Info(jsonObject);
048            default:
049                return null;
050        }
051    }
052
053    /**
054     * Gets the API connection used by this resource.
055     * @return the API connection used by this resource.
056     */
057    public BoxAPIConnection getAPI() {
058        return this.api;
059    }
060
061    /**
062     * Gets the ID of this resource.
063     * @return the ID of this resource.
064     */
065    public String getID() {
066        return this.id;
067    }
068
069    /**
070     * Indicates whether this BoxResource is equal to another BoxResource. Two BoxResources are equal if they have the
071     * same type and ID.
072     * @param  other the other BoxResource to compare.
073     * @return       true if the type and IDs of the two resources are equal; otherwise false.
074     */
075    @Override
076    public boolean equals(Object other) {
077        if (other == null) {
078            return false;
079        }
080
081        if (this.getClass().equals(other.getClass())) {
082            BoxResource otherResource = (BoxResource) other;
083            return this.getID().equals(otherResource.getID());
084        }
085
086        return false;
087    }
088
089    /**
090     * Returns a hash code value for this BoxResource.
091     * @return a hash code value for this BoxResource.
092     */
093    @Override
094    public int hashCode() {
095        return this.getID().hashCode();
096    }
097
098    /**
099     * Contains information about a BoxResource.
100     */
101    public abstract class Info extends BoxJSONObject {
102        /**
103         * Constructs an empty Info object.
104         */
105        public Info() {
106            super();
107        }
108
109        /**
110         * Constructs an Info object by parsing information from a JSON string.
111         * @param  json the JSON string to parse.
112         */
113        public Info(String json) {
114            super(json);
115        }
116
117        /**
118         * Constructs an Info object using an already parsed JSON object.
119         * @param  jsonObject the parsed JSON object.
120         */
121        Info(JsonObject jsonObject) {
122            super(jsonObject);
123        }
124
125        /**
126         * Gets the ID of the resource associated with this Info.
127         * @return the ID of the associated resource.
128         */
129        public String getID() {
130            return BoxResource.this.getID();
131        }
132
133        /**
134         * Gets the resource associated with this Info.
135         * @return the associated resource.
136         */
137        public abstract BoxResource getResource();
138    }
139}