package com.jscape.util.file;

import com.jscape.util.SuppressObfuscation;

/**
 * @author Alex Usun
 */
@SuppressObfuscation
public enum UnixFileType {

    REGULAR('-', 0x8000),
    DIRECTORY('d', 0x4000),
    SYMBOLIC_LINK('l', 0xA000),
    NAMED_PIPE('p', 0x1000),
    SOCKET('s', 0xC000),
    CHARACTER_DEVICE('c', 0x2000),
    BLOCK_DEVICE('b', 0x6000),
    DOOR('D', 0x0000); // not sure about it's mask

    private static final int TYPE_MASK = 0xF000;

    public final char lsCode;
    public final int mask;

    public static UnixFileType parse(int data) {
        for (UnixFileType type : values()) {
            if ((data & TYPE_MASK) == type.mask) {
                return type;
            }
        }

        return REGULAR;
    }

    public static boolean isDirectory(char code) {
        return DIRECTORY.lsCode == code;
    }

    UnixFileType(char lsCode, int mask) {
        this.lsCode = lsCode;
        this.mask = mask;
    }

    public int format(int data) {
        return data | this.mask;
    }
}
