001    /* JAPI - (Yet another (hopefully) useful) Java API
002     *
003     * Copyright (C) 2004-2006 Christian Hujer
004     *
005     * This program is free software; you can redistribute it and/or
006     * modify it under the terms of the GNU General Public License as
007     * published by the Free Software Foundation; either version 2 of the
008     * License, or (at your option) any later version.
009     *
010     * This program is distributed in the hope that it will be useful, but
011     * WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013     * General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018     * 02111-1307, USA.
019     */
020    
021    package net.sf.japi.swing.prefs.keys;
022    
023    import org.jetbrains.annotations.Nullable;
024    
025    /** Base class for simple nodes.
026     * @author <a href="mailto:chris@riedquat.de">Christian Hujer</a>
027     */
028    public abstract class AbstractSimpleNode<C> implements SimpleNode<C> {
029    
030        /** The Children. */
031        @Nullable private final C[] children;
032    
033        /** The number of children. */
034        private final int length;
035    
036        /** Create an AbstractSimpleNode.
037         * @param children
038         */
039        protected AbstractSimpleNode(@Nullable final C[] children) {
040            if (children != null) {
041                this.children = children.clone();
042                assert this.children != null;
043                length = this.children.length;
044            } else {
045                this.children = null;
046                length = 0;
047            }
048        }
049    
050        /** {@inheritDoc} */
051        @Nullable public final C getChild(final int index) {
052            if (children == null) {
053                return null;
054            }
055            try {
056                return children[index];
057            } catch (final ArrayIndexOutOfBoundsException e) {
058                return null;
059            }
060        }
061    
062        /** {@inheritDoc} */
063        public final int getChildCount() {
064            return length;
065        }
066    
067    } // class AbstractSimpleNode