Skip to content

Commit 4d7b823

Browse files
committed
Post current revisions to get Unix values
1 parent 5184186 commit 4d7b823

File tree

3 files changed

+166
-1
lines changed

3 files changed

+166
-1
lines changed
Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package com.nordstrom.common.file;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
import java.util.ListIterator;
7+
import java.util.Map;
8+
import java.util.Map.Entry;
9+
10+
/**
11+
* This class provides utility methods and abstractions for host operating system features.
12+
*
13+
* @param <T> an operating system mapping enumeration that implements the {@link OSProps} interface
14+
*/
15+
public class OSUtils<T extends Enum<T> & OSUtils.OSProps> {
16+
17+
private static String osName = System.getProperty("os.name");
18+
private static String version = System.getProperty("os.version");
19+
private static String arch = System.getProperty("os.arch");
20+
21+
private final Map<T, String> typeMap = new LinkedHashMap<>();
22+
23+
/**
24+
* Get an object that supports the set of operating systems defined in the {@link OSType} enumeration.
25+
*
26+
* @return OSUtils object that supports the operating systems defined in {@link OSType}
27+
*/
28+
public static OSUtils<OSType> getDefault() {
29+
return new OSUtils<>(OSType.class);
30+
}
31+
32+
/**
33+
* Create an object that supports the mappings defined by the specified enumeration.
34+
*
35+
* @param enumClass operating system mapping enumeration
36+
*/
37+
public OSUtils(Class<T> enumClass) {
38+
putAll(enumClass);
39+
}
40+
41+
/**
42+
* Get the enumerated type constant for the active operating system.
43+
*
44+
* @return OS type constant; if no match, returns 'null'
45+
*/
46+
public T getType() {
47+
// populate a linked list with the entries of the linked type map
48+
List<Entry<T, String>> entryList = new LinkedList<>(typeMap.entrySet());
49+
// get a list iterator, setting the cursor at the tail end
50+
ListIterator<Entry<T, String>> iterator = entryList.listIterator(entryList.size());
51+
// iterate from last to first
52+
while (iterator.hasPrevious()) {
53+
Entry<T, String> thisEntry = iterator.previous();
54+
if (osName.matches(thisEntry.getValue())) {
55+
return thisEntry.getKey();
56+
}
57+
}
58+
return null;
59+
}
60+
61+
/**
62+
* Add the specified mapping to the collection.<br>
63+
* <b>NOTE</b>: If a mapping for the specified constant already exists, this mapping will be replaced.
64+
*
65+
* @param typeConst OS type constant
66+
* @param pattern OS name match pattern
67+
* @return value of previous mapping; 'null' if no mapping existed
68+
*/
69+
public String put(T typeConst, String pattern) {
70+
return typeMap.put(typeConst, pattern);
71+
}
72+
73+
/**
74+
* Add the mappings defined by the specified enumeration to the collection.<br>
75+
* <b>NOTE</b>: If any of the specified mappings already exist, the previous mappings will be replaced.
76+
*
77+
* @param enumClass operating system mapping enumeration
78+
*/
79+
public void putAll(Class<T> enumClass) {
80+
for (T typeConst : enumClass.getEnumConstants()) {
81+
typeMap.put(typeConst, typeConst.pattern());
82+
}
83+
}
84+
85+
/**
86+
* Get the name of the active operating system.
87+
*
88+
* @return name of the active operating system
89+
*/
90+
public static String osName() {
91+
return osName;
92+
}
93+
94+
/**
95+
* Get the version of the existing operating system.
96+
*
97+
* @return version of the existing operating system
98+
*/
99+
public static String version() {
100+
return version;
101+
}
102+
103+
/**
104+
* Get the architecture of the active operating system.
105+
*
106+
* @return architecture of the active operating system
107+
*/
108+
public static String arch() {
109+
return arch;
110+
}
111+
112+
/**
113+
* This enumeration defines the default set of operating system mappings.
114+
*/
115+
public enum OSType implements OSProps {
116+
WINDOWS("(?i).*win.*"),
117+
MACINTOSH("(?i).*mac.*"),
118+
UNIX("(?i).*(?:nix|nux|aix).*"),
119+
SOLARIS("(?i).*sunos.*");
120+
121+
OSType(String pattern) {
122+
this.pattern = pattern;
123+
}
124+
125+
private String pattern;
126+
127+
@Override
128+
public String pattern() {
129+
return pattern;
130+
}
131+
}
132+
133+
/**
134+
* This interface defines the required contract for operating system mapping enumerations.
135+
*/
136+
public interface OSProps {
137+
138+
/**
139+
* Get the OS name match pattern for this mapping.
140+
*
141+
* @return OS name match pattern
142+
*/
143+
String pattern();
144+
145+
}
146+
147+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ public static Path getNextPath(Path targetPath, String baseName, String extensio
171171
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("regex:" + baseName + "(-\\d+)?\\." + extension);
172172

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

177177
Optional<Integer> optional =
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.nordstrom.common.file;
2+
3+
import org.testng.annotations.Test;
4+
5+
import com.nordstrom.common.file.OSUtils.OSType;
6+
7+
public class OSUtilsTest {
8+
9+
@Test
10+
public void testDefault() {
11+
OSUtils<OSType> osUtils = OSUtils.getDefault();
12+
System.out.println(osUtils.getType());
13+
System.out.println(OSUtils.osName());
14+
System.out.println(OSUtils.version());
15+
System.out.println(OSUtils.arch());
16+
}
17+
18+
}

0 commit comments

Comments
 (0)