@@ -7,4 +7,6 @@
|
||||
- [Efficiently Merge Sorted Java Sequences](https://www.baeldung.com/java-merge-sorted-sequences)
|
||||
- [Introduction to Greedy Algorithms with Java](https://www.baeldung.com/java-greedy-algorithms)
|
||||
- [The Caesar Cipher in Java](https://www.baeldung.com/java-caesar-cipher)
|
||||
- [Implementing a 2048 Solver in Java](https://www.baeldung.com/2048-java-solver)
|
||||
- [Finding Top K Elements in an Array](https://www.baeldung.com/java-array-top-elements)
|
||||
- More articles: [[<-- prev]](/../algorithms-miscellaneous-5)
|
||||
|
||||
@@ -6,4 +6,4 @@ This module contains articles about Apache Shiro
|
||||
|
||||
- [Introduction to Apache Shiro](https://www.baeldung.com/apache-shiro)
|
||||
- [Permissions-Based Access Control with Apache Shiro](https://www.baeldung.com/apache-shiro-access-control)
|
||||
|
||||
- [Spring Security vs Apache Shiro](https://www.baeldung.com/spring-security-vs-apache-shiro)
|
||||
|
||||
@@ -13,4 +13,5 @@ This module contains articles about core Groovy concepts
|
||||
- [Converting a String to a Date in Groovy](https://www.baeldung.com/groovy-string-to-date)
|
||||
- [Guide to I/O in Groovy](https://www.baeldung.com/groovy-io)
|
||||
- [Convert String to Integer in Groovy](https://www.baeldung.com/groovy-convert-string-to-integer)
|
||||
- [Groovy Variable Scope](https://www.baeldung.com/groovy/variable-scope)
|
||||
- [[More -->]](/core-groovy-2)
|
||||
|
||||
@@ -8,7 +8,7 @@ This module contains articles about Java 11 core features
|
||||
- [Java 11 Local Variable Syntax for Lambda Parameters](https://www.baeldung.com/java-var-lambda-params)
|
||||
- [Java 11 String API Additions](https://www.baeldung.com/java-11-string-api)
|
||||
- [Java 11 Nest Based Access Control](https://www.baeldung.com/java-nest-based-access-control)
|
||||
- [Exploring the New HTTP Client in Java 9 and 11](https://www.baeldung.com/java-9-http-client)
|
||||
- [Exploring the New HTTP Client in Java](https://www.baeldung.com/java-9-http-client)
|
||||
- [An Introduction to Epsilon GC: A No-Op Experimental Garbage Collector](https://www.baeldung.com/jvm-epsilon-gc-garbage-collector)
|
||||
- [Guide to jlink](https://www.baeldung.com/jlink)
|
||||
- [Negate a Predicate Method Reference with Java 11](https://www.baeldung.com/java-negate-predicate-method-reference)
|
||||
|
||||
+6
-3
@@ -1,12 +1,15 @@
|
||||
package com.baeldung.modules.main;
|
||||
|
||||
import com.baeldung.modules.hello.HelloInterface;
|
||||
import com.baeldung.modules.hello.HelloModules;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
public class MainApp {
|
||||
public static void main(String[] args) {
|
||||
HelloModules.doSomething();
|
||||
|
||||
HelloModules module = new HelloModules();
|
||||
module.sayHello();
|
||||
|
||||
Iterable<HelloInterface> services = ServiceLoader.load(HelloInterface.class);
|
||||
HelloInterface service = services.iterator().next();
|
||||
service.sayHello();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,3 +13,4 @@ This module contains articles about core Java features that have been introduced
|
||||
- [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api)
|
||||
- [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams)
|
||||
- [Multi-Release JAR Files with Maven](https://www.baeldung.com/maven-multi-release-jars)
|
||||
- [The Difference between RxJava API and the Java 9 Flow API](https://www.baeldung.com/rxjava-vs-java-flow-api)
|
||||
|
||||
@@ -9,3 +9,4 @@ This module contains articles about Java 9 core features
|
||||
- [Iterate Through a Range of Dates in Java](https://www.baeldung.com/java-iterate-date-range)
|
||||
- [Initialize a HashMap in Java](https://www.baeldung.com/java-initialize-hashmap)
|
||||
- [Immutable ArrayList in Java](https://www.baeldung.com/java-immutable-list)
|
||||
- [Easy Ways to Write a Java InputStream to an OutputStream](https://www.baeldung.com/java-inputstream-to-outputstream)
|
||||
|
||||
@@ -5,4 +5,4 @@ This module contains complete guides about arrays in Java
|
||||
### Relevant Articles:
|
||||
- [Arrays in Java: A Reference Guide](https://www.baeldung.com/java-arrays-guide)
|
||||
- [Guide to the java.util.Arrays Class](https://www.baeldung.com/java-util-arrays)
|
||||
- [What is [Ljava.lang.Object;?]](https://www.baeldung.com/java-tostring-array)
|
||||
- [What is \[Ljava.lang.Object;?](https://www.baeldung.com/java-tostring-array)
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
## Core Java Collections Cookbooks and Examples
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Time Comparison of Arrays.sort(Object[]) and Arrays.sort(int[])](https://www.baeldung.com/arrays-sortobject-vs-sortint)
|
||||
- [Java ArrayList vs Vector](https://www.baeldung.com/java-arraylist-vs-vector)
|
||||
- [Differences Between HashMap and Hashtable](https://www.baeldung.com/hashmap-hashtable-differences)
|
||||
@@ -10,3 +11,4 @@
|
||||
- [Performance of contains() in a HashSet vs ArrayList](https://www.baeldung.com/java-hashset-arraylist-contains-performance)
|
||||
- [Fail-Safe Iterator vs Fail-Fast Iterator](https://www.baeldung.com/java-fail-safe-vs-fail-fast-iterator)
|
||||
- [Quick Guide to the Java Stack](https://www.baeldung.com/java-stack)
|
||||
- [Convert an Array of Primitives to a List](https://www.baeldung.com/java-primitive-array-to-list)
|
||||
|
||||
@@ -16,6 +16,11 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jol</groupId>
|
||||
<artifactId>jol-core</artifactId>
|
||||
<version>${jol-core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.openjdk.jmh</groupId>
|
||||
<artifactId>jmh-core</artifactId>
|
||||
@@ -37,6 +42,7 @@
|
||||
<properties>
|
||||
<openjdk.jmh.version>1.19</openjdk.jmh.version>
|
||||
<assertj.version>3.11.1</assertj.version>
|
||||
<jol-core.version>0.10</jol-core.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+178
@@ -0,0 +1,178 @@
|
||||
package com.baeldung.collections.bitset;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.openjdk.jol.info.ClassLayout;
|
||||
import org.openjdk.jol.info.GraphLayout;
|
||||
|
||||
import java.util.BitSet;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class BitSetUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenBoolArray_whenMemoryLayout_thenConsumeMoreThanOneBit() {
|
||||
boolean[] bits = new boolean[1024 * 1024];
|
||||
|
||||
System.out.println(ClassLayout.parseInstance(bits).toPrintable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenMemoryLayout_thenConsumeOneBitPerFlag() {
|
||||
BitSet bitSet = new BitSet(1024 * 1024);
|
||||
|
||||
System.out.println(GraphLayout.parseInstance(bitSet).toPrintable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenSetting_thenShouldBeTrue() {
|
||||
BitSet bitSet = new BitSet();
|
||||
|
||||
bitSet.set(10);
|
||||
assertThat(bitSet.get(10)).isTrue();
|
||||
|
||||
bitSet.set(20, 30);
|
||||
for (int i = 20; i <= 29; i++) {
|
||||
assertThat(bitSet.get(i)).isTrue();
|
||||
}
|
||||
assertThat(bitSet.get(30)).isFalse();
|
||||
|
||||
bitSet.set(10, false);
|
||||
assertThat(bitSet.get(10)).isFalse();
|
||||
|
||||
bitSet.set(20, 30, false);
|
||||
for (int i = 20; i <= 30; i++) {
|
||||
assertThat(bitSet.get(i)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenClearing_thenShouldBeFalse() {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(42);
|
||||
assertThat(bitSet.get(42)).isTrue();
|
||||
|
||||
bitSet.clear(42);
|
||||
assertThat(bitSet.get(42)).isFalse();
|
||||
|
||||
bitSet.set(10, 20);
|
||||
for (int i = 10; i < 20; i++) {
|
||||
assertThat(bitSet.get(i)).isTrue();
|
||||
}
|
||||
|
||||
bitSet.clear(10, 20);
|
||||
for (int i = 10; i < 20; i++) {
|
||||
assertThat(bitSet.get(i)).isFalse();
|
||||
}
|
||||
|
||||
bitSet.set(10, 20);
|
||||
bitSet.clear();
|
||||
for (int i = 0; i < 100; i++) {
|
||||
assertThat(bitSet.get(i)).isFalse();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenGettingElements_thenShouldReturnRequestedBits() {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(42);
|
||||
|
||||
assertThat(bitSet.get(42)).isTrue();
|
||||
assertThat(bitSet.get(43)).isFalse();
|
||||
|
||||
bitSet.set(10, 20);
|
||||
BitSet newBitSet = bitSet.get(10, 20);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
assertThat(newBitSet.get(i)).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenFlip_thenTogglesTrueToFalseAndViceVersa() {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(42);
|
||||
bitSet.flip(42);
|
||||
assertThat(bitSet.get(42)).isFalse();
|
||||
|
||||
bitSet.flip(12);
|
||||
assertThat(bitSet.get(12)).isTrue();
|
||||
|
||||
bitSet.flip(30, 40);
|
||||
for (int i = 30; i < 40; i++) {
|
||||
assertThat(bitSet.get(i)).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenGettingTheSize_thenReturnsTheSize() {
|
||||
BitSet defaultBitSet = new BitSet();
|
||||
assertThat(defaultBitSet.size()).isEqualTo(64);
|
||||
|
||||
BitSet bitSet = new BitSet(1024);
|
||||
assertThat(bitSet.size()).isEqualTo(1024);
|
||||
|
||||
assertThat(bitSet.cardinality()).isEqualTo(0);
|
||||
bitSet.set(10, 30);
|
||||
assertThat(bitSet.cardinality()).isEqualTo(30 - 10);
|
||||
|
||||
assertThat(bitSet.length()).isEqualTo(30);
|
||||
bitSet.set(100);
|
||||
assertThat(bitSet.length()).isEqualTo(101);
|
||||
|
||||
assertThat(bitSet.isEmpty()).isFalse();
|
||||
bitSet.clear();
|
||||
assertThat(bitSet.isEmpty()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenSetOperations_thenShouldReturnAnotherBitSet() {
|
||||
BitSet first = new BitSet();
|
||||
first.set(5, 10);
|
||||
|
||||
BitSet second = new BitSet();
|
||||
second.set(7, 15);
|
||||
|
||||
assertThat(first.intersects(second)).isTrue();
|
||||
|
||||
first.and(second);
|
||||
assertThat(first.get(7)).isTrue();
|
||||
assertThat(first.get(8)).isTrue();
|
||||
assertThat(first.get(9)).isTrue();
|
||||
assertThat(first.get(10)).isFalse();
|
||||
|
||||
first.clear();
|
||||
first.set(5, 10);
|
||||
|
||||
first.xor(second);
|
||||
for (int i = 5; i < 7; i++) {
|
||||
assertThat(first.get(i)).isTrue();
|
||||
}
|
||||
for (int i = 10; i < 15; i++) {
|
||||
assertThat(first.get(i)).isTrue();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenStream_thenStreamsAllSetBits() {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(15, 25);
|
||||
|
||||
bitSet.stream().forEach(System.out::println);
|
||||
assertThat(bitSet.stream().count()).isEqualTo(10);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBitSet_whenNextOrPrev_thenReturnsTheNextOrPrevClearOrSetBit() {
|
||||
BitSet bitSet = new BitSet();
|
||||
bitSet.set(15, 25);
|
||||
|
||||
assertThat(bitSet.nextSetBit(13)).isEqualTo(15);
|
||||
assertThat(bitSet.nextSetBit(25)).isEqualTo(-1);
|
||||
|
||||
assertThat(bitSet.nextClearBit(23)).isEqualTo(25);
|
||||
|
||||
assertThat(bitSet.previousClearBit(24)).isEqualTo(14);
|
||||
assertThat(bitSet.previousSetBit(29)).isEqualTo(24);
|
||||
assertThat(bitSet.previousSetBit(14)).isEqualTo(-1);
|
||||
}
|
||||
}
|
||||
@@ -15,4 +15,5 @@ This module contains articles about advanced topics about multithreading with co
|
||||
- [The ABA Problem in Concurrency](https://www.baeldung.com/cs/aba-concurrency)
|
||||
- [Introduction to Lock-Free Data Structures](https://www.baeldung.com/lock-free-programming)
|
||||
- [Introduction to Exchanger in Java](https://www.baeldung.com/java-exchanger)
|
||||
- [Why Not To Start A Thread In The Constructor?](https://www.baeldung.com/java-thread-constructor)
|
||||
- [[<-- previous]](/core-java-modules/core-java-concurrency-advanced-2)
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
This module contains articles about basic Java concurrency
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
|
||||
- [wait and notify() Methods in Java](https://www.baeldung.com/java-wait-notify)
|
||||
- [Difference Between Wait and Sleep in Java](https://www.baeldung.com/java-wait-and-sleep)
|
||||
- [Guide to the Synchronized Keyword in Java](https://www.baeldung.com/java-synchronized)
|
||||
- [Life Cycle of a Thread in Java](https://www.baeldung.com/java-thread-lifecycle)
|
||||
- [Guide to AtomicMarkableReference](https://www.baeldung.com/java-atomicmarkablereference)
|
||||
- [Why are Local Variables Thread-Safe in Java](https://www.baeldung.com/java-local-variables-thread-safe)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-concurrency-basic)
|
||||
|
||||
+31
@@ -0,0 +1,31 @@
|
||||
package com.baeldung.consoleout;
|
||||
|
||||
import java.io.Console;
|
||||
|
||||
public class ConsoleAndOut {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
printConsoleObject();
|
||||
readPasswordFromConsole();
|
||||
} catch (Exception ex) {
|
||||
// Eating NullPointerExcpetion which will occur when this
|
||||
// program will be run from mediums other than console
|
||||
}
|
||||
printSysOut();
|
||||
}
|
||||
|
||||
static void printConsoleObject() {
|
||||
Console console = System.console();
|
||||
console.writer().print(console);
|
||||
}
|
||||
|
||||
static void readPasswordFromConsole() {
|
||||
Console console = System.console();
|
||||
char[] password = console.readPassword("Enter password: ");
|
||||
console.printf(String.valueOf(password));
|
||||
}
|
||||
|
||||
static void printSysOut() {
|
||||
System.out.println(System.out);
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.consoleout;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class ConsoleAndOutUnitTest {
|
||||
|
||||
@Test
|
||||
void whenRetreivingConsole_thenPrintConsoleObject() {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
ConsoleAndOut.printConsoleObject();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenReadingPassword_thenReadPassword() {
|
||||
assertThrows(NullPointerException.class, () -> {
|
||||
ConsoleAndOut.readPasswordFromConsole();
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenRetrievingSysOut_thenPrintSysOutObject() {
|
||||
ConsoleAndOut.printSysOut();
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
This module contains articles about date operations in Java.
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Get the Current Date Prior to Java 8](https://www.baeldung.com/java-get-the-current-date-legacy)
|
||||
- [Skipping Weekends While Adding Days to LocalDate in Java 8](https://www.baeldung.com/java-localdate-add-days-skip-weekends)
|
||||
- [Checking If Two Java Dates Are on the Same Day](https://www.baeldung.com/java-check-two-dates-on-same-day)
|
||||
@@ -9,4 +10,5 @@ This module contains articles about date operations in Java.
|
||||
- [How to Set the JVM Time Zone](https://www.baeldung.com/java-jvm-time-zone)
|
||||
- [How to determine day of week by passing specific date in Java?](https://www.baeldung.com/java-get-day-of-week)
|
||||
- [Finding Leap Years in Java](https://www.baeldung.com/java-leap-year)
|
||||
- [Getting the Week Number From Any Date](https://www.baeldung.com/java-get-week-number)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-date-operations-1)
|
||||
|
||||
@@ -12,3 +12,5 @@ This module contains articles about core java exceptions
|
||||
- [Java – Try with Resources](https://www.baeldung.com/java-try-with-resources)
|
||||
- [Java Global Exception Handler](https://www.baeldung.com/java-global-exception-handler)
|
||||
- [How to Find an Exception’s Root Cause in Java](https://www.baeldung.com/java-exception-root-cause)
|
||||
- [Java IOException “Too many open files”](https://www.baeldung.com/java-too-many-open-files)
|
||||
- [When Does Java Throw the ExceptionInInitializerError?](https://www.baeldung.com/java-exceptionininitializererror)
|
||||
|
||||
@@ -13,4 +13,4 @@ This module contains articles about core Java input and output (IO)
|
||||
- [How to Copy a File with Java](https://www.baeldung.com/java-copy-file)
|
||||
- [Create a Directory in Java](https://www.baeldung.com/java-create-directory)
|
||||
- [Java IO vs NIO](https://www.baeldung.com/java-io-vs-nio)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io)[[More -->]](/core-java-modules/core-java-io-3)
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
## Core Java IO
|
||||
|
||||
This module contains articles about core Java input and output (IO)
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java – Create a File](https://www.baeldung.com/java-how-to-create-a-file)
|
||||
- [Check If a Directory Is Empty in Java](https://www.baeldung.com/java-check-empty-directory)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-io-2)
|
||||
@@ -0,0 +1,59 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-io-3</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-io-3</name>
|
||||
<packaging>jar</packaging>
|
||||
<parent>
|
||||
<groupId>com.baeldung.core-java-modules</groupId>
|
||||
<artifactId>core-java-modules</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>com.google.guava</groupId>
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<!-- logging -->
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.baeldung.createfile;
|
||||
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class CreateFileUnitTest {
|
||||
|
||||
private final String FILE_NAME = "src/test/resources/fileToCreate.txt";
|
||||
|
||||
@AfterEach
|
||||
@BeforeEach
|
||||
public void cleanUpFiles() {
|
||||
File targetFile = new File(FILE_NAME);
|
||||
targetFile.delete();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingNio_whenCreatingFile_thenCorrect() throws IOException {
|
||||
Path newFilePath = Paths.get(FILE_NAME);
|
||||
Files.createFile(newFilePath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingFile_whenCreatingFile_thenCorrect() throws IOException {
|
||||
File newFile = new File(FILE_NAME);
|
||||
boolean success = newFile.createNewFile();
|
||||
assertTrue(success);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingGuava_whenCreatingFile_thenCorrect() throws IOException {
|
||||
com.google.common.io.Files.touch(new File(FILE_NAME));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUsingCommonsIo_whenCreatingFile_thenCorrect() throws IOException {
|
||||
FileUtils.touch(new File(FILE_NAME));
|
||||
}
|
||||
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.baeldung.emptiness;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.DirectoryStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class DirectoryEmptinessUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenPath_whenInvalid_thenReturnsFalse() throws IOException {
|
||||
assertThat(isEmpty(Paths.get("invalid-addr"))).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenNotDirectory_thenReturnsFalse() throws IOException {
|
||||
Path aFile = Paths.get(getClass().getResource("/notDir.txt").getPath());
|
||||
assertThat(isEmpty(aFile)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenNotEmptyDir_thenReturnsFalse() throws IOException {
|
||||
Path currentDir = new File("").toPath().toAbsolutePath();
|
||||
assertThat(isEmpty(currentDir)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPath_whenIsEmpty_thenReturnsTrue() throws Exception {
|
||||
Path path = Files.createTempDirectory("baeldung-empty");
|
||||
assertThat(isEmpty(path)).isTrue();
|
||||
}
|
||||
|
||||
private static boolean isEmpty(Path path) throws IOException {
|
||||
if (Files.isDirectory(path)) {
|
||||
try (DirectoryStream<Path> directory = Files.newDirectoryStream(path)) {
|
||||
return !directory.iterator().hasNext();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isEmpty2(Path path) throws IOException {
|
||||
if (Files.isDirectory(path)) {
|
||||
try (Stream<Path> entries = Files.list(path)) {
|
||||
return !entries.findFirst().isPresent();
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private static boolean isEmptyInefficient(Path path) {
|
||||
return path.toFile().listFiles().length == 0;
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,6 @@ This module contains articles about core Java input/output(IO) APIs.
|
||||
- [A Guide to the Java FileReader Class](https://www.baeldung.com/java-filereader)
|
||||
- [The Java File Class](https://www.baeldung.com/java-io-file)
|
||||
- [Java FileWriter](https://www.baeldung.com/java-filewriter)
|
||||
- [Differences Between the Java WatchService API and the Apache Commons IO Monitor Library](https://www.baeldung.com/java-watchservice-vs-apache-commons-io-monitor-library)
|
||||
- [Comparing getPath(), getAbsolutePath(), and getCanonicalPath() in Java](https://www.baeldung.com/java-path)
|
||||
- [Quick Use of FilenameFilter](https://www.baeldung.com/java-filename-filter)
|
||||
- [Guide to BufferedReader](https://www.baeldung.com/java-buffered-reader)
|
||||
|
||||
@@ -8,4 +8,6 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
||||
- [Measuring Object Sizes in the JVM](https://www.baeldung.com/jvm-measuring-object-sizes)
|
||||
- [Adding Shutdown Hooks for JVM Applications](https://www.baeldung.com/jvm-shutdown-hooks)
|
||||
- [boolean and boolean[] Memory Layout in the JVM](https://www.baeldung.com/jvm-boolean-memory-layout)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm)
|
||||
- [Where Is the Array Length Stored in JVM?](https://www.baeldung.com/java-jvm-array-length)
|
||||
- [Memory Address of Objects in Java](https://www.baeldung.com/java-object-memory-address)
|
||||
- More articles: [[<-- prev]](/core-java-modules/core-java-jvm)
|
||||
|
||||
@@ -2,4 +2,6 @@
|
||||
|
||||
This module contains articles about core features in the Java language
|
||||
|
||||
- [Class.isInstance vs Class.isAssignableFrom](https://www.baeldung.com/java-isinstance-isassignablefrom)
|
||||
- [Converting a Java String Into a Boolean](https://www.baeldung.com/java-string-to-boolean)
|
||||
- [[<-- Prev]](/core-java-modules/core-java-lang-2)
|
||||
|
||||
@@ -39,4 +39,4 @@
|
||||
<assertj.version>3.12.2</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.staticvariables;
|
||||
|
||||
public class StaticVariableDemo {
|
||||
public static int i;
|
||||
public static int j = 20;
|
||||
public static int z;
|
||||
|
||||
static {
|
||||
z = 30;
|
||||
a = 40;
|
||||
}
|
||||
|
||||
public static int a = 50;
|
||||
|
||||
public static final int b = 100;
|
||||
|
||||
public StaticVariableDemo() {
|
||||
}
|
||||
|
||||
static class Nested {
|
||||
public static String nestedClassStaticVariable = "test";
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
package com.baeldung.staticvariables;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class StaticVariableUnitTest {
|
||||
|
||||
@Test
|
||||
public void initializeStaticVariable_checkAssignedValues() {
|
||||
|
||||
try {
|
||||
Class<?> staticVariableDemo = this.getClass()
|
||||
.getClassLoader()
|
||||
.loadClass("com.baeldung.staticvariables.StaticVariableDemo");
|
||||
|
||||
Field field1 = staticVariableDemo.getField("i");
|
||||
|
||||
assertThat(field1.getInt(staticVariableDemo)).isEqualTo(0);
|
||||
|
||||
Field field2 = staticVariableDemo.getField("j");
|
||||
|
||||
assertThat(field2.getInt(staticVariableDemo)).isEqualTo(20);
|
||||
|
||||
} catch (ClassNotFoundException | NoSuchFieldException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initializeStaticVariable_checkStaticBlock() {
|
||||
|
||||
try {
|
||||
Class<?> staticVariableDemo = this.getClass()
|
||||
.getClassLoader()
|
||||
.loadClass("com.baeldung.staticvariables.StaticVariableDemo");
|
||||
|
||||
Field field1 = staticVariableDemo.getField("z");
|
||||
|
||||
assertThat(field1.getInt(staticVariableDemo)).isEqualTo(30);
|
||||
|
||||
Field field2 = staticVariableDemo.getField("a");
|
||||
|
||||
assertThat(field2.getInt(staticVariableDemo)).isEqualTo(50);
|
||||
|
||||
} catch (ClassNotFoundException | NoSuchFieldException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initializeStaticVariable_checkFinalValues() {
|
||||
|
||||
try {
|
||||
Class<?> staticVariableDemo = this.getClass()
|
||||
.getClassLoader()
|
||||
.loadClass("com.baeldung.staticvariables.StaticVariableDemo");
|
||||
|
||||
Field field1 = staticVariableDemo.getField("b");
|
||||
|
||||
assertThat(field1.getInt(staticVariableDemo)).isEqualTo(100);
|
||||
|
||||
} catch (ClassNotFoundException | NoSuchFieldException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void initializeStaticVariable_checkInnerClassValues() {
|
||||
|
||||
try {
|
||||
Class<?> staticVariableDemo = this.getClass()
|
||||
.getClassLoader()
|
||||
.loadClass("com.baeldung.staticvariables.StaticVariableDemo");
|
||||
|
||||
Class<?>[] nestedClasses = staticVariableDemo.getClasses();
|
||||
|
||||
for (Class<?> nestedClass : nestedClasses) {
|
||||
if (nestedClass.getName()
|
||||
.equals("Nested")) {
|
||||
|
||||
Field field1 = nestedClass.getField("nestedClassStaticVariable");
|
||||
assertThat(field1.get(nestedClass)).isEqualTo("test");
|
||||
}
|
||||
}
|
||||
|
||||
} catch (ClassNotFoundException | NoSuchFieldException | SecurityException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalArgumentException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IllegalAccessException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user