commit
4b5c120b1c
16
.gitignore
vendored
16
.gitignore
vendored
@ -48,3 +48,19 @@ dependency-reduced-pom.xml
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
xml/src/test/resources/example_dom4j_new.xml
|
||||
xml/src/test/resources/example_dom4j_updated.xml
|
||||
xml/src/test/resources/example_jaxb_new.xml
|
||||
core-java-io/hard_link.txt
|
||||
core-java-io/target_link.txt
|
||||
core-java/src/main/java/com/baeldung/manifest/MANIFEST.MF
|
||||
ethereum/logs/
|
||||
jmeter/src/main/resources/*-JMeter.csv
|
||||
|
||||
**/node_modules/
|
||||
**/dist
|
||||
**/tmp
|
||||
**/out-tsc
|
||||
**/nbproject/
|
||||
**/nb-configuration.xml
|
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +0,0 @@
|
||||
[submodule "testgitrepo"]
|
||||
path = testgitrepo
|
||||
url = /home/prd/Development/projects/idea/tutorials/spring-boot/src/main/resources/testgitrepo/
|
@ -4,7 +4,7 @@ before_install:
|
||||
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
|
||||
|
||||
install: skip
|
||||
script: travis_wait 60 mvn -q install
|
||||
script: travis_wait 60 mvn -q install -Pdefault
|
||||
|
||||
sudo: required
|
||||
|
||||
|
@ -1,11 +1,13 @@
|
||||
<?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">
|
||||
<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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>JGitSnippets</artifactId>
|
||||
<artifactId>JGit</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<url>http://maven.apache.org</url>
|
||||
<name>JGit</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
@ -45,7 +47,6 @@
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<org.eclipse.jgit.version>4.5.0.201609210915-r</org.eclipse.jgit.version>
|
||||
</properties>
|
||||
|
||||
|
13
JGit/src/main/resources/logback.xml
Normal file
13
JGit/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
36
README.md
36
README.md
@ -2,41 +2,27 @@
|
||||
The "REST with Spring" Classes
|
||||
==============================
|
||||
|
||||
After 5 months of work, here's the Master Class of REST With Spring: <br/>
|
||||
**[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
|
||||
Here's the Master Class of REST With Spring (along with the newly announced Boot 2 material): <br/>
|
||||
**[>> THE REST WITH SPRING - MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
|
||||
|
||||
And here's the Master Class of Learn Spring Security: <br/>
|
||||
**[>> LEARN SPRING SECURITY MASTER CLASS](http://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
|
||||
**[>> LEARN SPRING SECURITY - MASTER CLASS](http://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
|
||||
|
||||
|
||||
|
||||
Spring Tutorials
|
||||
Java and Spring Tutorials
|
||||
================
|
||||
|
||||
This project is **a collection of small and focused tutorials** each covering a single and well defined area of development.
|
||||
Most of the tutorial projects are focused on the `Spring Framework` (and `Spring Security`).
|
||||
This project is **a collection of small and focused tutorials** - each covering a single and well defined area of development in the Java ecosystem.
|
||||
A strong focus of these is, of course, the Spring Framework - Spring, Spring Boot and Spring Securiyt.
|
||||
In additional to Spring, the following technologies are in focus: `core Java`, `Jackson`, `HttpClient`, `Guava`.
|
||||
|
||||
|
||||
Building the project
|
||||
====================
|
||||
To do the full build, do: `mvn install -Dgib.enabled=false`
|
||||
To do the full build, do: `mvn install -Pdefault -Dgib.enabled=false`
|
||||
|
||||
|
||||
Working with the code in Eclipse
|
||||
================================
|
||||
Any IDE can be used to work with the projects, but if you're using Eclipse, consider the following.
|
||||
|
||||
- import the included **formatter** in Eclipse:
|
||||
`https://github.com/eugenp/tutorials/tree/master/eclipse`
|
||||
|
||||
|
||||
CI - Jenkins
|
||||
================================
|
||||
This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/tutorials-unit/)**
|
||||
|
||||
### Relevant Articles:
|
||||
================================
|
||||
|
||||
- [Apache Maven Standard Directory Layout](http://www.baeldung.com/maven-directory-structure)
|
||||
- [Apache Maven Tutorial](http://www.baeldung.com/maven)
|
||||
- [Designing a User Friendly Java Library](http://www.baeldung.com/design-a-user-friendly-java-library)
|
||||
Building a single module
|
||||
====================
|
||||
To build a specific module run the command: `mvn clean install -Dgib.enabled=false` in the module directory
|
||||
|
@ -1,12 +1,9 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.mabsisa</groupId>
|
||||
<artifactId>Twitter4J</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>Twitter4J</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
@ -14,39 +11,16 @@
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.twitter4j</groupId>
|
||||
<artifactId>twitter4j-stream</artifactId>
|
||||
<version>4.0.6</version>
|
||||
<version>${twitter4j-stream.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/ApplicationTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<properties>
|
||||
<twitter4j-stream.version>4.0.6</twitter4j-stream.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
13
Twitter4J/src/main/resources/logback.xml
Normal file
13
Twitter4J/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -1,7 +1,6 @@
|
||||
<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">
|
||||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>activejdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
@ -15,12 +14,6 @@
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>activejdbc</artifactId>
|
||||
@ -85,58 +78,12 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<reportFormat>brief</reportFormat>
|
||||
<trimStackTrace>true</trimStackTrace>
|
||||
<useFile>false</useFile>
|
||||
<includes>
|
||||
<include>**/*Spec*.java</include>
|
||||
<include>**/*Test*.java</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/helpers/*</exclude>
|
||||
<exclude>**/*$*</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>snapshots1</id>
|
||||
<name>JavaLite Snapshots1</name>
|
||||
<url>http://repo.javalite.io/</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>always</updatePolicy>
|
||||
<checksumPolicy>warn</checksumPolicy>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>snapshots2</id>
|
||||
<name>JavaLite Snapshots2</name>
|
||||
<url>http://repo.javalite.io/</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
<updatePolicy>always</updatePolicy>
|
||||
<checksumPolicy>warn</checksumPolicy>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<activejdbc.version>1.4.13</activejdbc.version>
|
||||
<activejdbc.version>2.0</activejdbc.version>
|
||||
<environments>development.test,development</environments>
|
||||
<org.slf4j.version>1.7.9</org.slf4j.version>
|
||||
<mysql.connector.version>5.1.34</mysql.connector.version>
|
||||
</properties>
|
||||
|
||||
|
@ -16,4 +16,8 @@ public class Employee extends Model {
|
||||
set("created_by",createdBy);
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return getString("last_name");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,4 +15,8 @@ public class Role extends Model {
|
||||
set("role_name",role);
|
||||
set("created_by",createdBy);
|
||||
}
|
||||
|
||||
public String getRoleName() {
|
||||
return getString("role_name");
|
||||
}
|
||||
}
|
||||
|
13
activejdbc/src/main/resources/logback.xml
Normal file
13
activejdbc/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -7,7 +7,7 @@ import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ActiveJDBCAppTest extends DBSpec
|
||||
public class ActiveJDBCAppManualTest extends DBSpec
|
||||
{
|
||||
@Test
|
||||
public void ifEmployeeCreated_thenIsValid() {
|
@ -1,6 +1,6 @@
|
||||
<?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">
|
||||
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>akka-streams</artifactId>
|
||||
<name>akka-streams</name>
|
||||
|
13
akka-streams/src/main/resources/logback.xml
Normal file
13
akka-streams/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -5,7 +5,6 @@
|
||||
- [Ant Colony Optimization](http://www.baeldung.com/java-ant-colony-optimization)
|
||||
- [Validating Input With Finite Automata in Java](http://www.baeldung.com/java-finite-automata)
|
||||
- [Introduction to Jenetics Library](http://www.baeldung.com/jenetics)
|
||||
- [Check If a Number Is Prime in Java](http://www.baeldung.com/java-prime-numbers)
|
||||
- [Example of Hill Climbing Algorithm](http://www.baeldung.com/java-hill-climbing-algorithm)
|
||||
- [Monte Carlo Tree Search for Tic-Tac-Toe Game](http://www.baeldung.com/java-monte-carlo-tree-search)
|
||||
- [String Search Algorithms for Large Texts](http://www.baeldung.com/java-full-text-search-algorithms)
|
||||
@ -21,4 +20,14 @@
|
||||
- [Create a Sudoku Solver in Java](http://www.baeldung.com/java-sudoku)
|
||||
- [Displaying Money Amounts in Words](http://www.baeldung.com/java-money-into-words)
|
||||
- [A Collaborative Filtering Recommendation System in Java](http://www.baeldung.com/java-collaborative-filtering-recommendations)
|
||||
- [Find All Pairs of Numbers in an Array That Add Up to a Given Sum](http://www.baeldung.com/java-algorithm-number-pairs-sum)
|
||||
- [Converting Between Roman and Arabic Numerals in Java](http://www.baeldung.com/java-convert-roman-arabic)
|
||||
- [Practical Java Examples of the Big O Notation](http://www.baeldung.com/java-algorithm-complexity)
|
||||
- [Find the Middle Element of a Linked List](http://www.baeldung.com/java-linked-list-middle-element)
|
||||
- [An Introduction to the Theory of Big-O Notation](http://www.baeldung.com/big-o-notation)
|
||||
- [Check If Two Rectangles Overlap In Java](https://www.baeldung.com/java-check-if-two-rectangles-overlap)
|
||||
- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
|
||||
- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
|
||||
- [Check If a String Contains All The Letters of The Alphabet](https://www.baeldung.com/java-string-contains-all-letters)
|
||||
- [Round Up to the Nearest Hundred](https://www.baeldung.com/java-round-up-nearest-hundred)
|
||||
- [Merge Sort in Java](https://www.baeldung.com/java-merge-sort)
|
||||
- [Calculate Percentage in Java](https://www.baeldung.com/java-calculate-percentage)
|
||||
|
@ -1,5 +1,5 @@
|
||||
<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">
|
||||
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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>algorithms</artifactId>
|
||||
@ -17,6 +17,11 @@
|
||||
<artifactId>commons-math3</artifactId>
|
||||
<version>${commons-math3.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-codec</groupId>
|
||||
<artifactId>commons-codec</artifactId>
|
||||
<version>${commons-codec.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
@ -79,13 +84,13 @@
|
||||
</reporting>
|
||||
|
||||
<properties>
|
||||
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
|
||||
<lombok.version>1.16.12</lombok.version>
|
||||
<commons-math3.version>3.6.1</commons-math3.version>
|
||||
<tradukisto.version>1.0.1</tradukisto.version>
|
||||
<io.jenetics.version>3.7.0</io.jenetics.version>
|
||||
<org.jgrapht.core.version>1.0.1</org.jgrapht.core.version>
|
||||
<org.assertj.core.version>3.9.0</org.assertj.core.version>
|
||||
<commons-codec.version>1.11</commons-codec.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
1
algorithms/roundUpToHundred/.gitignore
vendored
Normal file
1
algorithms/roundUpToHundred/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/bin/
|
@ -0,0 +1,20 @@
|
||||
package com.java.src;
|
||||
|
||||
import java.util.Scanner;
|
||||
|
||||
public class RoundUpToHundred {
|
||||
|
||||
public static void main(String[] args) {
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
double input = scanner.nextDouble();
|
||||
scanner.close();
|
||||
|
||||
RoundUpToHundred.round(input);
|
||||
}
|
||||
|
||||
static long round(double input) {
|
||||
long i = (long) Math.ceil(input);
|
||||
return ((i + 99) / 100) * 100;
|
||||
};
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.java.src;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RoundUpToHundredTest {
|
||||
@Test
|
||||
public void givenInput_whenRound_thenRoundUpToTheNearestHundred() {
|
||||
assertEquals("Rounded up to hundred", 100, RoundUpToHundred.round(99));
|
||||
assertEquals("Rounded up to three hundred ", 300, RoundUpToHundred.round(200.2));
|
||||
assertEquals("Returns same rounded value", 400, RoundUpToHundred.round(400));
|
||||
}
|
||||
}
|
@ -6,7 +6,6 @@ import com.baeldung.algorithms.ga.annealing.SimulatedAnnealing;
|
||||
import com.baeldung.algorithms.ga.ant_colony.AntColonyOptimization;
|
||||
import com.baeldung.algorithms.ga.binary.SimpleGeneticAlgorithm;
|
||||
import com.baeldung.algorithms.slope_one.SlopeOne;
|
||||
import com.baeldung.algorithms.pairsaddupnumber.FindPairs;
|
||||
|
||||
public class RunAlgorithm {
|
||||
|
||||
@ -39,12 +38,6 @@ public class RunAlgorithm {
|
||||
case 5:
|
||||
System.out.println("Please run the DijkstraAlgorithmTest.");
|
||||
break;
|
||||
case 6:
|
||||
final FindPairs findPairs = new FindPairs();
|
||||
final int[] input = {1, 4, 3, 2, 1, 4, 4, 3, 3};
|
||||
final int sum = 6;
|
||||
findPairs.execute(input, sum);
|
||||
break;
|
||||
default:
|
||||
System.out.println("Unknown option");
|
||||
break;
|
||||
|
@ -0,0 +1,110 @@
|
||||
package com.baeldung.algorithms.conversion;
|
||||
|
||||
import java.math.BigInteger;
|
||||
|
||||
import javax.xml.bind.DatatypeConverter;
|
||||
|
||||
import org.apache.commons.codec.DecoderException;
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
|
||||
import com.google.common.io.BaseEncoding;
|
||||
|
||||
public class HexStringConverter {
|
||||
|
||||
/**
|
||||
* Create a byte Array from String of hexadecimal digits using Character conversion
|
||||
* @param hexString - Hexadecimal digits as String
|
||||
* @return Desired byte Array
|
||||
*/
|
||||
public byte[] decodeHexString(String hexString) {
|
||||
if (hexString.length() % 2 == 1) {
|
||||
throw new IllegalArgumentException("Invalid hexadecimal String supplied.");
|
||||
}
|
||||
byte[] bytes = new byte[hexString.length() / 2];
|
||||
|
||||
for (int i = 0; i < hexString.length(); i += 2) {
|
||||
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a String of hexadecimal digits from a byte Array using Character conversion
|
||||
* @param byteArray - The byte Array
|
||||
* @return Desired String of hexadecimal digits in lower case
|
||||
*/
|
||||
public String encodeHexString(byte[] byteArray) {
|
||||
StringBuffer hexStringBuffer = new StringBuffer();
|
||||
for (int i = 0; i < byteArray.length; i++) {
|
||||
hexStringBuffer.append(byteToHex(byteArray[i]));
|
||||
}
|
||||
return hexStringBuffer.toString();
|
||||
}
|
||||
|
||||
public String byteToHex(byte num) {
|
||||
char[] hexDigits = new char[2];
|
||||
hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
|
||||
hexDigits[1] = Character.forDigit((num & 0xF), 16);
|
||||
return new String(hexDigits);
|
||||
}
|
||||
|
||||
public byte hexToByte(String hexString) {
|
||||
int firstDigit = toDigit(hexString.charAt(0));
|
||||
int secondDigit = toDigit(hexString.charAt(1));
|
||||
return (byte) ((firstDigit << 4) + secondDigit);
|
||||
}
|
||||
|
||||
private int toDigit(char hexChar) {
|
||||
int digit = Character.digit(hexChar, 16);
|
||||
if(digit == -1) {
|
||||
throw new IllegalArgumentException("Invalid Hexadecimal Character: "+ hexChar);
|
||||
}
|
||||
return digit;
|
||||
}
|
||||
|
||||
public String encodeUsingBigIntegerToString(byte[] bytes) {
|
||||
BigInteger bigInteger = new BigInteger(1, bytes);
|
||||
return bigInteger.toString(16);
|
||||
}
|
||||
|
||||
public String encodeUsingBigIntegerStringFormat(byte[] bytes) {
|
||||
BigInteger bigInteger = new BigInteger(1, bytes);
|
||||
return String.format("%0" + (bytes.length << 1) + "x", bigInteger);
|
||||
}
|
||||
|
||||
public byte[] decodeUsingBigInteger(String hexString) {
|
||||
byte[] byteArray = new BigInteger(hexString, 16).toByteArray();
|
||||
if (byteArray[0] == 0) {
|
||||
byte[] output = new byte[byteArray.length - 1];
|
||||
System.arraycopy(byteArray, 1, output, 0, output.length);
|
||||
return output;
|
||||
}
|
||||
return byteArray;
|
||||
}
|
||||
|
||||
public String encodeUsingDataTypeConverter(byte[] bytes) {
|
||||
return DatatypeConverter.printHexBinary(bytes);
|
||||
}
|
||||
|
||||
public byte[] decodeUsingDataTypeConverter(String hexString) {
|
||||
return DatatypeConverter.parseHexBinary(hexString);
|
||||
}
|
||||
|
||||
public String encodeUsingApacheCommons(byte[] bytes) throws DecoderException {
|
||||
return Hex.encodeHexString(bytes);
|
||||
}
|
||||
|
||||
public byte[] decodeUsingApacheCommons(String hexString) throws DecoderException {
|
||||
return Hex.decodeHex(hexString);
|
||||
}
|
||||
|
||||
public String encodeUsingGuava(byte[] bytes) {
|
||||
return BaseEncoding.base16()
|
||||
.encode(bytes);
|
||||
}
|
||||
|
||||
public byte[] decodeUsingGuava(String hexString) {
|
||||
return BaseEncoding.base16()
|
||||
.decode(hexString.toUpperCase());
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.algorithms.distancebetweenpoints;
|
||||
|
||||
import java.awt.geom.Point2D;
|
||||
|
||||
public class DistanceBetweenPointsService {
|
||||
|
||||
public double calculateDistanceBetweenPoints(
|
||||
double x1,
|
||||
double y1,
|
||||
double x2,
|
||||
double y2) {
|
||||
|
||||
return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
|
||||
}
|
||||
|
||||
public double calculateDistanceBetweenPointsWithHypot(
|
||||
double x1,
|
||||
double y1,
|
||||
double x2,
|
||||
double y2) {
|
||||
|
||||
double ac = Math.abs(y2 - y1);
|
||||
double cb = Math.abs(x2 - x1);
|
||||
|
||||
return Math.hypot(ac, cb);
|
||||
}
|
||||
|
||||
public double calculateDistanceBetweenPointsWithPoint2D(
|
||||
double x1,
|
||||
double y1,
|
||||
double x2,
|
||||
double y2) {
|
||||
|
||||
return Point2D.distance(x1, y1, x2, y2);
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.baeldung.algorithms.insertionsort;
|
||||
|
||||
public class InsertionSort {
|
||||
|
||||
public static void insertionSortImperative(int[] input) {
|
||||
for (int i = 1; i < input.length; i++) {
|
||||
int key = input[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && input[j] > key) {
|
||||
input[j + 1] = input[j];
|
||||
j = j - 1;
|
||||
}
|
||||
input[j + 1] = key;
|
||||
}
|
||||
}
|
||||
|
||||
public static void insertionSortRecursive(int[] input) {
|
||||
insertionSortRecursive(input, input.length);
|
||||
}
|
||||
|
||||
private static void insertionSortRecursive(int[] input, int i) {
|
||||
// base case
|
||||
if (i <= 1) {
|
||||
return;
|
||||
}
|
||||
|
||||
// sort the first i - 1 elements of the array
|
||||
insertionSortRecursive(input, i - 1);
|
||||
|
||||
// then find the correct position of the element at position i
|
||||
int key = input[i - 1];
|
||||
int j = i - 2;
|
||||
// shifting the elements from their position by 1
|
||||
while (j >= 0 && input[j] > key) {
|
||||
input[j + 1] = input[j];
|
||||
j = j - 1;
|
||||
}
|
||||
// inserting the key at the appropriate position
|
||||
input[j + 1] = key;
|
||||
}
|
||||
}
|
@ -98,7 +98,7 @@ public class FindKthLargest {
|
||||
|
||||
private int randomPartition(Integer arr[], int left, int right) {
|
||||
int n = right - left + 1;
|
||||
int pivot = (int) (Math.random()) % n;
|
||||
int pivot = (int) (Math.random() * n);
|
||||
swap(arr, left + pivot, right);
|
||||
return partition(arr, left, right);
|
||||
}
|
||||
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.algorithms.linesintersection;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.Optional;
|
||||
|
||||
public class LinesIntersectionService {
|
||||
|
||||
public Optional<Point> calculateIntersectionPoint(double m1, double b1, double m2, double b2) {
|
||||
|
||||
if (m1 == m2) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
double x = (b2 - b1) / (m1 - m2);
|
||||
double y = m1 * x + b1;
|
||||
|
||||
Point point = new Point();
|
||||
point.setLocation(x, y);
|
||||
return Optional.of(point);
|
||||
}
|
||||
}
|
@ -10,7 +10,7 @@ public class MonteCarloTreeSearch {
|
||||
|
||||
private static final int WIN_SCORE = 10;
|
||||
private int level;
|
||||
private int oponent;
|
||||
private int opponent;
|
||||
|
||||
public MonteCarloTreeSearch() {
|
||||
this.level = 3;
|
||||
@ -32,11 +32,11 @@ public class MonteCarloTreeSearch {
|
||||
long start = System.currentTimeMillis();
|
||||
long end = start + 60 * getMillisForCurrentLevel();
|
||||
|
||||
oponent = 3 - playerNo;
|
||||
opponent = 3 - playerNo;
|
||||
Tree tree = new Tree();
|
||||
Node rootNode = tree.getRoot();
|
||||
rootNode.getState().setBoard(board);
|
||||
rootNode.getState().setPlayerNo(oponent);
|
||||
rootNode.getState().setPlayerNo(opponent);
|
||||
|
||||
while (System.currentTimeMillis() < end) {
|
||||
// Phase 1 - Selection
|
||||
@ -93,7 +93,7 @@ public class MonteCarloTreeSearch {
|
||||
State tempState = tempNode.getState();
|
||||
int boardStatus = tempState.getBoard().checkStatus();
|
||||
|
||||
if (boardStatus == oponent) {
|
||||
if (boardStatus == opponent) {
|
||||
tempNode.getParent().getState().setWinScore(Integer.MIN_VALUE);
|
||||
return boardStatus;
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ public class State {
|
||||
void randomPlay() {
|
||||
List<Position> availablePositions = this.board.getEmptyPositions();
|
||||
int totalPossibilities = availablePositions.size();
|
||||
int selectRandom = (int) (Math.random() * ((totalPossibilities - 1) + 1));
|
||||
int selectRandom = (int) (Math.random() * totalPossibilities);
|
||||
this.board.performMove(this.playerNo, availablePositions.get(selectRandom));
|
||||
}
|
||||
|
||||
|
@ -65,7 +65,7 @@ public class Node {
|
||||
|
||||
public Node getRandomChildNode() {
|
||||
int noOfPossibleMoves = this.childArray.size();
|
||||
int selectRandom = (int) (Math.random() * ((noOfPossibleMoves - 1) + 1));
|
||||
int selectRandom = (int) (Math.random() * noOfPossibleMoves);
|
||||
return this.childArray.get(selectRandom);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,50 @@
|
||||
package com.baeldung.algorithms.mergesort;
|
||||
|
||||
public class MergeSort {
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] a = { 5, 1, 6, 2, 3, 4 };
|
||||
mergeSort(a, a.length);
|
||||
for (int i = 0; i < a.length; i++)
|
||||
System.out.println(a[i]);
|
||||
}
|
||||
|
||||
public static void mergeSort(int[] a, int n) {
|
||||
if (n < 2)
|
||||
return;
|
||||
int mid = n / 2;
|
||||
int[] l = new int[mid];
|
||||
int[] r = new int[n - mid];
|
||||
|
||||
for (int i = 0; i < mid; i++) {
|
||||
l[i] = a[i];
|
||||
}
|
||||
for (int i = mid; i < n; i++) {
|
||||
r[i - mid] = a[i];
|
||||
}
|
||||
mergeSort(l, mid);
|
||||
mergeSort(r, n - mid);
|
||||
|
||||
merge(a, l, r, mid, n - mid);
|
||||
}
|
||||
|
||||
public static void merge(int[] a, int[] l, int[] r, int left, int right) {
|
||||
|
||||
int i = 0, j = 0, k = 0;
|
||||
|
||||
while (i < left && j < right) {
|
||||
|
||||
if (l[i] < r[j])
|
||||
a[k++] = l[i++];
|
||||
else
|
||||
a[k++] = r[j++];
|
||||
|
||||
}
|
||||
|
||||
while (i < left)
|
||||
a[k++] = l[i++];
|
||||
|
||||
while (j < right)
|
||||
a[k++] = r[j++];
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package com.baeldung.algorithms.prime;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class PrimeGenerator {
|
||||
public static List<Integer> sieveOfEratosthenes(int n) {
|
||||
final boolean prime[] = new boolean[n + 1];
|
||||
Arrays.fill(prime, true);
|
||||
|
||||
for (int p = 2; p * p <= n; p++) {
|
||||
if (prime[p]) {
|
||||
for (int i = p * 2; i <= n; i += p)
|
||||
prime[i] = false;
|
||||
}
|
||||
}
|
||||
|
||||
final List<Integer> primes = new LinkedList<>();
|
||||
for (int i = 2; i <= n; i++) {
|
||||
if (prime[i])
|
||||
primes.add(i);
|
||||
}
|
||||
return primes;
|
||||
}
|
||||
|
||||
public static List<Integer> primeNumbersBruteForce(int max) {
|
||||
final List<Integer> primeNumbers = new LinkedList<Integer>();
|
||||
for (int i = 2; i <= max; i++) {
|
||||
if (isPrimeBruteForce(i)) {
|
||||
primeNumbers.add(i);
|
||||
}
|
||||
}
|
||||
return primeNumbers;
|
||||
}
|
||||
|
||||
private static boolean isPrimeBruteForce(int x) {
|
||||
for (int i = 2; i < x; i++) {
|
||||
if (x % i == 0) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static List<Integer> primeNumbersTill(int max) {
|
||||
return IntStream.rangeClosed(2, max)
|
||||
.filter(x -> isPrime(x))
|
||||
.boxed()
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private static boolean isPrime(int x) {
|
||||
return IntStream.rangeClosed(2, (int) (Math.sqrt(x)))
|
||||
.allMatch(n -> x % n != 0);
|
||||
}
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
public class QuickSort {
|
||||
|
||||
public static void quickSort(int arr[], int begin, int end)
|
||||
{
|
||||
if (begin < end) {
|
||||
int partitionIndex = partition(arr, begin, end);
|
||||
|
||||
// Recursively sort elements of the 2 sub-arrays
|
||||
quickSort(arr, begin, partitionIndex-1);
|
||||
quickSort(arr, partitionIndex+1, end);
|
||||
}
|
||||
}
|
||||
|
||||
private static int partition(int arr[], int begin, int end)
|
||||
{
|
||||
int pivot = arr[end];
|
||||
int i = (begin-1);
|
||||
|
||||
for (int j=begin; j<end; j++)
|
||||
{
|
||||
if (arr[j] <= pivot) {
|
||||
i++;
|
||||
|
||||
int swapTemp = arr[i];
|
||||
arr[i] = arr[j];
|
||||
arr[j] = swapTemp;
|
||||
}
|
||||
}
|
||||
|
||||
int swapTemp = arr[i+1];
|
||||
arr[i+1] = arr[end];
|
||||
arr[end] = swapTemp;
|
||||
|
||||
return i+1;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
public class ThreeWayQuickSort {
|
||||
|
||||
public static void threeWayQuickSort(int[] a, int begin, int end)
|
||||
{
|
||||
if (end <= begin) return;
|
||||
|
||||
// partition
|
||||
int i = begin;
|
||||
int less = begin;
|
||||
int greater = end;
|
||||
|
||||
while (i <= greater){
|
||||
if (a[i] < a[less]) {
|
||||
int tmp = a[i];
|
||||
a[i] = a[less];
|
||||
a[less] = tmp;
|
||||
|
||||
i++;
|
||||
less++;
|
||||
}
|
||||
else if (a[less] < a[i]) {
|
||||
int tmp = a[i];
|
||||
a[i] = a[greater];
|
||||
a[greater] = tmp;
|
||||
|
||||
greater--;
|
||||
}
|
||||
else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
threeWayQuickSort(a, begin, less - 1);
|
||||
threeWayQuickSort(a, greater + 1, end);
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package com.baeldung.algorithms.rectanglesoverlap;
|
||||
|
||||
public class Point {
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Point(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public void setX(int x) {
|
||||
this.x = x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
public void setY(int y) {
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.baeldung.algorithms.rectanglesoverlap;
|
||||
|
||||
public class Rectangle {
|
||||
|
||||
private Point bottomLeft;
|
||||
private Point topRight;
|
||||
|
||||
public Rectangle(Point bottomLeft, Point topRight) {
|
||||
this.bottomLeft = bottomLeft;
|
||||
this.topRight = topRight;
|
||||
}
|
||||
|
||||
public Point getBottomLeft() {
|
||||
return bottomLeft;
|
||||
}
|
||||
|
||||
public void setBottomLeft(Point bottomLeft) {
|
||||
this.bottomLeft = bottomLeft;
|
||||
}
|
||||
|
||||
public Point getTopRight() {
|
||||
return topRight;
|
||||
}
|
||||
|
||||
public void setTopRight(Point topRight) {
|
||||
this.topRight = topRight;
|
||||
}
|
||||
|
||||
public boolean isOverlapping(Rectangle other) {
|
||||
// one rectangle is to the top of the other
|
||||
if (this.topRight.getY() < other.bottomLeft.getY() || this.bottomLeft.getY() > other.topRight.getY()) {
|
||||
return false;
|
||||
}
|
||||
// one rectangle is to the left of the other
|
||||
if (this.topRight.getX() < other.bottomLeft.getX() || this.bottomLeft.getX() > other.topRight.getX()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.baeldung.algorithms.string;
|
||||
|
||||
public class EnglishAlphabetLetters {
|
||||
|
||||
public static boolean checkStringForAllTheLetters(String input) {
|
||||
boolean[] visited = new boolean[26];
|
||||
|
||||
int index = 0;
|
||||
|
||||
for (int id = 0; id < input.length(); id++) {
|
||||
if ('a' <= input.charAt(id) && input.charAt(id) <= 'z') {
|
||||
index = input.charAt(id) - 'a';
|
||||
} else if ('A' <= input.charAt(id) && input.charAt(id) <= 'Z') {
|
||||
index = input.charAt(id) - 'A';
|
||||
}
|
||||
visited[index] = true;
|
||||
}
|
||||
|
||||
for (int id = 0; id < 26; id++) {
|
||||
if (!visited[id]) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public static boolean checkStringForAllLetterUsingStream(String input) {
|
||||
long c = input.toLowerCase().chars().filter(ch -> ch >= 'a' && ch <= 'z').distinct().count();
|
||||
return c == 26;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
checkStringForAllLetterUsingStream("intit");
|
||||
}
|
||||
}
|
13
algorithms/src/main/resources/logback.xml
Normal file
13
algorithms/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -1,4 +1,4 @@
|
||||
package algorithms;
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
@ -1,4 +1,4 @@
|
||||
package algorithms;
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
@ -1,4 +1,4 @@
|
||||
package algorithms;
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
import org.junit.Test;
|
||||
|
@ -1,4 +1,4 @@
|
||||
package algorithms;
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
import com.baeldung.algorithms.hillclimbing.HillClimbing;
|
||||
import com.baeldung.algorithms.hillclimbing.State;
|
@ -1,4 +1,4 @@
|
||||
package algorithms;
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
import com.baeldung.algorithms.middleelementlookup.MiddleElementLookup;
|
||||
import com.baeldung.algorithms.middleelementlookup.Node;
|
@ -1,4 +1,4 @@
|
||||
package algorithms;
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
import com.baeldung.algorithms.automata.*;
|
||||
import org.junit.Test;
|
@ -1,4 +1,4 @@
|
||||
package algorithms;
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
@ -1,4 +1,4 @@
|
||||
package algorithms;
|
||||
package com.baeldung.algorithms;
|
||||
|
||||
|
||||
import org.junit.Assert;
|
@ -1,4 +1,4 @@
|
||||
package algorithms.binarysearch;
|
||||
package com.baeldung.algorithms.binarysearch;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
@ -0,0 +1,127 @@
|
||||
package com.baeldung.algorithms.conversion;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import org.apache.commons.codec.DecoderException;
|
||||
import org.hamcrest.text.IsEqualIgnoringCase;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.conversion.HexStringConverter;
|
||||
|
||||
public class ByteArrayConverterUnitTest {
|
||||
|
||||
private HexStringConverter hexStringConverter;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
hexStringConverter = new HexStringConverter();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodeByteArrayToHexStringUsingBigIntegerToString() {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
if(hexString.charAt(0) == '0') {
|
||||
hexString = hexString.substring(1);
|
||||
}
|
||||
String output = hexStringConverter.encodeUsingBigIntegerToString(bytes);
|
||||
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodeByteArrayToHexStringUsingBigIntegerStringFormat() {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
String output = hexStringConverter.encodeUsingBigIntegerStringFormat(bytes);
|
||||
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDecodeHexStringToByteArrayUsingBigInteger() {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
byte[] output = hexStringConverter.decodeUsingBigInteger(hexString);
|
||||
assertArrayEquals(bytes, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodeByteArrayToHexStringUsingCharacterConversion() {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
String output = hexStringConverter.encodeHexString(bytes);
|
||||
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDecodeHexStringToByteArrayUsingCharacterConversion() {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
byte[] output = hexStringConverter.decodeHexString(hexString);
|
||||
assertArrayEquals(bytes, output);
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void shouldDecodeHexToByteWithInvalidHexCharacter() {
|
||||
hexStringConverter.hexToByte("fg");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodeByteArrayToHexStringDataTypeConverter() {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
String output = hexStringConverter.encodeUsingDataTypeConverter(bytes);
|
||||
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDecodeHexStringToByteArrayUsingDataTypeConverter() {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
byte[] output = hexStringConverter.decodeUsingDataTypeConverter(hexString);
|
||||
assertArrayEquals(bytes, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodeByteArrayToHexStringUsingGuava() {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
String output = hexStringConverter.encodeUsingGuava(bytes);
|
||||
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDecodeHexStringToByteArrayUsingGuava() {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
byte[] output = hexStringConverter.decodeUsingGuava(hexString);
|
||||
assertArrayEquals(bytes, output);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldEncodeByteArrayToHexStringUsingApacheCommons() throws DecoderException {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
String output = hexStringConverter.encodeUsingApacheCommons(bytes);
|
||||
assertThat(output, IsEqualIgnoringCase.equalToIgnoringCase(hexString));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldDecodeHexStringToByteArrayUsingApacheCommons() throws DecoderException {
|
||||
byte[] bytes = getSampleBytes();
|
||||
String hexString = getSampleHexString();
|
||||
byte[] output = hexStringConverter.decodeUsingApacheCommons(hexString);
|
||||
assertArrayEquals(bytes, output);
|
||||
}
|
||||
|
||||
private String getSampleHexString() {
|
||||
return "0af50c0e2d10";
|
||||
}
|
||||
|
||||
private byte[] getSampleBytes() {
|
||||
return new byte[] { 10, -11, 12, 14, 45, 16 };
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.baeldung.algorithms.distancebetweenpoints;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.distancebetweenpoints.DistanceBetweenPointsService;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class DistanceBetweenPointsServiceUnitTest {
|
||||
|
||||
private DistanceBetweenPointsService service = new DistanceBetweenPointsService();
|
||||
|
||||
@Test
|
||||
public void givenTwoPoints_whenCalculateDistanceByFormula_thenCorrect() {
|
||||
|
||||
double x1 = 3;
|
||||
double y1 = 4;
|
||||
double x2 = 7;
|
||||
double y2 = 1;
|
||||
|
||||
double distance = service.calculateDistanceBetweenPoints(x1, y1, x2, y2);
|
||||
|
||||
assertEquals(distance, 5, 0.001);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoPoints_whenCalculateDistanceWithHypot_thenCorrect() {
|
||||
|
||||
double x1 = 3;
|
||||
double y1 = 4;
|
||||
double x2 = 7;
|
||||
double y2 = 1;
|
||||
|
||||
double distance = service.calculateDistanceBetweenPointsWithHypot(x1, y1, x2, y2);
|
||||
|
||||
assertEquals(distance, 5, 0.001);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoPoints_whenCalculateDistanceWithPoint2D_thenCorrect() {
|
||||
|
||||
double x1 = 3;
|
||||
double y1 = 4;
|
||||
double x2 = 7;
|
||||
double y2 = 1;
|
||||
|
||||
double distance = service.calculateDistanceBetweenPointsWithPoint2D(x1, y1, x2, y2);
|
||||
|
||||
assertEquals(distance, 5, 0.001);
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.algorithms.insertionsort;
|
||||
|
||||
import com.baeldung.algorithms.insertionsort.InsertionSort;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
public class InsertionSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenInsertionSortImperative_thenSortedAsc() {
|
||||
int[] input = {6, 2, 3, 4, 5, 1};
|
||||
InsertionSort.insertionSortImperative(input);
|
||||
int[] expected = {1, 2, 3, 4, 5, 6};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenUnsortedArray_whenInsertionSortRecursive_thenSortedAsc() {
|
||||
int[] input = {6, 4, 5, 2, 3, 1};
|
||||
InsertionSort.insertionSortRecursive(input);
|
||||
int[] expected = {1, 2, 3, 4, 5, 6};
|
||||
assertArrayEquals("the two arrays are not equal", expected, input);
|
||||
}
|
||||
}
|
@ -0,0 +1,40 @@
|
||||
package com.baeldung.algorithms.linesintersection;
|
||||
|
||||
import java.awt.Point;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class LinesIntersectionServiceUnitTest {
|
||||
private LinesIntersectionService service = new LinesIntersectionService();
|
||||
|
||||
@Test
|
||||
public void givenNotParallelLines_whenCalculatePoint_thenPresent() {
|
||||
|
||||
double m1 = 0;
|
||||
double b1 = 0;
|
||||
double m2 = 1;
|
||||
double b2 = -1;
|
||||
|
||||
Optional<Point> point = service.calculateIntersectionPoint(m1, b1, m2, b2);
|
||||
|
||||
assertTrue(point.isPresent());
|
||||
assertEquals(point.get().getX(), 1, 0.001);
|
||||
assertEquals(point.get().getY(), 0, 0.001);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenParallelLines_whenCalculatePoint_thenEmpty() {
|
||||
double m1 = 1;
|
||||
double b1 = 0;
|
||||
double m2 = 1;
|
||||
double b2 = -1;
|
||||
|
||||
Optional<Point> point = service.calculateIntersectionPoint(m1, b1, m2, b2);
|
||||
|
||||
assertFalse(point.isPresent());
|
||||
}
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package algorithms.mcts;
|
||||
package com.baeldung.algorithms.mcts;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
@ -0,0 +1,17 @@
|
||||
package com.baeldung.algorithms.mergesort;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class MergeSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void positiveTest() {
|
||||
int[] actual = { 5, 1, 6, 2, 3, 4 };
|
||||
int[] expected = { 1, 2, 3, 4, 5, 6 };
|
||||
MergeSort.mergeSort(actual, actual.length);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package algorithms.minimax;
|
||||
package com.baeldung.algorithms.minimax;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
@ -1,28 +0,0 @@
|
||||
package com.baeldung.algorithms.prime;
|
||||
|
||||
import static com.baeldung.algorithms.prime.PrimeGenerator.*;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class PrimeGeneratorUnitTest {
|
||||
@Test
|
||||
public void whenBruteForced_returnsSuccessfully() {
|
||||
final List<Integer> primeNumbers = primeNumbersBruteForce(20);
|
||||
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOptimized_returnsSuccessfully() {
|
||||
final List<Integer> primeNumbers = primeNumbersTill(20);
|
||||
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenSieveOfEratosthenes_returnsSuccessfully() {
|
||||
final List<Integer> primeNumbers = sieveOfEratosthenes(20);
|
||||
assertEquals(Arrays.asList(new Integer[] { 2, 3, 5, 7, 11, 13, 17, 19 }), primeNumbers);
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
import com.baeldung.algorithms.quicksort.QuickSort;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class QuickSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithQuickSort_thenGetSortedArray() {
|
||||
int[] actual = { 9, 5, 1, 0, 6, 2, 3, 4, 7, 8 };
|
||||
int[] expected = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
||||
QuickSort.quickSort(actual, 0, actual.length-1);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.baeldung.algorithms.quicksort;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class ThreeWayQuickSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithThreeWayQuickSort_thenGetSortedArray() {
|
||||
int[] actual = { 3, 5, 5, 5, 3, 7, 7, 3, 5, 5, 7, 3, 3 };
|
||||
int[] expected = { 3, 3, 3, 3, 3, 5, 5, 5, 5, 5, 7, 7, 7 };
|
||||
ThreeWayQuickSort.threeWayQuickSort(actual, 0, actual.length-1);
|
||||
Assert.assertArrayEquals(expected, actual);
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.baeldung.algorithms.rectanglesoverlap;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.rectanglesoverlap.Point;
|
||||
import com.baeldung.algorithms.rectanglesoverlap.Rectangle;
|
||||
|
||||
public class RectangleUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoOverlappingRectangles_whenisOverlappingCalled_shouldReturnTrue() {
|
||||
Rectangle rectangle1 = new Rectangle(new Point(2, 1), new Point(4, 3));
|
||||
Rectangle rectangle2 = new Rectangle(new Point(1, 1), new Point(6, 4));
|
||||
assertTrue(rectangle1.isOverlapping(rectangle2));
|
||||
|
||||
rectangle1 = new Rectangle(new Point(-5, -2), new Point(2, 3));
|
||||
rectangle2 = new Rectangle(new Point(-2, -1), new Point(5, 2));
|
||||
assertTrue(rectangle1.isOverlapping(rectangle2));
|
||||
|
||||
rectangle1 = new Rectangle(new Point(-5, 1), new Point(2, 4));
|
||||
rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, 5));
|
||||
assertTrue(rectangle1.isOverlapping(rectangle2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenTwoNonOverlappingRectangles_whenisOverlappingCalled_shouldReturnFalse() {
|
||||
Rectangle rectangle1 = new Rectangle(new Point(-5, 1), new Point(-3, 4));
|
||||
Rectangle rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, 5));
|
||||
assertFalse(rectangle1.isOverlapping(rectangle2));
|
||||
|
||||
rectangle1 = new Rectangle(new Point(-5, 1), new Point(3, 4));
|
||||
rectangle2 = new Rectangle(new Point(-2, -2), new Point(5, -1));
|
||||
assertFalse(rectangle1.isOverlapping(rectangle2));
|
||||
|
||||
rectangle1 = new Rectangle(new Point(-2, 1), new Point(0, 3));
|
||||
rectangle2 = new Rectangle(new Point(3, 1), new Point(5, 4));
|
||||
assertFalse(rectangle1.isOverlapping(rectangle2));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.string;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class EnglishAlphabetLettersUnitTest {
|
||||
|
||||
@Test
|
||||
void givenString_whenContainsAllCharacter_thenTrue() {
|
||||
String input = "Farmer jack realized that big yellow quilts were expensive";
|
||||
Assertions.assertTrue(EnglishAlphabetLetters.checkStringForAllTheLetters(input));
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenString_whenContainsAllCharacter_thenUsingStreamExpectTrue() {
|
||||
String input = "Farmer jack realized that big yellow quilts were expensive";
|
||||
Assertions.assertTrue(EnglishAlphabetLetters.checkStringForAllLetterUsingStream(input));
|
||||
}
|
||||
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>animal-sniffer-mvn-plugin</artifactId>
|
||||
@ -14,15 +14,6 @@
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
|
13
animal-sniffer-mvn-plugin/src/main/resources/logback.xml
Normal file
13
animal-sniffer-mvn-plugin/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -7,7 +7,7 @@ import junit.framework.TestSuite;
|
||||
/**
|
||||
* Unit test for simple App.
|
||||
*/
|
||||
public class AppTest
|
||||
public class AppUnitTest
|
||||
extends TestCase
|
||||
{
|
||||
/**
|
||||
@ -15,7 +15,7 @@ public class AppTest
|
||||
*
|
||||
* @param testName name of the test case
|
||||
*/
|
||||
public AppTest( String testName )
|
||||
public AppUnitTest( String testName )
|
||||
{
|
||||
super( testName );
|
||||
}
|
||||
@ -25,7 +25,7 @@ public class AppTest
|
||||
*/
|
||||
public static Test suite()
|
||||
{
|
||||
return new TestSuite( AppTest.class );
|
||||
return new TestSuite( AppUnitTest.class );
|
||||
}
|
||||
|
||||
/**
|
@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
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>annotation-processing</artifactId>
|
||||
|
||||
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -1,6 +1,6 @@
|
||||
<?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">
|
||||
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>annotation-user</artifactId>
|
||||
|
||||
|
13
annotations/annotation-user/src/main/resources/logback.xml
Normal file
13
annotations/annotation-user/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -1,6 +1,6 @@
|
||||
<?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">
|
||||
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>annotations</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
3
antlr/README.md
Normal file
3
antlr/README.md
Normal file
@ -0,0 +1,3 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Java with ANTLR](http://www.baeldung.com/java-antlr)
|
@ -1,5 +1,5 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>antlr</artifactId>
|
||||
<name>antlr</name>
|
||||
@ -45,22 +45,14 @@
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-runtime</artifactId>
|
||||
<version>${antlr.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-runtime</artifactId>
|
||||
<version>${antlr.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<antlr.version>4.7.1</antlr.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
<mojo.version>3.0.0</mojo.version>
|
||||
</properties>
|
||||
</project>
|
13
antlr/src/main/resources/logback.xml
Normal file
13
antlr/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
2
apache-avro/README.md
Normal file
2
apache-avro/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Guide to Apache Avro](http://www.baeldung.com/java-apache-avro)
|
89
apache-avro/pom.xml
Normal file
89
apache-avro/pom.xml
Normal file
@ -0,0 +1,89 @@
|
||||
<?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>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-avro</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>Apache Avro</name>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<compiler-plugin.version>3.5</compiler-plugin.version>
|
||||
<avro.version>1.8.2</avro.version>
|
||||
<java.version>1.8</java.version>
|
||||
<slf4j.version>1.7.25</slf4j.version>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.10</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro-compiler</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro-maven-plugin</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro-maven-plugin</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>schemas</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>schema</goal>
|
||||
<goal>protocol</goal>
|
||||
<goal>idl-protocol</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirectory>${project.basedir}/src/main/resources/</sourceDirectory>
|
||||
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
@ -0,0 +1,14 @@
|
||||
package com.baeldung.avro.util;
|
||||
|
||||
import org.apache.avro.Schema;
|
||||
import org.apache.avro.compiler.specific.SpecificCompiler;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AvroClassGenerator {
|
||||
public void generateAvroClasses() throws IOException {
|
||||
SpecificCompiler compiler = new SpecificCompiler(new Schema.Parser().parse(new File("src/main/resources/avroHttpRequest-schema.avsc")));
|
||||
compiler.compileToDestination(new File("src/main/resources"), new File("src/main/java"));
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.avro.util;
|
||||
|
||||
|
||||
import org.apache.avro.Schema;
|
||||
import org.apache.avro.SchemaBuilder;
|
||||
|
||||
public class AvroSchemaBuilder {
|
||||
|
||||
public Schema createAvroHttpRequestSchema(){
|
||||
|
||||
Schema clientIdentifier = SchemaBuilder.record("ClientIdentifier").namespace("com.baeldung.avro.model")
|
||||
.fields().requiredString("hostName").requiredString("ipAddress").endRecord();
|
||||
|
||||
Schema avroHttpRequest = SchemaBuilder.record("AvroHttpRequest").namespace("com.baeldung.avro.model").fields()
|
||||
.requiredLong("requestTime")
|
||||
.name("clientIdentifier").type(clientIdentifier).noDefault()
|
||||
.name("employeeNames").type().array().items().stringType().arrayDefault(null)
|
||||
.name("active").type().enumeration("Active").symbols("YES", "NO").noDefault()
|
||||
.endRecord();
|
||||
return avroHttpRequest;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,13 @@
|
||||
/**
|
||||
* Autogenerated by Avro
|
||||
*
|
||||
* DO NOT EDIT DIRECTLY
|
||||
*/
|
||||
package com.baeldung.avro.util.model;
|
||||
@SuppressWarnings("all")
|
||||
@org.apache.avro.specific.AvroGenerated
|
||||
public enum Active {
|
||||
YES, NO ;
|
||||
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"enum\",\"name\":\"Active\",\"namespace\":\"com.baeldung.avro.model\",\"symbols\":[\"YES\",\"NO\"]}");
|
||||
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
|
||||
}
|
@ -0,0 +1,491 @@
|
||||
/**
|
||||
* Autogenerated by Avro
|
||||
*
|
||||
* DO NOT EDIT DIRECTLY
|
||||
*/
|
||||
package com.baeldung.avro.util.model;
|
||||
|
||||
import org.apache.avro.specific.SpecificData;
|
||||
import org.apache.avro.message.BinaryMessageEncoder;
|
||||
import org.apache.avro.message.BinaryMessageDecoder;
|
||||
import org.apache.avro.message.SchemaStore;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
@org.apache.avro.specific.AvroGenerated
|
||||
public class AvroHttpRequest extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
|
||||
private static final long serialVersionUID = -8649010116827875312L;
|
||||
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"AvroHttpRequest\",\"namespace\":\"com.baeldung.avro.model\",\"fields\":[{\"name\":\"requestTime\",\"type\":\"long\"},{\"name\":\"clientIdentifier\",\"type\":{\"type\":\"record\",\"name\":\"ClientIdentifier\",\"fields\":[{\"name\":\"hostName\",\"type\":\"string\"},{\"name\":\"ipAddress\",\"type\":\"string\"}]}},{\"name\":\"employeeNames\",\"type\":{\"type\":\"array\",\"items\":\"string\"},\"default\":null},{\"name\":\"active\",\"type\":{\"type\":\"enum\",\"name\":\"Active\",\"symbols\":[\"YES\",\"NO\"]}}]}");
|
||||
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
|
||||
|
||||
private static SpecificData MODEL$ = new SpecificData();
|
||||
|
||||
private static final BinaryMessageEncoder<AvroHttpRequest> ENCODER =
|
||||
new BinaryMessageEncoder<AvroHttpRequest>(MODEL$, SCHEMA$);
|
||||
|
||||
private static final BinaryMessageDecoder<AvroHttpRequest> DECODER =
|
||||
new BinaryMessageDecoder<AvroHttpRequest>(MODEL$, SCHEMA$);
|
||||
|
||||
/**
|
||||
* Return the BinaryMessageDecoder instance used by this class.
|
||||
*/
|
||||
public static BinaryMessageDecoder<AvroHttpRequest> getDecoder() {
|
||||
return DECODER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
|
||||
* @param resolver a {@link SchemaStore} used to find schemas by fingerprint
|
||||
*/
|
||||
public static BinaryMessageDecoder<AvroHttpRequest> createDecoder(SchemaStore resolver) {
|
||||
return new BinaryMessageDecoder<AvroHttpRequest>(MODEL$, SCHEMA$, resolver);
|
||||
}
|
||||
|
||||
/** Serializes this AvroHttpRequest to a ByteBuffer. */
|
||||
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
|
||||
return ENCODER.encode(this);
|
||||
}
|
||||
|
||||
/** Deserializes a AvroHttpRequest from a ByteBuffer. */
|
||||
public static AvroHttpRequest fromByteBuffer(
|
||||
java.nio.ByteBuffer b) throws java.io.IOException {
|
||||
return DECODER.decode(b);
|
||||
}
|
||||
|
||||
@Deprecated public long requestTime;
|
||||
@Deprecated public ClientIdentifier clientIdentifier;
|
||||
@Deprecated public java.util.List<java.lang.CharSequence> employeeNames;
|
||||
@Deprecated public Active active;
|
||||
|
||||
/**
|
||||
* Default constructor. Note that this does not initialize fields
|
||||
* to their default values from the schema. If that is desired then
|
||||
* one should use <code>newBuilder()</code>.
|
||||
*/
|
||||
public AvroHttpRequest() {}
|
||||
|
||||
/**
|
||||
* All-args constructor.
|
||||
* @param requestTime The new value for requestTime
|
||||
* @param clientIdentifier The new value for clientIdentifier
|
||||
* @param employeeNames The new value for employeeNames
|
||||
* @param active The new value for active
|
||||
*/
|
||||
public AvroHttpRequest(java.lang.Long requestTime, ClientIdentifier clientIdentifier, java.util.List<java.lang.CharSequence> employeeNames, Active active) {
|
||||
this.requestTime = requestTime;
|
||||
this.clientIdentifier = clientIdentifier;
|
||||
this.employeeNames = employeeNames;
|
||||
this.active = active;
|
||||
}
|
||||
|
||||
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
|
||||
// Used by DatumWriter. Applications should not call.
|
||||
public java.lang.Object get(int field$) {
|
||||
switch (field$) {
|
||||
case 0: return requestTime;
|
||||
case 1: return clientIdentifier;
|
||||
case 2: return employeeNames;
|
||||
case 3: return active;
|
||||
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
|
||||
}
|
||||
}
|
||||
|
||||
// Used by DatumReader. Applications should not call.
|
||||
@SuppressWarnings(value="unchecked")
|
||||
public void put(int field$, java.lang.Object value$) {
|
||||
switch (field$) {
|
||||
case 0: requestTime = (java.lang.Long)value$; break;
|
||||
case 1: clientIdentifier = (ClientIdentifier)value$; break;
|
||||
case 2: employeeNames = (java.util.List<java.lang.CharSequence>)value$; break;
|
||||
case 3: active = (Active)value$; break;
|
||||
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'requestTime' field.
|
||||
* @return The value of the 'requestTime' field.
|
||||
*/
|
||||
public java.lang.Long getRequestTime() {
|
||||
return requestTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'requestTime' field.
|
||||
* @param value the value to set.
|
||||
*/
|
||||
public void setRequestTime(java.lang.Long value) {
|
||||
this.requestTime = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'clientIdentifier' field.
|
||||
* @return The value of the 'clientIdentifier' field.
|
||||
*/
|
||||
public ClientIdentifier getClientIdentifier() {
|
||||
return clientIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'clientIdentifier' field.
|
||||
* @param value the value to set.
|
||||
*/
|
||||
public void setClientIdentifier(ClientIdentifier value) {
|
||||
this.clientIdentifier = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'employeeNames' field.
|
||||
* @return The value of the 'employeeNames' field.
|
||||
*/
|
||||
public java.util.List<java.lang.CharSequence> getEmployeeNames() {
|
||||
return employeeNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'employeeNames' field.
|
||||
* @param value the value to set.
|
||||
*/
|
||||
public void setEmployeeNames(java.util.List<java.lang.CharSequence> value) {
|
||||
this.employeeNames = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'active' field.
|
||||
* @return The value of the 'active' field.
|
||||
*/
|
||||
public Active getActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'active' field.
|
||||
* @param value the value to set.
|
||||
*/
|
||||
public void setActive(Active value) {
|
||||
this.active = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new AvroHttpRequest RecordBuilder.
|
||||
* @return A new AvroHttpRequest RecordBuilder
|
||||
*/
|
||||
public static AvroHttpRequest.Builder newBuilder() {
|
||||
return new AvroHttpRequest.Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new AvroHttpRequest RecordBuilder by copying an existing Builder.
|
||||
* @param other The existing builder to copy.
|
||||
* @return A new AvroHttpRequest RecordBuilder
|
||||
*/
|
||||
public static AvroHttpRequest.Builder newBuilder(AvroHttpRequest.Builder other) {
|
||||
return new AvroHttpRequest.Builder(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new AvroHttpRequest RecordBuilder by copying an existing AvroHttpRequest instance.
|
||||
* @param other The existing instance to copy.
|
||||
* @return A new AvroHttpRequest RecordBuilder
|
||||
*/
|
||||
public static AvroHttpRequest.Builder newBuilder(AvroHttpRequest other) {
|
||||
return new AvroHttpRequest.Builder(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* RecordBuilder for AvroHttpRequest instances.
|
||||
*/
|
||||
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<AvroHttpRequest>
|
||||
implements org.apache.avro.data.RecordBuilder<AvroHttpRequest> {
|
||||
|
||||
private long requestTime;
|
||||
private ClientIdentifier clientIdentifier;
|
||||
private ClientIdentifier.Builder clientIdentifierBuilder;
|
||||
private java.util.List<java.lang.CharSequence> employeeNames;
|
||||
private Active active;
|
||||
|
||||
/** Creates a new Builder */
|
||||
private Builder() {
|
||||
super(SCHEMA$);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Builder by copying an existing Builder.
|
||||
* @param other The existing Builder to copy.
|
||||
*/
|
||||
private Builder(AvroHttpRequest.Builder other) {
|
||||
super(other);
|
||||
if (isValidValue(fields()[0], other.requestTime)) {
|
||||
this.requestTime = data().deepCopy(fields()[0].schema(), other.requestTime);
|
||||
fieldSetFlags()[0] = true;
|
||||
}
|
||||
if (isValidValue(fields()[1], other.clientIdentifier)) {
|
||||
this.clientIdentifier = data().deepCopy(fields()[1].schema(), other.clientIdentifier);
|
||||
fieldSetFlags()[1] = true;
|
||||
}
|
||||
if (other.hasClientIdentifierBuilder()) {
|
||||
this.clientIdentifierBuilder = ClientIdentifier.newBuilder(other.getClientIdentifierBuilder());
|
||||
}
|
||||
if (isValidValue(fields()[2], other.employeeNames)) {
|
||||
this.employeeNames = data().deepCopy(fields()[2].schema(), other.employeeNames);
|
||||
fieldSetFlags()[2] = true;
|
||||
}
|
||||
if (isValidValue(fields()[3], other.active)) {
|
||||
this.active = data().deepCopy(fields()[3].schema(), other.active);
|
||||
fieldSetFlags()[3] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Builder by copying an existing AvroHttpRequest instance
|
||||
* @param other The existing instance to copy.
|
||||
*/
|
||||
private Builder(AvroHttpRequest other) {
|
||||
super(SCHEMA$);
|
||||
if (isValidValue(fields()[0], other.requestTime)) {
|
||||
this.requestTime = data().deepCopy(fields()[0].schema(), other.requestTime);
|
||||
fieldSetFlags()[0] = true;
|
||||
}
|
||||
if (isValidValue(fields()[1], other.clientIdentifier)) {
|
||||
this.clientIdentifier = data().deepCopy(fields()[1].schema(), other.clientIdentifier);
|
||||
fieldSetFlags()[1] = true;
|
||||
}
|
||||
this.clientIdentifierBuilder = null;
|
||||
if (isValidValue(fields()[2], other.employeeNames)) {
|
||||
this.employeeNames = data().deepCopy(fields()[2].schema(), other.employeeNames);
|
||||
fieldSetFlags()[2] = true;
|
||||
}
|
||||
if (isValidValue(fields()[3], other.active)) {
|
||||
this.active = data().deepCopy(fields()[3].schema(), other.active);
|
||||
fieldSetFlags()[3] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'requestTime' field.
|
||||
* @return The value.
|
||||
*/
|
||||
public java.lang.Long getRequestTime() {
|
||||
return requestTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'requestTime' field.
|
||||
* @param value The value of 'requestTime'.
|
||||
* @return This builder.
|
||||
*/
|
||||
public AvroHttpRequest.Builder setRequestTime(long value) {
|
||||
validate(fields()[0], value);
|
||||
this.requestTime = value;
|
||||
fieldSetFlags()[0] = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the 'requestTime' field has been set.
|
||||
* @return True if the 'requestTime' field has been set, false otherwise.
|
||||
*/
|
||||
public boolean hasRequestTime() {
|
||||
return fieldSetFlags()[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the value of the 'requestTime' field.
|
||||
* @return This builder.
|
||||
*/
|
||||
public AvroHttpRequest.Builder clearRequestTime() {
|
||||
fieldSetFlags()[0] = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'clientIdentifier' field.
|
||||
* @return The value.
|
||||
*/
|
||||
public ClientIdentifier getClientIdentifier() {
|
||||
return clientIdentifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'clientIdentifier' field.
|
||||
* @param value The value of 'clientIdentifier'.
|
||||
* @return This builder.
|
||||
*/
|
||||
public AvroHttpRequest.Builder setClientIdentifier(ClientIdentifier value) {
|
||||
validate(fields()[1], value);
|
||||
this.clientIdentifierBuilder = null;
|
||||
this.clientIdentifier = value;
|
||||
fieldSetFlags()[1] = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the 'clientIdentifier' field has been set.
|
||||
* @return True if the 'clientIdentifier' field has been set, false otherwise.
|
||||
*/
|
||||
public boolean hasClientIdentifier() {
|
||||
return fieldSetFlags()[1];
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Builder instance for the 'clientIdentifier' field and creates one if it doesn't exist yet.
|
||||
* @return This builder.
|
||||
*/
|
||||
public ClientIdentifier.Builder getClientIdentifierBuilder() {
|
||||
if (clientIdentifierBuilder == null) {
|
||||
if (hasClientIdentifier()) {
|
||||
setClientIdentifierBuilder(ClientIdentifier.newBuilder(clientIdentifier));
|
||||
} else {
|
||||
setClientIdentifierBuilder(ClientIdentifier.newBuilder());
|
||||
}
|
||||
}
|
||||
return clientIdentifierBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Builder instance for the 'clientIdentifier' field
|
||||
* @param value The builder instance that must be set.
|
||||
* @return This builder.
|
||||
*/
|
||||
public AvroHttpRequest.Builder setClientIdentifierBuilder(ClientIdentifier.Builder value) {
|
||||
clearClientIdentifier();
|
||||
clientIdentifierBuilder = value;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the 'clientIdentifier' field has an active Builder instance
|
||||
* @return True if the 'clientIdentifier' field has an active Builder instance
|
||||
*/
|
||||
public boolean hasClientIdentifierBuilder() {
|
||||
return clientIdentifierBuilder != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears the value of the 'clientIdentifier' field.
|
||||
* @return This builder.
|
||||
*/
|
||||
public AvroHttpRequest.Builder clearClientIdentifier() {
|
||||
clientIdentifier = null;
|
||||
clientIdentifierBuilder = null;
|
||||
fieldSetFlags()[1] = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'employeeNames' field.
|
||||
* @return The value.
|
||||
*/
|
||||
public java.util.List<java.lang.CharSequence> getEmployeeNames() {
|
||||
return employeeNames;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'employeeNames' field.
|
||||
* @param value The value of 'employeeNames'.
|
||||
* @return This builder.
|
||||
*/
|
||||
public AvroHttpRequest.Builder setEmployeeNames(java.util.List<java.lang.CharSequence> value) {
|
||||
validate(fields()[2], value);
|
||||
this.employeeNames = value;
|
||||
fieldSetFlags()[2] = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the 'employeeNames' field has been set.
|
||||
* @return True if the 'employeeNames' field has been set, false otherwise.
|
||||
*/
|
||||
public boolean hasEmployeeNames() {
|
||||
return fieldSetFlags()[2];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the value of the 'employeeNames' field.
|
||||
* @return This builder.
|
||||
*/
|
||||
public AvroHttpRequest.Builder clearEmployeeNames() {
|
||||
employeeNames = null;
|
||||
fieldSetFlags()[2] = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'active' field.
|
||||
* @return The value.
|
||||
*/
|
||||
public Active getActive() {
|
||||
return active;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'active' field.
|
||||
* @param value The value of 'active'.
|
||||
* @return This builder.
|
||||
*/
|
||||
public AvroHttpRequest.Builder setActive(Active value) {
|
||||
validate(fields()[3], value);
|
||||
this.active = value;
|
||||
fieldSetFlags()[3] = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the 'active' field has been set.
|
||||
* @return True if the 'active' field has been set, false otherwise.
|
||||
*/
|
||||
public boolean hasActive() {
|
||||
return fieldSetFlags()[3];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the value of the 'active' field.
|
||||
* @return This builder.
|
||||
*/
|
||||
public AvroHttpRequest.Builder clearActive() {
|
||||
active = null;
|
||||
fieldSetFlags()[3] = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public AvroHttpRequest build() {
|
||||
try {
|
||||
AvroHttpRequest record = new AvroHttpRequest();
|
||||
record.requestTime = fieldSetFlags()[0] ? this.requestTime : (java.lang.Long) defaultValue(fields()[0]);
|
||||
if (clientIdentifierBuilder != null) {
|
||||
record.clientIdentifier = this.clientIdentifierBuilder.build();
|
||||
} else {
|
||||
record.clientIdentifier = fieldSetFlags()[1] ? this.clientIdentifier : (ClientIdentifier) defaultValue(fields()[1]);
|
||||
}
|
||||
record.employeeNames = fieldSetFlags()[2] ? this.employeeNames : (java.util.List<java.lang.CharSequence>) defaultValue(fields()[2]);
|
||||
record.active = fieldSetFlags()[3] ? this.active : (Active) defaultValue(fields()[3]);
|
||||
return record;
|
||||
} catch (java.lang.Exception e) {
|
||||
throw new org.apache.avro.AvroRuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final org.apache.avro.io.DatumWriter<AvroHttpRequest>
|
||||
WRITER$ = (org.apache.avro.io.DatumWriter<AvroHttpRequest>)MODEL$.createDatumWriter(SCHEMA$);
|
||||
|
||||
@Override public void writeExternal(java.io.ObjectOutput out)
|
||||
throws java.io.IOException {
|
||||
WRITER$.write(this, SpecificData.getEncoder(out));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final org.apache.avro.io.DatumReader<AvroHttpRequest>
|
||||
READER$ = (org.apache.avro.io.DatumReader<AvroHttpRequest>)MODEL$.createDatumReader(SCHEMA$);
|
||||
|
||||
@Override public void readExternal(java.io.ObjectInput in)
|
||||
throws java.io.IOException {
|
||||
READER$.read(this, SpecificData.getDecoder(in));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,308 @@
|
||||
/**
|
||||
* Autogenerated by Avro
|
||||
*
|
||||
* DO NOT EDIT DIRECTLY
|
||||
*/
|
||||
package com.baeldung.avro.util.model;
|
||||
|
||||
import org.apache.avro.specific.SpecificData;
|
||||
import org.apache.avro.message.BinaryMessageEncoder;
|
||||
import org.apache.avro.message.BinaryMessageDecoder;
|
||||
import org.apache.avro.message.SchemaStore;
|
||||
|
||||
@SuppressWarnings("all")
|
||||
@org.apache.avro.specific.AvroGenerated
|
||||
public class ClientIdentifier extends org.apache.avro.specific.SpecificRecordBase implements org.apache.avro.specific.SpecificRecord {
|
||||
private static final long serialVersionUID = 8754570983127295424L;
|
||||
public static final org.apache.avro.Schema SCHEMA$ = new org.apache.avro.Schema.Parser().parse("{\"type\":\"record\",\"name\":\"ClientIdentifier\",\"namespace\":\"com.baeldung.avro.model\",\"fields\":[{\"name\":\"hostName\",\"type\":\"string\"},{\"name\":\"ipAddress\",\"type\":\"string\"}]}");
|
||||
public static org.apache.avro.Schema getClassSchema() { return SCHEMA$; }
|
||||
|
||||
private static SpecificData MODEL$ = new SpecificData();
|
||||
|
||||
private static final BinaryMessageEncoder<ClientIdentifier> ENCODER =
|
||||
new BinaryMessageEncoder<ClientIdentifier>(MODEL$, SCHEMA$);
|
||||
|
||||
private static final BinaryMessageDecoder<ClientIdentifier> DECODER =
|
||||
new BinaryMessageDecoder<ClientIdentifier>(MODEL$, SCHEMA$);
|
||||
|
||||
/**
|
||||
* Return the BinaryMessageDecoder instance used by this class.
|
||||
*/
|
||||
public static BinaryMessageDecoder<ClientIdentifier> getDecoder() {
|
||||
return DECODER;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new BinaryMessageDecoder instance for this class that uses the specified {@link SchemaStore}.
|
||||
* @param resolver a {@link SchemaStore} used to find schemas by fingerprint
|
||||
*/
|
||||
public static BinaryMessageDecoder<ClientIdentifier> createDecoder(SchemaStore resolver) {
|
||||
return new BinaryMessageDecoder<ClientIdentifier>(MODEL$, SCHEMA$, resolver);
|
||||
}
|
||||
|
||||
/** Serializes this ClientIdentifier to a ByteBuffer. */
|
||||
public java.nio.ByteBuffer toByteBuffer() throws java.io.IOException {
|
||||
return ENCODER.encode(this);
|
||||
}
|
||||
|
||||
/** Deserializes a ClientIdentifier from a ByteBuffer. */
|
||||
public static ClientIdentifier fromByteBuffer(
|
||||
java.nio.ByteBuffer b) throws java.io.IOException {
|
||||
return DECODER.decode(b);
|
||||
}
|
||||
|
||||
@Deprecated public java.lang.CharSequence hostName;
|
||||
@Deprecated public java.lang.CharSequence ipAddress;
|
||||
|
||||
/**
|
||||
* Default constructor. Note that this does not initialize fields
|
||||
* to their default values from the schema. If that is desired then
|
||||
* one should use <code>newBuilder()</code>.
|
||||
*/
|
||||
public ClientIdentifier() {}
|
||||
|
||||
/**
|
||||
* All-args constructor.
|
||||
* @param hostName The new value for hostName
|
||||
* @param ipAddress The new value for ipAddress
|
||||
*/
|
||||
public ClientIdentifier(java.lang.CharSequence hostName, java.lang.CharSequence ipAddress) {
|
||||
this.hostName = hostName;
|
||||
this.ipAddress = ipAddress;
|
||||
}
|
||||
|
||||
public org.apache.avro.Schema getSchema() { return SCHEMA$; }
|
||||
// Used by DatumWriter. Applications should not call.
|
||||
public java.lang.Object get(int field$) {
|
||||
switch (field$) {
|
||||
case 0: return hostName;
|
||||
case 1: return ipAddress;
|
||||
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
|
||||
}
|
||||
}
|
||||
|
||||
// Used by DatumReader. Applications should not call.
|
||||
@SuppressWarnings(value="unchecked")
|
||||
public void put(int field$, java.lang.Object value$) {
|
||||
switch (field$) {
|
||||
case 0: hostName = (java.lang.CharSequence)value$; break;
|
||||
case 1: ipAddress = (java.lang.CharSequence)value$; break;
|
||||
default: throw new org.apache.avro.AvroRuntimeException("Bad index");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'hostName' field.
|
||||
* @return The value of the 'hostName' field.
|
||||
*/
|
||||
public java.lang.CharSequence getHostName() {
|
||||
return hostName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'hostName' field.
|
||||
* @param value the value to set.
|
||||
*/
|
||||
public void setHostName(java.lang.CharSequence value) {
|
||||
this.hostName = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'ipAddress' field.
|
||||
* @return The value of the 'ipAddress' field.
|
||||
*/
|
||||
public java.lang.CharSequence getIpAddress() {
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'ipAddress' field.
|
||||
* @param value the value to set.
|
||||
*/
|
||||
public void setIpAddress(java.lang.CharSequence value) {
|
||||
this.ipAddress = value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new ClientIdentifier RecordBuilder.
|
||||
* @return A new ClientIdentifier RecordBuilder
|
||||
*/
|
||||
public static ClientIdentifier.Builder newBuilder() {
|
||||
return new ClientIdentifier.Builder();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new ClientIdentifier RecordBuilder by copying an existing Builder.
|
||||
* @param other The existing builder to copy.
|
||||
* @return A new ClientIdentifier RecordBuilder
|
||||
*/
|
||||
public static ClientIdentifier.Builder newBuilder(ClientIdentifier.Builder other) {
|
||||
return new ClientIdentifier.Builder(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new ClientIdentifier RecordBuilder by copying an existing ClientIdentifier instance.
|
||||
* @param other The existing instance to copy.
|
||||
* @return A new ClientIdentifier RecordBuilder
|
||||
*/
|
||||
public static ClientIdentifier.Builder newBuilder(ClientIdentifier other) {
|
||||
return new ClientIdentifier.Builder(other);
|
||||
}
|
||||
|
||||
/**
|
||||
* RecordBuilder for ClientIdentifier instances.
|
||||
*/
|
||||
public static class Builder extends org.apache.avro.specific.SpecificRecordBuilderBase<ClientIdentifier>
|
||||
implements org.apache.avro.data.RecordBuilder<ClientIdentifier> {
|
||||
|
||||
private java.lang.CharSequence hostName;
|
||||
private java.lang.CharSequence ipAddress;
|
||||
|
||||
/** Creates a new Builder */
|
||||
private Builder() {
|
||||
super(SCHEMA$);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Builder by copying an existing Builder.
|
||||
* @param other The existing Builder to copy.
|
||||
*/
|
||||
private Builder(ClientIdentifier.Builder other) {
|
||||
super(other);
|
||||
if (isValidValue(fields()[0], other.hostName)) {
|
||||
this.hostName = data().deepCopy(fields()[0].schema(), other.hostName);
|
||||
fieldSetFlags()[0] = true;
|
||||
}
|
||||
if (isValidValue(fields()[1], other.ipAddress)) {
|
||||
this.ipAddress = data().deepCopy(fields()[1].schema(), other.ipAddress);
|
||||
fieldSetFlags()[1] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a Builder by copying an existing ClientIdentifier instance
|
||||
* @param other The existing instance to copy.
|
||||
*/
|
||||
private Builder(ClientIdentifier other) {
|
||||
super(SCHEMA$);
|
||||
if (isValidValue(fields()[0], other.hostName)) {
|
||||
this.hostName = data().deepCopy(fields()[0].schema(), other.hostName);
|
||||
fieldSetFlags()[0] = true;
|
||||
}
|
||||
if (isValidValue(fields()[1], other.ipAddress)) {
|
||||
this.ipAddress = data().deepCopy(fields()[1].schema(), other.ipAddress);
|
||||
fieldSetFlags()[1] = true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'hostName' field.
|
||||
* @return The value.
|
||||
*/
|
||||
public java.lang.CharSequence getHostName() {
|
||||
return hostName;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'hostName' field.
|
||||
* @param value The value of 'hostName'.
|
||||
* @return This builder.
|
||||
*/
|
||||
public ClientIdentifier.Builder setHostName(java.lang.CharSequence value) {
|
||||
validate(fields()[0], value);
|
||||
this.hostName = value;
|
||||
fieldSetFlags()[0] = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the 'hostName' field has been set.
|
||||
* @return True if the 'hostName' field has been set, false otherwise.
|
||||
*/
|
||||
public boolean hasHostName() {
|
||||
return fieldSetFlags()[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the value of the 'hostName' field.
|
||||
* @return This builder.
|
||||
*/
|
||||
public ClientIdentifier.Builder clearHostName() {
|
||||
hostName = null;
|
||||
fieldSetFlags()[0] = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the value of the 'ipAddress' field.
|
||||
* @return The value.
|
||||
*/
|
||||
public java.lang.CharSequence getIpAddress() {
|
||||
return ipAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the value of the 'ipAddress' field.
|
||||
* @param value The value of 'ipAddress'.
|
||||
* @return This builder.
|
||||
*/
|
||||
public ClientIdentifier.Builder setIpAddress(java.lang.CharSequence value) {
|
||||
validate(fields()[1], value);
|
||||
this.ipAddress = value;
|
||||
fieldSetFlags()[1] = true;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether the 'ipAddress' field has been set.
|
||||
* @return True if the 'ipAddress' field has been set, false otherwise.
|
||||
*/
|
||||
public boolean hasIpAddress() {
|
||||
return fieldSetFlags()[1];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Clears the value of the 'ipAddress' field.
|
||||
* @return This builder.
|
||||
*/
|
||||
public ClientIdentifier.Builder clearIpAddress() {
|
||||
ipAddress = null;
|
||||
fieldSetFlags()[1] = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public ClientIdentifier build() {
|
||||
try {
|
||||
ClientIdentifier record = new ClientIdentifier();
|
||||
record.hostName = fieldSetFlags()[0] ? this.hostName : (java.lang.CharSequence) defaultValue(fields()[0]);
|
||||
record.ipAddress = fieldSetFlags()[1] ? this.ipAddress : (java.lang.CharSequence) defaultValue(fields()[1]);
|
||||
return record;
|
||||
} catch (java.lang.Exception e) {
|
||||
throw new org.apache.avro.AvroRuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final org.apache.avro.io.DatumWriter<ClientIdentifier>
|
||||
WRITER$ = (org.apache.avro.io.DatumWriter<ClientIdentifier>)MODEL$.createDatumWriter(SCHEMA$);
|
||||
|
||||
@Override public void writeExternal(java.io.ObjectOutput out)
|
||||
throws java.io.IOException {
|
||||
WRITER$.write(this, SpecificData.getEncoder(out));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private static final org.apache.avro.io.DatumReader<ClientIdentifier>
|
||||
READER$ = (org.apache.avro.io.DatumReader<ClientIdentifier>)MODEL$.createDatumReader(SCHEMA$);
|
||||
|
||||
@Override public void readExternal(java.io.ObjectInput in)
|
||||
throws java.io.IOException {
|
||||
READER$.read(this, SpecificData.getDecoder(in));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
package com.baeldung.avro.util.serealization;
|
||||
|
||||
import com.baeldung.avro.util.model.AvroHttpRequest;
|
||||
import org.apache.avro.io.DatumReader;
|
||||
import org.apache.avro.io.Decoder;
|
||||
import org.apache.avro.io.DecoderFactory;
|
||||
import org.apache.avro.specific.SpecificDatumReader;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class AvroDeSerealizer {
|
||||
|
||||
private static Logger logger = LoggerFactory.getLogger(AvroDeSerealizer.class);
|
||||
|
||||
public AvroHttpRequest deSerealizeAvroHttpRequestJSON(byte[] data) {
|
||||
DatumReader<AvroHttpRequest> reader = new SpecificDatumReader<>(AvroHttpRequest.class);
|
||||
Decoder decoder = null;
|
||||
try {
|
||||
decoder = DecoderFactory.get()
|
||||
.jsonDecoder(AvroHttpRequest.getClassSchema(), new String(data));
|
||||
return reader.read(null, decoder);
|
||||
} catch (IOException e) {
|
||||
logger.error("Deserialization error" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public AvroHttpRequest deSerealizeAvroHttpRequestBinary(byte[] data) {
|
||||
DatumReader<AvroHttpRequest> employeeReader = new SpecificDatumReader<>(AvroHttpRequest.class);
|
||||
Decoder decoder = DecoderFactory.get()
|
||||
.binaryDecoder(data, null);
|
||||
try {
|
||||
return employeeReader.read(null, decoder);
|
||||
} catch (IOException e) {
|
||||
logger.error("Deserialization error" + e.getMessage());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.baeldung.avro.util.serealization;
|
||||
|
||||
import com.baeldung.avro.util.model.AvroHttpRequest;
|
||||
import org.apache.avro.io.*;
|
||||
import org.apache.avro.specific.SpecificDatumWriter;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
public class AvroSerealizer {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(AvroSerealizer.class);
|
||||
|
||||
public byte[] serealizeAvroHttpRequestJSON(AvroHttpRequest request) {
|
||||
DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class);
|
||||
byte[] data = new byte[0];
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
Encoder jsonEncoder = null;
|
||||
try {
|
||||
jsonEncoder = EncoderFactory.get()
|
||||
.jsonEncoder(AvroHttpRequest.getClassSchema(), stream);
|
||||
writer.write(request, jsonEncoder);
|
||||
jsonEncoder.flush();
|
||||
data = stream.toByteArray();
|
||||
} catch (IOException e) {
|
||||
logger.error("Serialization error " + e.getMessage());
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
public byte[] serealizeAvroHttpRequestBinary(AvroHttpRequest request) {
|
||||
DatumWriter<AvroHttpRequest> writer = new SpecificDatumWriter<>(AvroHttpRequest.class);
|
||||
byte[] data = new byte[0];
|
||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||
Encoder jsonEncoder = EncoderFactory.get()
|
||||
.binaryEncoder(stream, null);
|
||||
try {
|
||||
writer.write(request, jsonEncoder);
|
||||
jsonEncoder.flush();
|
||||
data = stream.toByteArray();
|
||||
} catch (IOException e) {
|
||||
logger.error("Serialization error " + e.getMessage());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
47
apache-avro/src/main/resources/avroHttpRequest-schema.avsc
Normal file
47
apache-avro/src/main/resources/avroHttpRequest-schema.avsc
Normal file
@ -0,0 +1,47 @@
|
||||
{
|
||||
"type":"record",
|
||||
"name":"AvroHttpRequest",
|
||||
"namespace":"com.baeldung.avro.model",
|
||||
"fields":[
|
||||
{
|
||||
"name":"requestTime",
|
||||
"type":"long"
|
||||
},
|
||||
{
|
||||
"name":"clientIdentifier",
|
||||
"type":{
|
||||
"type":"record",
|
||||
"name":"ClientIdentifier",
|
||||
"fields":[
|
||||
{
|
||||
"name":"hostName",
|
||||
"type":"string"
|
||||
},
|
||||
{
|
||||
"name":"ipAddress",
|
||||
"type":"string"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
{
|
||||
"name":"employeeNames",
|
||||
"type":{
|
||||
"type":"array",
|
||||
"items":"string"
|
||||
},
|
||||
"default":null
|
||||
},
|
||||
{
|
||||
"name":"active",
|
||||
"type":{
|
||||
"type":"enum",
|
||||
"name":"Active",
|
||||
"symbols":[
|
||||
"YES",
|
||||
"NO"
|
||||
]
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
13
apache-avro/src/main/resources/logback.xml
Normal file
13
apache-avro/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -0,0 +1,83 @@
|
||||
package com.baeldung.avro.util.serealization;
|
||||
|
||||
import com.baeldung.avro.util.model.Active;
|
||||
import com.baeldung.avro.util.model.AvroHttpRequest;
|
||||
import com.baeldung.avro.util.model.ClientIdentifier;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
public class AvroSerealizerDeSerealizerUnitTest {
|
||||
|
||||
AvroSerealizer serealizer;
|
||||
AvroDeSerealizer deSerealizer;
|
||||
AvroHttpRequest request;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
serealizer = new AvroSerealizer();
|
||||
deSerealizer = new AvroDeSerealizer();
|
||||
|
||||
ClientIdentifier clientIdentifier = ClientIdentifier.newBuilder()
|
||||
.setHostName("localhost")
|
||||
.setIpAddress("255.255.255.0")
|
||||
.build();
|
||||
|
||||
List<CharSequence> employees = new ArrayList();
|
||||
employees.add("James");
|
||||
employees.add("Alice");
|
||||
employees.add("David");
|
||||
employees.add("Han");
|
||||
|
||||
request = AvroHttpRequest.newBuilder()
|
||||
.setRequestTime(01l)
|
||||
.setActive(Active.YES)
|
||||
.setClientIdentifier(clientIdentifier)
|
||||
.setEmployeeNames(employees)
|
||||
.build();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenSerializedUsingJSONEncoder_thenObjectGetsSerialized() {
|
||||
byte[] data = serealizer.serealizeAvroHttpRequestJSON(request);
|
||||
assertTrue(Objects.nonNull(data));
|
||||
assertTrue(data.length > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenSerializedUsingBinaryEncoder_thenObjectGetsSerialized() {
|
||||
byte[] data = serealizer.serealizeAvroHttpRequestBinary(request);
|
||||
assertTrue(Objects.nonNull(data));
|
||||
assertTrue(data.length > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenDeserializeUsingJSONDecoder_thenActualAndExpectedObjectsAreEqual() {
|
||||
byte[] data = serealizer.serealizeAvroHttpRequestJSON(request);
|
||||
AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestJSON(data);
|
||||
assertEquals(actualRequest, request);
|
||||
assertTrue(actualRequest.getRequestTime()
|
||||
.equals(request.getRequestTime()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void WhenDeserializeUsingBinaryecoder_thenActualAndExpectedObjectsAreEqual() {
|
||||
byte[] data = serealizer.serealizeAvroHttpRequestBinary(request);
|
||||
AvroHttpRequest actualRequest = deSerealizer.deSerealizeAvroHttpRequestBinary(data);
|
||||
assertEquals(actualRequest, request);
|
||||
assertTrue(actualRequest.getRequestTime()
|
||||
.equals(request.getRequestTime()));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
<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">
|
||||
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>
|
||||
<groupId>apache-bval</groupId>
|
||||
<artifactId>apache-bval</artifactId>
|
||||
|
13
apache-bval/src/main/resources/logback.xml
Normal file
13
apache-bval/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -28,12 +28,6 @@
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -47,12 +41,8 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
<mysql.connector.version>5.1.44</mysql.connector.version>
|
||||
<cayenne.version>4.0.M5</cayenne.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
13
apache-cayenne/src/main/resources/logback.xml
Normal file
13
apache-cayenne/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -1,5 +1,5 @@
|
||||
<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">
|
||||
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>apache-curator</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
13
apache-curator/src/main/resources/logback.xml
Normal file
13
apache-curator/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -3,3 +3,4 @@
|
||||
- [Apache CXF Support for RESTful Web Services](http://www.baeldung.com/apache-cxf-rest-api)
|
||||
- [A Guide to Apache CXF with Spring](http://www.baeldung.com/apache-cxf-with-spring)
|
||||
- [Introduction to Apache CXF](http://www.baeldung.com/introduction-to-apache-cxf)
|
||||
- [Server-Sent Events (SSE) In JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse)
|
||||
|
@ -1,5 +1,5 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cxf-aegis</artifactId>
|
||||
|
||||
@ -17,8 +17,4 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<cxf.version>3.1.8</cxf.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
13
apache-cxf/cxf-aegis/src/main/resources/logback.xml
Normal file
13
apache-cxf/cxf-aegis/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -1,7 +1,7 @@
|
||||
<?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/maven-v4_0_0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cxf-introduction</artifactId>
|
||||
|
||||
@ -36,9 +36,4 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<cxf.version>3.1.8</cxf.version>
|
||||
<surefire.version>2.19.1</surefire.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
13
apache-cxf/cxf-introduction/src/main/resources/logback.xml
Normal file
13
apache-cxf/cxf-introduction/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -1,7 +1,7 @@
|
||||
<?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/maven-v4_0_0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cxf-jaxrs-implementation</artifactId>
|
||||
|
||||
@ -48,10 +48,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<cxf.version>3.1.8</cxf.version>
|
||||
<httpclient.version>4.5.2</httpclient.version>
|
||||
<surefire.version>2.19.1</surefire.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -1,5 +1,5 @@
|
||||
<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/maven-v4_0_0.xsd">
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>cxf-spring</artifactId>
|
||||
<packaging>war</packaging>
|
||||
@ -101,11 +101,7 @@
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<cxf.version>3.1.8</cxf.version>
|
||||
<spring.version>4.3.4.RELEASE</spring.version>
|
||||
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<surefire.version>2.19.1</surefire.version>
|
||||
<cargo-maven2-plugin.version>1.6.1</cargo-maven2-plugin.version>
|
||||
</properties>
|
||||
|
||||
|
13
apache-cxf/cxf-spring/src/main/resources/logback.xml
Normal file
13
apache-cxf/cxf-spring/src/main/resources/logback.xml
Normal file
@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
@ -17,6 +17,7 @@
|
||||
<module>cxf-spring</module>
|
||||
<module>cxf-jaxrs-implementation</module>
|
||||
<module>cxf-aegis</module>
|
||||
<module>sse-jaxrs</module>
|
||||
</modules>
|
||||
|
||||
<build>
|
||||
@ -33,7 +34,7 @@
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<exec-maven-plugin.version>1.5.0</exec-maven-plugin.version>
|
||||
<cxf.version>3.1.8</cxf.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
21
apache-cxf/sse-jaxrs/pom.xml
Normal file
21
apache-cxf/sse-jaxrs/pom.xml
Normal file
@ -0,0 +1,21 @@
|
||||
<?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>sse-jaxrs</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-cxf</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<modules>
|
||||
<module>sse-jaxrs-server</module>
|
||||
<module>sse-jaxrs-client</module>
|
||||
</modules>
|
||||
|
||||
</project>
|
62
apache-cxf/sse-jaxrs/sse-jaxrs-client/pom.xml
Normal file
62
apache-cxf/sse-jaxrs/sse-jaxrs-client/pom.xml
Normal file
@ -0,0 +1,62 @@
|
||||
<?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>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>sse-jaxrs</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>sse-jaxrs-client</artifactId>
|
||||
|
||||
<properties>
|
||||
<cxf-version>3.2.0</cxf-version>
|
||||
</properties>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>1.6.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>singleEvent</id>
|
||||
<goals>
|
||||
<goal>java</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.sse.jaxrs.client.SseClientApp</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>broadcast</id>
|
||||
<goals>
|
||||
<goal>java</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<mainClass>com.baeldung.sse.jaxrs.client.SseClientBroadcastApp</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-client</artifactId>
|
||||
<version>${cxf-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.cxf</groupId>
|
||||
<artifactId>cxf-rt-rs-sse</artifactId>
|
||||
<version>${cxf-version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,48 @@
|
||||
package com.baeldung.sse.jaxrs.client;
|
||||
|
||||
import javax.ws.rs.client.Client;
|
||||
import javax.ws.rs.client.ClientBuilder;
|
||||
import javax.ws.rs.client.WebTarget;
|
||||
import javax.ws.rs.sse.InboundSseEvent;
|
||||
import javax.ws.rs.sse.SseEventSource;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class SseClientApp {
|
||||
|
||||
private static final String url = "http://127.0.0.1:9080/sse-jaxrs-server/sse/stock/prices";
|
||||
|
||||
public static void main(String... args) throws Exception {
|
||||
|
||||
Client client = ClientBuilder.newClient();
|
||||
WebTarget target = client.target(url);
|
||||
try (SseEventSource eventSource = SseEventSource.target(target).build()) {
|
||||
|
||||
eventSource.register(onEvent, onError, onComplete);
|
||||
eventSource.open();
|
||||
|
||||
//Consuming events for one hour
|
||||
Thread.sleep(60 * 60 * 1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
client.close();
|
||||
System.out.println("End");
|
||||
}
|
||||
|
||||
// A new event is received
|
||||
private static Consumer<InboundSseEvent> onEvent = (inboundSseEvent) -> {
|
||||
String data = inboundSseEvent.readData();
|
||||
System.out.println(data);
|
||||
};
|
||||
|
||||
//Error
|
||||
private static Consumer<Throwable> onError = (throwable) -> {
|
||||
throwable.printStackTrace();
|
||||
};
|
||||
|
||||
//Connection close and there is nothing to receive
|
||||
private static Runnable onComplete = () -> {
|
||||
System.out.println("Done!");
|
||||
};
|
||||
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user