--- old/src/java.base/share/classes/java/nio/file/Path.java 2018-02-13 17:25:19.000000000 -0800 +++ new/src/java.base/share/classes/java/nio/file/Path.java 2018-02-13 17:25:19.000000000 -0800 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. + * 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 @@ -30,6 +30,8 @@ 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 @@ -323,6 +325,43 @@ } /** + * 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. * *

The precise definition of this method is implementation dependent but