Skip to content

Pr/support java 7 #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<testng.version>6.11</testng.version>
<surefire-plugin.version>2.19.1</surefire-plugin.version>
<source-plugin.version>3.0.1</source-plugin.version>
<javadoc-plugin.version>2.10.4</javadoc-plugin.version>
<guava.version>23.5-jre</guava.version>
<gpg-plugin.version>1.6</gpg-plugin.version>
<staging-plugin.version>1.6.7</staging-plugin.version>
<release-plugin.version>2.5.3</release-plugin.version>
Expand Down Expand Up @@ -68,6 +69,11 @@
<artifactId>derby</artifactId>
<version>${apache-derby.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${guava.version}</version>
</dependency>
</dependencies>
</dependencyManagement>

Expand All @@ -82,6 +88,10 @@
<artifactId>derby</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -197,5 +207,5 @@
</properties>
</profile>
</profiles>
<version>1.8.1-SNAPSHOT</version>
<version>1.9.0-SNAPSHOT</version>
</project>
109 changes: 73 additions & 36 deletions src/main/java/com/nordstrom/common/file/PathUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.FileVisitor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.util.Comparator;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

/**
* This utility class provides a {@link #getNextPath(Path, String, String) getNextPath} method to acquire the next file
Expand Down Expand Up @@ -151,8 +156,6 @@ private static Path getTargetPath() {
* @throws IOException if an I/O error is thrown when accessing the starting file.
*/
public static Path getNextPath(Path targetPath, String baseName, String extension) throws IOException {
String newName;

Objects.requireNonNull(targetPath, "[targetPath] must be non-null");
Objects.requireNonNull(baseName, "[baseName] must be non-null");
Objects.requireNonNull(extension, "[extension] must be non-null");
Expand All @@ -168,41 +171,75 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio
throw new IllegalArgumentException("[extension] must specify a non-empty string");
}

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

try (Stream<Path> stream = Files.walk(targetPath, 1)) {
int base = baseName.length();
int ext = extension.length() + 1;

Optional<Integer> optional =
stream
.map(Path::getFileName)
.filter(pathMatcher::matches)
.map(String::valueOf)
.map(s -> "0" + s.substring(base, s.length() - ext))
.map(s -> s.replace("0-", ""))
.map(Integer::valueOf)
.map(i -> i + 1)
.sorted(Comparator.reverseOrder())
.findFirst();
return targetPath.resolve(visitor.getNewName());
}

/**
* Get project base directory.
*
* @return project base directory
*/
public static String getBaseDir() {
Path currentRelativePath = Paths.get(System.getProperty("user.dir"));
return currentRelativePath.toAbsolutePath().toString();
}

private static class Visitor implements FileVisitor<Path> {

private String baseName;
private String extension;
private int base, ext;
private PathMatcher pathMatcher;
private List<Integer> intList = new ArrayList<>();

Visitor(String baseName, String extension) {
this.baseName = baseName;
this.extension = extension;
this.base = baseName.length();
this.ext = extension.length() + 1;
this.pathMatcher = FileSystems.getDefault().getPathMatcher("regex:\\Q" + baseName + "\\E(-\\d+)?\\." + extension);
}

@Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (attrs.isRegularFile() && pathMatcher.matches(file.getFileName())) {
String name = file.getFileName().toString();
String iStr = "0" + name.substring(base, name.length() - ext);
iStr = iStr.replace("0-", "");
intList.add(Integer.valueOf(iStr) + 1);
}
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}

@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}

public String getNewName() {
String newName;

if (optional.isPresent()) {
newName = baseName + "-" + optional.get() + "." + extension;
} else {
if (intList.isEmpty()) {
newName = baseName + "." + extension;
} else {
Collections.sort(intList, Collections.reverseOrder());
newName = baseName + "-" + intList.get(0) + "." + extension;
}

return newName;
}

return targetPath.resolve(newName);
}

/**
* Get project base directory.
*
* @return project base directory
*/
public static String getBaseDir() {
Path currentRelativePath = Paths.get(System.getProperty("user.dir"));
return currentRelativePath.toAbsolutePath().toString();
}
}
63 changes: 31 additions & 32 deletions src/main/java/com/nordstrom/common/params/Params.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import com.google.common.base.Optional;

