/* * Copyright (c) 2018-2019. Developed by Hedgecode. * * 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 org.hedgecode.chess.img; import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.Files; import java.nio.file.Path; /** * Filter for images by name and extention. * * @author Dmitry Samoshin aka gotty * @see DirectoryStream.Filter */ public class ImageFilter implements DirectoryStream.Filter { private static final String[] IMAGES_EXTS = ImageFormat.getAllExts(); private String[] names; public ImageFilter() { this.names = null; } public ImageFilter(String[] names) { this.names = names; } @Override public boolean accept(Path file) throws IOException { if (Files.isDirectory(file)) { return false; } String filename = file.getFileName().toString(); return acceptExt(filename) && acceptName(filename); } private boolean acceptName(String filename) { if (names != null) { String name = FilenameUtils.getBaseName(filename); for (String imageName : names) { if (imageName.equalsIgnoreCase(name)) return true; } return false; } return true; } private boolean acceptExt(String filename) { String ext = FilenameUtils.getExtension(filename); for (String imageExt : IMAGES_EXTS) { if (imageExt.equalsIgnoreCase(ext)) return true; } return false; } }