Skip to content

Commit abe647c

Browse files
authored
Merge pull request #11 from Nordstrom/pr/support-java-7
Pr/support java 7
2 parents a43c546 + 2ab0128 commit abe647c

File tree

5 files changed

+127
-85
lines changed

5 files changed

+127
-85
lines changed

pom.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626

2727
<properties>
2828
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
29-
<maven.compiler.source>1.8</maven.compiler.source>
30-
<maven.compiler.target>1.8</maven.compiler.target>
29+
<maven.compiler.source>1.7</maven.compiler.source>
30+
<maven.compiler.target>1.7</maven.compiler.target>
3131
<testng.version>6.11</testng.version>
3232
<surefire-plugin.version>2.19.1</surefire-plugin.version>
3333
<source-plugin.version>3.0.1</source-plugin.version>
3434
<javadoc-plugin.version>2.10.4</javadoc-plugin.version>
35+
<guava.version>23.5-jre</guava.version>
3536
<gpg-plugin.version>1.6</gpg-plugin.version>
3637
<staging-plugin.version>1.6.7</staging-plugin.version>
3738
<release-plugin.version>2.5.3</release-plugin.version>
@@ -68,6 +69,11 @@
6869
<artifactId>derby</artifactId>
6970
<version>${apache-derby.version}</version>
7071
</dependency>
72+
<dependency>
73+
<groupId>com.google.guava</groupId>
74+
<artifactId>guava</artifactId>
75+
<version>${guava.version}</version>
76+
</dependency>
7177
</dependencies>
7278
</dependencyManagement>
7379

@@ -82,6 +88,10 @@
8288
<artifactId>derby</artifactId>
8389
<scope>test</scope>
8490
</dependency>
91+
<dependency>
92+
<groupId>com.google.guava</groupId>
93+
<artifactId>guava</artifactId>
94+
</dependency>
8595
</dependencies>
8696

8797
<build>
@@ -197,5 +207,5 @@
197207
</properties>
198208
</profile>
199209
</profiles>
200-
<version>1.8.1-SNAPSHOT</version>
210+
<version>1.9.0-SNAPSHOT</version>
201211
</project>

src/main/java/com/nordstrom/common/file/PathUtils.java

Lines changed: 73 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
import java.io.File;
44
import java.io.IOException;
55
import java.nio.file.FileSystems;
6+
import java.nio.file.FileVisitOption;
7+
import java.nio.file.FileVisitResult;
8+
import java.nio.file.FileVisitor;
69
import java.nio.file.Files;
710
import java.nio.file.Path;
811
import java.nio.file.PathMatcher;
912
import java.nio.file.Paths;
10-
import java.util.Comparator;
13+
import java.nio.file.attribute.BasicFileAttributes;
14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.EnumSet;
17+
import java.util.List;
1118
import java.util.Objects;
12-
import java.util.Optional;
13-
import java.util.stream.Stream;
1419

1520
/**
1621
* This utility class provides a {@link #getNextPath(Path, String, String) getNextPath} method to acquire the next file
@@ -151,8 +156,6 @@ private static Path getTargetPath() {
151156
* @throws IOException if an I/O error is thrown when accessing the starting file.
152157
*/
153158
public static Path getNextPath(Path targetPath, String baseName, String extension) throws IOException {
154-
String newName;
155-
156159
Objects.requireNonNull(targetPath, "[targetPath] must be non-null");
157160
Objects.requireNonNull(baseName, "[baseName] must be non-null");
158161
Objects.requireNonNull(extension, "[extension] must be non-null");
@@ -168,41 +171,75 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio
168171
throw new IllegalArgumentException("[extension] must specify a non-empty string");
169172
}
170173

171-
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("regex:\\Q" + baseName + "\\E(-\\d+)?\\." + extension);
174+
Visitor visitor = new Visitor(baseName, extension);
175+
Files.walkFileTree(targetPath, EnumSet.noneOf(FileVisitOption.class), 1, visitor);
172176

