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.File;
20  
21  import org.apache.struts.flow.core.source.SourceValidity;
22  
23  /***
24   * A validation object for time-stamps.
25   *
26   * @author <a href="mailto:dev@avalon.apache.org">Avalon Development Team</a>
27   * @version CVS $Revision: 1.4 $
28   */
29  public final class FileTimeStampValidity
30      implements SourceValidity
31  {
32      private long m_timeStamp;
33      private File m_file;
34  
35      public FileTimeStampValidity( final String filename )
36      {
37          this( new File( filename ) );
38      }
39  
40      public FileTimeStampValidity( final File file )
41      {
42          this( file, file.lastModified() );
43      }
44  
45      public FileTimeStampValidity( final File file,
46                                    final long timeStamp )
47      {
48          m_file = file;
49          m_timeStamp = timeStamp;
50      }
51  
52      /***
53       * Check if the component is still valid.
54       * If <code>0</code> is returned the isValid(SourceValidity) must be
55       * called afterwards!
56       * If -1 is returned, the component is not valid anymore and if +1
57       * is returnd, the component is valid.
58       */
59      public int isValid()
60      {
61          return ( m_file.lastModified() == m_timeStamp ? 1 : -1 );
62      }
63  
64      public int isValid( final SourceValidity newValidity )
65      {
66          if( newValidity instanceof FileTimeStampValidity )
67          {
68              final long timeStamp =
69                  ( (FileTimeStampValidity)newValidity ).getTimeStamp();
70              return ( m_timeStamp == timeStamp ? 1 : -1);
71          }
72          return -1;
73      }
74  
75      public File getFile()
76      {
77          return this.m_file;
78      }
79  
80      public long getTimeStamp()
81      {
82          return this.m_timeStamp;
83      }
84  
85      public String toString()
86      {
87          return "FileTimeStampValidity: " + m_file.getPath() + ": " + this.m_timeStamp;
88      }
89  }