    001    /* Copyright 2000, 2001, Compaq Computer Corporation */
    002
    003    package javafe.filespace;
    004
    005
    006    import java.util.Enumeration;
    007
    008
    009    /**
    010     * A PreloadedTree is a HashTree whose edges map is loaded exactly once
    011     * before any children-fetching queries complete; the loading is lazy,
    012     * however, and occurs when the first children-fetching method is
    013     * called.<p>
    014     *
    015     * If a subclass adds additional methods that refer to edges, it should
    016     * make sure ensureEdgesLoaded is called before edges is used.<p>
    017     */
    018
    019    abstract class PreloadedTree extends HashTree {
    020
    021        /***************************************************
    022         *                                                 *
    023         * Loading the edges map:                          *
    024         *                                                 *
    025         **************************************************/
    026
    027        /** Have we loaded the edges map yet? */
    028        private boolean loaded = false;
    029
    030        /** Ensure that the edges map is ready for use */
    031        public final void ensureEdgesLoaded() {
    032            if (!loaded) {
    033                loadEdges();
    034                loaded = true;
    035            }
    036        }
    037
    038        /** Load the edges map for use.  */
    039        protected abstract void loadEdges();
    040
    041
    042        /***************************************************
    043         *                                                 *
    044         * Creation:                                       *
    045         *                                                 *
    046         **************************************************/
    047
    048        /** Create a root node: */
    049        public PreloadedTree(Object data) {
    050            super(data);
    051        }
    052
    053        /** Create a non-root node: */
    054        //@ requires parent != null && label != null;
    055        protected PreloadedTree(Tree parent, String label, Object data) {
    056            super(parent, label, data);
    057        }
    058
    059
    060        /***************************************************
    061         *                                                 *
    062         * Fetching and counting children:                 *
    063         *                                                 *
    064         **************************************************/
    065
    066        public final Enumeration children() {
    067            /* Ensure edges is loaded before we use it: */
    068            ensureEdgesLoaded();
    069
    070            return super.children();
    071        }
    072
    073        public final Tree getChild(String label) {
    074            /* Ensure edges is loaded before we use it: */
    075            ensureEdgesLoaded();
    076
    077            return super.getChild(label);
    078        }
    079
    080        /*
    081         * isLeaf and getChildrenCount should be considered as made final
    082         * here as well.
    083         */
    084    }