8298303: (fs) temporarily remove Path.getExtension
Reviewed-by: smarks, alanb
This commit is contained in:
parent
3b8c7ef8e7
commit
10356e767a
@ -50,7 +50,7 @@ import java.util.Objects;
|
||||
* file system. {@code Path} defines the {@link #getFileName() getFileName},
|
||||
* {@link #getParent getParent}, {@link #getRoot getRoot}, and {@link #subpath
|
||||
* subpath} methods to access the path components or a subsequence of its name
|
||||
* elements, and {@link #getExtension() getExtension} to obtain its extension.
|
||||
* elements.
|
||||
*
|
||||
* <p> In addition to accessing the components of a path, a {@code Path} also
|
||||
* defines the {@link #resolve(Path) resolve} and {@link #resolveSibling(Path)
|
||||
@ -249,63 +249,6 @@ public interface Path
|
||||
*/
|
||||
Path getFileName();
|
||||
|
||||
/**
|
||||
* Returns the file extension of this path's file name as a {@code String}.
|
||||
* The extension is derived from this {@code Path} by obtaining the
|
||||
* {@linkplain #getFileName file name element}, deriving its {@linkplain
|
||||
* #toString string representation}, and then extracting a substring
|
||||
* determined by the position of a period character ('.', U+002E FULL STOP)
|
||||
* within the file name string. If the file name element is {@code null},
|
||||
* or if the file name string does not contain a period character, or if
|
||||
* the only period in the file name string is its first character, then
|
||||
* the extension is {@code null}. Otherwise, the extension is the substring
|
||||
* after the last period in the file name string. If this last period is
|
||||
* also the last character in the file name string, then the extension is
|
||||
* {@linkplain String#isEmpty empty}.
|
||||
*
|
||||
* @implSpec
|
||||
* The default implementation is equivalent for this path to:
|
||||
* <pre>{@code
|
||||
* int lastPeriod = fileName.lastIndexOf('.');
|
||||
* if (lastPeriod <= 0)
|
||||
* return null;
|
||||
* return (lastPeriod == fileName.length() - 1)
|
||||
* ? ""
|
||||
* : fileName.substring(lastPeriod + 1);
|
||||
* }</pre>
|
||||
*
|
||||
* @return the file name extension of this path, which might be the
|
||||
* empty string, or {@code null} if no extension is found
|
||||
*
|
||||
* @since 20
|
||||
*/
|
||||
default String getExtension() {
|
||||
Path fileName = getFileName();
|
||||
if (fileName == null)
|
||||
return null;
|
||||
|
||||
String fileNameString = fileName.toString();
|
||||
int length = fileNameString.length();
|
||||
|
||||
// An empty or unity length file name string has a null extension
|
||||
if (length > 1) {
|
||||
int lastPeriodIndex = fileNameString.lastIndexOf('.');
|
||||
|
||||
// Indeterminate if there is no period character or
|
||||
// only the first character is a period character
|
||||
if (lastPeriodIndex > 0) {
|
||||
if (lastPeriodIndex == length - 1) {
|
||||
// empty string
|
||||
return "";
|
||||
} else {
|
||||
return fileNameString.substring(lastPeriodIndex + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the <em>parent path</em>, or {@code null} if this path does not
|
||||
* have a parent.
|
||||
|
@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2022, 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.
|
||||
*
|
||||
* This code 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 General Public License
|
||||
* version 2 for more details (a copy is included in the LICENSE file that
|
||||
* accompanied this code).
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License version
|
||||
* 2 along with this work; if not, write to the Free Software Foundation,
|
||||
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
||||
* or visit www.oracle.com if you need additional information or have any
|
||||
* questions.
|
||||
*/
|
||||
|
||||
import java.nio.file.Path;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.DataProvider;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @bug 8057113
|
||||
* @summary Verify getExtension method
|
||||
* @run testng Extensions
|
||||
*/
|
||||
public class Extensions {
|
||||
/**
|
||||
* Returns path name string and expected extension pairs.
|
||||
*
|
||||
* @return {@code {{"pathname", "extension"},...}}
|
||||
*/
|
||||
@DataProvider
|
||||
static Object[][] getProvider() {
|
||||
Object[][] pairs = {
|
||||
{"", null},
|
||||
{".", null},
|
||||
{"..", ""},
|
||||
{"...", ""},
|
||||
{"....", ""},
|
||||
{".....", ""},
|
||||
{"aa", null},
|
||||
{"a.", ""},
|
||||
{".a", null},
|
||||
{"..a", "a"},
|
||||
{"...a", "a"},
|
||||
{"....a", "a"},
|
||||
{".a.b", "b"},
|
||||
{"...a.b", "b"},
|
||||
{"...a.b.", ""},
|
||||
{"..foo", "foo"},
|
||||
{"foo.", ""},
|
||||
{"test.", ""},
|
||||
{"test..", ""},
|
||||
{"test...", ""},
|
||||
{"test.rb", "rb"},
|
||||
{"a/b/d/test.rb" , "rb"},
|
||||
{".a/b/d/test.rb", "rb"},
|
||||
{"test", null},
|
||||
{".profile", null},
|
||||
{".profile.sh", "sh"},
|
||||
{"foo.tar.gz", "gz"},
|
||||
{"foo.bar.", ""},
|
||||
{"archive.zip", "zip"},
|
||||
{"compress.gzip", "gzip"},
|
||||
{"waitwhat.&$!#%", "&$!#%"},
|
||||
{"6.283185307", "283185307"}
|
||||
};
|
||||
return pairs;
|
||||
}
|
||||
|
||||
@Test(dataProvider = "getProvider")
|
||||
public static void get(String pathname, String extension) {
|
||||
Assert.assertEquals(Path.of(pathname).getExtension(), extension);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user