/*
   Copyright 2015 The Trustees of Princeton University

   Licensed under the Apache License, Version 2.0 (the "License" );
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package hsyndicate.fs;

import com.google.common.primitives.UnsignedLong;
import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author iychoi
 */
public class SyndicateLocalBlockCache {
    public static final String LOCAL_BLOCK_CACHE_FILENAME_PATTERN_STRING = "(-?\\d+)\\.(-?\\d+)";
    public Pattern filenamePattern = Pattern.compile(LOCAL_BLOCK_CACHE_FILENAME_PATTERN_STRING);
    
    public class SyndicateLocalBlockCacheFileFilter implements FilenameFilter {
        @Override
        public boolean accept(File file, String filename) {
            Matcher matcher = filenamePattern.matcher(filename);
            return matcher.matches();
        }
    }
    
    public UnsignedLong getBlockID(String filename) {
        Matcher matcher = filenamePattern.matcher(filename);
        String blockId = matcher.group(1);
        return UnsignedLong.valueOf(blockId);
    }
    
    public long getBlockVersion(String filename) {
        Matcher matcher = filenamePattern.matcher(filename);
        String blockVer = matcher.group(2);
        return Long.parseLong(blockVer);
    }
    
    public FilenameFilter getFilenameFilter() {
        return new SyndicateLocalBlockCacheFileFilter();
    }
    
    public boolean isValidFileName(String filename) {
        Matcher matcher = filenamePattern.matcher(filename);
        return matcher.matches();
    }
}

@@ -729,24 +729,52 @@
 nameFilter;%0A
+import java.io.IOException;%0A
 import java.
@@ -829,77 +829,253 @@
 rn;%0A
-%0A/**%0A *%0A * @author iychoi%0A */%0Apublic class SyndicateLocalBlockCache %7B
+import org.apache.commons.logging.Log;%0Aimport org.apache.commons.logging.LogFactory;%0A%0A/**%0A *%0A * @author iychoi%0A */%0Apublic class SyndicateLocalBlockCache %7B%0A    private static final Log LOG = LogFactory.getLog(SyndicateLocalBlockCache.class);%0A    
 %0A   
@@ -1596,32 +1596,51 @@
 String filename)
+ throws IOException
  %7B%0A        Match
@@ -1679,32 +1679,68 @@
 cher(filename);%0A
+        if(matcher.matches()) %7B%0A    
         String b
@@ -1766,16 +1766,20 @@
 oup(1);%0A
+    
         
@@ -1816,16 +1816,122 @@
 ockId);%0A
+        %7D else %7B%0A            throw new IOException(%22Unable to parse BlockID from %22 + filename);%0A        %7D%0A
     %7D%0A  
@@ -1973,32 +1973,51 @@
 String filename)
+ throws IOException
  %7B%0A        Match
@@ -2056,32 +2056,68 @@
 cher(filename);%0A
+        if(matcher.matches()) %7B%0A    
         String b
@@ -2148,24 +2148,28 @@
 2);%0A        
+    
 return Long.
@@ -2189,16 +2189,122 @@
 ckVer);%0A
+        %7D else %7B%0A            throw new IOException(%22Unable to parse BlockID from %22 + filename);%0A        %7D%0A
     %7D%0A  

Check if local block cache file matches to pattern first before parsing