1/*
2 * Copyright 2008 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.mockftpserver.fake.filesystem;
17
18import org.mockftpserver.core.util.Assert;
19
20import java.util.Date;
21
22/**
23 * The abstract superclass for concrete file system entry classes representing files and directories.
24 *
25 * @author Chris Mair
26 * @version $Revision$ - $Date$
27 */
28public abstract class AbstractFileSystemEntry implements FileSystemEntry {
29
30    private String path;
31    private boolean pathLocked = false;
32
33    private Date lastModified;
34    private String owner;
35    private String group;
36
37    public Date getLastModified() {
38        return lastModified;
39    }
40
41    public void setLastModified(Date lastModified) {
42        this.lastModified = lastModified;
43    }
44
45    public String getOwner() {
46        return owner;
47    }
48
49    public void setOwner(String owner) {
50        this.owner = owner;
51    }
52
53    public String getGroup() {
54        return group;
55    }
56
57    public void setGroup(String group) {
58        this.group = group;
59    }
60
61    public Permissions getPermissions() {
62        return permissions;
63    }
64
65    public void setPermissions(Permissions permissions) {
66        this.permissions = permissions;
67    }
68
69    private Permissions permissions;
70
71    /**
72     * Construct a new instance without setting its path
73     */
74    public AbstractFileSystemEntry() {
75    }
76
77    /**
78     * Construct a new instance with the specified value for its path
79     *
80     * @param path - the value for path
81     */
82    public AbstractFileSystemEntry(String path) {
83        this.path = path;
84    }
85
86    /**
87     * @return the path for this entry
88     */
89    public String getPath() {
90        return path;
91    }
92
93    /**
94     * @return the file name or directory name (no path) for this entry
95     */
96    public String getName() {
97        int separatorIndex1 = path.lastIndexOf('/');
98        int separatorIndex2 = path.lastIndexOf('\\');
99//        int separatorIndex = [separatorIndex1, separatorIndex2].max();
100        int separatorIndex = separatorIndex1 > separatorIndex2 ? separatorIndex1 : separatorIndex2;
101        return (separatorIndex == -1) ? path : path.substring(separatorIndex + 1);
102    }
103
104    /**
105     * Set the path for this entry. Throw an exception if pathLocked is true.
106     *
107     * @param path - the new path value
108     */
109    public void setPath(String path) {
110        Assert.isFalse(pathLocked, "path is locked");
111        this.path = path;
112    }
113
114    public void lockPath() {
115        this.pathLocked = true;
116    }
117
118    public void setPermissionsFromString(String permissionsString) {
119        this.permissions = new Permissions(permissionsString);
120    }
121
122    /**
123     * Abstract method -- must be implemented within concrete subclasses
124     *
125     * @return true if this file system entry represents a directory
126     */
127    public abstract boolean isDirectory();
128
129}
130