Skip to content

Commit 310ffb2

Browse files
committed
refactoring and test covering
1 parent 24978ac commit 310ffb2

10 files changed

Lines changed: 243 additions & 171 deletions

File tree

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
package com.ss.rlib.common.function;
22

3-
import org.jetbrains.annotations.Nullable;
4-
5-
/**
6-
* The function.
7-
*
8-
* @param <F> the type parameter
9-
* @param <S> the type parameter
10-
* @param <R> the type parameter
11-
* @author JavaSaBr
12-
*/
133
@FunctionalInterface
144
public interface SafeBiFunction<F, S, R> {
155

16-
/**
17-
* Apply r.
18-
*
19-
* @param first the first
20-
* @param second the second
21-
* @return the r
22-
* @throws Exception the exception
23-
*/
24-
@Nullable
25-
R apply(@Nullable F first, @Nullable S second) throws Exception;
6+
R apply(F first, S second) throws Exception;
267
}

rlib-common/src/main/java/com/ss/rlib/common/util/ArrayUtils.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public final class ArrayUtils {
3737
*
3838
* @param array the object integer array.
3939
* @return the primitive int array.
40+
* @since 9.2.1
4041
*/
4142
public static @NotNull int[] toIntArray(@NotNull Integer[] array) {
4243

@@ -60,6 +61,7 @@ public final class ArrayUtils {
6061
* @param regex the regex.
6162
* @return the primitive int array.
6263
* @throws NumberFormatException if some elements in the string are not an integer.
64+
* @since 9.2.1
6365
*/
6466
public static @NotNull int[] toIntArray(@NotNull String string, @NotNull String regex) {
6567

rlib-common/src/main/java/com/ss/rlib/common/util/FileUtils.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ public class FileUtils {
6969
private static final SimpleFileVisitor<Path> DELETE_FOLDER_VISITOR = new SimpleFileVisitor<>() {
7070

7171
@Override
72-
public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
72+
public FileVisitResult postVisitDirectory(@NotNull Path dir, @Nullable IOException exc) throws IOException {
7373
Files.delete(dir);
7474
return FileVisitResult.CONTINUE;
7575
}
7676

7777
@Override
78-
public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
78+
public FileVisitResult visitFile(@NotNull Path file, @NotNull BasicFileAttributes attrs) throws IOException {
7979
Files.delete(file);
8080
return FileVisitResult.CONTINUE;
8181
}
@@ -105,7 +105,7 @@ public static boolean isValidName(@Nullable String filename) {
105105
* @param filename the string with file name.
106106
* @return normalized file name.
107107
*/
108-
public static @NotNull String normalizeName(@Nullable final String filename) {
108+
public static @NotNull String normalizeName(@Nullable String filename) {
109109

110110
if (StringUtils.isEmpty(filename)) {
111111
return "_";
@@ -124,10 +124,10 @@ public static boolean isValidName(@Nullable String filename) {
124124
* @param extensions extensions filter.
125125
*/
126126
public static void addFilesTo(
127-
@NotNull Array<Path> container,
128-
@NotNull Path dir,
129-
boolean withFolders,
130-
@Nullable String... extensions
127+
@NotNull Array<Path> container,
128+
@NotNull Path dir,
129+
boolean withFolders,
130+
@Nullable String... extensions
131131
) {
132132

133133
if (Files.isDirectory(dir) && withFolders) {
@@ -139,8 +139,8 @@ public static void addFilesTo(
139139
return;
140140
}
141141

142-
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
143-
for (Path path : stream) {
142+
try (var stream = Files.newDirectoryStream(dir)) {
143+
for (var path : stream) {
144144
if (Files.isDirectory(path)) {
145145
addFilesTo(container, path, withFolders, extensions);
146146
} else if (extensions == null || extensions.length < 1 || containsExtensions(extensions, path.getFileName())) {
@@ -508,11 +508,11 @@ public static boolean hasExtension(@Nullable String path) {
508508
*/
509509
public static @NotNull String read(@NotNull InputStream in) {
510510

511-
StringBuilder content = new StringBuilder();
511+
var content = new StringBuilder();
512512

513-
try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
513+
try (var reader = new BufferedReader(new InputStreamReader(in))) {
514514

515-
CharBuffer buffer = CharBuffer.allocate(512);
515+
var buffer = CharBuffer.allocate(512);
516516

517517
while (reader.ready()) {
518518

@@ -660,7 +660,7 @@ public static void createDirectories(@NotNull Path directory, @NotNull FileAttri
660660
* @see Files#getLastModifiedTime(Path, LinkOption...)
661661
*/
662662
public static @NotNull FileTime getLastModifiedTime(@NotNull Path file, @NotNull LinkOption... options) {
663-
return notNull(Utils.safeGet(file, options, Files::getLastModifiedTime));
663+
return notNull(Utils.uncheckedGet(file, options, Files::getLastModifiedTime));
664664
}
665665

666666
/**
@@ -697,7 +697,7 @@ public static void createDirectories(@NotNull Path directory, @NotNull FileAttri
697697
* @see Path#relativize(Path)
698698
*/
699699
public static @NotNull Path relativize(@NotNull Path base, @NotNull Path other) {
700-
return Utils.get(base, other, Path::relativize);
700+
return Utils.uncheckedGet(base, other, Path::relativize);
701701
}
702702

703703
/**
@@ -707,7 +707,7 @@ public static void createDirectories(@NotNull Path directory, @NotNull FileAttri
707707
if (base == null || other == null) {
708708
return null;
709709
} else {
710-
return Utils.safeGet(base, other, Path::relativize);
710+
return Utils.uncheckedGet(base, other, Path::relativize);
711711
}
712712
}
713713

@@ -740,9 +740,9 @@ public static void createDirectories(@NotNull Path directory, @NotNull FileAttri
740740
* @see Files#createTempFile(String, String, FileAttribute[])
741741
*/
742742
public static @NotNull Path createTempFile(
743-
@NotNull String prefix,
744-
@NotNull String suffix,
745-
@Nullable FileAttribute<?>... attrs
743+
@NotNull String prefix,
744+
@NotNull String suffix,
745+
@Nullable FileAttribute<?>... attrs
746746
) {
747747
try {
748748
return Files.createTempFile(prefix, suffix, attrs);
@@ -760,8 +760,8 @@ public static void createDirectories(@NotNull Path directory, @NotNull FileAttri
760760
public static void forEach(@NotNull Path directory, @NotNull Consumer<@NotNull Path> consumer) {
761761
validateDirectory(directory);
762762

763-
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) {
764-
for (Path path : stream) {
763+
try (var stream = Files.newDirectoryStream(directory)) {
764+
for (var path : stream) {
765765
consumer.accept(path);
766766
}
767767
} catch (IOException e) {
@@ -786,14 +786,14 @@ private static void validateDirectory(@NotNull Path directory) {
786786
* @param <T> the argument's type.
787787
*/
788788
public static <T> void forEach(
789-
@NotNull Path directory,
790-
@NotNull T argument,
791-
@NotNull BiConsumer<@NotNull Path, @NotNull T> consumer
789+
@NotNull Path directory,
790+
@NotNull T argument,
791+
@NotNull BiConsumer<@NotNull Path, @NotNull T> consumer
792792
) {
793793
validateDirectory(directory);
794794

795-
try (DirectoryStream<Path> stream = Files.newDirectoryStream(directory)) {
796-
for (Path path : stream) {
795+
try (var stream = Files.newDirectoryStream(directory)) {
796+
for (var path : stream) {
797797
consumer.accept(path, argument);
798798
}
799799
} catch (IOException e) {
@@ -830,14 +830,14 @@ public static <T> void forEach(
830830
* @param <T> the argument's type.
831831
*/
832832
public static <T> void forEachR(
833-
@NotNull Path directory,
834-
@NotNull T argument,
835-
@NotNull BiConsumer<@NotNull T, @NotNull Path> consumer
833+
@NotNull Path directory,
834+
@NotNull T argument,
835+
@NotNull BiConsumer<@NotNull T, @NotNull Path> consumer
836836
) {
837837
validateDirectory(directory);
838838

839839
try (var stream = Files.newDirectoryStream(directory)) {
840-
for (Path path : stream) {
840+
for (var path : stream) {
841841
consumer.accept(argument, path);
842842
}
843843
} catch (IOException e) {
@@ -855,15 +855,15 @@ public static <T> void forEachR(
855855
* @param <T> the argument's type.
856856
*/
857857
public static <T> void forEachR(
858-
@NotNull Path directory,
859-
@NotNull T argument,
860-
@NotNull Predicate<@NotNull Path> condition,
861-
@NotNull BiConsumer<@NotNull T, @NotNull Path> consumer
858+
@NotNull Path directory,
859+
@NotNull T argument,
860+
@NotNull Predicate<@NotNull Path> condition,
861+
@NotNull BiConsumer<@NotNull T, @NotNull Path> consumer
862862
) {
863863
validateDirectory(directory);
864864

865865
try (var stream = Files.newDirectoryStream(directory)) {
866-
for (Path path : stream) {
866+
for (var path : stream) {
867867
if (condition.test(path)) {
868868
consumer.accept(argument, path);
869869
}

rlib-common/src/main/java/com/ss/rlib/common/util/IOUtils.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@ private static void toString(@NotNull StringBuilder result, @NotNull InputStream
9696
* @throws IOException the io exception
9797
*/
9898
public static void copy(
99-
@NotNull InputStream in,
100-
@NotNull OutputStream out,
101-
@NotNull byte[] buffer,
102-
boolean needClose
99+
@NotNull InputStream in,
100+
@NotNull OutputStream out,
101+
@NotNull byte[] buffer,
102+
boolean needClose
103103
) throws IOException {
104104

105105
for (int i = in.read(buffer); i != -1; i = in.read(buffer)) {

rlib-common/src/main/java/com/ss/rlib/common/util/NumberUtils.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ public static long makeLong(@NotNull byte[] bytes) {
7676
((long) bytes[1] & 0xFF) << 8 | (long) bytes[0] & 0xFF;
7777
}
7878

79+
/**
80+
* Get a short value from a byte array.
81+
*
82+
* @param bytes the byte array.
83+
* @param offset the offset.
84+
* @return the short value.
85+
*/
86+
public static short getShort(@NotNull byte[] bytes, int offset) {
87+
return (short) (bytes[offset + 1] << 8 | bytes[offset] & 0xff);
88+
}
89+
7990
private NumberUtils() {
8091
throw new IllegalArgumentException();
8192
}

rlib-common/src/main/java/com/ss/rlib/common/util/ObjectUtils.java

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
11
package com.ss.rlib.common.util;
22

3-
import com.ss.rlib.common.util.array.Array;
4-
import com.ss.rlib.common.util.array.ArrayFactory;
5-
import com.ss.rlib.common.util.dictionary.ConcurrentObjectDictionary;
6-
import com.ss.rlib.common.util.dictionary.DictionaryFactory;
7-
import com.ss.rlib.common.util.dictionary.DictionaryUtils;
8-
import com.ss.rlib.common.util.dictionary.ObjectDictionary;
93
import com.ss.rlib.common.util.pools.Reusable;
104
import org.jetbrains.annotations.NotNull;
115
import org.jetbrains.annotations.Nullable;
126

13-
import java.lang.reflect.Field;
147
import java.util.Objects;
158
import java.util.function.Function;
169
import java.util.function.LongFunction;
@@ -23,9 +16,6 @@
2316
*/
2417
public final class ObjectUtils {
2518

26-
private static final ConcurrentObjectDictionary<Class<?>, Array<Field>> FIELDS_CACHE =
27-
DictionaryFactory.newConcurrentAtomicObjectDictionary();
28-
2919
/**
3020
* @see Objects#requireNonNull(Object, String)
3121
*/
@@ -88,7 +78,7 @@ public final class ObjectUtils {
8878
* @return the object.
8979
* @since 9.0.3
9080
*/
91-
public static <T, F> @NotNull T notNull(
81+
public static <T> @NotNull T notNull(
9282
@Nullable T obj,
9383
long arg,
9484
@NotNull LongFunction<? extends RuntimeException> factory
@@ -155,56 +145,6 @@ public static int hash(@Nullable Object object) {
155145
return object == null ? 0 : object.hashCode();
156146
}
157147

158-
/**
159-
* Update all fields of an original object to a target object.
160-
*
161-
* @param <O> the type parameter
162-
* @param <N> the type parameter
163-
* @param original the original object.
164-
* @param target the target object.
165-
* @param cache the flag of using cache.
166-
*/
167-
public static <O, N extends O> void reload(@NotNull O original, @NotNull N target, boolean cache) {
168-
169-
final Class<?> type = original.getClass();
170-
171-
boolean needPutToCache = false;
172-
Array<Field> array = null;
173-
174-
175-
if (cache) {
176-
array = DictionaryUtils.getInReadLock(FIELDS_CACHE, type, ObjectDictionary::get);
177-
}
178-
179-
if (array == null) {
180-
needPutToCache = cache;
181-
182-
array = ArrayFactory.newArray(Field.class);
183-
184-
for (Class<?> cs = type; cs != null; cs = cs.getSuperclass()) {
185-
array.addAll(cs.getDeclaredFields());
186-
}
187-
188-
array.forEach(filtered -> {
189-
final String fieldName = filtered.toString();
190-
return !(fieldName.contains("final") || fieldName.contains("static"));
191-
}, toUpdate -> toUpdate.setAccessible(true));
192-
}
193-
194-
195-
array.forEach(field -> {
196-
try {
197-
field.set(original, field.get(target));
198-
} catch (final IllegalArgumentException | IllegalAccessException e) {
199-
Utils.print(ObjectUtils.class, e);
200-
}
201-
});
202-
203-
if (needPutToCache) {
204-
DictionaryUtils.runInWriteLock(FIELDS_CACHE, type, array, ObjectDictionary::put);
205-
}
206-
}
207-
208148
/**
209149
* Call the method {@link Reusable#release()} if the object is instanceof {@link Reusable}.
210150
*

0 commit comments

Comments
 (0)