summaryrefslogtreecommitdiff
path: root/tags/2.3/src/main/java/org/mockftpserver/fake/filesystem/Permissions.java
blob: 8c39637f35c3ad562975b63bcb4a3e03dc6e1ac2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.mockftpserver.fake.filesystem;

import org.mockftpserver.core.util.Assert;

/**
 * Represents and encapsulates the read/write/execute permissions for a file or directory.
 * This is conceptually (and somewhat loosely) based on the permissions flags within the Unix
 * file system. An instance of this class is immutable.
 *
 * @author Chris Mair
 * @version $Revision$ - $Date$
 */
public class Permissions {
    public static final Permissions ALL = new Permissions("rwxrwxrwx");
    public static final Permissions NONE = new Permissions("---------");
    public static final Permissions DEFAULT = ALL;

    private static final char READ_CHAR = 'r';
    private static final char WRITE_CHAR = 'w';
    private static final char EXECUTE_CHAR = 'x';

    private String rwxString;

    /**
     * Costruct a new instance for the specified read/write/execute specification String
     *
     * @param rwxString - the read/write/execute specification String; must be 9 characters long, with chars
     *                  at index 0,3,6 == '-' or 'r', chars at index 1,4,7 == '-' or 'w' and chars at index 2,5,8 == '-' or 'x'.
     */
    public Permissions(String rwxString) {
        Assert.isTrue(rwxString.length() == 9, "The permissions string must be exactly 9 characters");
        final String RWX = "(-|r)(-|w)(-|x)";
        final String PATTERN = RWX + RWX + RWX;
        Assert.isTrue(rwxString.matches(PATTERN), "The permissions string must match [" + PATTERN + "]");
        this.rwxString = rwxString;
    }

    /**
     * Return the read/write/execute specification String representing the set of permissions. For example:
     * "rwxrwxrwx" or "rw-r-----".
     *
     * @return the String containing 9 characters that represent the read/write/execute permissions.
     */
    public String asRwxString() {
        return rwxString;
    }

    /**
     * @return the RWX string for this instance
     */
    public String getRwxString() {
        return rwxString;
    }

    /**
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object object) {
        return (object != null)
                && (object.getClass() == this.getClass())
                && (object.hashCode() == hashCode());
    }

    /**
     * Return the hash code for this object.
     *
     * @see java.lang.Object#hashCode()
     */
    public int hashCode() {
        return rwxString.hashCode();
    }

    /**
     * @return true if and only if the user has read permission
     */
    public boolean canUserRead() {
        return rwxString.charAt(0) == READ_CHAR;
    }

    /**
     * @return true if and only if the user has write permission
     */
    public boolean canUserWrite() {
        return rwxString.charAt(1) == WRITE_CHAR;
    }

    /**
     * @return true if and only if the user has execute permission
     */
    public boolean canUserExecute() {
        return rwxString.charAt(2) == EXECUTE_CHAR;
    }

    /**
     * @return true if and only if the group has read permission
     */
    public boolean canGroupRead() {
        return rwxString.charAt(3) == READ_CHAR;
    }

    /**
     * @return true if and only if the group has write permission
     */
    public boolean canGroupWrite() {
        return rwxString.charAt(4) == WRITE_CHAR;
    }

    /**
     * @return true if and only if the group has execute permission
     */
    public boolean canGroupExecute() {
        return rwxString.charAt(5) == EXECUTE_CHAR;
    }

    /**
     * @return true if and only if the world has read permission
     */
    public boolean canWorldRead() {
        return rwxString.charAt(6) == READ_CHAR;
    }

    /**
     * @return true if and only if the world has write permission
     */
    public boolean canWorldWrite() {
        return rwxString.charAt(7) == WRITE_CHAR;
    }

    /**
     * @return true if and only if the world has execute permission
     */
    public boolean canWorldExecute() {
        return rwxString.charAt(8) == EXECUTE_CHAR;
    }

    /**
     * @return the String representation of this object.
     */
    public String toString() {
        return "Permissions[" + rwxString + "]";
    }
}