Skip to content

Commit 5464093

Browse files
author
Niall Pemberton
committed
IO-261 Add getFile() methods with varargs parameter
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/io/trunk@1081025 13f79535-47bb-0310-9956-ffa450edef68
1 parent b188f59 commit 5464093

2 files changed

Lines changed: 87 additions & 0 deletions

File tree

src/main/java/org/apache/commons/io/FileUtils.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,50 @@ public FileUtils() {
121121
private static final Charset UTF8 = Charset.forName("UTF-8");
122122

123123
//-----------------------------------------------------------------------
124+
/**
125+
* Construct a file from the set of name elements.
126+
*
127+
* @param directory the parent directory
128+
* @param names the name elements
129+
* @return the file
130+
* @since Commons IO 2.1
131+
*/
132+
public static File getFile(File directory, String... names) {
133+
if (directory == null) {
134+
throw new NullPointerException("directorydirectory must not be null");
135+
}
136+
if (names == null) {
137+
throw new NullPointerException("names must not be null");
138+
}
139+
File file = directory;
140+
for (String name : names) {
141+
file = new File(file, name);
142+
}
143+
return file;
144+
}
145+
146+
/**
147+
* Construct a file from the set of name elements.
148+
*
149+
* @param names the name elements
150+
* @return the file
151+
* @since Commons IO 2.1
152+
*/
153+
public static File getFile(String... names) {
154+
if (names == null) {
155+
throw new NullPointerException("names must not be null");
156+
}
157+
File file = null;
158+
for (String name : names) {
159+
if (file == null) {
160+
file = new File(name);
161+
} else {
162+
file = new File(file, name);
163+
}
164+
}
165+
return file;
166+
}
167+
124168
/**
125169
* Returns the path to the system temporary directory.
126170
*

src/test/java/org/apache/commons/io/FileUtilsTestCase.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,49 @@ protected void tearDown() throws Exception {
101101
}
102102

103103
//-----------------------------------------------------------------------
104+
/**
105+
* Tests the {@link FileUtils#getFile(String...)} method.
106+
*/
107+
public void testGetFile() {
108+
File expected_A = new File("src");
109+
File expected_B = new File(expected_A, "main");
110+
File expected_C = new File(expected_B, "java");
111+
assertEquals("A", expected_A, FileUtils.getFile("src"));
112+
assertEquals("B", expected_B, FileUtils.getFile("src", "main"));
113+
assertEquals("C", expected_C, FileUtils.getFile("src", "main", "java"));
114+
try {
115+
FileUtils.getFile((String[])null);
116+
fail("Expected NullPointerException");
117+
} catch (NullPointerException e) {
118+
// expected
119+
}
120+
}
121+
122+
/**
123+
* Tests the {@link FileUtils#getFile(File, String...)} method.
124+
*/
125+
public void testGetFile_Parent() {
126+
File parent = new File("parent");
127+
File expected_A = new File(parent, "src");
128+
File expected_B = new File(expected_A, "main");
129+
File expected_C = new File(expected_B, "java");
130+
assertEquals("A", expected_A, FileUtils.getFile(parent, "src"));
131+
assertEquals("B", expected_B, FileUtils.getFile(parent, "src", "main"));
132+
assertEquals("C", expected_C, FileUtils.getFile(parent, "src", "main", "java"));
133+
try {
134+
FileUtils.getFile(parent, (String[])null);
135+
fail("Expected NullPointerException");
136+
} catch (NullPointerException e) {
137+
// expected
138+
}
139+
try {
140+
FileUtils.getFile((File)null, "src");
141+
fail("Expected NullPointerException");
142+
} catch (NullPointerException e) {
143+
// expected
144+
}
145+
}
146+
104147
/**
105148
* Tests the {@link FileUtils#getTempDirectoryPath()} method.
106149
*/

0 commit comments

Comments
 (0)