View Javadoc

1   /* 
2    * Copyright 2002-2004 The Apache Software Foundation
3    * Licensed  under the  Apache License,  Version 2.0  (the "License");
4    * you may not use  this file  except in  compliance with the License.
5    * You may obtain a copy of the License at 
6    * 
7    *   http://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed  under the  License is distributed on an "AS IS" BASIS,
11   * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
12   * implied.
13   * 
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.struts.flow.core.source.impl.validity;
18  
19  import java.io.IOException;
20  import java.util.ArrayList;
21  import java.util.Iterator;
22  import java.util.ListIterator;
23  
24  import org.apache.struts.flow.core.source.SourceValidity;
25  
26  /***
27   * A validation object using a List.
28   * This validity object does the same as the {@link AggregatedValidity}
29   * object, but the contained validity objects are only fetched when
30   * required.
31   *
32   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
33   * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:17 $
34   */
35  public final class DeferredAggregatedValidity
36          extends AbstractAggregatedValidity
37      implements SourceValidity
38  {
39  
40      public void add( final DeferredValidity validity )
41      {
42          m_list.add( validity );
43      }
44  
45      /***
46       * Check if the component is still valid.
47       * If <code>0</code> is returned the isValid(SourceValidity) must be
48       * called afterwards!
49       * If -1 is returned, the component is not valid anymore and if +1
50       * is returnd, the component is valid.
51       */
52      public int isValid()
53      {
54          for( final ListIterator i = m_list.listIterator(); i.hasNext(); )
55          {
56              final Object o = i.next();
57              final SourceValidity validity;
58              if (o instanceof SourceValidity) {
59                  validity = (SourceValidity)o;
60              } else {
61                  validity = ((DeferredValidity)o).getValidity();
62                  i.set(validity);
63              }
64              final int v = validity.isValid();
65              if( v < 1 )
66              {
67                  return v;
68              }
69          }
70          return 1;
71      }
72  
73      public int isValid( final SourceValidity validity )
74      {
75          AbstractAggregatedValidity aggregatedValidity = null;
76          
77          if (validity instanceof AbstractAggregatedValidity) 
78          {
79              aggregatedValidity = (AbstractAggregatedValidity)validity;
80          }
81          
82          if ( null != aggregatedValidity) 
83          {
84              ArrayList otherList = aggregatedValidity.m_list;
85              if( m_list.size() != otherList.size() )
86              {
87                  return -1;
88              }
89  
90              for(int i=0; i < m_list.size(); i++) {
91                  final SourceValidity srcA = this.getValidity(i);
92                  int result = srcA.isValid();
93                  if ( result == -1) 
94                  {
95                      return -1;
96                  }
97                  if ( result == 0 )
98                  {
99                      final SourceValidity srcB = aggregatedValidity.getValidity(i);
100                     result = srcA.isValid( srcB );
101                     if ( result < 1)
102                     {
103                         return result;
104                     }
105                 }
106             }
107             return 1;
108         }
109         return -1;
110     }
111 
112     public String toString()
113     {
114         final StringBuffer sb = new StringBuffer( "SourceValidity " );
115         for( final Iterator i = m_list.iterator(); i.hasNext(); )
116         {
117             sb.append( i.next() );
118             if( i.hasNext() ) sb.append( ':' );
119         }
120         return sb.toString();
121     }
122     
123     SourceValidity getValidity(final int index) 
124     {
125         final Object o = m_list.get(index);
126         final SourceValidity validity;
127         if (o instanceof SourceValidity) {
128             validity = (SourceValidity)o;
129         } else {
130             validity = ((DeferredValidity)o).getValidity();
131             m_list.set(index, validity);
132         }
133         return validity;
134     }
135     
136     private void writeObject(java.io.ObjectOutputStream out)
137          throws IOException
138     {
139         // resolve all deferred source validities first
140         for(int i=0; i<m_list.size();i++) {
141             this.getValidity(i);
142         }
143         out.defaultWriteObject();
144     }
145 
146 }
147