Coverage Report - org.codehaus.plexus.compiler.util.scan.StaleSourceScanner
 
Classes in this File Line Coverage Branch Coverage Complexity
StaleSourceScanner
96%
25/26
91%
11/12
2,75
 
 1  
 package org.codehaus.plexus.compiler.util.scan;
 2  
 
 3  
 /*
 4  
  * Copyright 2001-2005 The Apache Software Foundation.
 5  
  *
 6  
  * Licensed under the Apache License, Version 2.0 (the "License");
 7  
  * you may not use this file except in compliance with the License.
 8  
  * You may obtain a copy of the License at
 9  
  *
 10  
  *      http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing, software
 13  
  * distributed under the License is distributed on an "AS IS" BASIS,
 14  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15  
  * See the License for the specific language governing permissions and
 16  
  * limitations under the License.
 17  
  */
 18  
 
 19  
 import org.codehaus.plexus.compiler.util.scan.mapping.SourceMapping;
 20  
 
 21  
 import java.io.File;
 22  
 import java.util.Collections;
 23  
 import java.util.HashSet;
 24  
 import java.util.List;
 25  
 import java.util.Set;
 26  
 
 27  
 /**
 28  
  * @author jdcasey
 29  
  */
 30  
 public class StaleSourceScanner
 31  
     extends AbstractSourceInclusionScanner
 32  
 {
 33  
     private final long lastUpdatedWithinMsecs;
 34  
 
 35  
     private final Set<String> sourceIncludes;
 36  
 
 37  
     private final Set<String> sourceExcludes;
 38  
 
 39  
     // ----------------------------------------------------------------------
 40  
     //
 41  
     // ----------------------------------------------------------------------
 42  
 
 43  
     public StaleSourceScanner()
 44  
     {
 45  10
         this( 0, Collections.singleton( "**/*" ), Collections.<String>emptySet() );
 46  10
     }
 47  
 
 48  
     public StaleSourceScanner( long lastUpdatedWithinMsecs )
 49  
     {
 50  2
         this( lastUpdatedWithinMsecs, Collections.singleton( "**/*" ), Collections.<String>emptySet() );
 51  2
     }
 52  
 
 53  
     public StaleSourceScanner( long lastUpdatedWithinMsecs, Set<String> sourceIncludes, Set<String> sourceExcludes )
 54  14
     {
 55  14
         this.lastUpdatedWithinMsecs = lastUpdatedWithinMsecs;
 56  
 
 57  14
         this.sourceIncludes = sourceIncludes;
 58  
 
 59  14
         this.sourceExcludes = sourceExcludes;
 60  14
     }
 61  
 
 62  
     // ----------------------------------------------------------------------
 63  
     // SourceInclusionScanner Implementation
 64  
     // ----------------------------------------------------------------------
 65  
 
 66  
     public Set<File> getIncludedSources( File sourceDir, File targetDir )
 67  
         throws InclusionScanException
 68  
     {
 69  13
         List<SourceMapping> srcMappings = getSourceMappings();
 70  
 
 71  13
         if ( srcMappings.isEmpty() )
 72  
         {
 73  0
             return Collections.emptySet();
 74  
         }
 75  
 
 76  13
         String[] potentialIncludes = scanForSources( sourceDir, sourceIncludes, sourceExcludes );
 77  
 
 78  13
         Set<File> matchingSources = new HashSet<File>();
 79  
 
 80  42
         for ( String path : potentialIncludes )
 81  
         {
 82  29
             File sourceFile = new File( sourceDir, path );
 83  
 
 84  
             staleSourceFileTesting:
 85  29
             for ( SourceMapping mapping : srcMappings )
 86  
             {
 87  29
                 Set<File> targetFiles = mapping.getTargetFiles( targetDir, path );
 88  
 
 89  
                 // never include files that don't have corresponding target mappings.
 90  
                 // the targets don't have to exist on the filesystem, but the
 91  
                 // mappers must tell us to look for them.
 92  29
                 for ( File targetFile : targetFiles )
 93  
                 {
 94  20
                     if ( !targetFile.exists() || ( targetFile.lastModified() + lastUpdatedWithinMsecs
 95  14
                         < sourceFile.lastModified() ) )
 96  
                     {
 97  14
                         matchingSources.add( sourceFile );
 98  14
                         break staleSourceFileTesting;
 99  
                     }
 100  6
                 }
 101  15
             }
 102  
         }
 103  
 
 104  13
         return matchingSources;
 105  
     }
 106  
 }