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    /**
026     * Gets the API connection used by this resource.
027     * @return the API connection used by this resource.
028     */
029    public BoxAPIConnection getAPI() {
030        return this.api;
031    }
032
033    /**
034     * Gets the ID of this resource.
035     * @return the ID of this resource.
036     */
037    public String getID() {
038        return this.id;
039    }
040
041    /**
042     * Indicates whether this BoxResource is equal to another BoxResource. Two BoxResources are equal if they have the
043     * same type and ID.
044     * @param  other the other BoxResource to compare.
045     * @return       true if the type and IDs of the two resources are equal; otherwise false.
046     */
047    @Override
048    public boolean equals(Object other) {
049        if (other == null) {
050            return false;
051        }
052
053        if (this.getClass().equals(other.getClass())) {
054            BoxResource otherResource = (BoxResource) other;
055            return this.getID().equals(otherResource.getID());
056        }
057
058        return false;
059    }
060
061    /**
062     * Returns a hash code value for this BoxResource.
063     * @return a hash code value for this BoxResource.
064     */
065    @Override
066    public int hashCode() {
067        return this.getID().hashCode();
068    }
069
070    /**
071     * Contains information about a BoxResource.
072     */
073    public abstract class Info extends BoxJSONObject {
074        /**
075         * Constructs an empty Info object.
076         */
077        public Info() {
078            super();
079        }
080
081        /**
082         * Constructs an Info object by parsing information from a JSON string.
083         * @param  json the JSON string to parse.
084         */
085        public Info(String json) {
086            super(json);
087        }
088
089        /**
090         * Constructs an Info object using an already parsed JSON object.
091         * @param  jsonObject the parsed JSON object.
092         */
093        Info(JsonObject jsonObject) {
094            super(jsonObject);
095        }
096
097        /**
098         * Gets the ID of the resource associated with this Info.
099         * @return the ID of the associated resource.
100         */
101        public String getID() {
102            return BoxResource.this.getID();
103        }
104
105        /**
106         * Gets the resource associated with this Info.
107         * @return the associated resource.
108         */
109        public abstract BoxResource getResource();
110    }
111}