001    package net.sf.japi.swing.prefs.keys;
002    
003    import org.jetbrains.annotations.Nullable;
004    
005    /** Base class for simple nodes.
006     * @author <a href="mailto:chris@itcqis.com">Christian Hujer</a>
007     */
008    public abstract class AbstractSimpleNode<C> implements SimpleNode<C> {
009    
010        /** The Children. */
011        @Nullable private final C[] children;
012    
013        /** The number of children. */
014        private final int length;
015    
016        /** Create an AbstractSimpleNode.
017         * @param children
018         */
019        protected AbstractSimpleNode(@Nullable final C[] children) {
020            if (children != null) {
021                this.children = children.clone();
022                assert this.children != null;
023                length = this.children.length;
024            } else {
025                this.children = null;
026                length = 0;
027            }
028        }
029    
030        /** {@inheritDoc} */
031        @Nullable public final C getChild(final int index) {
032            if (children == null) {
033                return null;
034            }
035            try {
036                return children[index];
037            } catch (final ArrayIndexOutOfBoundsException e) {
038                return null;
039            }
040        }
041    
042        /** {@inheritDoc} */
043        public final int getChildCount() {
044            return length;
045        }
046    
047    } // class AbstractSimpleNode