001package de.deepamehta.files;
002
003import de.deepamehta.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            JSONObject info = new JSONObject();
036            info.put("kind", kind.stringify());
037            return info;
038        } catch (Exception e) {
039            throw new RuntimeException("Serialization failed (" + this + ")", e);
040        }
041    }
042
043    @Override
044    public String toString() {
045        return "resource info (kind=\"" + kind + "\")";
046    }
047}