summaryrefslogtreecommitdiff
path: root/src/main/java/de/waldheinz/fs/AbstractFsObject.java
blob: fea022ab16349d59665650413750511d72efdcf1 (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
/*
 * Copyright (C) 2003-2009 JNode.org
 *               2009,2010 Matthias Treydte <mt@waldheinz.de>
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; If not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
package de.waldheinz.fs;

/**
 * A base class that helps to implement the {@code FsObject} interface.
 *
 * @author Ewout Prangsma &lt;epr at jnode.org&gt;
 * @author Matthias Treydte &lt;waldheinz at gmail.com&gt;
 * @since 0.6
 */
public class AbstractFsObject implements FsObject {

    /**
     * Holds the read-only state of this object.
     */
    private final boolean readOnly;
    
    /**
     * Remembers if this object still valid.
     */
    private boolean valid;

    /**
     * Creates a new instance of {@code AbstractFsObject} which will be valid
     * and have the specified read-only state.
     *
     * @param readOnly if the new object will be read-only
     */
    protected AbstractFsObject(boolean readOnly) {
        this.valid = true;
        this.readOnly = readOnly;
    }
    
    /** 
     * {@inheritDoc}
     *
     * @return {@inheritDoc}
     * @see #checkValid()
     * @see #invalidate() 
     */
    @Override
    public final boolean isValid() {
        return this.valid;
    }

    /**
     * Marks this object as invalid.
     * 
     * @see #isValid()
     * @see #checkValid()
     */
    protected final void invalidate() {
        this.valid = false;
    }

    /**
     * Convience method to check if this object is still valid and throw an
     * {@code IllegalStateException} if it is not.
     *
     * @throws IllegalStateException if this object was invalidated
     * @since 0.6
     * @see #isValid()
     * @see #invalidate() 
     */
    protected final void checkValid() throws IllegalStateException {
        if (!isValid()) throw new IllegalStateException(
                this + " is not valid");
    }

    /**
     * Convience method to check if this object is writable. An object is
     * writable if it is both, valid and not read-only. 
     *
     * @throws IllegalStateException if this object was invalidated
     * @throws ReadOnlyException if this object was created with the read-only
     *      flag set
     * @since 0.6
     */
    protected final void checkWritable()
            throws IllegalStateException, ReadOnlyException {
        
        checkValid();

        if (isReadOnly()) {
            throw new ReadOnlyException();
        }
    }
    
    @Override
    public final boolean isReadOnly() {
        return this.readOnly;
    }
    
}