001package systems.dmx.files;
002
003import systems.dmx.core.JSONEnabled;
004
005import org.codehaus.jettison.json.JSONObject;
006
007import java.io.File;
008
009
010
011public class ResourceInfo implements JSONEnabled {
012
013    // ---------------------------------------------------------------------------------------------- Instance Variables
014
015    private ItemKind kind;  // FILE or DIRECTORY
016
017    // ---------------------------------------------------------------------------------------------------- Constructors
018
019    /**
020     * Precondition: the file exists.
021     */
022    public ResourceInfo(File file) {
023        kind = file.isDirectory() ? ItemKind.DIRECTORY : ItemKind.FILE;
024    }
025
026    // -------------------------------------------------------------------------------------------------- Public Methods
027
028    public ItemKind getItemKind() {
029        return kind;
030    }
031
032    @Override
033    public JSONObject toJSON() {
034        try {
035            return new JSONObject()
036                .put("kind", kind.stringify());
037        } catch (Exception e) {
038            throw new RuntimeException("Serialization failed", e);
039        }
040    }
041
042    @Override
043    public String toString() {
044        return "resource info (kind=\"" + kind + "\")";
045    }
046}