// // NumberFilter.java // /* LOCI Bio-Formats package for reading and converting biological file formats. Copyright (C) 2005-@year@ Melissa Linkert, Curtis Rueden, Chris Allan, Eric Kjellman and Brian Loranger. This program is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ package loci.formats; import java.io.File; import java.io.FileFilter; import java.math.BigInteger; /** * NumberFilter is a helper filter for FilePattern.findPattern(). * * <dl><dt><b>Source code:</b></dt> * <dd><a href="https://skyking.microscopy.wisc.edu/trac/java/browser/trunk/loci/formats/NumberFilter.java">Trac</a>, * <a href="https://skyking.microscopy.wisc.edu/svn/java/trunk/loci/formats/NumberFilter.java">SVN</a></dd></dl> */ public class NumberFilter implements FileFilter { // -- Fields -- /** String appearing before the numerical block. */ private String pre; /** String appearing after the numerical block. */ private String post; // -- Constructor -- /** * Creates a filter for files containing a numerical block, * sandwiched between the given strings. */ public NumberFilter(String pre, String post) { this.pre = pre; this.post = post; } // -- NumberFilter API methods -- /** Gets numbers filling the asterisk positions. */ public BigInteger getNumber(String name) { if (!name.startsWith(pre) || !name.endsWith(post)) return null; int ndx = pre.length(); int end = name.length() - post.length(); try { return new BigInteger(name.substring(ndx, end)); } catch (NumberFormatException exc) { return null; } } /** Tests if a specified file should be included in a file list. */ public boolean accept(String name) { return getNumber(name) != null; } // -- FileFilter API methods -- /** Tests if a specified file should be included in a file list. */ public boolean accept(File pathname) { return accept(pathname.getName()); } }