/**
* This interface enables implementers to provide methods to support for concisely-defined parameters.
Expand All @@ -15,37 +16,7 @@ public interface Params {
*
* @return optional map of named parameters
*/
default Optional<Map<String, Object>> getParameters() {
return Optional.empty();
}

/**
* Assemble a map of parameters.
*
* @param params array of {@link Param} objects; may be {@code null} or empty
* @return optional map of parameters (may be empty)
*/
public static Optional<Map<String, Object>> mapOf(Param... params) {
if ((params == null) || (params.length == 0)) {
return Optional.empty();
}
Map<String, Object> paramMap = new HashMap<>();
for (Param param : params) {
paramMap.put(param.key, param.val);
}
return Optional.of(Collections.unmodifiableMap(paramMap));
}

/**
* Create a parameter object for the specified key/value pair.
*
* @param key parameter key (name)
* @param val parameter value
* @return parameter object
*/
public static Param param(String key, Object val) {
return new Param(key, val);
}
Optional<Map<String, Object>> getParameters();

/**
* This class defines a parameter object.
Expand Down Expand Up @@ -86,5 +57,33 @@ public String getKey() {
public Object getVal() {
return val;
}

/**
* Assemble a map of parameters.
*
* @param params array of {@link Param} objects; may be {@code null} or empty
* @return optional map of parameters (may be empty)
*/
public static Optional<Map<String, Object>> mapOf(Param... params) {
if ((params == null) || (params.length == 0)) {
return Optional.absent();
}
Map<String, Object> paramMap = new HashMap<>();
for (Param param : params) {
paramMap.put(param.key, param.val);
}
return Optional.of(Collections.unmodifiableMap(paramMap));
}

/**
* Create a parameter object for the specified key/value pair.
*
* @param key parameter key (name)
* @param val parameter value
* @return parameter object
*/
public static Param param(String key, Object val) {
return new Param(key, val);
}
}
}
3 changes: 2 additions & 1 deletion src/test/java/com/nordstrom/common/file/VolumeInfoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import org.testng.annotations.Test;

import com.google.common.base.Joiner;
import com.nordstrom.common.file.VolumeInfo.VolumeProps;

public class VolumeInfoTest {
Expand All @@ -15,7 +16,7 @@ public void test() throws IOException {
for (VolumeProps thisProps : propsList.values()) {
System.out.println("file: " + thisProps.getFile());
System.out.println("type: " + thisProps.getType());
System.out.println("opts: " + String.join(",", thisProps.getOpts()));
System.out.println("opts: " + Joiner.on(",").join(thisProps.getOpts()));
System.out.println("size: " + thisProps.getSize());
System.out.println("free: " + thisProps.getFree());
System.out.println("");
Expand Down
21 changes: 8 additions & 13 deletions src/test/java/com/nordstrom/common/params/ParamTest.java
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
package com.nordstrom.common.params;

import static com.nordstrom.common.params.Params.param;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;

import java.util.Map;
import java.util.Optional;

import org.testng.annotations.Test;

import com.google.common.base.Optional;

public class ParamTest implements Params {

@Test
public void testDefault() {
assertFalse(Params.super.getParameters().isPresent());
}

@Test
public void testParam() {
Param param = param("boolean", true);
Param param = Param.param("boolean", true);
assertEquals(param.getKey(), "boolean");
verifyBoolean(param.getVal());

param = param("int", 1);
param = Param.param("int", 1);
assertEquals(param.getKey(), "int");
verifyInt(param.getVal());

param = param("String", "one");
param = Param.param("String", "one");
assertEquals(param.getKey(), "String");
verifyString(param.getVal());

param = param("Map", Params.mapOf(param("key", "value")));
param = Param.param("Map", Param.mapOf(Param.param("key", "value")));
assertEquals(param.getKey(), "Map");
verifyMap(param.getVal());
}
Expand Down Expand Up @@ -84,7 +79,7 @@ public void verifyMap(Object value) {

@Override
public Optional<Map<String, Object>> getParameters() {
return Params.mapOf(param("boolean", true), param("int", 1), param("String", "one"),
param("Map", Params.mapOf(param("key", "value"))));
return Param.mapOf(Param.param("boolean", true), Param.param("int", 1), Param.param("String", "one"),
Param.param("Map", Param.mapOf(Param.param("key", "value"))));
}
}