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 * @version $Id: AbstractFatherBlock.java 705065 2008-10-15 21:46:08Z vsiveton $
31 */
32 abstract class AbstractFatherBlock
33 implements Block
34 {
35 /**
36 * @see AbstractFatherBlock#AbstractFatherBlock(Block[])
37 */
38 private final Block[] childBlocks;
39
40 /**
41 * method called before traversing the childs
42 *
43 * @param sink a sink to fill
44 */
45 abstract void before( Sink sink );
46
47 /**
48 * method called after traversing the childs
49 *
50 * @param sink a sink to fill
51 */
52 abstract void after( Sink sink );
53
54 /**
55 * Creates the AbstractFatherBlock.
56 *
57 * @param childBlocks child blocks
58 */
59 AbstractFatherBlock( final Block[] childBlocks )
60 {
61 if ( childBlocks == null )
62 {
63 throw new IllegalArgumentException( "argument can't be null" );
64 }
65
66 for ( int i = 0; i < childBlocks.length; i++ )
67 {
68 if ( childBlocks[i] == null )
69 {
70 throw new IllegalArgumentException( "bucket " + i + " can't be null" );
71 }
72 }
73 this.childBlocks = childBlocks;
74 }
75
76 /** {@inheritDoc} */
77 public final void traverse( final Sink sink )
78 {
79 before( sink );
80 for ( int i = 0; i < childBlocks.length; i++ )
81 {
82 Block block = childBlocks[i];
83
84 block.traverse( sink );
85 }
86 after( sink );
87 }
88
89 /**
90 * Returns the childBlocks.
91 *
92 * @return <code>Block[]</code> with the childBlocks.
93 */
94 public final Block[] getBlocks()
95 {
96 return childBlocks;
97 }
98
99 /** {@inheritDoc}*/
100 public boolean equals( final Object obj )
101 {
102 boolean ret = false;
103
104 if ( obj == this )
105 {
106 ret = true;
107 }
108 else if ( obj == null )
109 {
110 ret = false;
111 }
112 else if ( obj.getClass().equals( this.getClass() ) )
113 {
114 if ( obj instanceof AbstractFatherBlock )
115 {
116 final AbstractFatherBlock a = (AbstractFatherBlock) obj;
117 ret = Arrays.equals( a.childBlocks, this.childBlocks );
118 }
119 }
120
121 return ret;
122 }
123
124 /** {@inheritDoc}*/
125 public int hashCode()
126 {
127 int result = 1;
128 if ( childBlocks != null )
129 {
130 for ( int i = 0; i < childBlocks.length; i++ )
131 {
132 result += childBlocks[i].hashCode();
133 }
134 }
135
136 return result;
137 }
138 }