< prev index next >

src/java.base/share/classes/java/nio/file/Path.java

Print this page

        

*** 1,7 **** /* ! * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this --- 1,7 ---- /* ! * Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. Oracle designates this
*** 28,37 **** --- 28,39 ---- import java.io.File; import java.io.IOException; import java.net.URI; import java.util.Iterator; import java.util.NoSuchElementException; + import java.util.Objects; + import java.util.stream.Stream; /** * An object that may be used to locate a file in a file system. It will * typically represent a system dependent file path. *
*** 321,330 **** --- 323,369 ---- default boolean endsWith(String other) { return endsWith(getFileSystem().getPath(other)); } /** + * Returns whether the extension of this path is in a specified list. + * The extension is that portion of the path after the last dot + * ({@code '.'}). If this path is empty, ends with a dot, or has a + * {@link FileSystem#getSeparator() name-separator} after the last dot, + * or the parameter is empty, then this method returns {@code false}. + * Any arguments in the parameter which are either empty or contain a + * dot are ignored. + * + * @param extensions + * the extensions to be checked + * + * @return whether this path has one of the specified extensions + */ + default boolean hasExtension(String... extensions) { + Objects.requireNonNull(extensions); + + String path = toString(); + if (path.isEmpty()) { + return false; + } + + int lastDot = path.lastIndexOf("."); + if (lastDot == path.length() - 1) { + return false; + } + + String separator = getFileSystem().getSeparator(); + if (path.indexOf(separator, lastDot) != -1) { + return false; + } + + return Stream.of(extensions) + .filter(e -> !e.isEmpty() && !e.contains(".")) + .anyMatch(path::endsWith); + } + + /** * Returns a path that is this path with redundant name elements eliminated. * * <p> The precise definition of this method is implementation dependent but * in general it derives from this path, a path that does not contain * <em>redundant</em> name elements. In many file systems, the "{@code .}"
< prev index next >