Skip to content

Commit 94a0aef

Browse files
javaqueryVicky Thakor
authored andcommitted
feat: File util
1 parent cc796a1 commit 94a0aef

File tree

13 files changed

+412
-13
lines changed

13 files changed

+412
-13
lines changed

build.gradle

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ repositories {
1212
}
1313

1414
dependencies {
15-
testCompile('org.junit.jupiter:junit-jupiter:5.7.0')
15+
implementation('org.slf4j:slf4j-api:1.7.30')
16+
testImplementation('org.junit.jupiter:junit-jupiter:5.7.0')
1617
}
1718

1819
test {
@@ -71,7 +72,7 @@ publishing {
7172
url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
7273
credentials {
7374
username "javaquery"
74-
password "a2a@\$ASDF#777"
75+
password "******"
7576
}
7677
}
7778
}

src/main/java/com/javaquery/util/Assert.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*/
1515
public final class Assert {
1616

17-
private Assert() {}
17+
private Assert() {
18+
}
1819

1920
/**
2021
* <em>Assert</em> that {@code object} is {@code not null}.

src/main/java/com/javaquery/util/Objects.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
*/
77
public final class Objects {
88

9-
private Objects(){}
9+
private Objects() {
10+
}
1011

1112
/**
1213
* Returns {@code true} if the provided reference is {@code null} otherwise

src/main/java/com/javaquery/util/Regex.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
*/
99
public final class Regex {
1010

11-
private Regex(){}
12-
1311
public static final String REGEX_NUMBER = "^-?[0-9]\\d*(\\.\\d+)?$";
1412
public static final String REGEX_ALPHA_NUMERIC = "^[a-zA-Z0-9]*$";
1513
public static final String REGEX_EMAIL =
1614
"^[a-zA-Z0-9.!#$%&'*+\\/=?^_`{|}~-]+@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))[^_@!*]*?$";
17-
1815
public static final Pattern EMAIL_PATTERN = Pattern.compile(REGEX_EMAIL);
1916

17+
private Regex() {
18+
}
19+
2020
/**
2121
* Returns {@code true} if the provided String is valid number otherwise
2222
* returns {@code false}.

src/main/java/com/javaquery/util/collection/Collections.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
*/
1515
public final class Collections {
1616

17-
private Collections(){}
17+
private Collections() {
18+
}
1819

1920
/**
2021
* Returns {@code true} if the provided Collection [List, Set] is {@code null} or empty otherwise
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package com.javaquery.util.io;
2+
3+
import com.javaquery.util.Assert;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
7+
import java.io.BufferedReader;
8+
import java.io.File;
9+
import java.io.IOException;
10+
import java.io.InputStream;
11+
import java.io.InputStreamReader;
12+
import java.nio.file.InvalidPathException;
13+
import java.nio.file.Path;
14+
import java.nio.file.Paths;
15+
import java.nio.file.StandardOpenOption;
16+
17+
/**
18+
* @author vicky.thakor
19+
* @since 1.0
20+
*/
21+
public final class Files {
22+
23+
private static final Logger LOGGER = LoggerFactory.getLogger(Files.class);
24+
25+
/**
26+
* Create new, empty file at specified path in {@link File} object.
27+
* This method will also creates folder structure if not exists.
28+
*
29+
* Note: Exception is logged not thrown.
30+
*
31+
* @param file - file to create
32+
* @param <T>
33+
* @return <code>true</code> if the named file does not exist and was
34+
* successfully created; <code>false</code> if the named file
35+
* already exists
36+
*/
37+
public static <T extends File> boolean createNewFile(T file) {
38+
Assert.nonNull(file, NullPointerException::new);
39+
if (!file.exists()) {
40+
file.getParentFile().mkdirs();
41+
try {
42+
return file.createNewFile();
43+
} catch (IOException e) {
44+
LOGGER.error(e.getMessage(), e);
45+
}
46+
}
47+
return false;
48+
}
49+
50+
/**
51+
* Delete existing file and then Create new, empty file at
52+
* specified path in {@link File} object.
53+
*
54+
* Note: Exception is logged not thrown.
55+
*
56+
* @param file - file to delete and create
57+
* @param <T>
58+
* @return <code>true</code> if the named file deleted and was
59+
* successfully created; <code>false</code> if the named file
60+
* already exists
61+
*/
62+
public static <T extends File> boolean deleteAndCreateNewFile(T file){
63+
Assert.nonNull(file, NullPointerException::new);
64+
if(file.exists()){
65+
file.delete();
66+
}
67+
return createNewFile(file);
68+
}
69+
70+
/**
71+
* Write data to provided file.
72+
*
73+
* Note: This method will also creates new <code>file</code> if not exist.
74+
* Exception is logged not thrown.
75+
*
76+
* @param file file to write
77+
* @param data data to write to file
78+
* @param <T>
79+
*/
80+
public static <T extends File> void writeToFile(T file, String data){
81+
Assert.nonNull(file, NullPointerException::new);
82+
Assert.nonNull(data, NullPointerException::new);
83+
if(!file.exists()){
84+
createNewFile(file);
85+
}
86+
try {
87+
java.nio.file.Files.write(getPath(file), data.getBytes());
88+
} catch (IOException | InvalidPathException e) {
89+
LOGGER.error(e.getMessage(), e);
90+
}
91+
}
92+
93+
/**
94+
* Append data to provided <code>file</code>
95+
*
96+
* Note: This method will also creates new <code>file</code> if not exist.
97+
* Exception is logged not thrown.
98+
*
99+
* @param file file to write
100+
* @param data data to append to file
101+
* @param appendNewLine <code>true</code> to append new line at the end of data
102+
* otherwise <code>false</code>.
103+
* @param <T>
104+
*/
105+
public static <T extends File> void appendToFile(T file, String data, boolean appendNewLine){
106+
Assert.nonNull(file, NullPointerException::new);
107+
Assert.nonNull(data, NullPointerException::new);
108+
if(!file.exists()){
109+
createNewFile(file);
110+
}
111+
data = appendNewLine ? (data + "\n") : data;
112+
try {
113+
java.nio.file.Files.write(getPath(file), data.getBytes(), StandardOpenOption.APPEND);
114+
} catch (IOException | InvalidPathException e) {
115+
LOGGER.error(e.getMessage(), e);
116+
}
117+
}
118+
119+
/**
120+
* Read <code>String</code> content of <code>file</code>.
121+
*
122+
* Note: Exception is logged not thrown.
123+
*
124+
* @param file file to read
125+
* @param <T>
126+
* @return String data of file if exists otherwise <code>null</code>
127+
*/
128+
public static <T extends File> String readFromFile(T file){
129+
Assert.nonNull(file, NullPointerException::new);
130+
if(file.exists()){
131+
try {
132+
return new String(java.nio.file.Files.readAllBytes(getPath(file)));
133+
} catch (IOException | InvalidPathException e) {
134+
LOGGER.error(e.getMessage(), e);
135+
}
136+
}
137+
return null;
138+
}
139+
140+
/**
141+
* Read any file from resources folder of project.
142+
* For example, read demo.json from provided path `resources folder` <code>/sample/demo.json</code>
143+
*
144+
* @param path path to resource
145+
* @return String data of file if exists otherwise <code>null</code>
146+
*/
147+
public String loadResource(String path){
148+
Assert.nonNull(path, NullPointerException::new);
149+
if(!path.trim().isEmpty()){
150+
try(
151+
InputStream inputStream = getClass().getResourceAsStream(path);
152+
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream))
153+
){
154+
String fileLine;
155+
StringBuilder stringBuilder = new StringBuilder();
156+
157+
while((fileLine = bufferedReader.readLine())!= null){
158+
stringBuilder.append(fileLine);
159+
}
160+
return stringBuilder.toString();
161+
} catch (IOException | NullPointerException e) {
162+
LOGGER.error(e.getMessage(), e);
163+
}
164+
}
165+
return null;
166+
}
167+
168+
/**
169+
* Path of file provided
170+
*
171+
* @param file file to get {@link Path}.
172+
* @param <T>
173+
* @return {@link Path} of file
174+
*/
175+
private static <T extends File> Path getPath(T file){
176+
return Paths.get(file.getAbsolutePath());
177+
}
178+
}

src/main/java/com/javaquery/util/string/Strings.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
*/
1313
public final class Strings {
1414

15-
private Strings(){}
16-
1715
private final static String UNSUPPORTED_ASCII_PATTERN = "[^\\x20-\\x7e]";
1816
private final static String UNSUPPORTED_UNICODE_PATTERN = "[\\uD83C-\\uDBFF\\uDC00-\\uDFFF]+";
17+
private Strings() {
18+
}
1919

2020
/**
2121
* Returns {@code true} if the provided String is {@code null} or empty otherwise
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.javaquery.util.time;
2+
3+
import java.util.Date;
4+
5+
/**
6+
* @author vicky.thakor
7+
* @since 1.0
8+
*/
9+
public final class DateRange {
10+
11+
private Date startDate;
12+
private Date endDate;
13+
14+
private DateRange() {
15+
}
16+
17+
public DateRange(Date startDate, Date endDate) {
18+
this.startDate = startDate;
19+
this.endDate = endDate;
20+
}
21+
22+
public DateRange(Date startDate) {
23+
this(startDate, new Date());
24+
}
25+
26+
public Date getStartDate() {
27+
return startDate;
28+
}
29+
30+
public Date getEndDate() {
31+
return endDate;
32+
}
33+
}

src/main/java/com/javaquery/util/time/Dates.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
*/
1515
public final class Dates {
1616

17-
private Dates(){}
18-
1917
public static final TimeZone SYSTEM_TIMEZONE = TimeZone.getDefault();
2018
private static final Calendar CALENDAR = Calendar.getInstance();
19+
private Dates() {
20+
}
2121

2222
/**
2323
* @return current time in milliseconds by using System.currentTimeMillis()
@@ -223,13 +223,14 @@ public enum Month {
223223
11), DECEMBER(12);
224224

225225
private int value;
226+
private final static Calendar calendar = Calendar.getInstance();
226227

227228
Month(int value) {
228229
this.value = value;
229230
}
230231

231232
public static Month currentMonth() {
232-
return Month.values()[CALENDAR.get(Calendar.MONTH)];
233+
return Month.values()[calendar.get(Calendar.MONTH)];
233234
}
234235

235236
public int getValue() {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name": "vicky thakor"}

0 commit comments

Comments
 (0)