173-
try (Stream<Path> stream = Files.walk(targetPath, 1)) {
174-
int base = baseName.length();
175-
int ext = extension.length() + 1;
176-
177-
Optional<Integer> optional =
178-
stream
179-
.map(Path::getFileName)
180-
.filter(pathMatcher::matches)
181-
.map(String::valueOf)
182-
.map(s -> "0" + s.substring(base, s.length() - ext))
183-
.map(s -> s.replace("0-", ""))
184-
.map(Integer::valueOf)
185-
.map(i -> i + 1)
186-
.sorted(Comparator.reverseOrder())
187-
.findFirst();
177+
return targetPath.resolve(visitor.getNewName());
178+
}
179+
180+
/**
181+
* Get project base directory.
182+
*
183+
* @return project base directory
184+
*/
185+
public static String getBaseDir() {
186+
Path currentRelativePath = Paths.get(System.getProperty("user.dir"));
187+
return currentRelativePath.toAbsolutePath().toString();
188+
}
189+
190+
private static class Visitor implements FileVisitor<Path> {
191+
192+
private String baseName;
193+
private String extension;
194+
private int base, ext;
195+
private PathMatcher pathMatcher;
196+
private List<Integer> intList = new ArrayList<>();
197+
198+
Visitor(String baseName, String extension) {
199+
this.baseName = baseName;
200+
this.extension = extension;
201+
this.base = baseName.length();
202+
this.ext = extension.length() + 1;
203+
this.pathMatcher = FileSystems.getDefault().getPathMatcher("regex:\\Q" + baseName + "\\E(-\\d+)?\\." + extension);
204+
}
205+
206+
@Override
207+
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
208+
return FileVisitResult.CONTINUE;
209+
}
210+
211+
@Override
212+
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
213+
if (attrs.isRegularFile() && pathMatcher.matches(file.getFileName())) {
214+
String name = file.getFileName().toString();
215+
String iStr = "0" + name.substring(base, name.length() - ext);
216+
iStr = iStr.replace("0-", "");
217+
intList.add(Integer.valueOf(iStr) + 1);
218+
}
219+
return FileVisitResult.CONTINUE;
220+
}
221+
222+
@Override
223+
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
224+
return FileVisitResult.CONTINUE;
225+
}
226+
227+
@Override
228+
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
229+
return FileVisitResult.CONTINUE;
230+
}
231+
232+
public String getNewName() {
233+
String newName;
188234

189-
if (optional.isPresent()) {
190-
newName = baseName + "-" + optional.get() + "." + extension;
191-
} else {
235+
if (intList.isEmpty()) {
192236
newName = baseName + "." + extension;
237+
} else {
238+
Collections.sort(intList, Collections.reverseOrder());
239+
newName = baseName + "-" + intList.get(0) + "." + extension;
193240
}
241+
242+
return newName;
194243
}
195-
196-
return targetPath.resolve(newName);
197244
}
198-
199-
/**
200-
* Get project base directory.
201-
*
202-
* @return project base directory
203-
*/
204-
public static String getBaseDir() {
205-
Path currentRelativePath = Paths.get(System.getProperty("user.dir"));
206-
return currentRelativePath.toAbsolutePath().toString();
207-
}
208245
}

src/main/java/com/nordstrom/common/params/Params.java

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import java.util.Collections;
44
import java.util.HashMap;
55
import java.util.Map;
6-
import java.util.Optional;
6+
7+
import com.google.common.base.Optional;
78

89
/**
910
* This interface enables implementers to provide methods to support for concisely-defined parameters.
@@ -15,37 +16,7 @@ public interface Params {
1516
*
1617
* @return optional map of named parameters
1718
*/
18-
default Optional<Map<String, Object>> getParameters() {
19-
return Optional.empty();
20-
}
21-
22-
/**
23-
* Assemble a map of parameters.
24-
*
25-
* @param params array of {@link Param} objects; may be {@code null} or empty
26-
* @return optional map of parameters (may be empty)
27-
*/
28-
public static Optional<Map<String, Object>> mapOf(Param... params) {
29-
if ((params == null) || (params.length == 0)) {
30-
return Optional.empty();
31-
}
32-
Map<String, Object> paramMap = new HashMap<>();
33-
for (Param param : params) {
34-
paramMap.put(param.key, param.val);
35-
}
36-
return Optional.of(Collections.unmodifiableMap(paramMap));
37-
}
38-
39-
/**
40-
* Create a parameter object for the specified key/value pair.
41-
*
42-
* @param key parameter key (name)
43-
* @param val parameter value
44-
* @return parameter object
45-
*/
46-
public static Param param(String key, Object val) {
47-
return new Param(key, val);
48-
}
19+
Optional<Map<String, Object>> getParameters();
4920

