Home | History | Annotate | Download | only in fs
      1 /*
      2  * Copyright (C) 2003-2009 JNode.org
      3  *               2009,2010 Matthias Treydte <mt (at) waldheinz.de>
      4  *
      5  * This library is free software; you can redistribute it and/or modify it
      6  * under the terms of the GNU Lesser General Public License as published
      7  * by the Free Software Foundation; either version 2.1 of the License, or
      8  * (at your option) any later version.
      9  *
     10  * This library is distributed in the hope that it will be useful, but
     11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
     12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
     13  * License for more details.
     14  *
     15  * You should have received a copy of the GNU Lesser General Public License
     16  * along with this library; If not, write to the Free Software Foundation, Inc.,
     17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
     18  */
     19 
     20 package de.waldheinz.fs;
     21 
     22 /**
     23  * A base class that helps to implement the {@code FsObject} interface.
     24  *
     25  * @author Ewout Prangsma &lt;epr at jnode.org&gt;
     26  * @author Matthias Treydte &lt;waldheinz at gmail.com&gt;
     27  * @since 0.6
     28  */
     29 public class AbstractFsObject implements FsObject {
     30 
     31     /**
     32      * Holds the read-only state of this object.
     33      */
     34     private final boolean readOnly;
     35 
     36     /**
     37      * Remembers if this object still valid.
     38      */
     39     private boolean valid;
     40 
     41     /**
     42      * Creates a new instance of {@code AbstractFsObject} which will be valid
     43      * and have the specified read-only state.
     44      *
     45      * @param readOnly if the new object will be read-only
     46      */
     47     protected AbstractFsObject(boolean readOnly) {
     48         this.valid = true;
     49         this.readOnly = readOnly;
     50     }
     51 
     52     /**
     53      * {@inheritDoc}
     54      *
     55      * @return {@inheritDoc}
     56      * @see #checkValid()
     57      * @see #invalidate()
     58      */
     59     @Override
     60     public final boolean isValid() {
     61         return this.valid;
     62     }
     63 
     64     /**
     65      * Marks this object as invalid.
     66      *
     67      * @see #isValid()
     68      * @see #checkValid()
     69      */
     70     protected final void invalidate() {
     71         this.valid = false;
     72     }
     73 
     74     /**
     75      * Convience method to check if this object is still valid and throw an
     76      * {@code IllegalStateException} if it is not.
     77      *
     78      * @throws IllegalStateException if this object was invalidated
     79      * @since 0.6
     80      * @see #isValid()
     81      * @see #invalidate()
     82      */
     83     protected final void checkValid() throws IllegalStateException {
     84         if (!isValid()) throw new IllegalStateException(
     85                 this + " is not valid");
     86     }
     87 
     88     /**
     89      * Convience method to check if this object is writable. An object is
     90      * writable if it is both, valid and not read-only.
     91      *
     92      * @throws IllegalStateException if this object was invalidated
     93      * @throws ReadOnlyException if this object was created with the read-only
     94      *      flag set
     95      * @since 0.6
     96      */
     97     protected final void checkWritable()
     98             throws IllegalStateException, ReadOnlyException {
     99 
    100         checkValid();
    101 
    102         if (isReadOnly()) {
    103             throw new ReadOnlyException();
    104         }
    105     }
    106 
    107     @Override
    108     public final boolean isReadOnly() {
    109         return this.readOnly;
    110     }
    111 
    112 }
    113