This commit is contained in:
Loredana Crusoveanu
2022-01-10 19:31:07 +02:00
228 changed files with 3751 additions and 700 deletions
+1
View File
@@ -1,3 +1,4 @@
*.docx
temp.xls
temp.xlsx
number_test.xlsx
+5 -2
View File
@@ -1,9 +1,12 @@
## Apache POI
This module contains articles about Apache POI
This module contains articles about Apache POI.
### Relevant Articles:
- [Adding a Column to an Excel Sheet Using Apache POI](https://www.baeldung.com/java-excel-add-column)
- [Add an Image to a Cell in an Excel File With Java](https://www.baeldung.com/java-add-image-excel)
- More articles: [[<-- prev]](/apache-poi)
- [Numeric Format Using POI](https://www.baeldung.com/apache-poi-numeric-format)
- [Microsoft Word Processing in Java with Apache POI](https://www.baeldung.com/java-microsoft-word-with-apache-poi)
- [Creating a MS PowerPoint Presentation in Java](https://www.baeldung.com/apache-poi-slideshow)
- More articles: [[<-- prev]](../apache-poi)
@@ -5,7 +5,22 @@ import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TextParagraph;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.*;
import org.apache.poi.xslf.usermodel.SlideLayout;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFAutoShape;
import org.apache.poi.xslf.usermodel.XSLFHyperlink;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
import org.apache.poi.xslf.usermodel.XSLFTable;
import org.apache.poi.xslf.usermodel.XSLFTableCell;
import org.apache.poi.xslf.usermodel.XSLFTableRow;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
import java.awt.*;
import java.io.FileInputStream;
@@ -155,8 +170,8 @@ public class PowerPointHelper {
/**
* Retrieve the placeholder inside a slide
*
* @param slide
*
* @param slide
* The slide
* @return List of placeholder inside a slide
*/
@@ -1,7 +1,11 @@
package com.baeldung.poi.word;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;

Before

Width:  |  Height:  |  Size: 782 B

After

Width:  |  Height:  |  Size: 782 B

@@ -1,8 +1,5 @@
package com.baeldung.poi.powerpoint;
import java.io.File;
import java.util.List;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
@@ -13,12 +10,15 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.util.List;
public class PowerPointIntegrationTest {
private PowerPointHelper pph;
private String fileLocation;
private static final String FILE_NAME = "presentation.pptx";
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@@ -1,19 +1,19 @@
package com.baeldung.poi.word;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.junit.BeforeClass;
import org.junit.Test;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class WordIntegrationTest {
static WordDocument wordDocument;
+2 -4
View File
@@ -1,12 +1,10 @@
## Apache POI
This module contains articles about Apache POI
This module contains articles about Apache POI.
### Relevant Articles:
- [Microsoft Word Processing in Java with Apache POI](https://www.baeldung.com/java-microsoft-word-with-apache-poi)
- [Working with Microsoft Excel in Java](https://www.baeldung.com/java-microsoft-excel)
- [Creating a MS PowerPoint Presentation in Java](https://www.baeldung.com/apache-poi-slideshow)
- [Merge Cells in Excel Using Apache POI](https://www.baeldung.com/java-apache-poi-merge-cells)
- [Get String Value of Excel Cell with Apache POI](https://www.baeldung.com/java-apache-poi-cell-string-value)
- [Read Excel Cell Value Rather Than Formula With Apache POI](https://www.baeldung.com/apache-poi-read-cell-value-formula)
@@ -16,4 +14,4 @@ This module contains articles about Apache POI
- [Set Background Color of a Cell with Apache POI](https://www.baeldung.com/apache-poi-background-color)
- [Add Borders to Excel Cells With Apache POI](https://www.baeldung.com/apache-poi-add-borders)
- [Reading Values From Excel in Java](https://www.baeldung.com/java-read-dates-excel)
- More articles: [[next -->]](/apache-poi-2)
- More articles: [[next -->]](../apache-poi-2)
+21
View File
@@ -27,6 +27,13 @@
<compilerArgs>--enable-preview</compilerArgs>
<source>${maven.compiler.source.version}</source>
<target>${maven.compiler.target.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
@@ -49,6 +56,20 @@
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh-core.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source.version>17</maven.compiler.source.version>
<maven.compiler.target.version>17</maven.compiler.target.version>
@@ -0,0 +1,115 @@
package com.baeldung.randomgenerators;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import java.util.concurrent.TimeUnit;
import java.util.random.RandomGenerator;
public class BenchmarkRunner {
public static void main(String[] args) throws Exception {
org.openjdk.jmh.Main.main(args);
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testL128X1024MixRandom() {
generateRandomNumbers(RandomGenerator.of("L128X1024MixRandom"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testL128X128MixRandom() {
generateRandomNumbers(RandomGenerator.of("L128X128MixRandom"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testL128X256MixRandom() {
generateRandomNumbers(RandomGenerator.of("L128X256MixRandom"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testL32X64MixRandom() {
generateRandomNumbers(RandomGenerator.of("L32X64MixRandom"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testL64X1024MixRandom() {
generateRandomNumbers(RandomGenerator.of("L64X1024MixRandom"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testL64X128MixRandom() {
generateRandomNumbers(RandomGenerator.of("L64X128MixRandom"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testL64X128StarStarRandom() {
generateRandomNumbers(RandomGenerator.of("L64X128StarStarRandom"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testL64X256MixRandom() {
generateRandomNumbers(RandomGenerator.of("L64X256MixRandom"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testRandom() {
generateRandomNumbers(RandomGenerator.of("Random"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testSecureRandom() {
generateRandomNumbers(RandomGenerator.of("SecureRandom"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testSplittableRandom() {
generateRandomNumbers(RandomGenerator.of("SplittableRandom"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testXoroshiro128PlusPlus() {
generateRandomNumbers(RandomGenerator.of("Xoroshiro128PlusPlus"));
}
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public static void testXoshiro256PlusPlus() {
generateRandomNumbers(RandomGenerator.of("Xoshiro256PlusPlus"));
}
private static void generateRandomNumbers(RandomGenerator generator) {
generator.nextLong();
generator.nextInt();
generator.nextFloat();
generator.nextDouble();
}
}
@@ -0,0 +1,36 @@
package com.baeldung.randomgenerators;
import java.util.Comparator;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
public class GeneratorFactory {
public static void main(String[] args) {
System.out.println("Group\tName\tJumpable?\tSplittable?");
RandomGeneratorFactory.all()
.sorted(Comparator.comparing(RandomGeneratorFactory::name))
.forEach(factory -> System.out.println(String.format("%s\t%s\t%s\t%s",
factory.group(),
factory.name(),
factory.isJumpable(),
factory.isSplittable())));
}
public static int getRandomInt(RandomGenerator generator, int bound) {
return generator.nextInt(bound);
}
public static RandomGenerator getDefaultGenerator() {
return RandomGeneratorFactory.getDefault().create();
}
public static RandomGenerator getJumpableGenerator() {
return RandomGeneratorFactory.all()
.filter(RandomGeneratorFactory::isJumpable)
.findAny()
.map(RandomGeneratorFactory::create)
.orElseThrow(() -> new RuntimeException("Error creating a generator"));
}
}
@@ -0,0 +1,12 @@
package com.baeldung.randomgenerators;
import java.util.Random;
public class OldRandom {
public static int getRandomInt(int bound) {
Random random = new Random();
return random.nextInt(bound);
}
}
@@ -0,0 +1,30 @@
package com.baeldung.randomgenerators;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.random.RandomGenerator;
import java.util.random.RandomGeneratorFactory;
public class SplittableGeneratorMultiThread {
public static List<Integer> generateNumbersInMultipleThreads() {
List<Integer> numbers = Collections.synchronizedList(new ArrayList<>());
ExecutorService executorService = Executors.newCachedThreadPool();
RandomGenerator.SplittableGenerator sourceGenerator = RandomGeneratorFactory
.<RandomGenerator.SplittableGenerator>of("L128X256MixRandom")
.create();
sourceGenerator.splits(20).forEach((splitGenerator) -> {
executorService.submit(() -> {
numbers.add(splitGenerator.nextInt(10));
});
});
return numbers;
}
}
@@ -1,4 +1,7 @@
module core.java {
requires jdk.incubator.vector;
requires jdk.incubator.foreign;
requires jmh.core;
requires jdk.unsupported;
exports com.baeldung.randomgenerators.jmh_generated;
}
@@ -0,0 +1,21 @@
package com.baeldung.randomgenerators;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class GeneratorFactoryUnitTest {
@Test
void givenDefaultGenerator_whenGeneratingAnInt_thenIntInRangeIsGenerated() {
int number = GeneratorFactory.getRandomInt(GeneratorFactory.getDefaultGenerator(), 10);
assertThat(number).isNotNegative().isLessThan(10);
}
@Test
void givenJumpableGenerator_whenGeneratingAnInt_thenIntInRangeIsGenerated() {
int number = GeneratorFactory.getRandomInt(GeneratorFactory.getJumpableGenerator(), 10);
assertThat(number).isNotNegative().isLessThan(10);
}
}
@@ -0,0 +1,25 @@
package com.baeldung.randomgenerators;
import org.junit.jupiter.api.Test;
import java.util.random.RandomGenerator;
import static org.assertj.core.api.Assertions.assertThat;
class GeneratorSelectionUnitTest {
@Test
void givenDefaultGenerator_whenGeneratingAnInt_thenIntInRangeIsGenerated() {
RandomGenerator generator = RandomGenerator.getDefault();
int number = generator.nextInt(10);
assertThat(number).isNotNegative().isLessThan(10);
}
@Test
void givenSpecificGenerator_whenGeneratingAnInt_thenIntInRangeIsGenerated() {
RandomGenerator generator = RandomGenerator.of("L128X256MixRandom");
int number = generator.nextInt(10);
assertThat(number).isNotNegative().isLessThan(10);
}
}
@@ -0,0 +1,15 @@
package com.baeldung.randomgenerators;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class OldRandomUnitTest {
@Test
void givenOldRandomApi_whenGeneratingAnInt_thenIntInRangeIsGenerated() {
int number = OldRandom.getRandomInt(10);
assertThat(number).isNotNegative().isLessThan(10);
}
}
@@ -0,0 +1,17 @@
package com.baeldung.randomgenerators;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
class SplittableGeneratorMultiThreadUnitTest {
@Test
void givenSplittableGenerator_whenUsingMultipleThreads_thenListOfIntsIsGenerated() {
List<Integer> numbers = SplittableGeneratorMultiThread.generateNumbersInMultipleThreads();
assertThat(numbers).hasSize(20).allMatch(number -> number >= 0 && number <= 10);
}
}
@@ -5,7 +5,7 @@ This module contains articles about the Date and Time API introduced with Java 8
### Relevant Articles:
- [Introduction to the Java 8 Date/Time API](http://www.baeldung.com/java-8-date-time-intro)
- [Migrating to the New Java 8 Date Time API](http://www.baeldung.com/migrating-to-java-8-date-time-api)
- [Get the Current Date, Time and Timestamp in Java 8](http://www.baeldung.com/current-date-time-and-timestamp-in-java-8)
- [Get the Current Date and Time in Java](https://www.baeldung.com/current-date-time-and-timestamp-in-java-8)
- [TemporalAdjuster in Java](http://www.baeldung.com/java-temporal-adjuster)
- [ZoneOffset in Java](https://www.baeldung.com/java-zone-offset)
- [Differences Between ZonedDateTime and OffsetDateTime](https://www.baeldung.com/java-zoneddatetime-offsetdatetime)
@@ -3,3 +3,4 @@
- [Binary Semaphore vs Reentrant Lock](https://www.baeldung.com/java-binary-semaphore-vs-reentrant-lock)
- [Bad Practices With Synchronization](https://www.baeldung.com/java-synchronization-bad-practices)
- [Start Two Threads at the Exact Same Time in Java](https://www.baeldung.com/java-start-two-threads-at-same-time)
- [Volatile Variables and Thread Safety](https://www.baeldung.com/java-volatile-variables-thread-safety)
@@ -0,0 +1,47 @@
package com.baeldung.volatilekeywordthreadsafety;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
public class VolatileVarNotThreadSafe {
private static final Logger LOG = LoggerFactory.getLogger(VolatileVarNotThreadSafe.class);
private static volatile int count = 0;
private static final int MAX_LIMIT = 1000;
public static void increment() {
count++;
}
public static int getCount() {
return count;
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
for(int index=0; index<MAX_LIMIT; index++) {
increment();
}
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
for(int index=0; index<MAX_LIMIT; index++) {
increment();
}
}
});
t1.start();
t2.start();
t1.join();
t2.join();
LOG.info("value of counter variable: "+count);
}
}
@@ -0,0 +1,13 @@
package com.baeldung.volatilekeywordthreadsafety;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class VolatileVarNotThreadSafeUnitTest {
@Test
public void whenCalledMainMethod_thenIncrementCount() throws InterruptedException {
VolatileVarNotThreadSafe.main(null);
Assertions.assertTrue(VolatileVarNotThreadSafe.getCount() > 0);
}
}
@@ -14,4 +14,5 @@ This module contains articles about basic Java concurrency
- [How to Stop Execution After a Certain Time in Java](https://www.baeldung.com/java-stop-execution-after-certain-time)
- [How to Handle InterruptedException in Java](https://www.baeldung.com/java-interrupted-exception)
- [How to Get the Number of Threads in a Java Process](https://www.baeldung.com/java-get-number-of-threads)
- [Set the Name of a Thread in Java](https://www.baeldung.com/java-set-thread-name)
- [[<-- Prev]](/core-java-modules/core-java-concurrency-basic)
@@ -9,3 +9,4 @@
- [Explanation of ClassCastException in Java](https://www.baeldung.com/java-classcastexception)
- [NoSuchFieldError in Java](https://www.baeldung.com/java-nosuchfielderror)
- [IllegalAccessError in Java](https://www.baeldung.com/java-illegalaccesserror)
- [Working with (Unknown Source) Stack Traces in Java](https://www.baeldung.com/java-unknown-source-stack-trace)
+1 -1
View File
@@ -14,4 +14,4 @@ This module contains articles about core Java input and output (IO)
- [Find the Last Modified File in a Directory with Java](https://www.baeldung.com/java-last-modified-file)
- [Get a Filename Without the Extension in Java](https://www.baeldung.com/java-filename-without-extension)
- [Writing byte[] to a File in Java](https://www.baeldung.com/java-write-byte-array-file)
- [[<-- Prev]](/core-java-modules/core-java-io-2)
- [[<-- Prev]](/core-java-modules/core-java-io-2)[[More -->]](/core-java-modules/core-java-io-4)
@@ -31,7 +31,7 @@ public class InputSequenceUnitTest {
InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(file1, file2);
String stringValue = inputSequenceHandler.readAsString();
inputSequenceHandler.close();
assertEquals(stringValue, FILE1_AND_FILE2_CONTENT);
assertEquals(FILE1_AND_FILE2_CONTENT, stringValue);
}
@Test
@@ -43,7 +43,7 @@ public class InputSequenceUnitTest {
InputSequenceHandler inputSequenceHandler = new InputSequenceHandler(filesList);
String stringValue = inputSequenceHandler.readAsString();
inputSequenceHandler.close();
assertEquals(stringValue, ALL_FILES_CONTENT);
assertEquals(ALL_FILES_CONTENT, stringValue);
}
@Test
@@ -10,4 +10,5 @@ This module contains articles about string conversions from/to another type.
- [Converting String to BigInteger in Java](https://www.baeldung.com/java-string-to-biginteger)
- [Convert a String to Camel Case](https://www.baeldung.com/java-string-to-camel-case)
- [Convert a ByteBuffer to String in Java](https://www.baeldung.com/java-bytebuffer-to-string)
- [Convert String to Float and Back in Java](https://www.baeldung.com/java-string-to-float)
- More articles: [[<-- prev]](/core-java-string-conversions)

Some files were not shown because too many files have changed in this diff Show More