Merge branch 'master' into master
This commit is contained in:
commit
f05972fd1e
23
.gitignore
vendored
23
.gitignore
vendored
@ -34,8 +34,6 @@ spring-security-openid/src/main/resources/application.properties
|
||||
|
||||
spring-all/*.log
|
||||
|
||||
*.jar
|
||||
|
||||
SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties
|
||||
|
||||
spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
|
||||
@ -43,3 +41,24 @@ spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
|
||||
spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
|
||||
*.springBeans
|
||||
|
||||
20171220-JMeter.csv
|
||||
|
||||
.factorypath
|
||||
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
|
||||
|
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 test -fae
|
||||
script: travis_wait 60 mvn -q install -Pdefault
|
||||
|
||||
sudo: required
|
||||
|
||||
|
75
JGit/pom.xml
75
JGit/pom.xml
@ -1,5 +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">
|
||||
<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>
|
||||
@ -11,41 +12,41 @@
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
</properties>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jgit-repository</id>
|
||||
<url>https://repo.eclipse.org/content/groups/releases/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
</parent>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jgit-repository</id>
|
||||
<url>https://repo.eclipse.org/content/groups/releases/</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<!-- Core Library -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jgit</groupId>
|
||||
<artifactId>org.eclipse.jgit</artifactId>
|
||||
<version>${org.eclipse.jgit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jgit</groupId>
|
||||
<artifactId>org.eclipse.jgit.archive</artifactId>
|
||||
<version>${org.eclipse.jgit.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>${commons-io.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<org.eclipse.jgit.version>4.5.0.201609210915-r</org.eclipse.jgit.version>
|
||||
</properties>
|
||||
|
||||
<!-- Core Library -->
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jgit</groupId>
|
||||
<artifactId>org.eclipse.jgit</artifactId>
|
||||
<version>4.5.0.201609210915-r</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse.jgit</groupId>
|
||||
<artifactId>org.eclipse.jgit.archive</artifactId>
|
||||
<version>4.5.0.201609210915-r</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.5</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.21</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
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>
|
11
README.md
11
README.md
@ -17,6 +17,10 @@ This project is **a collection of small and focused tutorials** each covering a
|
||||
Most of the tutorial projects are focused on the `Spring Framework` (and `Spring Security`).
|
||||
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 -Pdefault -Dgib.enabled=false`
|
||||
|
||||
|
||||
Working with the code in Eclipse
|
||||
================================
|
||||
@ -29,3 +33,10 @@ Any IDE can be used to work with the projects, but if you're using Eclipse, cons
|
||||
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)
|
||||
|
@ -1,52 +1,51 @@
|
||||
<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">
|
||||
<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>
|
||||
|
||||
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>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<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>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.twitter4j</groupId>
|
||||
<artifactId>twitter4j-stream</artifactId>
|
||||
<version>4.0.6</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.twitter4j</groupId>
|
||||
<artifactId>twitter4j-stream</artifactId>
|
||||
<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>
|
||||
<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>
|
2
activejdbc/README.md
Normal file
2
activejdbc/README.md
Normal file
@ -0,0 +1,2 @@
|
||||
### Relevant Articles:
|
||||
- [Introduction to ActiveJDBC](http://www.baeldung.com/active-jdbc)
|
@ -1,129 +1,135 @@
|
||||
<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>activejdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>activejdbc</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<activejdbc.version>1.4.13</activejdbc.version>
|
||||
<environments>development.test,development</environments>
|
||||
</properties>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.6.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>activejdbc-instrumentation</artifactId>
|
||||
<version>${activejdbc.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>instrument</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>db-migrator-maven-plugin</artifactId>
|
||||
<version>${activejdbc.version}</version>
|
||||
<configuration>
|
||||
<configFile>${project.basedir}/src/main/resources/database.properties</configFile>
|
||||
<environments>${environments}</environments>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
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>
|
||||
<name>activejdbc</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>activejdbc</artifactId>
|
||||
<version>${activejdbc.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>opensymphony</groupId>
|
||||
<artifactId>oscache</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.34</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>2.18.1</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>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>activejdbc</artifactId>
|
||||
<version>${activejdbc.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>opensymphony</groupId>
|
||||
<artifactId>oscache</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.34</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>1.7.9</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<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>
|
||||
</project>
|
||||
<version>${mysql.connector.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>${java.version}</source>
|
||||
<target>${java.version}</target>
|
||||
<encoding>UTF-8</encoding>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>activejdbc-instrumentation</artifactId>
|
||||
<version>${activejdbc.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>instrument</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.javalite</groupId>
|
||||
<artifactId>db-migrator-maven-plugin</artifactId>
|
||||
<version>${activejdbc.version}</version>
|
||||
<configuration>
|
||||
<configFile>${project.basedir}/src/main/resources/database.properties</configFile>
|
||||
<environments>${environments}</environments>
|
||||
</configuration>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>${mysql.connector.version}</version>
|
||||
</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>
|
||||
<activejdbc.version>1.4.13</activejdbc.version>
|
||||
<environments>development.test,development</environments>
|
||||
<mysql.connector.version>5.1.34</mysql.connector.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
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>
|
@ -1,15 +1,15 @@
|
||||
<?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>
|
||||
<artifactId>akka-streams</artifactId>
|
||||
<name>akka-streams</name>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>akka-streams</artifactId>
|
||||
<name>akka-streams</name>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
@ -23,6 +23,7 @@
|
||||
<version>${akkastreams.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<akkastreams.version>2.5.2</akkastreams.version>
|
||||
</properties>
|
||||
|
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)
|
||||
@ -16,3 +15,12 @@
|
||||
- [Introduction to Minimax Algorithm](http://www.baeldung.com/java-minimax-algorithm)
|
||||
- [How to Calculate Levenshtein Distance in Java?](http://www.baeldung.com/java-levenshtein-distance)
|
||||
- [How to Find the Kth Largest Element in Java](http://www.baeldung.com/java-kth-largest-element)
|
||||
- [Multi-Swarm Optimization Algorithm in Java](http://www.baeldung.com/java-multi-swarm-algorithm)
|
||||
- [A Maze Solver in Java](http://www.baeldung.com/java-solve-maze)
|
||||
- [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)
|
||||
- [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)
|
||||
|
@ -5,12 +5,6 @@
|
||||
<artifactId>algorithms</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<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>
|
||||
</properties>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
@ -32,17 +26,22 @@
|
||||
<dependency>
|
||||
<groupId>io.jenetics</groupId>
|
||||
<artifactId>jenetics</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<version>${io.jenetics.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jgrapht</groupId>
|
||||
<artifactId>jgrapht-core</artifactId>
|
||||
<version>1.0.1</version>
|
||||
<version>${org.jgrapht.core.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>pl.allegro.finance</groupId>
|
||||
<artifactId>tradukisto</artifactId>
|
||||
<version>${tradukisto.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>3.9.0</version>
|
||||
<version>${org.assertj.core.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
@ -58,6 +57,7 @@
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
</build>
|
||||
|
||||
<reporting>
|
||||
<plugins>
|
||||
<plugin>
|
||||
@ -77,4 +77,14 @@
|
||||
</plugin>
|
||||
</plugins>
|
||||
</reporting>
|
||||
</project>
|
||||
|
||||
<properties>
|
||||
<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>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -17,6 +17,7 @@ public class RunAlgorithm {
|
||||
System.out.println("3 - Simple Genetic Algorithm");
|
||||
System.out.println("4 - Ant Colony");
|
||||
System.out.println("5 - Dijkstra");
|
||||
System.out.println("6 - All pairs in an array that add up to a given sum");
|
||||
int decision = in.nextInt();
|
||||
switch (decision) {
|
||||
case 1:
|
||||
|
@ -0,0 +1,52 @@
|
||||
package com.baeldung.algorithms.maze.solver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class BFSMazeSolver {
|
||||
private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
|
||||
|
||||
public List<Coordinate> solve(Maze maze) {
|
||||
LinkedList<Coordinate> nextToVisit = new LinkedList<>();
|
||||
Coordinate start = maze.getEntry();
|
||||
nextToVisit.add(start);
|
||||
|
||||
while (!nextToVisit.isEmpty()) {
|
||||
Coordinate cur = nextToVisit.remove();
|
||||
|
||||
if (!maze.isValidLocation(cur.getX(), cur.getY()) || maze.isExplored(cur.getX(), cur.getY())) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (maze.isWall(cur.getX(), cur.getY())) {
|
||||
maze.setVisited(cur.getX(), cur.getY(), true);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (maze.isExit(cur.getX(), cur.getY())) {
|
||||
return backtrackPath(cur);
|
||||
}
|
||||
|
||||
for (int[] direction : DIRECTIONS) {
|
||||
Coordinate coordinate = new Coordinate(cur.getX() + direction[0], cur.getY() + direction[1], cur);
|
||||
nextToVisit.add(coordinate);
|
||||
maze.setVisited(cur.getX(), cur.getY(), true);
|
||||
}
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private List<Coordinate> backtrackPath(Coordinate cur) {
|
||||
List<Coordinate> path = new ArrayList<>();
|
||||
Coordinate iter = cur;
|
||||
|
||||
while (iter != null) {
|
||||
path.add(iter);
|
||||
iter = iter.parent;
|
||||
}
|
||||
|
||||
return path;
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.algorithms.maze.solver;
|
||||
|
||||
public class Coordinate {
|
||||
int x;
|
||||
int y;
|
||||
Coordinate parent;
|
||||
|
||||
public Coordinate(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.parent = null;
|
||||
}
|
||||
|
||||
public Coordinate(int x, int y, Coordinate parent) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.parent = parent;
|
||||
}
|
||||
|
||||
int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
Coordinate getParent() {
|
||||
return parent;
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package com.baeldung.algorithms.maze.solver;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class DFSMazeSolver {
|
||||
private static final int[][] DIRECTIONS = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
|
||||
|
||||
public List<Coordinate> solve(Maze maze) {
|
||||
List<Coordinate> path = new ArrayList<>();
|
||||
if (explore(maze, maze.getEntry()
|
||||
.getX(),
|
||||
maze.getEntry()
|
||||
.getY(),
|
||||
path)) {
|
||||
return path;
|
||||
}
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
private boolean explore(Maze maze, int row, int col, List<Coordinate> path) {
|
||||
if (!maze.isValidLocation(row, col) || maze.isWall(row, col) || maze.isExplored(row, col)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
path.add(new Coordinate(row, col));
|
||||
maze.setVisited(row, col, true);
|
||||
|
||||
if (maze.isExit(row, col)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
for (int[] direction : DIRECTIONS) {
|
||||
Coordinate coordinate = getNextCoordinate(row, col, direction[0], direction[1]);
|
||||
if (explore(maze, coordinate.getX(), coordinate.getY(), path)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
path.remove(path.size() - 1);
|
||||
return false;
|
||||
}
|
||||
|
||||
private Coordinate getNextCoordinate(int row, int col, int i, int j) {
|
||||
return new Coordinate(row + i, col + j);
|
||||
}
|
||||
}
|
@ -0,0 +1,141 @@
|
||||
package com.baeldung.algorithms.maze.solver;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
public class Maze {
|
||||
private static final int ROAD = 0;
|
||||
private static final int WALL = 1;
|
||||
private static final int START = 2;
|
||||
private static final int EXIT = 3;
|
||||
private static final int PATH = 4;
|
||||
|
||||
private int[][] maze;
|
||||
private boolean[][] visited;
|
||||
private Coordinate start;
|
||||
private Coordinate end;
|
||||
|
||||
public Maze(File maze) throws FileNotFoundException {
|
||||
String fileText = "";
|
||||
try (Scanner input = new Scanner(maze)) {
|
||||
while (input.hasNextLine()) {
|
||||
fileText += input.nextLine() + "\n";
|
||||
}
|
||||
}
|
||||
initializeMaze(fileText);
|
||||
}
|
||||
|
||||
private void initializeMaze(String text) {
|
||||
if (text == null || (text = text.trim()).length() == 0) {
|
||||
throw new IllegalArgumentException("empty lines data");
|
||||
}
|
||||
|
||||
String[] lines = text.split("[\r]?\n");
|
||||
maze = new int[lines.length][lines[0].length()];
|
||||
visited = new boolean[lines.length][lines[0].length()];
|
||||
|
||||
for (int row = 0; row < getHeight(); row++) {
|
||||
if (lines[row].length() != getWidth()) {
|
||||
throw new IllegalArgumentException("line " + (row + 1) + " wrong length (was " + lines[row].length() + " but should be " + getWidth() + ")");
|
||||
}
|
||||
|
||||
for (int col = 0; col < getWidth(); col++) {
|
||||
if (lines[row].charAt(col) == '#')
|
||||
maze[row][col] = WALL;
|
||||
else if (lines[row].charAt(col) == 'S') {
|
||||
maze[row][col] = START;
|
||||
start = new Coordinate(row, col);
|
||||
} else if (lines[row].charAt(col) == 'E') {
|
||||
maze[row][col] = EXIT;
|
||||
end = new Coordinate(row, col);
|
||||
} else
|
||||
maze[row][col] = ROAD;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int getHeight() {
|
||||
return maze.length;
|
||||
}
|
||||
|
||||
public int getWidth() {
|
||||
return maze[0].length;
|
||||
}
|
||||
|
||||
public Coordinate getEntry() {
|
||||
return start;
|
||||
}
|
||||
|
||||
public Coordinate getExit() {
|
||||
return end;
|
||||
}
|
||||
|
||||
public boolean isExit(int x, int y) {
|
||||
return x == end.getX() && y == end.getY();
|
||||
}
|
||||
|
||||
public boolean isStart(int x, int y) {
|
||||
return x == start.getX() && y == start.getY();
|
||||
}
|
||||
|
||||
public boolean isExplored(int row, int col) {
|
||||
return visited[row][col];
|
||||
}
|
||||
|
||||
public boolean isWall(int row, int col) {
|
||||
return maze[row][col] == WALL;
|
||||
}
|
||||
|
||||
public void setVisited(int row, int col, boolean value) {
|
||||
visited[row][col] = value;
|
||||
}
|
||||
|
||||
public boolean isValidLocation(int row, int col) {
|
||||
if (row < 0 || row >= getHeight() || col < 0 || col >= getWidth()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void printPath(List<Coordinate> path) {
|
||||
int[][] tempMaze = Arrays.stream(maze)
|
||||
.map(int[]::clone)
|
||||
.toArray(int[][]::new);
|
||||
for (Coordinate coordinate : path) {
|
||||
if (isStart(coordinate.getX(), coordinate.getY()) || isExit(coordinate.getX(), coordinate.getY())) {
|
||||
continue;
|
||||
}
|
||||
tempMaze[coordinate.getX()][coordinate.getY()] = PATH;
|
||||
}
|
||||
System.out.println(toString(tempMaze));
|
||||
}
|
||||
|
||||
public String toString(int[][] maze) {
|
||||
StringBuilder result = new StringBuilder(getWidth() * (getHeight() + 1));
|
||||
for (int row = 0; row < getHeight(); row++) {
|
||||
for (int col = 0; col < getWidth(); col++) {
|
||||
if (maze[row][col] == ROAD) {
|
||||
result.append(' ');
|
||||
} else if (maze[row][col] == WALL) {
|
||||
result.append('#');
|
||||
} else if (maze[row][col] == START) {
|
||||
result.append('S');
|
||||
} else if (maze[row][col] == EXIT) {
|
||||
result.append('E');
|
||||
} else {
|
||||
result.append('.');
|
||||
}
|
||||
}
|
||||
result.append('\n');
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
for (int i = 0; i < visited.length; i++)
|
||||
Arrays.fill(visited[i], false);
|
||||
}
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.algorithms.maze.solver;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
public class MazeDriver {
|
||||
public static void main(String[] args) throws Exception {
|
||||
File maze1 = new File("src/main/resources/maze/maze1.txt");
|
||||
File maze2 = new File("src/main/resources/maze/maze2.txt");
|
||||
|
||||
execute(maze1);
|
||||
execute(maze2);
|
||||
}
|
||||
|
||||
private static void execute(File file) throws Exception {
|
||||
Maze maze = new Maze(file);
|
||||
dfs(maze);
|
||||
bfs(maze);
|
||||
}
|
||||
|
||||
private static void bfs(Maze maze) {
|
||||
BFSMazeSolver bfs = new BFSMazeSolver();
|
||||
List<Coordinate> path = bfs.solve(maze);
|
||||
maze.printPath(path);
|
||||
maze.reset();
|
||||
}
|
||||
|
||||
private static void dfs(Maze maze) {
|
||||
DFSMazeSolver dfs = new DFSMazeSolver();
|
||||
List<Coordinate> path = dfs.solve(maze);
|
||||
maze.printPath(path);
|
||||
maze.reset();
|
||||
}
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
@ -148,7 +148,7 @@ public class Board {
|
||||
System.out.println("Game Draw");
|
||||
break;
|
||||
case IN_PROGRESS:
|
||||
System.out.println("Game In rogress");
|
||||
System.out.println("Game In Progress");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,88 @@
|
||||
package com.baeldung.algorithms.middleelementlookup;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Optional;
|
||||
|
||||
public class MiddleElementLookup {
|
||||
|
||||
public static Optional<String> findMiddleElementLinkedList(LinkedList<String> linkedList) {
|
||||
if (linkedList == null || linkedList.isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return Optional.ofNullable(linkedList.get((linkedList.size() - 1) / 2));
|
||||
}
|
||||
|
||||
public static Optional<String> findMiddleElementFromHead(Node head) {
|
||||
if (head == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
// calculate the size of the list
|
||||
Node current = head;
|
||||
int size = 1;
|
||||
while (current.hasNext()) {
|
||||
current = current.next();
|
||||
size++;
|
||||
}
|
||||
|
||||
// iterate till the middle element
|
||||
current = head;
|
||||
for (int i = 0; i < (size - 1) / 2; i++) {
|
||||
current = current.next();
|
||||
}
|
||||
|
||||
return Optional.ofNullable(current.data());
|
||||
}
|
||||
|
||||
public static Optional<String> findMiddleElementFromHead1PassRecursively(Node head) {
|
||||
if (head == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
MiddleAuxRecursion middleAux = new MiddleAuxRecursion();
|
||||
findMiddleRecursively(head, middleAux);
|
||||
return Optional.ofNullable(middleAux.middle.data());
|
||||
}
|
||||
|
||||
private static void findMiddleRecursively(Node node, MiddleAuxRecursion middleAux) {
|
||||
if (node == null) {
|
||||
// reached the end
|
||||
middleAux.length = middleAux.length / 2;
|
||||
return;
|
||||
}
|
||||
middleAux.length++;
|
||||
findMiddleRecursively(node.next(), middleAux);
|
||||
|
||||
if (middleAux.length == 0) {
|
||||
// found the middle
|
||||
middleAux.middle = node;
|
||||
}
|
||||
|
||||
middleAux.length--;
|
||||
}
|
||||
|
||||
public static Optional<String> findMiddleElementFromHead1PassIteratively(Node head) {
|
||||
if (head == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
Node slowPointer = head;
|
||||
Node fastPointer = head;
|
||||
|
||||
while (fastPointer.hasNext() && fastPointer.next()
|
||||
.hasNext()) {
|
||||
fastPointer = fastPointer.next()
|
||||
.next();
|
||||
slowPointer = slowPointer.next();
|
||||
}
|
||||
|
||||
return Optional.ofNullable(slowPointer.data());
|
||||
}
|
||||
|
||||
private static class MiddleAuxRecursion {
|
||||
Node middle;
|
||||
int length = 0;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,34 @@
|
||||
package com.baeldung.algorithms.middleelementlookup;
|
||||
|
||||
public class Node {
|
||||
private Node next;
|
||||
private String data;
|
||||
|
||||
public Node(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public String data() {
|
||||
return data;
|
||||
}
|
||||
|
||||
public void setData(String data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
public Node next() {
|
||||
return next;
|
||||
}
|
||||
|
||||
public void setNext(Node next) {
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return this.data;
|
||||
}
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
/**
|
||||
* Constants used by the Multi-swarm optimization algorithms.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class Constants {
|
||||
|
||||
/**
|
||||
* The inertia factor encourages a particle to continue moving in its
|
||||
* current direction.
|
||||
*/
|
||||
public static final double INERTIA_FACTOR = 0.729;
|
||||
|
||||
/**
|
||||
* The cognitive weight encourages a particle to move toward its historical
|
||||
* best-known position.
|
||||
*/
|
||||
public static final double COGNITIVE_WEIGHT = 1.49445;
|
||||
|
||||
/**
|
||||
* The social weight encourages a particle to move toward the best-known
|
||||
* position found by any of the particle’s swarm-mates.
|
||||
*/
|
||||
public static final double SOCIAL_WEIGHT = 1.49445;
|
||||
|
||||
/**
|
||||
* The global weight encourages a particle to move toward the best-known
|
||||
* position found by any particle in any swarm.
|
||||
*/
|
||||
public static final double GLOBAL_WEIGHT = 0.3645;
|
||||
|
||||
/**
|
||||
* Upper bound for the random generation. We use it to reduce the
|
||||
* computation time since we can rawly estimate it.
|
||||
*/
|
||||
public static final int PARTICLE_UPPER_BOUND = 10000000;
|
||||
|
||||
/**
|
||||
* Private constructor for utility class.
|
||||
*/
|
||||
private Constants() {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
/**
|
||||
* Interface for a fitness function, used to decouple the main algorithm logic
|
||||
* from the specific problem solution.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public interface FitnessFunction {
|
||||
|
||||
/**
|
||||
* Returns the fitness of a particle given its position.
|
||||
*
|
||||
* @param particlePosition
|
||||
* the position of the particle
|
||||
* @return the fitness of the particle
|
||||
*/
|
||||
public double getFitness(long[] particlePosition);
|
||||
|
||||
}
|
@ -0,0 +1,227 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Represents a collection of {@link Swarm}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class Multiswarm {
|
||||
|
||||
/**
|
||||
* The swarms managed by this multiswarm.
|
||||
*/
|
||||
private Swarm[] swarms;
|
||||
|
||||
/**
|
||||
* The best position found within all the {@link #swarms}.
|
||||
*/
|
||||
private long[] bestPosition;
|
||||
|
||||
/**
|
||||
* The best fitness score found within all the {@link #swarms}.
|
||||
*/
|
||||
private double bestFitness = Double.NEGATIVE_INFINITY;
|
||||
|
||||
/**
|
||||
* A random generator.
|
||||
*/
|
||||
private Random random = new Random();
|
||||
|
||||
/**
|
||||
* The fitness function used to determine how good is a particle.
|
||||
*/
|
||||
private FitnessFunction fitnessFunction;
|
||||
|
||||
/**
|
||||
* Instantiates a new Multiswarm.
|
||||
*
|
||||
* @param numSwarms
|
||||
* the number of {@link #swarms}
|
||||
* @param particlesPerSwarm
|
||||
* the number of particle for each {@link #swarms}
|
||||
* @param fitnessFunction
|
||||
* the {@link #fitnessFunction}
|
||||
*/
|
||||
public Multiswarm(int numSwarms, int particlesPerSwarm, FitnessFunction fitnessFunction) {
|
||||
this.fitnessFunction = fitnessFunction;
|
||||
this.swarms = new Swarm[numSwarms];
|
||||
for (int i = 0; i < numSwarms; i++) {
|
||||
swarms[i] = new Swarm(particlesPerSwarm);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main loop of the algorithm. Iterates all particles of all
|
||||
* {@link #swarms}. For each particle, computes the new fitness and checks
|
||||
* if a new best position has been found among itself, the swarm and all the
|
||||
* swarms and finally updates the particle position and speed.
|
||||
*/
|
||||
public void mainLoop() {
|
||||
for (Swarm swarm : swarms) {
|
||||
for (Particle particle : swarm.getParticles()) {
|
||||
|
||||
long[] particleOldPosition = particle.getPosition().clone();
|
||||
|
||||
// Calculate the particle fitness.
|
||||
particle.setFitness(fitnessFunction.getFitness(particleOldPosition));
|
||||
|
||||
// Check if a new best position has been found for the particle
|
||||
// itself, within the swarm and the multiswarm.
|
||||
if (particle.getFitness() > particle.getBestFitness()) {
|
||||
particle.setBestFitness(particle.getFitness());
|
||||
particle.setBestPosition(particleOldPosition);
|
||||
|
||||
if (particle.getFitness() > swarm.getBestFitness()) {
|
||||
swarm.setBestFitness(particle.getFitness());
|
||||
swarm.setBestPosition(particleOldPosition);
|
||||
|
||||
if (swarm.getBestFitness() > bestFitness) {
|
||||
bestFitness = swarm.getBestFitness();
|
||||
bestPosition = swarm.getBestPosition().clone();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// Updates the particle position by adding the speed to the
|
||||
// actual position.
|
||||
long[] position = particle.getPosition();
|
||||
long[] speed = particle.getSpeed();
|
||||
|
||||
position[0] += speed[0];
|
||||
position[1] += speed[1];
|
||||
|
||||
// Updates the particle speed.
|
||||
speed[0] = getNewParticleSpeedForIndex(particle, swarm, 0);
|
||||
speed[1] = getNewParticleSpeedForIndex(particle, swarm, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes a new speed for a given particle of a given swarm on a given
|
||||
* axis. The new speed is computed using the formula:
|
||||
*
|
||||
* <pre>
|
||||
* ({@link Constants#INERTIA_FACTOR} * {@link Particle#getSpeed()}) +
|
||||
* (({@link Constants#COGNITIVE_WEIGHT} * random(0,1)) * ({@link Particle#getBestPosition()} - {@link Particle#getPosition()})) +
|
||||
* (({@link Constants#SOCIAL_WEIGHT} * random(0,1)) * ({@link Swarm#getBestPosition()} - {@link Particle#getPosition()})) +
|
||||
* (({@link Constants#GLOBAL_WEIGHT} * random(0,1)) * ({@link #bestPosition} - {@link Particle#getPosition()}))
|
||||
* </pre>
|
||||
*
|
||||
* @param particle
|
||||
* the particle whose new speed needs to be computed
|
||||
* @param swarm
|
||||
* the swarm which contains the particle
|
||||
* @param index
|
||||
* the index of the particle axis whose speeds needs to be
|
||||
* computed
|
||||
* @return the new speed of the particle passed on the given axis
|
||||
*/
|
||||
private int getNewParticleSpeedForIndex(Particle particle, Swarm swarm, int index) {
|
||||
return (int) ((Constants.INERTIA_FACTOR * particle.getSpeed()[index])
|
||||
+ (randomizePercentage(Constants.COGNITIVE_WEIGHT)
|
||||
* (particle.getBestPosition()[index] - particle.getPosition()[index]))
|
||||
+ (randomizePercentage(Constants.SOCIAL_WEIGHT)
|
||||
* (swarm.getBestPosition()[index] - particle.getPosition()[index]))
|
||||
+ (randomizePercentage(Constants.GLOBAL_WEIGHT)
|
||||
* (bestPosition[index] - particle.getPosition()[index])));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a random number between 0 and the value passed as argument.
|
||||
*
|
||||
* @param value
|
||||
* the value to randomize
|
||||
* @return a random value between 0 and the one passed as argument
|
||||
*/
|
||||
private double randomizePercentage(double value) {
|
||||
return random.nextDouble() * value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestPosition}.
|
||||
*
|
||||
* @return the {@link #bestPosition}
|
||||
*/
|
||||
public long[] getBestPosition() {
|
||||
return bestPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestFitness}.
|
||||
*
|
||||
* @return the {@link #bestFitness}
|
||||
*/
|
||||
public double getBestFitness() {
|
||||
return bestFitness;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(bestFitness);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + Arrays.hashCode(bestPosition);
|
||||
result = prime * result + ((fitnessFunction == null) ? 0 : fitnessFunction.hashCode());
|
||||
result = prime * result + ((random == null) ? 0 : random.hashCode());
|
||||
result = prime * result + Arrays.hashCode(swarms);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Multiswarm other = (Multiswarm) obj;
|
||||
if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness))
|
||||
return false;
|
||||
if (!Arrays.equals(bestPosition, other.bestPosition))
|
||||
return false;
|
||||
if (fitnessFunction == null) {
|
||||
if (other.fitnessFunction != null)
|
||||
return false;
|
||||
} else if (!fitnessFunction.equals(other.fitnessFunction))
|
||||
return false;
|
||||
if (random == null) {
|
||||
if (other.random != null)
|
||||
return false;
|
||||
} else if (!random.equals(other.random))
|
||||
return false;
|
||||
if (!Arrays.equals(swarms, other.swarms))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Multiswarm [swarms=" + Arrays.toString(swarms) + ", bestPosition=" + Arrays.toString(bestPosition)
|
||||
+ ", bestFitness=" + bestFitness + ", random=" + random + ", fitnessFunction=" + fitnessFunction + "]";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,204 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* Represents a particle, the basic component of a {@link Swarm}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class Particle {
|
||||
|
||||
/**
|
||||
* The current position of this particle.
|
||||
*/
|
||||
private long[] position;
|
||||
|
||||
/**
|
||||
* The speed of this particle.
|
||||
*/
|
||||
private long[] speed;
|
||||
|
||||
/**
|
||||
* The fitness of this particle for the current position.
|
||||
*/
|
||||
private double fitness;
|
||||
|
||||
/**
|
||||
* The best position found by this particle.
|
||||
*/
|
||||
private long[] bestPosition;
|
||||
|
||||
/**
|
||||
* The best fitness found by this particle.
|
||||
*/
|
||||
private double bestFitness = Double.NEGATIVE_INFINITY;
|
||||
|
||||
/**
|
||||
* Instantiates a new Particle.
|
||||
*
|
||||
* @param initialPosition
|
||||
* the initial {@link #position}
|
||||
* @param initialSpeed
|
||||
* the initial {@link #speed}
|
||||
*/
|
||||
public Particle(long[] initialPosition, long[] initialSpeed) {
|
||||
this.position = initialPosition;
|
||||
this.speed = initialSpeed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #position}.
|
||||
*
|
||||
* @return the {@link #position}
|
||||
*/
|
||||
public long[] getPosition() {
|
||||
return position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #speed}.
|
||||
*
|
||||
* @return the {@link #speed}
|
||||
*/
|
||||
public long[] getSpeed() {
|
||||
return speed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #fitness}.
|
||||
*
|
||||
* @return the {@link #fitness}
|
||||
*/
|
||||
public double getFitness() {
|
||||
return fitness;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestPosition}.
|
||||
*
|
||||
* @return the {@link #bestPosition}
|
||||
*/
|
||||
public long[] getBestPosition() {
|
||||
return bestPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestFitness}.
|
||||
*
|
||||
* @return the {@link #bestFitness}
|
||||
*/
|
||||
public double getBestFitness() {
|
||||
return bestFitness;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #position}.
|
||||
*
|
||||
* @param position
|
||||
* the new {@link #position}
|
||||
*/
|
||||
public void setPosition(long[] position) {
|
||||
this.position = position;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #speed}.
|
||||
*
|
||||
* @param speed
|
||||
* the new {@link #speed}
|
||||
*/
|
||||
public void setSpeed(long[] speed) {
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #fitness}.
|
||||
*
|
||||
* @param fitness
|
||||
* the new {@link #fitness}
|
||||
*/
|
||||
public void setFitness(double fitness) {
|
||||
this.fitness = fitness;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #bestPosition}.
|
||||
*
|
||||
* @param bestPosition
|
||||
* the new {@link #bestPosition}
|
||||
*/
|
||||
public void setBestPosition(long[] bestPosition) {
|
||||
this.bestPosition = bestPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #bestFitness}.
|
||||
*
|
||||
* @param bestFitness
|
||||
* the new {@link #bestFitness}
|
||||
*/
|
||||
public void setBestFitness(double bestFitness) {
|
||||
this.bestFitness = bestFitness;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(bestFitness);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + Arrays.hashCode(bestPosition);
|
||||
temp = Double.doubleToLongBits(fitness);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + Arrays.hashCode(position);
|
||||
result = prime * result + Arrays.hashCode(speed);
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Particle other = (Particle) obj;
|
||||
if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness))
|
||||
return false;
|
||||
if (!Arrays.equals(bestPosition, other.bestPosition))
|
||||
return false;
|
||||
if (Double.doubleToLongBits(fitness) != Double.doubleToLongBits(other.fitness))
|
||||
return false;
|
||||
if (!Arrays.equals(position, other.position))
|
||||
return false;
|
||||
if (!Arrays.equals(speed, other.speed))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Particle [position=" + Arrays.toString(position) + ", speed=" + Arrays.toString(speed) + ", fitness="
|
||||
+ fitness + ", bestPosition=" + Arrays.toString(bestPosition) + ", bestFitness=" + bestFitness + "]";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,155 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
|
||||
/**
|
||||
* Represents a collection of {@link Particle}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class Swarm {
|
||||
|
||||
/**
|
||||
* The particles of this swarm.
|
||||
*/
|
||||
private Particle[] particles;
|
||||
|
||||
/**
|
||||
* The best position found within the particles of this swarm.
|
||||
*/
|
||||
private long[] bestPosition;
|
||||
|
||||
/**
|
||||
* The best fitness score found within the particles of this swarm.
|
||||
*/
|
||||
private double bestFitness = Double.NEGATIVE_INFINITY;
|
||||
|
||||
/**
|
||||
* A random generator.
|
||||
*/
|
||||
private Random random = new Random();
|
||||
|
||||
/**
|
||||
* Instantiates a new Swarm.
|
||||
*
|
||||
* @param numParticles
|
||||
* the number of particles of the swarm
|
||||
*/
|
||||
public Swarm(int numParticles) {
|
||||
particles = new Particle[numParticles];
|
||||
for (int i = 0; i < numParticles; i++) {
|
||||
long[] initialParticlePosition = { random.nextInt(Constants.PARTICLE_UPPER_BOUND),
|
||||
random.nextInt(Constants.PARTICLE_UPPER_BOUND) };
|
||||
long[] initialParticleSpeed = { random.nextInt(Constants.PARTICLE_UPPER_BOUND),
|
||||
random.nextInt(Constants.PARTICLE_UPPER_BOUND) };
|
||||
particles[i] = new Particle(initialParticlePosition, initialParticleSpeed);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #particles}.
|
||||
*
|
||||
* @return the {@link #particles}
|
||||
*/
|
||||
public Particle[] getParticles() {
|
||||
return particles;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestPosition}.
|
||||
*
|
||||
* @return the {@link #bestPosition}
|
||||
*/
|
||||
public long[] getBestPosition() {
|
||||
return bestPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the {@link #bestFitness}.
|
||||
*
|
||||
* @return the {@link #bestFitness}
|
||||
*/
|
||||
public double getBestFitness() {
|
||||
return bestFitness;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #bestPosition}.
|
||||
*
|
||||
* @param bestPosition
|
||||
* the new {@link #bestPosition}
|
||||
*/
|
||||
public void setBestPosition(long[] bestPosition) {
|
||||
this.bestPosition = bestPosition;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the {@link #bestFitness}.
|
||||
*
|
||||
* @param bestFitness
|
||||
* the new {@link #bestFitness}
|
||||
*/
|
||||
public void setBestFitness(double bestFitness) {
|
||||
this.bestFitness = bestFitness;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#hashCode()
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
long temp;
|
||||
temp = Double.doubleToLongBits(bestFitness);
|
||||
result = prime * result + (int) (temp ^ (temp >>> 32));
|
||||
result = prime * result + Arrays.hashCode(bestPosition);
|
||||
result = prime * result + Arrays.hashCode(particles);
|
||||
result = prime * result + ((random == null) ? 0 : random.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#equals(java.lang.Object)
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Swarm other = (Swarm) obj;
|
||||
if (Double.doubleToLongBits(bestFitness) != Double.doubleToLongBits(other.bestFitness))
|
||||
return false;
|
||||
if (!Arrays.equals(bestPosition, other.bestPosition))
|
||||
return false;
|
||||
if (!Arrays.equals(particles, other.particles))
|
||||
return false;
|
||||
if (random == null) {
|
||||
if (other.random != null)
|
||||
return false;
|
||||
} else if (!random.equals(other.random))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see java.lang.Object#toString()
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Swarm [particles=" + Arrays.toString(particles) + ", bestPosition=" + Arrays.toString(bestPosition)
|
||||
+ ", bestFitness=" + bestFitness + ", random=" + random + "]";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package com.baeldung.algorithms.numberwordconverter;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
|
||||
import pl.allegro.finance.tradukisto.MoneyConverters;
|
||||
|
||||
public class NumberWordConverter {
|
||||
|
||||
public static final String INVALID_INPUT_GIVEN = "Invalid input given";
|
||||
|
||||
public static final String[] ones = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen" };
|
||||
|
||||
public static final String[] tens = {
|
||||
"", // 0
|
||||
"", // 1
|
||||
"twenty", // 2
|
||||
"thirty", // 3
|
||||
"forty", // 4
|
||||
"fifty", // 5
|
||||
"sixty", // 6
|
||||
"seventy", // 7
|
||||
"eighty", // 8
|
||||
"ninety" // 9
|
||||
};
|
||||
|
||||
public static String getMoneyIntoWords(String input) {
|
||||
MoneyConverters converter = MoneyConverters.ENGLISH_BANKING_MONEY_VALUE;
|
||||
return converter.asWords(new BigDecimal(input));
|
||||
}
|
||||
|
||||
public static String getMoneyIntoWords(final double money) {
|
||||
long dollar = (long) money;
|
||||
long cents = Math.round((money - dollar) * 100);
|
||||
if (money == 0D) {
|
||||
return "";
|
||||
}
|
||||
if (money < 0) {
|
||||
return INVALID_INPUT_GIVEN;
|
||||
}
|
||||
String dollarPart = "";
|
||||
if (dollar > 0) {
|
||||
dollarPart = convert(dollar) + " dollar" + (dollar == 1 ? "" : "s");
|
||||
}
|
||||
String centsPart = "";
|
||||
if (cents > 0) {
|
||||
if (dollarPart.length() > 0) {
|
||||
centsPart = " and ";
|
||||
}
|
||||
centsPart += convert(cents) + " cent" + (cents == 1 ? "" : "s");
|
||||
}
|
||||
return dollarPart + centsPart;
|
||||
}
|
||||
|
||||
private static String convert(final long n) {
|
||||
if (n < 0) {
|
||||
return INVALID_INPUT_GIVEN;
|
||||
}
|
||||
if (n < 20) {
|
||||
return ones[(int) n];
|
||||
}
|
||||
if (n < 100) {
|
||||
return tens[(int) n / 10] + ((n % 10 != 0) ? " " : "") + ones[(int) n % 10];
|
||||
}
|
||||
if (n < 1000) {
|
||||
return ones[(int) n / 100] + " hundred" + ((n % 100 != 0) ? " " : "") + convert(n % 100);
|
||||
}
|
||||
if (n < 1_000_000) {
|
||||
return convert(n / 1000) + " thousand" + ((n % 1000 != 0) ? " " : "") + convert(n % 1000);
|
||||
}
|
||||
if (n < 1_000_000_000) {
|
||||
return convert(n / 1_000_000) + " million" + ((n % 1_000_000 != 0) ? " " : "") + convert(n % 1_000_000);
|
||||
}
|
||||
return convert(n / 1_000_000_000) + " billion" + ((n % 1_000_000_000 != 0) ? " " : "") + convert(n % 1_000_000_000);
|
||||
}
|
||||
}
|
@ -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,52 @@
|
||||
package com.baeldung.algorithms.romannumerals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
class RomanArabicConverter {
|
||||
|
||||
public static int romanToArabic(String input) {
|
||||
String romanNumeral = input.toUpperCase();
|
||||
int result = 0;
|
||||
|
||||
List<RomanNumeral> romanNumerals = RomanNumeral.getReverseSortedValues();
|
||||
|
||||
int i = 0;
|
||||
|
||||
while ((romanNumeral.length() > 0) && (i < romanNumerals.size())) {
|
||||
RomanNumeral symbol = romanNumerals.get(i);
|
||||
if (romanNumeral.startsWith(symbol.name())) {
|
||||
result += symbol.getValue();
|
||||
romanNumeral = romanNumeral.substring(symbol.name().length());
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
if (romanNumeral.length() > 0) {
|
||||
throw new IllegalArgumentException(input + " cannot be converted to a Roman Numeral");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String arabicToRoman(int number) {
|
||||
if ((number <= 0) || (number > 4000)) {
|
||||
throw new IllegalArgumentException(number + " is not in range (0,4000]");
|
||||
}
|
||||
|
||||
List<RomanNumeral> romanNumerals = RomanNumeral.getReverseSortedValues();
|
||||
|
||||
int i = 0;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
while (number > 0 && i < romanNumerals.size()) {
|
||||
RomanNumeral currentSymbol = romanNumerals.get(i);
|
||||
if (currentSymbol.getValue() <= number) {
|
||||
sb.append(currentSymbol.name());
|
||||
number -= currentSymbol.getValue();
|
||||
} else {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.romannumerals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
enum RomanNumeral {
|
||||
I(1), IV(4), V(5), IX(9), X(10), XL(40), L(50), XC(90), C(100), CD(400), D(500), CM(900), M(1000);
|
||||
|
||||
private int value;
|
||||
|
||||
RomanNumeral(int value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public int getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public static List<RomanNumeral> getReverseSortedValues() {
|
||||
return Arrays.stream(values())
|
||||
.sorted(Comparator.comparing((RomanNumeral e) -> e.value).reversed())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -0,0 +1,104 @@
|
||||
package com.baeldung.algorithms.sudoku;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class BacktrackingAlgorithm {
|
||||
|
||||
private static final int BOARD_SIZE = 9;
|
||||
private static final int SUBSECTION_SIZE = 3;
|
||||
private static final int BOARD_START_INDEX = 0;
|
||||
|
||||
private static final int NO_VALUE = 0;
|
||||
private static final int MIN_VALUE = 1;
|
||||
private static final int MAX_VALUE = 9;
|
||||
|
||||
private static int[][] board = {
|
||||
{8, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 3, 6, 0, 0, 0, 0, 0},
|
||||
{0, 7, 0, 0, 9, 0, 2, 0, 0},
|
||||
{0, 5, 0, 0, 0, 7, 0, 0, 0},
|
||||
{0, 0, 0, 0, 4, 5, 7, 0, 0},
|
||||
{0, 0, 0, 1, 0, 0, 0, 3, 0},
|
||||
{0, 0, 1, 0, 0, 0, 0, 6, 8},
|
||||
{0, 0, 8, 5, 0, 0, 0, 1, 0},
|
||||
{0, 9, 0, 0, 0, 0, 4, 0, 0}
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
BacktrackingAlgorithm solver = new BacktrackingAlgorithm();
|
||||
solver.solve(board);
|
||||
solver.printBoard();
|
||||
}
|
||||
|
||||
private void printBoard() {
|
||||
for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
|
||||
for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
|
||||
System.out.print(board[row][column] + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean solve(int[][] board) {
|
||||
for (int row = BOARD_START_INDEX; row < BOARD_SIZE; row++) {
|
||||
for (int column = BOARD_START_INDEX; column < BOARD_SIZE; column++) {
|
||||
if (board[row][column] == NO_VALUE) {
|
||||
for (int k = MIN_VALUE; k <= MAX_VALUE; k++) {
|
||||
board[row][column] = k;
|
||||
if (isValid(board, row, column) && solve(board)) {
|
||||
return true;
|
||||
}
|
||||
board[row][column] = NO_VALUE;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isValid(int[][] board, int row, int column) {
|
||||
return rowConstraint(board, row) &&
|
||||
columnConstraint(board, column) &&
|
||||
subsectionConstraint(board, row, column);
|
||||
}
|
||||
|
||||
private boolean subsectionConstraint(int[][] board, int row, int column) {
|
||||
boolean[] constraint = new boolean[BOARD_SIZE];
|
||||
int subsectionRowStart = (row / SUBSECTION_SIZE) * SUBSECTION_SIZE;
|
||||
int subsectionRowEnd = subsectionRowStart + SUBSECTION_SIZE;
|
||||
|
||||
int subsectionColumnStart = (column / SUBSECTION_SIZE) * SUBSECTION_SIZE;
|
||||
int subsectionColumnEnd = subsectionColumnStart + SUBSECTION_SIZE;
|
||||
|
||||
for (int r = subsectionRowStart; r < subsectionRowEnd; r++) {
|
||||
for (int c = subsectionColumnStart; c < subsectionColumnEnd; c++) {
|
||||
if (!checkConstraint(board, r, constraint, c)) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean columnConstraint(int[][] board, int column) {
|
||||
boolean[] constraint = new boolean[BOARD_SIZE];
|
||||
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
|
||||
.allMatch(row -> checkConstraint(board, row, constraint, column));
|
||||
}
|
||||
|
||||
private boolean rowConstraint(int[][] board, int row) {
|
||||
boolean[] constraint = new boolean[BOARD_SIZE];
|
||||
return IntStream.range(BOARD_START_INDEX, BOARD_SIZE)
|
||||
.allMatch(column -> checkConstraint(board, row, constraint, column));
|
||||
}
|
||||
|
||||
private boolean checkConstraint(int[][] board, int row, boolean[] constraint, int column) {
|
||||
if (board[row][column] != NO_VALUE) {
|
||||
if (!constraint[board[row][column] - 1]) {
|
||||
constraint[board[row][column] - 1] = true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.baeldung.algorithms.sudoku;
|
||||
|
||||
class ColumnNode extends DancingNode {
|
||||
int size;
|
||||
String name;
|
||||
|
||||
ColumnNode(String n) {
|
||||
super();
|
||||
size = 0;
|
||||
name = n;
|
||||
C = this;
|
||||
}
|
||||
|
||||
void cover() {
|
||||
unlinkLR();
|
||||
for (DancingNode i = this.D; i != this; i = i.D) {
|
||||
for (DancingNode j = i.R; j != i; j = j.R) {
|
||||
j.unlinkUD();
|
||||
j.C.size--;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void uncover() {
|
||||
for (DancingNode i = this.U; i != this; i = i.U) {
|
||||
for (DancingNode j = i.L; j != i; j = j.L) {
|
||||
j.C.size++;
|
||||
j.relinkUD();
|
||||
}
|
||||
}
|
||||
relinkLR();
|
||||
}
|
||||
}
|
@ -0,0 +1,133 @@
|
||||
package com.baeldung.algorithms.sudoku;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
public class DancingLinks {
|
||||
|
||||
private ColumnNode header;
|
||||
private List<DancingNode> answer;
|
||||
|
||||
private void search(int k) {
|
||||
if (header.R == header) {
|
||||
handleSolution(answer);
|
||||
} else {
|
||||
ColumnNode c = selectColumnNodeHeuristic();
|
||||
c.cover();
|
||||
|
||||
for (DancingNode r = c.D; r != c; r = r.D) {
|
||||
answer.add(r);
|
||||
|
||||
for (DancingNode j = r.R; j != r; j = j.R) {
|
||||
j.C.cover();
|
||||
}
|
||||
|
||||
search(k + 1);
|
||||
|
||||
r = answer.remove(answer.size() - 1);
|
||||
c = r.C;
|
||||
|
||||
for (DancingNode j = r.L; j != r; j = j.L) {
|
||||
j.C.uncover();
|
||||
}
|
||||
}
|
||||
c.uncover();
|
||||
}
|
||||
}
|
||||
|
||||
private ColumnNode selectColumnNodeHeuristic() {
|
||||
int min = Integer.MAX_VALUE;
|
||||
ColumnNode ret = null;
|
||||
for (ColumnNode c = (ColumnNode) header.R; c != header; c = (ColumnNode) c.R) {
|
||||
if (c.size < min) {
|
||||
min = c.size;
|
||||
ret = c;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
private ColumnNode makeDLXBoard(boolean[][] grid) {
|
||||
final int COLS = grid[0].length;
|
||||
|
||||
ColumnNode headerNode = new ColumnNode("header");
|
||||
List<ColumnNode> columnNodes = new ArrayList<>();
|
||||
|
||||
for (int i = 0; i < COLS; i++) {
|
||||
ColumnNode n = new ColumnNode(Integer.toString(i));
|
||||
columnNodes.add(n);
|
||||
headerNode = (ColumnNode) headerNode.hookRight(n);
|
||||
}
|
||||
headerNode = headerNode.R.C;
|
||||
|
||||
for (boolean[] aGrid : grid) {
|
||||
DancingNode prev = null;
|
||||
for (int j = 0; j < COLS; j++) {
|
||||
if (aGrid[j]) {
|
||||
ColumnNode col = columnNodes.get(j);
|
||||
DancingNode newNode = new DancingNode(col);
|
||||
if (prev == null)
|
||||
prev = newNode;
|
||||
col.U.hookDown(newNode);
|
||||
prev = prev.hookRight(newNode);
|
||||
col.size++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
headerNode.size = COLS;
|
||||
|
||||
return headerNode;
|
||||
}
|
||||
|
||||
DancingLinks(boolean[][] cover) {
|
||||
header = makeDLXBoard(cover);
|
||||
}
|
||||
|
||||
public void runSolver() {
|
||||
answer = new LinkedList<>();
|
||||
search(0);
|
||||
}
|
||||
|
||||
private void handleSolution(List<DancingNode> answer) {
|
||||
int[][] result = parseBoard(answer);
|
||||
printSolution(result);
|
||||
}
|
||||
|
||||
private int size = 9;
|
||||
|
||||
private int[][] parseBoard(List<DancingNode> answer) {
|
||||
int[][] result = new int[size][size];
|
||||
for (DancingNode n : answer) {
|
||||
DancingNode rcNode = n;
|
||||
int min = Integer.parseInt(rcNode.C.name);
|
||||
for (DancingNode tmp = n.R; tmp != n; tmp = tmp.R) {
|
||||
int val = Integer.parseInt(tmp.C.name);
|
||||
if (val < min) {
|
||||
min = val;
|
||||
rcNode = tmp;
|
||||
}
|
||||
}
|
||||
int ans1 = Integer.parseInt(rcNode.C.name);
|
||||
int ans2 = Integer.parseInt(rcNode.R.C.name);
|
||||
int r = ans1 / size;
|
||||
int c = ans1 % size;
|
||||
int num = (ans2 % size) + 1;
|
||||
result[r][c] = num;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static void printSolution(int[][] result) {
|
||||
int size = result.length;
|
||||
for (int[] aResult : result) {
|
||||
StringBuilder ret = new StringBuilder();
|
||||
for (int j = 0; j < size; j++) {
|
||||
ret.append(aResult[j]).append(" ");
|
||||
}
|
||||
System.out.println(ret);
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
}
|
@ -0,0 +1,121 @@
|
||||
package com.baeldung.algorithms.sudoku;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
public class DancingLinksAlgorithm {
|
||||
private static final int BOARD_SIZE = 9;
|
||||
private static final int SUBSECTION_SIZE = 3;
|
||||
private static final int NO_VALUE = 0;
|
||||
private static final int CONSTRAINTS = 4;
|
||||
private static final int MIN_VALUE = 1;
|
||||
private static final int MAX_VALUE = 9;
|
||||
private static final int COVER_START_INDEX = 1;
|
||||
|
||||
private static int[][] board = {
|
||||
{8, 0, 0, 0, 0, 0, 0, 0, 0},
|
||||
{0, 0, 3, 6, 0, 0, 0, 0, 0},
|
||||
{0, 7, 0, 0, 9, 0, 2, 0, 0},
|
||||
{0, 5, 0, 0, 0, 7, 0, 0, 0},
|
||||
{0, 0, 0, 0, 4, 5, 7, 0, 0},
|
||||
{0, 0, 0, 1, 0, 0, 0, 3, 0},
|
||||
{0, 0, 1, 0, 0, 0, 0, 6, 8},
|
||||
{0, 0, 8, 5, 0, 0, 0, 1, 0},
|
||||
{0, 9, 0, 0, 0, 0, 4, 0, 0}
|
||||
};
|
||||
|
||||
public static void main(String[] args) {
|
||||
DancingLinksAlgorithm solver = new DancingLinksAlgorithm();
|
||||
solver.solve(board);
|
||||
}
|
||||
|
||||
private void solve(int[][] board) {
|
||||
boolean[][] cover = initializeExactCoverBoard(board);
|
||||
DancingLinks dlx = new DancingLinks(cover);
|
||||
dlx.runSolver();
|
||||
}
|
||||
|
||||
private int getIndex(int row, int column, int num) {
|
||||
return (row - 1) * BOARD_SIZE * BOARD_SIZE + (column - 1) * BOARD_SIZE + (num - 1);
|
||||
}
|
||||
|
||||
private boolean[][] createExactCoverBoard() {
|
||||
boolean[][] coverBoard = new boolean[BOARD_SIZE * BOARD_SIZE * MAX_VALUE][BOARD_SIZE * BOARD_SIZE * CONSTRAINTS];
|
||||
|
||||
int hBase = 0;
|
||||
hBase = checkCellConstraint(coverBoard, hBase);
|
||||
hBase = checkRowConstraint(coverBoard, hBase);
|
||||
hBase = checkColumnConstraint(coverBoard, hBase);
|
||||
checkSubsectionConstraint(coverBoard, hBase);
|
||||
|
||||
return coverBoard;
|
||||
}
|
||||
|
||||
private int checkSubsectionConstraint(boolean[][] coverBoard, int hBase) {
|
||||
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row += SUBSECTION_SIZE) {
|
||||
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column += SUBSECTION_SIZE) {
|
||||
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
|
||||
for (int rowDelta = 0; rowDelta < SUBSECTION_SIZE; rowDelta++) {
|
||||
for (int columnDelta = 0; columnDelta < SUBSECTION_SIZE; columnDelta++) {
|
||||
int index = getIndex(row + rowDelta, column + columnDelta, n);
|
||||
coverBoard[index][hBase] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return hBase;
|
||||
}
|
||||
|
||||
private int checkColumnConstraint(boolean[][] coverBoard, int hBase) {
|
||||
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) {
|
||||
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
|
||||
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
|
||||
int index = getIndex(row, column, n);
|
||||
coverBoard[index][hBase] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return hBase;
|
||||
}
|
||||
|
||||
private int checkRowConstraint(boolean[][] coverBoard, int hBase) {
|
||||
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
|
||||
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
|
||||
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) {
|
||||
int index = getIndex(row, column, n);
|
||||
coverBoard[index][hBase] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return hBase;
|
||||
}
|
||||
|
||||
private int checkCellConstraint(boolean[][] coverBoard, int hBase) {
|
||||
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
|
||||
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++, hBase++) {
|
||||
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) {
|
||||
int index = getIndex(row, column, n);
|
||||
coverBoard[index][hBase] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return hBase;
|
||||
}
|
||||
|
||||
private boolean[][] initializeExactCoverBoard(int[][] board) {
|
||||
boolean[][] coverBoard = createExactCoverBoard();
|
||||
for (int row = COVER_START_INDEX; row <= BOARD_SIZE; row++) {
|
||||
for (int column = COVER_START_INDEX; column <= BOARD_SIZE; column++) {
|
||||
int n = board[row - 1][column - 1];
|
||||
if (n != NO_VALUE) {
|
||||
for (int num = MIN_VALUE; num <= MAX_VALUE; num++) {
|
||||
if (num != n) {
|
||||
Arrays.fill(coverBoard[getIndex(row, column, num)], false);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return coverBoard;
|
||||
}
|
||||
}
|
@ -0,0 +1,50 @@
|
||||
package com.baeldung.algorithms.sudoku;
|
||||
|
||||
class DancingNode {
|
||||
DancingNode L, R, U, D;
|
||||
ColumnNode C;
|
||||
|
||||
DancingNode hookDown(DancingNode node) {
|
||||
assert (this.C == node.C);
|
||||
node.D = this.D;
|
||||
node.D.U = node;
|
||||
node.U = this;
|
||||
this.D = node;
|
||||
return node;
|
||||
}
|
||||
|
||||
DancingNode hookRight(DancingNode node) {
|
||||
node.R = this.R;
|
||||
node.R.L = node;
|
||||
node.L = this;
|
||||
this.R = node;
|
||||
return node;
|
||||
}
|
||||
|
||||
void unlinkLR() {
|
||||
this.L.R = this.R;
|
||||
this.R.L = this.L;
|
||||
}
|
||||
|
||||
void relinkLR() {
|
||||
this.L.R = this.R.L = this;
|
||||
}
|
||||
|
||||
void unlinkUD() {
|
||||
this.U.D = this.D;
|
||||
this.D.U = this.U;
|
||||
}
|
||||
|
||||
void relinkUD() {
|
||||
this.U.D = this.D.U = this;
|
||||
}
|
||||
|
||||
DancingNode() {
|
||||
L = R = U = D = this;
|
||||
}
|
||||
|
||||
DancingNode(ColumnNode c) {
|
||||
this();
|
||||
C = c;
|
||||
}
|
||||
}
|
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>
|
12
algorithms/src/main/resources/maze/maze1.txt
Normal file
12
algorithms/src/main/resources/maze/maze1.txt
Normal file
@ -0,0 +1,12 @@
|
||||
S ########
|
||||
# #
|
||||
# ### ## #
|
||||
# # # #
|
||||
# # # # #
|
||||
# ## #####
|
||||
# # #
|
||||
# # # # #
|
||||
##### ####
|
||||
# # E
|
||||
# # # #
|
||||
##########
|
22
algorithms/src/main/resources/maze/maze2.txt
Normal file
22
algorithms/src/main/resources/maze/maze2.txt
Normal file
@ -0,0 +1,22 @@
|
||||
S ##########################
|
||||
# # # #
|
||||
# # #### ############### #
|
||||
# # # # # #
|
||||
# # #### # # ###############
|
||||
# # # # # # #
|
||||
# # # #### ### ########### #
|
||||
# # # # # #
|
||||
# ################## #
|
||||
######### # # # # #
|
||||
# # #### # ####### # #
|
||||
# # ### ### # # # # #
|
||||
# # ## # ##### # #
|
||||
##### ####### # # # # #
|
||||
# # ## ## #### # #
|
||||
# ##### ####### # #
|
||||
# # ############
|
||||
####### ######### # #
|
||||
# # ######## #
|
||||
# ####### ###### ## # E
|
||||
# # # ## #
|
||||
############################
|
@ -1,58 +0,0 @@
|
||||
package algorithms;
|
||||
|
||||
import com.baeldung.algorithms.hillclimbing.HillClimbing;
|
||||
import com.baeldung.algorithms.hillclimbing.State;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class HillClimbingAlgorithmTest {
|
||||
private Stack<String> initStack;
|
||||
private Stack<String> goalStack;
|
||||
|
||||
@Before
|
||||
public void initStacks() {
|
||||
String blockArr[] = { "B", "C", "D", "A" };
|
||||
String goalBlockArr[] = { "A", "B", "C", "D" };
|
||||
initStack = new Stack<>();
|
||||
for (String block : blockArr)
|
||||
initStack.push(block);
|
||||
goalStack = new Stack<>();
|
||||
for (String block : goalBlockArr)
|
||||
goalStack.push(block);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInitAndGoalState_whenGetPathWithHillClimbing_thenPathFound() {
|
||||
HillClimbing hillClimbing = new HillClimbing();
|
||||
|
||||
List<State> path;
|
||||
try {
|
||||
path = hillClimbing.getRouteWithHillClimbing(initStack, goalStack);
|
||||
assertNotNull(path);
|
||||
assertEquals(path.get(path.size() - 1)
|
||||
.getState()
|
||||
.get(0), goalStack);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCurrentState_whenFindNextState_thenBetterHeuristics() {
|
||||
HillClimbing hillClimbing = new HillClimbing();
|
||||
List<Stack<String>> initList = new ArrayList<>();
|
||||
initList.add(initStack);
|
||||
State currentState = new State(initList);
|
||||
currentState.setHeuristics(hillClimbing.getHeuristicsValue(initList, goalStack));
|
||||
State nextState = hillClimbing.findNextState(currentState, goalStack);
|
||||
assertTrue(nextState.getHeuristics() > currentState.getHeuristics());
|
||||
}
|
||||
}
|
@ -0,0 +1,58 @@
|
||||
package algorithms;
|
||||
|
||||
import com.baeldung.algorithms.hillclimbing.HillClimbing;
|
||||
import com.baeldung.algorithms.hillclimbing.State;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Stack;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class HillClimbingAlgorithmUnitTest {
|
||||
private Stack<String> initStack;
|
||||
private Stack<String> goalStack;
|
||||
|
||||
@Before
|
||||
public void initStacks() {
|
||||
String blockArr[] = { "B", "C", "D", "A" };
|
||||
String goalBlockArr[] = { "A", "B", "C", "D" };
|
||||
initStack = new Stack<>();
|
||||
for (String block : blockArr)
|
||||
initStack.push(block);
|
||||
goalStack = new Stack<>();
|
||||
for (String block : goalBlockArr)
|
||||
goalStack.push(block);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenInitAndGoalState_whenGetPathWithHillClimbing_thenPathFound() {
|
||||
HillClimbing hillClimbing = new HillClimbing();
|
||||
|
||||
List<State> path;
|
||||
try {
|
||||
path = hillClimbing.getRouteWithHillClimbing(initStack, goalStack);
|
||||
assertNotNull(path);
|
||||
assertEquals(path.get(path.size() - 1)
|
||||
.getState()
|
||||
.get(0), goalStack);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCurrentState_whenFindNextState_thenBetterHeuristics() {
|
||||
HillClimbing hillClimbing = new HillClimbing();
|
||||
List<Stack<String>> initList = new ArrayList<>();
|
||||
initList.add(initStack);
|
||||
State currentState = new State(initList);
|
||||
currentState.setHeuristics(hillClimbing.getHeuristicsValue(initList, goalStack));
|
||||
State nextState = hillClimbing.findNextState(currentState, goalStack);
|
||||
assertTrue(nextState.getHeuristics() > currentState.getHeuristics());
|
||||
}
|
||||
}
|
@ -0,0 +1,118 @@
|
||||
package algorithms;
|
||||
|
||||
import com.baeldung.algorithms.middleelementlookup.MiddleElementLookup;
|
||||
import com.baeldung.algorithms.middleelementlookup.Node;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.LinkedList;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
|
||||
public class MiddleElementLookupUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleLinkedList_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementLinkedList(createLinkedList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementLinkedList(createLinkedList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleFromHead_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementFromHead(createNodesList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementFromHead(createNodesList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleFromHead1PassRecursively_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(createNodesList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(createNodesList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFindingMiddleFromHead1PassIteratively_thenMiddleFound() {
|
||||
assertEquals("3", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(createNodesList(5))
|
||||
.get());
|
||||
assertEquals("2", MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(createNodesList(4))
|
||||
.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenListEmptyOrNull_thenMiddleNotFound() {
|
||||
// null list
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementLinkedList(null)
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead(null)
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(null)
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(null)
|
||||
.isPresent());
|
||||
|
||||
// empty LinkedList
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementLinkedList(new LinkedList<>())
|
||||
.isPresent());
|
||||
|
||||
// LinkedList with nulls
|
||||
LinkedList<String> nullsList = new LinkedList<>();
|
||||
nullsList.add(null);
|
||||
nullsList.add(null);
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementLinkedList(nullsList)
|
||||
.isPresent());
|
||||
|
||||
// nodes with null values
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead(new Node(null))
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassIteratively(new Node(null))
|
||||
.isPresent());
|
||||
assertFalse(MiddleElementLookup
|
||||
.findMiddleElementFromHead1PassRecursively(new Node(null))
|
||||
.isPresent());
|
||||
}
|
||||
|
||||
private static LinkedList<String> createLinkedList(int n) {
|
||||
LinkedList<String> list = new LinkedList<>();
|
||||
|
||||
for (int i = 1; i <= n; i++) {
|
||||
list.add(String.valueOf(i));
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
private static Node createNodesList(int n) {
|
||||
Node head = new Node("1");
|
||||
Node current = head;
|
||||
|
||||
for (int i = 2; i <= n; i++) {
|
||||
Node newNode = new Node(String.valueOf(i));
|
||||
current.setNext(newNode);
|
||||
current = newNode;
|
||||
}
|
||||
|
||||
return head;
|
||||
}
|
||||
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package algorithms;
|
||||
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.string.search.StringSearchAlgorithms;
|
||||
|
||||
public class StringSearchAlgorithmsTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testStringSearchAlgorithms(){
|
||||
String text = "This is some nice text.";
|
||||
String pattern = "some";
|
||||
|
||||
int realPosition = text.indexOf(pattern);
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.simpleTextSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.RabinKarpMethod(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.KnuthMorrisPrattSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.BoyerMooreHorspoolSimpleSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.BoyerMooreHorspoolSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
}
|
||||
|
||||
}
|
25
algorithms/src/test/java/algorithms/StringSearchAlgorithmsUnitTest.java
Executable file
25
algorithms/src/test/java/algorithms/StringSearchAlgorithmsUnitTest.java
Executable file
@ -0,0 +1,25 @@
|
||||
package algorithms;
|
||||
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.string.search.StringSearchAlgorithms;
|
||||
|
||||
public class StringSearchAlgorithmsUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testStringSearchAlgorithms(){
|
||||
String text = "This is some nice text.";
|
||||
String pattern = "some";
|
||||
|
||||
int realPosition = text.indexOf(pattern);
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.simpleTextSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.RabinKarpMethod(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.KnuthMorrisPrattSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.BoyerMooreHorspoolSimpleSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
Assert.assertTrue(realPosition == StringSearchAlgorithms.BoyerMooreHorspoolSearch(pattern.toCharArray(), text.toCharArray()));
|
||||
}
|
||||
|
||||
}
|
@ -1,43 +0,0 @@
|
||||
package algorithms.binarysearch;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import com.baeldung.algorithms.binarysearch.BinarySearch;
|
||||
|
||||
public class BinarySearchTest {
|
||||
|
||||
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||
int key = 6;
|
||||
int expectedIndexForSearchKey = 7;
|
||||
int low = 0;
|
||||
int high = sortedArray.length - 1;
|
||||
List<Integer> sortedList = Arrays.asList(0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9);
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunIterativelyForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchIteratively(sortedArray, key, low, high));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunRecursivelyForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchRecursively(sortedArray, key, low, high));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunUsingArraysClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaArrays(sortedArray, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedListOfIntegers_whenBinarySearchRunUsingCollectionsClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaCollections(sortedList, key));
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,43 @@
|
||||
package algorithms.binarysearch;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import com.baeldung.algorithms.binarysearch.BinarySearch;
|
||||
|
||||
public class BinarySearchUnitTest {
|
||||
|
||||
int[] sortedArray = { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
|
||||
int key = 6;
|
||||
int expectedIndexForSearchKey = 7;
|
||||
int low = 0;
|
||||
int high = sortedArray.length - 1;
|
||||
List<Integer> sortedList = Arrays.asList(0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9);
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunIterativelyForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchIteratively(sortedArray, key, low, high));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunRecursivelyForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchRecursively(sortedArray, key, low, high));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedArrayOfIntegers_whenBinarySearchRunUsingArraysClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaArrays(sortedArray, key));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenASortedListOfIntegers_whenBinarySearchRunUsingCollectionsClassStaticMethodForANumber_thenGetIndexOfTheNumber() {
|
||||
BinarySearch binSearch = new BinarySearch();
|
||||
Assert.assertEquals(expectedIndexForSearchKey, binSearch.runBinarySearchUsingJavaCollections(sortedList, key));
|
||||
}
|
||||
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
package algorithms.mcts;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.mcts.montecarlo.MonteCarloTreeSearch;
|
||||
import com.baeldung.algorithms.mcts.montecarlo.State;
|
||||
import com.baeldung.algorithms.mcts.montecarlo.UCT;
|
||||
import com.baeldung.algorithms.mcts.tictactoe.Board;
|
||||
import com.baeldung.algorithms.mcts.tictactoe.Position;
|
||||
import com.baeldung.algorithms.mcts.tree.Tree;
|
||||
|
||||
public class MCTSTest {
|
||||
private Tree gameTree;
|
||||
private MonteCarloTreeSearch mcts;
|
||||
|
||||
@Before
|
||||
public void initGameTree() {
|
||||
gameTree = new Tree();
|
||||
mcts = new MonteCarloTreeSearch();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStats_whenGetUCTForNode_thenUCTMatchesWithManualData() {
|
||||
double uctValue = 15.79;
|
||||
assertEquals(UCT.uctValue(600, 300, 20), uctValue, 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giveninitBoardState_whenGetAllPossibleStates_thenNonEmptyList() {
|
||||
State initState = gameTree.getRoot().getState();
|
||||
List<State> possibleStates = initState.getAllPossibleStates();
|
||||
assertTrue(possibleStates.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyBoard_whenPerformMove_thenLessAvailablePossitions() {
|
||||
Board board = new Board();
|
||||
int initAvailablePositions = board.getEmptyPositions().size();
|
||||
board.performMove(Board.P1, new Position(1, 1));
|
||||
int availablePositions = board.getEmptyPositions().size();
|
||||
assertTrue(initAvailablePositions > availablePositions);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyBoard_whenSimulateInterAIPlay_thenGameDraw() {
|
||||
Board board = new Board();
|
||||
|
||||
int player = Board.P1;
|
||||
int totalMoves = Board.DEFAULT_BOARD_SIZE * Board.DEFAULT_BOARD_SIZE;
|
||||
for (int i = 0; i < totalMoves; i++) {
|
||||
board = mcts.findNextMove(board, player);
|
||||
if (board.checkStatus() != -1) {
|
||||
break;
|
||||
}
|
||||
player = 3 - player;
|
||||
}
|
||||
int winStatus = board.checkStatus();
|
||||
assertEquals(winStatus, Board.DRAW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyBoard_whenLevel1VsLevel3_thenLevel3WinsOrDraw() {
|
||||
Board board = new Board();
|
||||
MonteCarloTreeSearch mcts1 = new MonteCarloTreeSearch();
|
||||
mcts1.setLevel(1);
|
||||
MonteCarloTreeSearch mcts3 = new MonteCarloTreeSearch();
|
||||
mcts3.setLevel(3);
|
||||
|
||||
int player = Board.P1;
|
||||
int totalMoves = Board.DEFAULT_BOARD_SIZE * Board.DEFAULT_BOARD_SIZE;
|
||||
for (int i = 0; i < totalMoves; i++) {
|
||||
if (player == Board.P1)
|
||||
board = mcts3.findNextMove(board, player);
|
||||
else
|
||||
board = mcts1.findNextMove(board, player);
|
||||
|
||||
if (board.checkStatus() != -1) {
|
||||
break;
|
||||
}
|
||||
player = 3 - player;
|
||||
}
|
||||
int winStatus = board.checkStatus();
|
||||
assertTrue(winStatus == Board.DRAW || winStatus == Board.P1);
|
||||
}
|
||||
|
||||
}
|
92
algorithms/src/test/java/algorithms/mcts/MCTSUnitTest.java
Normal file
92
algorithms/src/test/java/algorithms/mcts/MCTSUnitTest.java
Normal file
@ -0,0 +1,92 @@
|
||||
package algorithms.mcts;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.mcts.montecarlo.MonteCarloTreeSearch;
|
||||
import com.baeldung.algorithms.mcts.montecarlo.State;
|
||||
import com.baeldung.algorithms.mcts.montecarlo.UCT;
|
||||
import com.baeldung.algorithms.mcts.tictactoe.Board;
|
||||
import com.baeldung.algorithms.mcts.tictactoe.Position;
|
||||
import com.baeldung.algorithms.mcts.tree.Tree;
|
||||
|
||||
public class MCTSUnitTest {
|
||||
private Tree gameTree;
|
||||
private MonteCarloTreeSearch mcts;
|
||||
|
||||
@Before
|
||||
public void initGameTree() {
|
||||
gameTree = new Tree();
|
||||
mcts = new MonteCarloTreeSearch();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStats_whenGetUCTForNode_thenUCTMatchesWithManualData() {
|
||||
double uctValue = 15.79;
|
||||
assertEquals(UCT.uctValue(600, 300, 20), uctValue, 0.01);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void giveninitBoardState_whenGetAllPossibleStates_thenNonEmptyList() {
|
||||
State initState = gameTree.getRoot().getState();
|
||||
List<State> possibleStates = initState.getAllPossibleStates();
|
||||
assertTrue(possibleStates.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyBoard_whenPerformMove_thenLessAvailablePossitions() {
|
||||
Board board = new Board();
|
||||
int initAvailablePositions = board.getEmptyPositions().size();
|
||||
board.performMove(Board.P1, new Position(1, 1));
|
||||
int availablePositions = board.getEmptyPositions().size();
|
||||
assertTrue(initAvailablePositions > availablePositions);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyBoard_whenSimulateInterAIPlay_thenGameDraw() {
|
||||
Board board = new Board();
|
||||
|
||||
int player = Board.P1;
|
||||
int totalMoves = Board.DEFAULT_BOARD_SIZE * Board.DEFAULT_BOARD_SIZE;
|
||||
for (int i = 0; i < totalMoves; i++) {
|
||||
board = mcts.findNextMove(board, player);
|
||||
if (board.checkStatus() != -1) {
|
||||
break;
|
||||
}
|
||||
player = 3 - player;
|
||||
}
|
||||
int winStatus = board.checkStatus();
|
||||
assertEquals(winStatus, Board.DRAW);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmptyBoard_whenLevel1VsLevel3_thenLevel3WinsOrDraw() {
|
||||
Board board = new Board();
|
||||
MonteCarloTreeSearch mcts1 = new MonteCarloTreeSearch();
|
||||
mcts1.setLevel(1);
|
||||
MonteCarloTreeSearch mcts3 = new MonteCarloTreeSearch();
|
||||
mcts3.setLevel(3);
|
||||
|
||||
int player = Board.P1;
|
||||
int totalMoves = Board.DEFAULT_BOARD_SIZE * Board.DEFAULT_BOARD_SIZE;
|
||||
for (int i = 0; i < totalMoves; i++) {
|
||||
if (player == Board.P1)
|
||||
board = mcts3.findNextMove(board, player);
|
||||
else
|
||||
board = mcts1.findNextMove(board, player);
|
||||
|
||||
if (board.checkStatus() != -1) {
|
||||
break;
|
||||
}
|
||||
player = 3 - player;
|
||||
}
|
||||
int winStatus = board.checkStatus();
|
||||
assertTrue(winStatus == Board.DRAW || winStatus == Board.P1);
|
||||
}
|
||||
|
||||
}
|
@ -1,36 +0,0 @@
|
||||
package algorithms.minimax;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import com.baeldung.algorithms.minimax.MiniMax;
|
||||
import com.baeldung.algorithms.minimax.Tree;
|
||||
|
||||
public class MinimaxTest {
|
||||
private Tree gameTree;
|
||||
private MiniMax miniMax;
|
||||
|
||||
@Before
|
||||
public void initMiniMaxUtility() {
|
||||
miniMax = new MiniMax();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMiniMax_whenConstructTree_thenNotNullTree() {
|
||||
assertNull(gameTree);
|
||||
miniMax.constructTree(6);
|
||||
gameTree = miniMax.getTree();
|
||||
assertNotNull(gameTree);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMiniMax_whenCheckWin_thenComputeOptimal() {
|
||||
miniMax.constructTree(6);
|
||||
boolean result = miniMax.checkWin();
|
||||
assertTrue(result);
|
||||
miniMax.constructTree(8);
|
||||
result = miniMax.checkWin();
|
||||
assertFalse(result);
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package algorithms.minimax;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import com.baeldung.algorithms.minimax.MiniMax;
|
||||
import com.baeldung.algorithms.minimax.Tree;
|
||||
|
||||
public class MinimaxUnitTest {
|
||||
private Tree gameTree;
|
||||
private MiniMax miniMax;
|
||||
|
||||
@Before
|
||||
public void initMiniMaxUtility() {
|
||||
miniMax = new MiniMax();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMiniMax_whenConstructTree_thenNotNullTree() {
|
||||
assertNull(gameTree);
|
||||
miniMax.constructTree(6);
|
||||
gameTree = miniMax.getTree();
|
||||
assertNotNull(gameTree);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMiniMax_whenCheckWin_thenComputeOptimal() {
|
||||
miniMax.constructTree(6);
|
||||
boolean result = miniMax.checkWin();
|
||||
assertTrue(result);
|
||||
miniMax.constructTree(8);
|
||||
result = miniMax.checkWin();
|
||||
assertFalse(result);
|
||||
}
|
||||
}
|
@ -0,0 +1,139 @@
|
||||
package com.baeldung.algorithms.analysis;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class AnalysisRunnerLiveTest {
|
||||
|
||||
int n = 10;
|
||||
int total = 0;
|
||||
|
||||
@Test
|
||||
public void whenConstantComplexity_thenConstantRuntime() {
|
||||
|
||||
System.out.println("**** n = " + n + " ****");
|
||||
System.out.println();
|
||||
|
||||
// Constant Time
|
||||
System.out.println("**** Constant time ****");
|
||||
|
||||
System.out.println("Hey - your input is: " + n);
|
||||
System.out.println("Running time not dependent on input size!");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLogarithmicComplexity_thenLogarithmicRuntime() {
|
||||
// Logarithmic Time
|
||||
System.out.println("**** Logarithmic Time ****");
|
||||
for (int i = 1; i < n; i = i * 2) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenLinearComplexity_thenLinearRuntime() {
|
||||
// Linear Time
|
||||
System.out.println("**** Linear Time ****");
|
||||
for (int i = 0; i < n; i++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNLogNComplexity_thenNLogNRuntime() {
|
||||
// N Log N Time
|
||||
System.out.println("**** nlogn Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j < n; j = j * 2) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenQuadraticComplexity_thenQuadraticRuntime() {
|
||||
// Quadratic Time
|
||||
System.out.println("**** Quadratic Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenCubicComplexity_thenCubicRuntime() {
|
||||
// Cubic Time
|
||||
System.out.println("**** Cubic Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= n; i++) {
|
||||
for (int j = 1; j <= n; j++) {
|
||||
for (int k = 1; k <= n; k++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i + " and " + j + " and " + k);
|
||||
total++;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenExponentialComplexity_thenExponentialRuntime() {
|
||||
// Exponential Time
|
||||
System.out.println("**** Exponential Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <= Math.pow(2, n); i++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenFactorialComplexity_thenFactorialRuntime() {
|
||||
// Factorial Time
|
||||
System.out.println("**** Factorial Time ****");
|
||||
total = 0;
|
||||
for (
|
||||
|
||||
int i = 1; i <=
|
||||
|
||||
factorial(n); i++) {
|
||||
// System.out.println("Hey - I'm busy looking at: " + i);
|
||||
total++;
|
||||
}
|
||||
System.out.println("Total amount of times run: " + total);
|
||||
}
|
||||
|
||||
static int factorial(int n) {
|
||||
if (n == 0 || n == 1)
|
||||
return 1;
|
||||
else
|
||||
return n * factorial(n - 1);
|
||||
}
|
||||
}
|
@ -1,26 +0,0 @@
|
||||
package com.baeldung.algorithms.bubblesort;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BubbleSortTest {
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithBubbleSort_thenGetSortedArray() {
|
||||
Integer[] array = { 2, 1, 4, 6, 3, 5 };
|
||||
Integer[] sortedArray = { 1, 2, 3, 4, 5, 6 };
|
||||
BubbleSort bubbleSort = new BubbleSort();
|
||||
bubbleSort.bubbleSort(array);
|
||||
assertArrayEquals(array, sortedArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithOptimizedBubbleSort_thenGetSortedArray() {
|
||||
Integer[] array = { 2, 1, 4, 6, 3, 5 };
|
||||
Integer[] sortedArray = { 1, 2, 3, 4, 5, 6 };
|
||||
BubbleSort bubbleSort = new BubbleSort();
|
||||
bubbleSort.optimizedBubbleSort(array);
|
||||
assertArrayEquals(array, sortedArray);
|
||||
}
|
||||
}
|
@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.bubblesort;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class BubbleSortUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithBubbleSort_thenGetSortedArray() {
|
||||
Integer[] array = { 2, 1, 4, 6, 3, 5 };
|
||||
Integer[] sortedArray = { 1, 2, 3, 4, 5, 6 };
|
||||
BubbleSort bubbleSort = new BubbleSort();
|
||||
bubbleSort.bubbleSort(array);
|
||||
assertArrayEquals(array, sortedArray);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenIntegerArray_whenSortedWithOptimizedBubbleSort_thenGetSortedArray() {
|
||||
Integer[] array = { 2, 1, 4, 6, 3, 5 };
|
||||
Integer[] sortedArray = { 1, 2, 3, 4, 5, 6 };
|
||||
BubbleSort bubbleSort = new BubbleSort();
|
||||
bubbleSort.optimizedBubbleSort(array);
|
||||
assertArrayEquals(array, sortedArray);
|
||||
}
|
||||
}
|
@ -1,32 +0,0 @@
|
||||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class EditDistanceTest extends EditDistanceDataProvider {
|
||||
|
||||
private String x;
|
||||
private String y;
|
||||
private int result;
|
||||
|
||||
public EditDistanceTest(String a, String b, int res) {
|
||||
super();
|
||||
x = a;
|
||||
y = b;
|
||||
result = res;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEditDistance_RecursiveImplementation() {
|
||||
assertEquals(result, EditDistanceRecursive.calculate(x, y));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEditDistance_givenDynamicProgrammingImplementation() {
|
||||
assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
|
||||
}
|
||||
}
|
@ -0,0 +1,32 @@
|
||||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class EditDistanceUnitTest extends EditDistanceDataProvider {
|
||||
|
||||
private String x;
|
||||
private String y;
|
||||
private int result;
|
||||
|
||||
public EditDistanceUnitTest(String a, String b, int res) {
|
||||
super();
|
||||
x = a;
|
||||
y = b;
|
||||
result = res;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEditDistance_RecursiveImplementation() {
|
||||
assertEquals(result, EditDistanceRecursive.calculate(x, y));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEditDistance_givenDynamicProgrammingImplementation() {
|
||||
assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleDetectionBruteForceTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleDetectionBruteForceTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_detectLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleDetectionBruteForce.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleDetectionBruteForceUnitTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleDetectionBruteForceUnitTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_detectLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleDetectionBruteForce.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleDetectionByFastAndSlowIteratorsTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleDetectionByFastAndSlowIteratorsTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_detectLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleDetectionByFastAndSlowIteratorsUnitTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleDetectionByFastAndSlowIteratorsUnitTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_detectLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -1,23 +0,0 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleDetectionByHashingTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleDetectionByHashingTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_detectLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleDetectionByHashing.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -0,0 +1,23 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleDetectionByHashingUnitTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleDetectionByHashingUnitTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_detectLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleDetectionByHashing.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleRemovalBruteForceTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleRemovalBruteForceTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleRemovalBruteForce.detectAndRemoveCycle(head));
|
||||
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleRemovalBruteForceUnitTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleRemovalBruteForceUnitTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleRemovalBruteForce.detectAndRemoveCycle(head));
|
||||
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleRemovalByCountingLoopNodesTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleRemovalByCountingLoopNodesTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleRemovalByCountingLoopNodes.detectAndRemoveCycle(head));
|
||||
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleRemovalByCountingLoopNodesUnitTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleRemovalByCountingLoopNodesUnitTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleRemovalByCountingLoopNodes.detectAndRemoveCycle(head));
|
||||
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -1,24 +0,0 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleRemovalWithoutCountingLoopNodesTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleRemovalWithoutCountingLoopNodesTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleRemovalWithoutCountingLoopNodes.detectAndRemoveCycle(head));
|
||||
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package com.baeldung.algorithms.linkedlist;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(value = Parameterized.class)
|
||||
public class CycleRemovalWithoutCountingLoopNodesUnitTest extends CycleDetectionTestBase {
|
||||
boolean cycleExists;
|
||||
Node<Integer> head;
|
||||
|
||||
public CycleRemovalWithoutCountingLoopNodesUnitTest(Node<Integer> head, boolean cycleExists) {
|
||||
super();
|
||||
this.cycleExists = cycleExists;
|
||||
this.head = head;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenList_ifLoopExists_thenDetectAndRemoveLoop() {
|
||||
Assert.assertEquals(cycleExists, CycleRemovalWithoutCountingLoopNodes.detectAndRemoveCycle(head));
|
||||
Assert.assertFalse(CycleDetectionByFastAndSlowIterators.detectCycle(head).cycleExists);
|
||||
}
|
||||
}
|
@ -0,0 +1,84 @@
|
||||
package com.baeldung.algorithms.moneywords;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.numberwordconverter.NumberWordConverter;
|
||||
|
||||
public class NumberWordConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenMoneyNegative_thenReturnInvalidInput() {
|
||||
assertEquals(NumberWordConverter.INVALID_INPUT_GIVEN, NumberWordConverter.getMoneyIntoWords(-13));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenZeroDollarsGiven_thenReturnEmptyString() {
|
||||
assertEquals("", NumberWordConverter.getMoneyIntoWords(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOnlyDollarsGiven_thenReturnWords() {
|
||||
assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOnlyCentsGiven_thenReturnWords() {
|
||||
assertEquals("sixty cents", NumberWordConverter.getMoneyIntoWords(0.6));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAlmostAMillioDollarsGiven_thenReturnWords() {
|
||||
String expectedResult = "nine hundred ninety nine thousand nine hundred ninety nine dollars";
|
||||
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(999_999));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenThirtyMillionDollarsGiven_thenReturnWords() {
|
||||
String expectedResult = "thirty three million three hundred forty eight thousand nine hundred seventy eight dollars";
|
||||
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(33_348_978));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTwoBillionDollarsGiven_thenReturnWords() {
|
||||
String expectedResult = "two billion one hundred thirty three million two hundred forty seven thousand eight hundred ten dollars";
|
||||
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(2_133_247_810));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGivenDollarsAndCents_thenReturnWords() {
|
||||
String expectedResult = "nine hundred twenty four dollars and sixty cents";
|
||||
assertEquals(expectedResult, NumberWordConverter.getMoneyIntoWords(924.6));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenOneDollarAndNoCents_thenReturnDollarSingular() {
|
||||
assertEquals("one dollar", NumberWordConverter.getMoneyIntoWords(1));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoDollarsAndOneCent_thenReturnCentSingular() {
|
||||
assertEquals("one cent", NumberWordConverter.getMoneyIntoWords(0.01));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoDollarsAndTwoCents_thenReturnCentsPlural() {
|
||||
assertEquals("two cents", NumberWordConverter.getMoneyIntoWords(0.02));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoDollarsAndNinetyNineCents_thenReturnWords() {
|
||||
assertEquals("ninety nine cents", NumberWordConverter.getMoneyIntoWords(0.99));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenNoDollarsAndNineFiveNineCents_thenCorrectRounding() {
|
||||
assertEquals("ninety six cents", NumberWordConverter.getMoneyIntoWords(0.959));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGivenDollarsAndCents_thenReturnWordsVersionTwo() {
|
||||
assertEquals("three hundred ten £ 00/100", NumberWordConverter.getMoneyIntoWords("310"));
|
||||
}
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
/**
|
||||
* Specific fitness function implementation to solve the League of Legends
|
||||
* problem. This is the problem statement: <br>
|
||||
* <br>
|
||||
* In League of Legends, a player's Effective Health when defending against
|
||||
* physical damage is given by E=H(100+A)/100, where H is health and A is armor.
|
||||
* Health costs 2.5 gold per unit, and Armor costs 18 gold per unit. You have
|
||||
* 3600 gold, and you need to optimize the effectiveness E of your health and
|
||||
* armor to survive as long as possible against the enemy team's attacks. How
|
||||
* much of each should you buy? <br>
|
||||
* <br>
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class LolFitnessFunction implements FitnessFunction {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* com.baeldung.algorithms.multiswarm.FitnessFunction#getFitness(long[])
|
||||
*/
|
||||
@Override
|
||||
public double getFitness(long[] particlePosition) {
|
||||
|
||||
long health = particlePosition[0];
|
||||
long armor = particlePosition[1];
|
||||
|
||||
// No negatives values accepted.
|
||||
if (health < 0 && armor < 0) {
|
||||
return -(health * armor);
|
||||
} else if (health < 0) {
|
||||
return health;
|
||||
} else if (armor < 0) {
|
||||
return armor;
|
||||
}
|
||||
|
||||
// Checks if the solution is actually feasible provided our gold.
|
||||
double cost = (health * 2.5) + (armor * 18);
|
||||
if (cost > 3600) {
|
||||
return 3600 - cost;
|
||||
} else {
|
||||
// Check how good is the solution.
|
||||
long fitness = (health * (100 + armor)) / 100;
|
||||
return fitness;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.baeldung.algorithms.multiswarm;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.algorithms.support.MayFailRule;
|
||||
|
||||
/**
|
||||
* Test for {@link Multiswarm}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class MultiswarmUnitTest {
|
||||
|
||||
/**
|
||||
* Rule for handling expected failures. We use this since this test may
|
||||
* actually fail due to bad luck in the random generation.
|
||||
*/
|
||||
@Rule
|
||||
public MayFailRule mayFailRule = new MayFailRule();
|
||||
|
||||
/**
|
||||
* Tests the multiswarm algorithm with a generic problem. The problem is the
|
||||
* following: <br>
|
||||
* <br>
|
||||
* In League of Legends, a player's Effective Health when defending against
|
||||
* physical damage is given by E=H(100+A)/100, where H is health and A is
|
||||
* armor. Health costs 2.5 gold per unit, and Armor costs 18 gold per unit.
|
||||
* You have 3600 gold, and you need to optimize the effectiveness E of your
|
||||
* health and armor to survive as long as possible against the enemy team's
|
||||
* attacks. How much of each should you buy? <br>
|
||||
* <br>
|
||||
* The solution is H = 1080, A = 50 for a total fitness of 1620. Tested with
|
||||
* 50 swarms each with 1000 particles.
|
||||
*/
|
||||
@Test
|
||||
public void givenMultiswarm_whenThousandIteration_thenSolutionFound() {
|
||||
Multiswarm multiswarm = new Multiswarm(50, 1000, new LolFitnessFunction());
|
||||
|
||||
// Iterates 1000 times through the main loop and prints the result.
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
multiswarm.mainLoop();
|
||||
}
|
||||
|
||||
System.out.println("Best fitness found: " + multiswarm.getBestFitness() + "[" + multiswarm.getBestPosition()[0]
|
||||
+ "," + multiswarm.getBestPosition()[1] + "]");
|
||||
Assert.assertEquals(1080, multiswarm.getBestPosition()[0]);
|
||||
Assert.assertEquals(50, multiswarm.getBestPosition()[1]);
|
||||
Assert.assertEquals(1620, (int) multiswarm.getBestFitness());
|
||||
}
|
||||
|
||||
}
|
@ -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 PrimeGeneratorTest {
|
||||
@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,29 @@
|
||||
package com.baeldung.algorithms.romannumerals;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class RomanArabicConverterUnitTest {
|
||||
|
||||
@Test
|
||||
public void given2018Roman_WhenConvertingToArabic_ThenReturn2018() {
|
||||
|
||||
String roman2018 = "MMXVIII";
|
||||
|
||||
int result = RomanArabicConverter.romanToArabic(roman2018);
|
||||
|
||||
assertThat(result).isEqualTo(2018);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void given1999Arabic_WhenConvertingToRoman_ThenReturnMCMXCIX() {
|
||||
|
||||
int arabic1999 = 1999;
|
||||
|
||||
String result = RomanArabicConverter.arabicToRoman(arabic1999);
|
||||
|
||||
assertThat(result).isEqualTo("MCMXCIX");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.algorithms.support;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.Description;
|
||||
import org.junit.runners.model.Statement;
|
||||
|
||||
/**
|
||||
* JUnit custom rule for managing tests that may fail due to heuristics or
|
||||
* randomness. In order to use this, just instantiate this object as a public
|
||||
* field inside the test class and annotate it with {@link Rule}.
|
||||
*
|
||||
* @author Donato Rimenti
|
||||
*
|
||||
*/
|
||||
public class MayFailRule implements TestRule {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.junit.rules.TestRule#apply(org.junit.runners.model.Statement,
|
||||
* org.junit.runner.Description)
|
||||
*/
|
||||
@Override
|
||||
public Statement apply(Statement base, Description description) {
|
||||
return new Statement() {
|
||||
@Override
|
||||
public void evaluate() throws Throwable {
|
||||
try {
|
||||
base.evaluate();
|
||||
} catch (Throwable e) {
|
||||
// Ignore the exception since we expect this.
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.jgrapht.VertexFactory;
|
||||
import org.jgrapht.alg.HamiltonianCycle;
|
||||
import org.jgrapht.generate.CompleteGraphGenerator;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CompleteGraphTest {
|
||||
|
||||
static SimpleWeightedGraph<String, DefaultEdge> completeGraph;
|
||||
static int size = 10;
|
||||
|
||||
@Before
|
||||
public void createCompleteGraph() {
|
||||
completeGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
|
||||
CompleteGraphGenerator<String, DefaultEdge> completeGenerator = new CompleteGraphGenerator<String, DefaultEdge>(size);
|
||||
VertexFactory<String> vFactory = new VertexFactory<String>() {
|
||||
private int id = 0;
|
||||
public String createVertex() {
|
||||
return "v" + id++;
|
||||
}
|
||||
};
|
||||
completeGenerator.generateGraph(completeGraph, vFactory, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompleteGraph_whenGetHamiltonianCyclePath_thenGetVerticeListInSequence() {
|
||||
List<String> verticeList = HamiltonianCycle.getApproximateOptimalForCompleteGraph(completeGraph);
|
||||
assertEquals(verticeList.size(), completeGraph.vertexSet().size());
|
||||
}
|
||||
}
|
@ -0,0 +1,38 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.jgrapht.VertexFactory;
|
||||
import org.jgrapht.alg.HamiltonianCycle;
|
||||
import org.jgrapht.generate.CompleteGraphGenerator;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CompleteGraphUnitTest {
|
||||
|
||||
static SimpleWeightedGraph<String, DefaultEdge> completeGraph;
|
||||
static int size = 10;
|
||||
|
||||
@Before
|
||||
public void createCompleteGraph() {
|
||||
completeGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
|
||||
CompleteGraphGenerator<String, DefaultEdge> completeGenerator = new CompleteGraphGenerator<String, DefaultEdge>(size);
|
||||
VertexFactory<String> vFactory = new VertexFactory<String>() {
|
||||
private int id = 0;
|
||||
public String createVertex() {
|
||||
return "v" + id++;
|
||||
}
|
||||
};
|
||||
completeGenerator.generateGraph(completeGraph, vFactory, null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenCompleteGraph_whenGetHamiltonianCyclePath_thenGetVerticeListInSequence() {
|
||||
List<String> verticeList = HamiltonianCycle.getApproximateOptimalForCompleteGraph(completeGraph);
|
||||
assertEquals(verticeList.size(), completeGraph.vertexSet().size());
|
||||
}
|
||||
}
|
@ -1,95 +0,0 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.jgrapht.DirectedGraph;
|
||||
import org.jgrapht.GraphPath;
|
||||
import org.jgrapht.alg.CycleDetector;
|
||||
import org.jgrapht.alg.KosarajuStrongConnectivityInspector;
|
||||
import org.jgrapht.alg.interfaces.StrongConnectivityAlgorithm;
|
||||
import org.jgrapht.alg.shortestpath.AllDirectedPaths;
|
||||
import org.jgrapht.alg.shortestpath.BellmanFordShortestPath;
|
||||
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
|
||||
import org.jgrapht.graph.DefaultDirectedGraph;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.graph.DirectedSubgraph;
|
||||
import org.jgrapht.traverse.BreadthFirstIterator;
|
||||
import org.jgrapht.traverse.DepthFirstIterator;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DirectedGraphTests {
|
||||
DirectedGraph<String, DefaultEdge> directedGraph;
|
||||
|
||||
@Before
|
||||
public void createDirectedGraph() {
|
||||
directedGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
|
||||
IntStream.range(1, 10).forEach(i -> {
|
||||
directedGraph.addVertex("v" + i);
|
||||
});
|
||||
directedGraph.addEdge("v1", "v2");
|
||||
directedGraph.addEdge("v2", "v4");
|
||||
directedGraph.addEdge("v4", "v3");
|
||||
directedGraph.addEdge("v3", "v1");
|
||||
directedGraph.addEdge("v5", "v4");
|
||||
directedGraph.addEdge("v5", "v6");
|
||||
directedGraph.addEdge("v6", "v7");
|
||||
directedGraph.addEdge("v7", "v5");
|
||||
directedGraph.addEdge("v8", "v5");
|
||||
directedGraph.addEdge("v9", "v8");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetStronglyConnectedSubgraphs_thenPathExistsBetweenStronglyconnectedVertices() {
|
||||
StrongConnectivityAlgorithm<String, DefaultEdge> scAlg = new KosarajuStrongConnectivityInspector<>(directedGraph);
|
||||
List<DirectedSubgraph<String, DefaultEdge>> stronglyConnectedSubgraphs = scAlg.stronglyConnectedSubgraphs();
|
||||
List<String> stronglyConnectedVertices = new ArrayList<>(stronglyConnectedSubgraphs.get(3).vertexSet());
|
||||
|
||||
String randomVertex1 = stronglyConnectedVertices.get(0);
|
||||
String randomVertex2 = stronglyConnectedVertices.get(3);
|
||||
AllDirectedPaths<String, DefaultEdge> allDirectedPaths = new AllDirectedPaths<>(directedGraph);
|
||||
|
||||
List<GraphPath<String, DefaultEdge>> possiblePathList = allDirectedPaths.getAllPaths(randomVertex1, randomVertex2, false, stronglyConnectedVertices.size());
|
||||
assertTrue(possiblePathList.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraphWithCycle_whenCheckCycles_thenDetectCycles() {
|
||||
CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(directedGraph);
|
||||
assertTrue(cycleDetector.detectCycles());
|
||||
Set<String> cycleVertices = cycleDetector.findCycles();
|
||||
assertTrue(cycleVertices.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenCreateInstanceDepthFirstIterator_thenGetIterator() {
|
||||
DepthFirstIterator depthFirstIterator = new DepthFirstIterator<>(directedGraph);
|
||||
assertNotNull(depthFirstIterator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenCreateInstanceBreadthFirstIterator_thenGetIterator() {
|
||||
BreadthFirstIterator breadthFirstIterator = new BreadthFirstIterator<>(directedGraph);
|
||||
assertNotNull(breadthFirstIterator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetDijkstraShortestPath_thenGetNotNullPath() {
|
||||
DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(directedGraph);
|
||||
List<String> shortestPath = dijkstraShortestPath.getPath("v1", "v4").getVertexList();
|
||||
assertNotNull(shortestPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetBellmanFordShortestPath_thenGetNotNullPath() {
|
||||
BellmanFordShortestPath bellmanFordShortestPath = new BellmanFordShortestPath(directedGraph);
|
||||
List<String> shortestPath = bellmanFordShortestPath.getPath("v1", "v4").getVertexList();
|
||||
assertNotNull(shortestPath);
|
||||
}
|
||||
}
|
@ -0,0 +1,95 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.jgrapht.DirectedGraph;
|
||||
import org.jgrapht.GraphPath;
|
||||
import org.jgrapht.alg.CycleDetector;
|
||||
import org.jgrapht.alg.KosarajuStrongConnectivityInspector;
|
||||
import org.jgrapht.alg.interfaces.StrongConnectivityAlgorithm;
|
||||
import org.jgrapht.alg.shortestpath.AllDirectedPaths;
|
||||
import org.jgrapht.alg.shortestpath.BellmanFordShortestPath;
|
||||
import org.jgrapht.alg.shortestpath.DijkstraShortestPath;
|
||||
import org.jgrapht.graph.DefaultDirectedGraph;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.graph.DirectedSubgraph;
|
||||
import org.jgrapht.traverse.BreadthFirstIterator;
|
||||
import org.jgrapht.traverse.DepthFirstIterator;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class DirectedGraphUnitTest {
|
||||
DirectedGraph<String, DefaultEdge> directedGraph;
|
||||
|
||||
@Before
|
||||
public void createDirectedGraph() {
|
||||
directedGraph = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
|
||||
IntStream.range(1, 10).forEach(i -> {
|
||||
directedGraph.addVertex("v" + i);
|
||||
});
|
||||
directedGraph.addEdge("v1", "v2");
|
||||
directedGraph.addEdge("v2", "v4");
|
||||
directedGraph.addEdge("v4", "v3");
|
||||
directedGraph.addEdge("v3", "v1");
|
||||
directedGraph.addEdge("v5", "v4");
|
||||
directedGraph.addEdge("v5", "v6");
|
||||
directedGraph.addEdge("v6", "v7");
|
||||
directedGraph.addEdge("v7", "v5");
|
||||
directedGraph.addEdge("v8", "v5");
|
||||
directedGraph.addEdge("v9", "v8");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetStronglyConnectedSubgraphs_thenPathExistsBetweenStronglyconnectedVertices() {
|
||||
StrongConnectivityAlgorithm<String, DefaultEdge> scAlg = new KosarajuStrongConnectivityInspector<>(directedGraph);
|
||||
List<DirectedSubgraph<String, DefaultEdge>> stronglyConnectedSubgraphs = scAlg.stronglyConnectedSubgraphs();
|
||||
List<String> stronglyConnectedVertices = new ArrayList<>(stronglyConnectedSubgraphs.get(3).vertexSet());
|
||||
|
||||
String randomVertex1 = stronglyConnectedVertices.get(0);
|
||||
String randomVertex2 = stronglyConnectedVertices.get(3);
|
||||
AllDirectedPaths<String, DefaultEdge> allDirectedPaths = new AllDirectedPaths<>(directedGraph);
|
||||
|
||||
List<GraphPath<String, DefaultEdge>> possiblePathList = allDirectedPaths.getAllPaths(randomVertex1, randomVertex2, false, stronglyConnectedVertices.size());
|
||||
assertTrue(possiblePathList.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraphWithCycle_whenCheckCycles_thenDetectCycles() {
|
||||
CycleDetector<String, DefaultEdge> cycleDetector = new CycleDetector<String, DefaultEdge>(directedGraph);
|
||||
assertTrue(cycleDetector.detectCycles());
|
||||
Set<String> cycleVertices = cycleDetector.findCycles();
|
||||
assertTrue(cycleVertices.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenCreateInstanceDepthFirstIterator_thenGetIterator() {
|
||||
DepthFirstIterator depthFirstIterator = new DepthFirstIterator<>(directedGraph);
|
||||
assertNotNull(depthFirstIterator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenCreateInstanceBreadthFirstIterator_thenGetIterator() {
|
||||
BreadthFirstIterator breadthFirstIterator = new BreadthFirstIterator<>(directedGraph);
|
||||
assertNotNull(breadthFirstIterator);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetDijkstraShortestPath_thenGetNotNullPath() {
|
||||
DijkstraShortestPath dijkstraShortestPath = new DijkstraShortestPath(directedGraph);
|
||||
List<String> shortestPath = dijkstraShortestPath.getPath("v1", "v4").getVertexList();
|
||||
assertNotNull(shortestPath);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenDirectedGraph_whenGetBellmanFordShortestPath_thenGetNotNullPath() {
|
||||
BellmanFordShortestPath bellmanFordShortestPath = new BellmanFordShortestPath(directedGraph);
|
||||
List<String> shortestPath = bellmanFordShortestPath.getPath("v1", "v4").getVertexList();
|
||||
assertNotNull(shortestPath);
|
||||
}
|
||||
}
|
@ -1,42 +0,0 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.jgrapht.GraphPath;
|
||||
import org.jgrapht.alg.cycle.HierholzerEulerianCycle;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class EulerianCircuitTest {
|
||||
SimpleWeightedGraph<String, DefaultEdge> simpleGraph;
|
||||
|
||||
@Before
|
||||
public void createGraphWithEulerianCircuit() {
|
||||
simpleGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
|
||||
IntStream.range(1, 6).forEach(i -> {
|
||||
simpleGraph.addVertex("v" + i);
|
||||
});
|
||||
IntStream.range(1, 6).forEach(i -> {
|
||||
int endVertexNo = (i + 1) > 5 ? 1 : i + 1;
|
||||
simpleGraph.addEdge("v" + i, "v" + endVertexNo);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGraph_whenCheckEluerianCycle_thenGetResult() {
|
||||
HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
|
||||
assertTrue(eulerianCycle.isEulerian(simpleGraph));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGraphWithEulerianCircuit_whenGetEulerianCycle_thenGetGraphPath() {
|
||||
HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
|
||||
GraphPath path = eulerianCycle.getEulerianCycle(simpleGraph);
|
||||
assertTrue(path.getEdgeList().containsAll(simpleGraph.edgeSet()));
|
||||
}
|
||||
}
|
@ -0,0 +1,42 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.jgrapht.GraphPath;
|
||||
import org.jgrapht.alg.cycle.HierholzerEulerianCycle;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.jgrapht.graph.SimpleWeightedGraph;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class EulerianCircuitUnitTest {
|
||||
SimpleWeightedGraph<String, DefaultEdge> simpleGraph;
|
||||
|
||||
@Before
|
||||
public void createGraphWithEulerianCircuit() {
|
||||
simpleGraph = new SimpleWeightedGraph<>(DefaultEdge.class);
|
||||
IntStream.range(1, 6).forEach(i -> {
|
||||
simpleGraph.addVertex("v" + i);
|
||||
});
|
||||
IntStream.range(1, 6).forEach(i -> {
|
||||
int endVertexNo = (i + 1) > 5 ? 1 : i + 1;
|
||||
simpleGraph.addEdge("v" + i, "v" + endVertexNo);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGraph_whenCheckEluerianCycle_thenGetResult() {
|
||||
HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
|
||||
assertTrue(eulerianCycle.isEulerian(simpleGraph));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenGraphWithEulerianCircuit_whenGetEulerianCycle_thenGetGraphPath() {
|
||||
HierholzerEulerianCycle eulerianCycle = new HierholzerEulerianCycle<>();
|
||||
GraphPath path = eulerianCycle.getEulerianCycle(simpleGraph);
|
||||
assertTrue(path.getEdgeList().containsAll(simpleGraph.edgeSet()));
|
||||
}
|
||||
}
|
@ -1,57 +1,47 @@
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>animal-sniffer-mvn-plugin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>example-animal-sniffer-mvn-plugin</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<properties>
|
||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.7.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>animal-sniffer-maven-plugin</artifactId>
|
||||
<version>1.16</version>
|
||||
<configuration>
|
||||
<signature>
|
||||
<groupId>org.codehaus.mojo.signature</groupId>
|
||||
<artifactId>java16</artifactId>
|
||||
<version>1.0</version>
|
||||
</signature>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>animal-sniffer</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
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>
|
||||
<packaging>jar</packaging>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<name>animal-sniffer-mvn-plugin</name>
|
||||
<url>http://maven.apache.org</url>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>animal-sniffer-maven-plugin</artifactId>
|
||||
<version>${animal-sniffer-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<signature>
|
||||
<groupId>org.codehaus.mojo.signature</groupId>
|
||||
<artifactId>java16</artifactId>
|
||||
<version>${org.codehaus.mojo.signature.java16.version}</version>
|
||||
</signature>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>animal-sniffer</id>
|
||||
<phase>verify</phase>
|
||||
<goals>
|
||||
<goal>check</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<animal-sniffer-maven-plugin.version>1.16</animal-sniffer-maven-plugin.version>
|
||||
<org.codehaus.mojo.signature.java16.version>1.0</org.codehaus.mojo.signature.java16.version>
|
||||
</properties>
|
||||
</project>
|
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>
|
@ -1,8 +1,8 @@
|
||||
<?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"
|
||||
<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>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
@ -11,40 +11,17 @@
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>annotation-processing</artifactId>
|
||||
|
||||
<properties>
|
||||
<auto-service.version>1.0-rc2</auto-service.version>
|
||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.google.auto.service</groupId>
|
||||
<artifactId>auto-service</artifactId>
|
||||
<version>${auto-service.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
<plugins>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
<properties>
|
||||
<auto-service.version>1.0-rc2</auto-service.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>
|
@ -2,6 +2,7 @@
|
||||
<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>annotation-user</artifactId>
|
||||
|
||||
<parent>
|
||||
<artifactId>annotations</artifactId>
|
||||
@ -10,16 +11,12 @@
|
||||
<relativePath>../</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>annotation-user</artifactId>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>annotation-processing</artifactId>
|
||||
<version>${project.parent.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
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,15 +1,15 @@
|
||||
<?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>annotations</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>annotations</artifactId>
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>annotation-processing</module>
|
||||
|
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)
|
58
antlr/pom.xml
Normal file
58
antlr/pom.xml
Normal file
@ -0,0 +1,58 @@
|
||||
<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">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>antlr</artifactId>
|
||||
<name>antlr</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-maven-plugin</artifactId>
|
||||
<version>${antlr.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>antlr4</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<version>${mojo.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>add-source</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sources>
|
||||
<source>${basedir}/target/generated-sources/antlr4</source>
|
||||
</sources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.antlr</groupId>
|
||||
<artifactId>antlr4-runtime</artifactId>
|
||||
<version>${antlr.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<properties>
|
||||
<antlr.version>4.7.1</antlr.version>
|
||||
<mojo.version>3.0.0</mojo.version>
|
||||
</properties>
|
||||
</project>
|
1775
antlr/src/main/antlr4/com/baeldung/antlr/Java8.g4
Normal file
1775
antlr/src/main/antlr4/com/baeldung/antlr/Java8.g4
Normal file
File diff suppressed because it is too large
Load Diff
16
antlr/src/main/antlr4/com/baeldung/antlr/Log.g4
Normal file
16
antlr/src/main/antlr4/com/baeldung/antlr/Log.g4
Normal file
@ -0,0 +1,16 @@
|
||||
grammar Log;
|
||||
|
||||
log : entry+;
|
||||
entry : timestamp ' ' level ' ' message CRLF;
|
||||
timestamp : DATE ' ' TIME;
|
||||
level : 'ERROR' | 'INFO' | 'DEBUG';
|
||||
message : (TEXT | ' ')+;
|
||||
|
||||
fragment DIGIT : [0-9];
|
||||
fragment TWODIGIT : DIGIT DIGIT;
|
||||
fragment LETTER : [A-Za-z];
|
||||
|
||||
DATE : TWODIGIT TWODIGIT '-' LETTER LETTER LETTER '-' TWODIGIT;
|
||||
TIME : TWODIGIT ':' TWODIGIT ':' TWODIGIT;
|
||||
TEXT : LETTER+;
|
||||
CRLF : '\r'? '\n' | '\r';
|
@ -0,0 +1,28 @@
|
||||
package com.baeldung.antlr.java;
|
||||
|
||||
import com.baeldung.antlr.Java8BaseListener;
|
||||
import com.baeldung.antlr.Java8Parser;
|
||||
import org.antlr.v4.runtime.tree.TerminalNode;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class UppercaseMethodListener extends Java8BaseListener {
|
||||
|
||||
private List<String> errors = new ArrayList<String>();
|
||||
|
||||
@Override
|
||||
public void enterMethodDeclarator(Java8Parser.MethodDeclaratorContext ctx) {
|
||||
TerminalNode node = ctx.Identifier();
|
||||
String methodName = node.getText();
|
||||
|
||||
if (Character.isUpperCase(methodName.charAt(0))){
|
||||
errors.add(String.format("Method %s is uppercased!", methodName));
|
||||
}
|
||||
}
|
||||
|
||||
public List<String> getErrors(){
|
||||
return Collections.unmodifiableList(errors);
|
||||
}
|
||||
}
|
51
antlr/src/main/java/com/baeldung/antlr/log/LogListener.java
Normal file
51
antlr/src/main/java/com/baeldung/antlr/log/LogListener.java
Normal file
@ -0,0 +1,51 @@
|
||||
package com.baeldung.antlr.log;
|
||||
|
||||
import com.baeldung.antlr.LogBaseListener;
|
||||
import com.baeldung.antlr.LogParser;
|
||||
import com.baeldung.antlr.log.model.LogLevel;
|
||||
import com.baeldung.antlr.log.model.LogEntry;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
|
||||
public class LogListener extends LogBaseListener {
|
||||
|
||||
private static final DateTimeFormatter DEFAULT_DATETIME_FORMATTER
|
||||
= DateTimeFormatter.ofPattern("yyyy-MMM-dd HH:mm:ss", Locale.ENGLISH);
|
||||
|
||||
private List<LogEntry> entries = new ArrayList<>();
|
||||
private LogEntry currentLogEntry;
|
||||
|
||||
@Override
|
||||
public void enterEntry(LogParser.EntryContext ctx) {
|
||||
this.currentLogEntry = new LogEntry();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exitEntry(LogParser.EntryContext ctx) {
|
||||
entries.add(currentLogEntry);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterTimestamp(LogParser.TimestampContext ctx) {
|
||||
currentLogEntry.setTimestamp(LocalDateTime.parse(ctx.getText(), DEFAULT_DATETIME_FORMATTER));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterMessage(LogParser.MessageContext ctx) {
|
||||
currentLogEntry.setMessage(ctx.getText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterLevel(LogParser.LevelContext ctx) {
|
||||
currentLogEntry.setLevel(LogLevel.valueOf(ctx.getText()));
|
||||
}
|
||||
|
||||
public List<LogEntry> getEntries() {
|
||||
return Collections.unmodifiableList(entries);
|
||||
}
|
||||
}
|
@ -0,0 +1,35 @@
|
||||
package com.baeldung.antlr.log.model;
|
||||
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
public class LogEntry {
|
||||
|
||||
private LogLevel level;
|
||||
private String message;
|
||||
private LocalDateTime timestamp;
|
||||
|
||||
public LogLevel getLevel() {
|
||||
return level;
|
||||
}
|
||||
|
||||
public void setLevel(LogLevel level) {
|
||||
this.level = level;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public LocalDateTime getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
public void setTimestamp(LocalDateTime timestamp) {
|
||||
this.timestamp = timestamp;
|
||||
}
|
||||
}
|
@ -0,0 +1,5 @@
|
||||
package com.baeldung.antlr.log.model;
|
||||
|
||||
public enum LogLevel {
|
||||
DEBUG, INFO, ERROR
|
||||
}
|
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>
|
@ -0,0 +1,30 @@
|
||||
package com.baeldung.antlr;
|
||||
|
||||
import com.baeldung.antlr.java.UppercaseMethodListener;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeWalker;
|
||||
import org.junit.Test;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
public class JavaParserUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenOneMethodStartsWithUpperCase_thenOneErrorReturned() throws Exception{
|
||||
|
||||
String javaClassContent = "public class SampleClass { void DoSomething(){} }";
|
||||
Java8Lexer java8Lexer = new Java8Lexer(CharStreams.fromString(javaClassContent));
|
||||
CommonTokenStream tokens = new CommonTokenStream(java8Lexer);
|
||||
Java8Parser java8Parser = new Java8Parser(tokens);
|
||||
ParseTree tree = java8Parser.compilationUnit();
|
||||
ParseTreeWalker walker = new ParseTreeWalker();
|
||||
UppercaseMethodListener uppercaseMethodListener = new UppercaseMethodListener();
|
||||
walker.walk(uppercaseMethodListener, tree);
|
||||
|
||||
assertThat(uppercaseMethodListener.getErrors().size(), is(1));
|
||||
assertThat(uppercaseMethodListener.getErrors().get(0),
|
||||
is("Method DoSomething is uppercased!"));
|
||||
}
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package com.baeldung.antlr;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
||||
import com.baeldung.antlr.log.LogListener;
|
||||
import com.baeldung.antlr.log.model.LogLevel;
|
||||
import com.baeldung.antlr.log.model.LogEntry;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeWalker;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
|
||||
|
||||
public class LogParserUnitTest {
|
||||
|
||||
@Test
|
||||
public void whenLogContainsOneErrorLogEntry_thenOneErrorIsReturned() throws Exception {
|
||||
String logLines = "2018-May-05 14:20:21 DEBUG entering awesome method\r\n" +
|
||||
"2018-May-05 14:20:24 ERROR Bad thing happened\r\n";
|
||||
LogLexer serverLogLexer = new LogLexer(CharStreams.fromString(logLines));
|
||||
CommonTokenStream tokens = new CommonTokenStream( serverLogLexer );
|
||||
LogParser logParser = new LogParser(tokens);
|
||||
ParseTreeWalker walker = new ParseTreeWalker();
|
||||
LogListener logWalker = new LogListener();
|
||||
walker.walk(logWalker, logParser.log());
|
||||
|
||||
assertThat(logWalker.getEntries().size(), is(2));
|
||||
LogEntry error = logWalker.getEntries().get(1);
|
||||
assertThat(error.getLevel(), is(LogLevel.ERROR));
|
||||
assertThat(error.getMessage(), is("Bad thing happened"));
|
||||
assertThat(error.getTimestamp(), is(LocalDateTime.of(2018,5,5,14,20,24)));
|
||||
}
|
||||
}
|
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