Skip to content

Commit 38b0150

Browse files
javaqueryVicky Thakor
authored andcommitted
fix: JFile added
1 parent b837f70 commit 38b0150

File tree

2 files changed

+201
-0
lines changed

2 files changed

+201
-0
lines changed
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package com.javaquery.util.io;
2+
3+
import com.javaquery.util.Assert;
4+
import com.javaquery.util.collection.Collections;
5+
import com.javaquery.util.string.Strings;
6+
7+
import java.io.File;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
/**
12+
* @author vicky.thakor
13+
* @since 1.0.3
14+
*/
15+
public final class JFile extends File {
16+
17+
private final Map<String, String> attributes;
18+
19+
public JFile(String pathname) {
20+
super(pathname);
21+
attributes = new HashMap<>();
22+
}
23+
24+
/**
25+
* @return file extension
26+
*/
27+
public String getExtension(){
28+
String name = getName();
29+
if(Strings.nonNullNonEmpty(name)){
30+
int lastIndexOfDot = name.lastIndexOf(".");
31+
if(lastIndexOfDot > 0){
32+
return name.substring(lastIndexOfDot + 1);
33+
}
34+
}
35+
return "";
36+
}
37+
38+
/**
39+
* @param name a name to change for current file
40+
* @return renamed file object if and only if the renaming succeeded;
41+
* <code>null</code> otherwise
42+
*
43+
* @throws SecurityException
44+
* If a security manager exists and its <code>{@link
45+
* java.lang.SecurityManager#checkWrite(java.lang.String)}</code>
46+
* method denies write access to either the old or new pathnames
47+
*
48+
* @throws NullPointerException
49+
* If parameter <code>dest</code> is <code>null</code>
50+
*/
51+
public final JFile rename(String name){
52+
Assert.nonNull(name, NullPointerException::new);
53+
if(exists()){
54+
boolean result = renameTo(new File(getParent() + File.separatorChar + name));
55+
if(result){
56+
return new JFile(getParent() + File.separatorChar + name);
57+
}
58+
}
59+
return null;
60+
}
61+
62+
/**
63+
* @return String content of this file.
64+
*/
65+
public final String read(){
66+
return Files.readFromFile(this);
67+
}
68+
69+
/**
70+
* @param data data to write to this file
71+
*/
72+
public final void write(String data) {
73+
Files.writeToFile(this, data);
74+
}
75+
76+
/**
77+
* @param data to append to this file
78+
* @param appendNewLine <code>true</code> to append new line at the end of data
79+
* otherwise <code>false</code>
80+
*/
81+
public final void append(String data, boolean appendNewLine) {
82+
Files.appendToFile(this, data, appendNewLine);
83+
}
84+
85+
/**
86+
* @return attributes of file
87+
*/
88+
public final Map<String, String> getAttributes() {
89+
return attributes;
90+
}
91+
92+
/**
93+
* @param key a key of attribute
94+
* @param value a value of attribute
95+
*/
96+
public final void addAttribute(String key, String value){
97+
attributes.put(key, value);
98+
}
99+
100+
/**
101+
* @param attributes add attributes map to file
102+
*/
103+
public final void addAllAttribute(Map<String, String> attributes){
104+
if(Collections.nonNullNonEmpty(attributes)){
105+
this.attributes.putAll(attributes);
106+
}
107+
}
108+
109+
/**
110+
* @param key a key to find attribute
111+
* @param defaultValue default value in case attribute not found
112+
* @return attribute value if found otherwise defaultValue
113+
*/
114+
public final String optAttribute(String key, String defaultValue){
115+
return attributes.getOrDefault(key, defaultValue);
116+
}
117+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.javaquery.util.io;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.io.File;
7+
import java.io.IOException;
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
import java.util.UUID;
11+
12+
/**
13+
* @author vicky.thakor
14+
* @since 1.0.3
15+
*/
16+
public class TestJFile {
17+
18+
private static final String TEMP_DIR = System.getProperty("java.io.tmpdir");
19+
20+
@Test
21+
public void test_getExtension(){
22+
JFile jFile = new JFile(TEMP_DIR + File.separatorChar + randomFileName(".txt"));
23+
Assertions.assertEquals("txt", jFile.getExtension());
24+
}
25+
26+
@Test
27+
public void test_getExtension_1(){
28+
JFile jFile = new JFile(TEMP_DIR + File.separatorChar + randomFileName(null));
29+
Assertions.assertEquals("", jFile.getExtension());
30+
}
31+
32+
@Test
33+
public void test_rename() throws IOException {
34+
String renameFile = "hello-world.log";
35+
JFile jFile = new JFile(TEMP_DIR + File.separatorChar + randomFileName(".txt"));
36+
if(jFile.createNewFile()){
37+
jFile = jFile.rename(renameFile);
38+
Assertions.assertEquals(renameFile, jFile.getName());
39+
}
40+
jFile.delete();
41+
}
42+
43+
@Test
44+
public void test_rename_1() {
45+
JFile jFile = new JFile(TEMP_DIR + File.separatorChar + randomFileName(".txt"));
46+
Assertions.assertThrows(NullPointerException.class, () -> jFile.rename(null));
47+
}
48+
49+
@Test
50+
public void test_rename_2() {
51+
JFile jFile = new JFile(TEMP_DIR + File.separatorChar + randomFileName(".txt"));
52+
Assertions.assertNull(jFile.rename("hello-world.log"));
53+
}
54+
55+
@Test
56+
public void test_write_read_append() {
57+
JFile jFile = new JFile(TEMP_DIR + File.separatorChar + randomFileName(".txt"));
58+
jFile.write("hello");
59+
60+
String data = jFile.read();
61+
Assertions.assertEquals("hello", data);
62+
63+
jFile.append("world!", false);
64+
String appendedData = jFile.read();
65+
Assertions.assertEquals("helloworld!", appendedData);
66+
jFile.delete();
67+
}
68+
69+
@Test
70+
public void test_attributes() {
71+
JFile jFile = new JFile(TEMP_DIR + File.separatorChar + randomFileName(".txt"));
72+
jFile.addAttribute("hello", "world");
73+
Assertions.assertEquals("world", jFile.optAttribute("hello", "oh snap!"));
74+
75+
Map<String, String> map = new HashMap<>(1);
76+
map.put("1", "2");
77+
jFile.addAllAttribute(map);
78+
Assertions.assertEquals(2, jFile.getAttributes().size());
79+
}
80+
81+
private String randomFileName(String suffix){
82+
return UUID.randomUUID().toString().replace("-", "") + suffix;
83+
}
84+
}

0 commit comments

Comments
 (0)