5021
/**
5122
* This class defines a parameter object.
@@ -86,5 +57,33 @@ public String getKey() {
8657
public Object getVal() {
8758
return val;
8859
}
60+
61+
/**
62+
* Assemble a map of parameters.
63+
*
64+
* @param params array of {@link Param} objects; may be {@code null} or empty
65+
* @return optional map of parameters (may be empty)
66+
*/
67+
public static Optional<Map<String, Object>> mapOf(Param... params) {
68+
if ((params == null) || (params.length == 0)) {
69+
return Optional.absent();
70+
}
71+
Map<String, Object> paramMap = new HashMap<>();
72+
for (Param param : params) {
73+
paramMap.put(param.key, param.val);
74+
}
75+
return Optional.of(Collections.unmodifiableMap(paramMap));
76+
}
77+
78+
/**
79+
* Create a parameter object for the specified key/value pair.
80+
*
81+
* @param key parameter key (name)
82+
* @param val parameter value
83+
* @return parameter object
84+
*/
85+
public static Param param(String key, Object val) {
86+
return new Param(key, val);
87+
}
8988
}
9089
}

src/test/java/com/nordstrom/common/file/VolumeInfoTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import org.testng.annotations.Test;
77

8+
import com.google.common.base.Joiner;
89
import com.nordstrom.common.file.VolumeInfo.VolumeProps;
910

1011
public class VolumeInfoTest {
@@ -15,7 +16,7 @@ public void test() throws IOException {
1516
for (VolumeProps thisProps : propsList.values()) {
1617
System.out.println("file: " + thisProps.getFile());
1718
System.out.println("type: " + thisProps.getType());
18-
System.out.println("opts: " + String.join(",", thisProps.getOpts()));
19+
System.out.println("opts: " + Joiner.on(",").join(thisProps.getOpts()));
1920
System.out.println("size: " + thisProps.getSize());
2021
System.out.println("free: " + thisProps.getFree());
2122
System.out.println("");

src/test/java/com/nordstrom/common/params/ParamTest.java

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,32 @@
11
package com.nordstrom.common.params;
22

3-
import static com.nordstrom.common.params.Params.param;
43
import static org.testng.Assert.assertEquals;
54
import static org.testng.Assert.assertFalse;
65
import static org.testng.Assert.assertTrue;
76

87
import java.util.Map;
9-
import java.util.Optional;
108

119
import org.testng.annotations.Test;
1210

11+
import com.google.common.base.Optional;
12+
1313
public class ParamTest implements Params {
1414

15-
@Test
16-
public void testDefault() {
17-
assertFalse(Params.super.getParameters().isPresent());
18-
}
19-
2015
@Test
2116
public void testParam() {
22-
Param param = param("boolean", true);
17+
Param param = Param.param("boolean", true);
2318
assertEquals(param.getKey(), "boolean");
2419
verifyBoolean(param.getVal());
2520

26-
param = param("int", 1);
21+
param = Param.param("int", 1);
2722
assertEquals(param.getKey(), "int");
2823
verifyInt(param.getVal());
2924

30-
param = param("String", "one");
25+
param = Param.param("String", "one");
3126
assertEquals(param.getKey(), "String");
3227
verifyString(param.getVal());
3328

34-
param = param("Map", Params.mapOf(param("key", "value")));
29+
param = Param.param("Map", Param.mapOf(Param.param("key", "value")));
3530
assertEquals(param.getKey(), "Map");
3631
verifyMap(param.getVal());
3732
}
@@ -84,7 +79,7 @@ public void verifyMap(Object value) {
8479

8580
@Override
8681
public Optional<Map<String, Object>> getParameters() {
87-
return Params.mapOf(param("boolean", true), param("int", 1), param("String", "one"),
88-
param("Map", Params.mapOf(param("key", "value"))));
82+
return Param.mapOf(Param.param("boolean", true), Param.param("int", 1), Param.param("String", "one"),
83+
Param.param("Map", Param.mapOf(Param.param("key", "value"))));
8984
}
9085
}

0 commit comments

Comments
 (0)