View Javadoc
1   package org.apache.maven.doxia.module.twiki.parser;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Arrays;
23  
24  import org.apache.maven.doxia.sink.Sink;
25  
26  /**
27   * Generic Block for the Block that have child blocks.
28   *
29   * @author Juan F. Codagnone
30   */
31  abstract class AbstractFatherBlock
32      implements Block
33  {
34      /**
35       * @see AbstractFatherBlock#AbstractFatherBlock(Block[])
36       */
37      private final Block[] childBlocks;
38  
39      /**
40       * method called before traversing the childs
41       *
42       * @param sink a sink to fill
43       */
44      abstract void before( Sink sink );
45  
46      /**
47       * method called after traversing the childs
48       *
49       * @param sink a sink to fill
50       */
51      abstract void after( Sink sink );
52  
53      /**
54       * Creates the AbstractFatherBlock.
55       *
56       * @param childBlocks child blocks
57       */
58      AbstractFatherBlock( final Block[] childBlocks )
59      {
60          if ( childBlocks == null )
61          {
62              throw new IllegalArgumentException( "argument can't be null" );
63          }
64  
65          for ( int i = 0; i < childBlocks.length; i++ )
66          {
67              if ( childBlocks[i] == null )
68              {
69                  throw new IllegalArgumentException( "bucket " + i + " can't be null" );
70              }
71          }
72          this.childBlocks = childBlocks;
73      }
74  
75      /** {@inheritDoc} */
76      public final void traverse( final Sink sink )
77      {
78          before( sink );
79          for ( Block block : childBlocks )
80          {
81              block.traverse( sink );
82          }
83          after( sink );
84      }
85  
86      /**
87       * Returns the childBlocks.
88       *
89       * @return <code>Block[]</code> with the childBlocks.
90       */
91      public final Block[] getBlocks()
92      {
93          return childBlocks;
94      }
95  
96      /** {@inheritDoc} */
97      public boolean equals( final Object obj )
98      {
99          boolean ret = false;
100 
101         if ( obj == this )
102         {
103             ret = true;
104         }
105         else if ( obj == null )
106         {
107             ret = false;
108         }
109         else if ( obj.getClass().equals( this.getClass() ) )
110         {
111             if ( obj instanceof AbstractFatherBlock )
112             {
113                 final AbstractFatherBlock a = (AbstractFatherBlock) obj;
114                 ret = Arrays.equals( a.childBlocks, this.childBlocks );
115             }
116         }
117 
118         return ret;
119     }
120 
121     /**
122      * {@inheritDoc}
123      *
124      * @return a int.
125      */
126     public int hashCode()
127     {
128         int result = 1;
129         if ( childBlocks != null )
130         {
131             for ( Block childBlock : childBlocks )
132             {
133                 result += childBlock.hashCode();
134             }
135         }
136 
137         return result;
138     }
139 }