Merge branch 'master' of https://github.com/eugenp/tutorials into zuul_throttling
This commit is contained in:
+23
-2
@@ -34,8 +34,6 @@ spring-security-openid/src/main/resources/application.properties
|
|||||||
|
|
||||||
spring-all/*.log
|
spring-all/*.log
|
||||||
|
|
||||||
*.jar
|
|
||||||
|
|
||||||
SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties
|
SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties
|
||||||
|
|
||||||
spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
|
spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
|
||||||
@@ -43,3 +41,26 @@ spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties
|
|||||||
spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
|
spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties
|
||||||
*.springBeans
|
*.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
|
||||||
|
**/nbproject/
|
||||||
|
**/nb-configuration.xml
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
[submodule "testgitrepo"]
|
|
||||||
path = testgitrepo
|
|
||||||
url = /home/prd/Development/projects/idea/tutorials/spring-boot/src/main/resources/testgitrepo/
|
|
||||||
+1
-1
@@ -4,7 +4,7 @@ before_install:
|
|||||||
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
|
- echo "MAVEN_OPTS='-Xmx2048M -Xss128M -XX:+CMSClassUnloadingEnabled -XX:+UseG1GC -XX:-UseGCOverheadLimit'" > ~/.mavenrc
|
||||||
|
|
||||||
install: skip
|
install: skip
|
||||||
script: travis_wait 60 mvn -q test -fae
|
script: travis_wait 60 mvn -q install -Pdefault-first,default-second
|
||||||
|
|
||||||
sudo: required
|
sudo: required
|
||||||
|
|
||||||
|
|||||||
+38
-36
@@ -1,51 +1,53 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>JGitSnippets</artifactId>
|
<artifactId>JGit</artifactId>
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<url>http://maven.apache.org</url>
|
<url>http://maven.apache.org</url>
|
||||||
|
<name>JGit</name>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</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>
|
<properties>
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
<org.eclipse.jgit.version>4.5.0.201609210915-r</org.eclipse.jgit.version>
|
||||||
<maven.compiler.source>1.8</maven.compiler.source>
|
|
||||||
<maven.compiler.target>1.8</maven.compiler.target>
|
|
||||||
</properties>
|
</properties>
|
||||||
<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>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>
|
</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,30 +2,37 @@
|
|||||||
The "REST with Spring" Classes
|
The "REST with Spring" Classes
|
||||||
==============================
|
==============================
|
||||||
|
|
||||||
After 5 months of work, here's the Master Class of REST With Spring: <br/>
|
Here's the Master Class of REST With Spring (along with the newly announced Boot 2 material): <br/>
|
||||||
**[>> THE REST WITH SPRING MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
|
**[>> THE REST WITH SPRING - MASTER CLASS](http://www.baeldung.com/rest-with-spring-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=rws#master-class)**
|
||||||
|
|
||||||
And here's the Master Class of Learn Spring Security: <br/>
|
And here's the Master Class of Learn Spring Security: <br/>
|
||||||
**[>> LEARN SPRING SECURITY MASTER CLASS](http://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
|
**[>> LEARN SPRING SECURITY - MASTER CLASS](http://www.baeldung.com/learn-spring-security-course?utm_source=github&utm_medium=social&utm_content=tutorials&utm_campaign=lss#master-class)**
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Spring Tutorials
|
Java and Spring Tutorials
|
||||||
================
|
================
|
||||||
|
|
||||||
This project is **a collection of small and focused tutorials** each covering a single and well defined area of development.
|
This project is **a collection of small and focused tutorials** - each covering a single and well defined area of development in the Java ecosystem.
|
||||||
Most of the tutorial projects are focused on the `Spring Framework` (and `Spring Security`).
|
A strong focus of these is, of course, the Spring Framework - Spring, Spring Boot and Spring Securiyt.
|
||||||
In additional to Spring, the following technologies are in focus: `core Java`, `Jackson`, `HttpClient`, `Guava`.
|
In additional to Spring, the following technologies are in focus: `core Java`, `Jackson`, `HttpClient`, `Guava`.
|
||||||
|
|
||||||
|
|
||||||
Working with the code in Eclipse
|
Building the project
|
||||||
================================
|
====================
|
||||||
Any IDE can be used to work with the projects, but if you're using Eclipse, consider the following.
|
To do the full build, do: `mvn install -Pdefault -Dgib.enabled=false`
|
||||||
|
|
||||||
- import the included **formatter** in Eclipse:
|
|
||||||
`https://github.com/eugenp/tutorials/tree/master/eclipse`
|
Building a single module
|
||||||
|
====================
|
||||||
|
To build a specific module run the command: `mvn clean install -Dgib.enabled=false` in the module directory
|
||||||
|
|
||||||
|
|
||||||
|
Running a Spring Boot module
|
||||||
|
====================
|
||||||
|
To run a Spring Boot module run the command: `mvn spring-boot:run -Dgib.enabled=false` in the module directory
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
CI - Jenkins
|
|
||||||
================================
|
|
||||||
This tutorials project is being built **[>> HERE](https://rest-security.ci.cloudbees.com/job/tutorials-unit/)**
|
|
||||||
|
|||||||
+16
-42
@@ -1,52 +1,26 @@
|
|||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<groupId>com.mabsisa</groupId>
|
<artifactId>Twitter4J</artifactId>
|
||||||
<artifactId>Twitter4J</artifactId>
|
<packaging>jar</packaging>
|
||||||
<packaging>jar</packaging>
|
<name>Twitter4J</name>
|
||||||
<version>1.0-SNAPSHOT</version>
|
|
||||||
<name>Twitter4J</name>
|
|
||||||
<url>http://maven.apache.org</url>
|
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<properties>
|
|
||||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
|
||||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
|
||||||
<java.version>1.8</java.version>
|
|
||||||
</properties>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.twitter4j</groupId>
|
<groupId>org.twitter4j</groupId>
|
||||||
<artifactId>twitter4j-stream</artifactId>
|
<artifactId>twitter4j-stream</artifactId>
|
||||||
<version>4.0.6</version>
|
<version>${twitter4j-stream.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<build>
|
<properties>
|
||||||
<finalName>${project.artifactId}</finalName>
|
<twitter4j-stream.version>4.0.6</twitter4j-stream.version>
|
||||||
<resources>
|
</properties>
|
||||||
<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>
|
|
||||||
|
|
||||||
</project>
|
</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>
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
### Relevant Articles:
|
||||||
|
- [Introduction to ActiveJDBC](http://www.baeldung.com/active-jdbc)
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
<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>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>${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>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<activejdbc.version>2.0</activejdbc.version>
|
||||||
|
<environments>development.test,development</environments>
|
||||||
|
<mysql.connector.version>5.1.34</mysql.connector.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,61 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
import com.baeldung.model.Role;
|
||||||
|
import org.javalite.activejdbc.Base;
|
||||||
|
import org.javalite.activejdbc.LazyList;
|
||||||
|
import org.javalite.activejdbc.Model;
|
||||||
|
|
||||||
|
public class ActiveJDBCApp
|
||||||
|
{
|
||||||
|
public static void main( String[] args )
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
Base.open();
|
||||||
|
ActiveJDBCApp app = new ActiveJDBCApp();
|
||||||
|
app.create();
|
||||||
|
app.update();
|
||||||
|
app.delete();
|
||||||
|
app.deleteCascade();
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
Base.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void create() {
|
||||||
|
Employee employee = new Employee("Hugo","C","M","BN");
|
||||||
|
employee.saveIt();
|
||||||
|
employee.add(new Role("Java Developer","BN"));
|
||||||
|
LazyList<Model> all = Employee.findAll();
|
||||||
|
System.out.println(all.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void update() {
|
||||||
|
Employee employee = Employee.findFirst("first_name = ?","Hugo");
|
||||||
|
employee.set("last_namea","Choi").saveIt();
|
||||||
|
employee = Employee.findFirst("last_name = ?","Choi");
|
||||||
|
System.out.println(employee.getString("first_name") + " " + employee.getString("last_name"));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void delete() {
|
||||||
|
Employee employee = Employee.findFirst("first_name = ?","Hugo");
|
||||||
|
employee.delete();
|
||||||
|
employee = Employee.findFirst("last_name = ?","Choi");
|
||||||
|
if(null == employee){
|
||||||
|
System.out.println("No such Employee found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void deleteCascade() {
|
||||||
|
create();
|
||||||
|
Employee employee = Employee.findFirst("first_name = ?","Hugo");
|
||||||
|
employee.deleteCascade();
|
||||||
|
employee = Employee.findFirst("last_name = ?","C");
|
||||||
|
if(null == employee){
|
||||||
|
System.out.println("No such Employee found!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
package com.baeldung.model;
|
||||||
|
|
||||||
|
|
||||||
|
import org.javalite.activejdbc.Model;
|
||||||
|
|
||||||
|
public class Employee extends Model {
|
||||||
|
|
||||||
|
public Employee(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Employee(String firstName, String lastName, String gender, String createdBy) {
|
||||||
|
set("first_name1",firstName);
|
||||||
|
set("last_name",lastName);
|
||||||
|
set("gender",gender);
|
||||||
|
set("created_by",createdBy);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLastName() {
|
||||||
|
return getString("last_name");
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.model;
|
||||||
|
|
||||||
|
|
||||||
|
import org.javalite.activejdbc.Model;
|
||||||
|
import org.javalite.activejdbc.annotations.Table;
|
||||||
|
|
||||||
|
@Table("EMP_ROLES")
|
||||||
|
public class Role extends Model {
|
||||||
|
|
||||||
|
public Role(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public Role(String role,String createdBy){
|
||||||
|
set("role_name",role);
|
||||||
|
set("created_by",createdBy);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getRoleName() {
|
||||||
|
return getString("role_name");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
# noinspection SqlNoDataSourceInspectionForFile
|
||||||
|
|
||||||
|
create table organisation.employees
|
||||||
|
(
|
||||||
|
id int not null auto_increment
|
||||||
|
primary key,
|
||||||
|
first_name varchar(100) not null,
|
||||||
|
last_name varchar(100) not null,
|
||||||
|
gender varchar(1) not null,
|
||||||
|
created_at datetime not null,
|
||||||
|
updated_at datetime null,
|
||||||
|
created_by varchar(100) not null,
|
||||||
|
updated_by varchar(100) null
|
||||||
|
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
|
||||||
|
|
||||||
|
create table organisation.emp_roles
|
||||||
|
(
|
||||||
|
id int not null auto_increment primary key,
|
||||||
|
employee_id int not null,
|
||||||
|
role_name varchar(100) not null,
|
||||||
|
created_at datetime not null,
|
||||||
|
updated_at datetime null,
|
||||||
|
created_by varchar(100) not null,
|
||||||
|
updated_by varchar(100) null
|
||||||
|
)ENGINE = InnoDB DEFAULT CHARSET = utf8;
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
development.driver=com.mysql.jdbc.Driver
|
||||||
|
development.username=root
|
||||||
|
development.password=123456
|
||||||
|
development.url=jdbc:mysql://localhost/organisation
|
||||||
|
|
||||||
|
|
||||||
|
development.test.driver=com.mysql.jdbc.Driver
|
||||||
|
development.test.username=root
|
||||||
|
development.test.password=123456
|
||||||
|
development.test.url=jdbc:mysql://localhost/organisation_test
|
||||||
@@ -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,51 @@
|
|||||||
|
package com.baeldung;
|
||||||
|
|
||||||
|
import com.baeldung.model.Employee;
|
||||||
|
import com.baeldung.model.Role;
|
||||||
|
import org.javalite.activejdbc.test.DBSpec;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class ActiveJDBCAppManualTest extends DBSpec
|
||||||
|
{
|
||||||
|
@Test
|
||||||
|
public void ifEmployeeCreated_thenIsValid() {
|
||||||
|
Employee employee = new Employee("B", "N", "M", "BN");
|
||||||
|
the(employee).shouldBe("valid");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifEmployeeCreatedWithRoles_thenShouldPersist() {
|
||||||
|
Employee employee = new Employee("B", "N", "M", "BN");
|
||||||
|
employee.saveIt();
|
||||||
|
employee.add(new Role("Java Developer","BN"));
|
||||||
|
employee.add(new Role("Lead Java Developer","BN"));
|
||||||
|
a(Role.count()).shouldBeEqual(2);
|
||||||
|
List<Role> roles = employee.getAll(Role.class).orderBy("created_at");
|
||||||
|
the(roles.get(0).getRoleName()).shouldBeEqual("Java Developer");
|
||||||
|
the(roles.get(1).getRoleName()).shouldBeEqual("Lead Java Developer");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifEmployeeCreatedWithRoles_whenNameUpdated_thenShouldShowNewName() {
|
||||||
|
Employee employee = new Employee("Binesh", "N", "M", "BN");
|
||||||
|
employee.saveIt();
|
||||||
|
employee.add(new Role("Java Developer","BN"));
|
||||||
|
employee.add(new Role("Lead Java Developer","BN"));
|
||||||
|
employee = Employee.findFirst("first_name = ?", "Binesh");
|
||||||
|
employee.set("last_name","Narayanan").saveIt();
|
||||||
|
Employee updated = Employee.findFirst("first_name = ?", "Binesh");
|
||||||
|
the(updated.getLastName()).shouldBeEqual("Narayanan");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void ifEmployeeCreatedWithRoles_whenDeleted_thenShouldNotBeFound() {
|
||||||
|
Employee employee = new Employee("Binesh", "N", "M", "BN");
|
||||||
|
employee.saveIt();
|
||||||
|
employee.add(new Role("Java Developer","BN"));
|
||||||
|
employee.delete();
|
||||||
|
employee = Employee.findFirst("first_name = ?", "Binesh");
|
||||||
|
the(employee).shouldBeNull();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,15 +1,15 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>akka-streams</artifactId>
|
||||||
|
<name>akka-streams</name>
|
||||||
|
|
||||||
<parent>
|
<parent>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<version>1.0.0-SNAPSHOT</version>
|
<version>1.0.0-SNAPSHOT</version>
|
||||||
</parent>
|
</parent>
|
||||||
<modelVersion>4.0.0</modelVersion>
|
|
||||||
<artifactId>akka-streams</artifactId>
|
|
||||||
<name>akka-streams</name>
|
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -23,6 +23,7 @@
|
|||||||
<version>${akkastreams.version}</version>
|
<version>${akkastreams.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<akkastreams.version>2.5.2</akkastreams.version>
|
<akkastreams.version>2.5.2</akkastreams.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<configuration>
|
||||||
|
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||||
|
<encoder>
|
||||||
|
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||||
|
</pattern>
|
||||||
|
</encoder>
|
||||||
|
</appender>
|
||||||
|
|
||||||
|
<root level="INFO">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
|
</configuration>
|
||||||
+17
-1
@@ -5,7 +5,6 @@
|
|||||||
- [Ant Colony Optimization](http://www.baeldung.com/java-ant-colony-optimization)
|
- [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)
|
- [Validating Input With Finite Automata in Java](http://www.baeldung.com/java-finite-automata)
|
||||||
- [Introduction to Jenetics Library](http://www.baeldung.com/jenetics)
|
- [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)
|
- [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)
|
- [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)
|
- [String Search Algorithms for Large Texts](http://www.baeldung.com/java-full-text-search-algorithms)
|
||||||
@@ -15,3 +14,20 @@
|
|||||||
- [Introduction to JGraphT](http://www.baeldung.com/jgrapht)
|
- [Introduction to JGraphT](http://www.baeldung.com/jgrapht)
|
||||||
- [Introduction to Minimax Algorithm](http://www.baeldung.com/java-minimax-algorithm)
|
- [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 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)
|
||||||
|
- [Check If Two Rectangles Overlap In Java](https://www.baeldung.com/java-check-if-two-rectangles-overlap)
|
||||||
|
- [Calculate the Distance Between Two Points in Java](https://www.baeldung.com/java-distance-between-two-points)
|
||||||
|
- [Find the Intersection of Two Lines in Java](https://www.baeldung.com/java-intersection-of-two-lines)
|
||||||
|
- [Check If a String Contains All The Letters of The Alphabet](https://www.baeldung.com/java-string-contains-all-letters)
|
||||||
|
- [Round Up to the Nearest Hundred](https://www.baeldung.com/java-round-up-nearest-hundred)
|
||||||
|
- [Merge Sort in Java](https://www.baeldung.com/java-merge-sort)
|
||||||
|
- [Calculate Percentage in Java](https://www.baeldung.com/java-calculate-percentage)
|
||||||
|
|||||||
+26
-10
@@ -5,12 +5,6 @@
|
|||||||
<artifactId>algorithms</artifactId>
|
<artifactId>algorithms</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<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>
|
<parent>
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-modules</artifactId>
|
<artifactId>parent-modules</artifactId>
|
||||||
@@ -23,6 +17,11 @@
|
|||||||
<artifactId>commons-math3</artifactId>
|
<artifactId>commons-math3</artifactId>
|
||||||
<version>${commons-math3.version}</version>
|
<version>${commons-math3.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>commons-codec</groupId>
|
||||||
|
<artifactId>commons-codec</artifactId>
|
||||||
|
<version>${commons-codec.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.projectlombok</groupId>
|
<groupId>org.projectlombok</groupId>
|
||||||
<artifactId>lombok</artifactId>
|
<artifactId>lombok</artifactId>
|
||||||
@@ -32,17 +31,22 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>io.jenetics</groupId>
|
<groupId>io.jenetics</groupId>
|
||||||
<artifactId>jenetics</artifactId>
|
<artifactId>jenetics</artifactId>
|
||||||
<version>3.7.0</version>
|
<version>${io.jenetics.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.jgrapht</groupId>
|
<groupId>org.jgrapht</groupId>
|
||||||
<artifactId>jgrapht-core</artifactId>
|
<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>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
<version>3.9.0</version>
|
<version>${org.assertj.core.version}</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
@@ -58,6 +62,7 @@
|
|||||||
</plugins>
|
</plugins>
|
||||||
</pluginManagement>
|
</pluginManagement>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
<reporting>
|
<reporting>
|
||||||
<plugins>
|
<plugins>
|
||||||
<plugin>
|
<plugin>
|
||||||
@@ -77,4 +82,15 @@
|
|||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
</reporting>
|
</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>
|
||||||
|
<commons-codec.version>1.11</commons-codec.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
/bin/
|
||||||
@@ -0,0 +1,20 @@
|
|||||||
|
package com.java.src;
|
||||||
|
|
||||||
|
import java.util.Scanner;
|
||||||
|
|
||||||
|
public class RoundUpToHundred {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Scanner scanner = new Scanner(System.in);
|
||||||
|
double input = scanner.nextDouble();
|
||||||
|
scanner.close();
|
||||||
|
|
||||||
|
RoundUpToHundred.round(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long round(double input) {
|
||||||
|
long i = (long) Math.ceil(input);
|
||||||
|
return ((i + 99) / 100) * 100;
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.java.src;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
public class RoundUpToHundredTest {
|
||||||
|
@Test
|
||||||
|
public void givenInput_whenRound_thenRoundUpToTheNearestHundred() {
|
||||||
|
assertEquals("Rounded up to hundred", 100, RoundUpToHundred.round(99));
|
||||||
|
assertEquals("Rounded up to three hundred ", 300, RoundUpToHundred.round(200.2));
|
||||||
|
assertEquals("Returns same rounded value", 400, RoundUpToHundred.round(400));
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,6 +17,7 @@ public class RunAlgorithm {
|
|||||||
System.out.println("3 - Simple Genetic Algorithm");
|
System.out.println("3 - Simple Genetic Algorithm");
|
||||||
System.out.println("4 - Ant Colony");
|
System.out.println("4 - Ant Colony");
|
||||||
System.out.println("5 - Dijkstra");
|
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();
|
int decision = in.nextInt();
|
||||||
switch (decision) {
|
switch (decision) {
|
||||||
case 1:
|
case 1:
|
||||||
|
|||||||
@@ -0,0 +1,110 @@
|
|||||||
|
package com.baeldung.algorithms.conversion;
|
||||||
|
|
||||||
|
import java.math.BigInteger;
|
||||||
|
|
||||||
|
import javax.xml.bind.DatatypeConverter;
|
||||||
|
|
||||||
|
import org.apache.commons.codec.DecoderException;
|
||||||
|
import org.apache.commons.codec.binary.Hex;
|
||||||
|
|
||||||
|
import com.google.common.io.BaseEncoding;
|
||||||
|
|
||||||
|
public class HexStringConverter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a byte Array from String of hexadecimal digits using Character conversion
|
||||||
|
* @param hexString - Hexadecimal digits as String
|
||||||
|
* @return Desired byte Array
|
||||||
|
*/
|
||||||
|
public byte[] decodeHexString(String hexString) {
|
||||||
|
if (hexString.length() % 2 == 1) {
|
||||||
|
throw new IllegalArgumentException("Invalid hexadecimal String supplied.");
|
||||||
|
}
|
||||||
|
byte[] bytes = new byte[hexString.length() / 2];
|
||||||
|
|
||||||
|
for (int i = 0; i < hexString.length(); i += 2) {
|
||||||
|
bytes[i / 2] = hexToByte(hexString.substring(i, i + 2));
|
||||||
|
}
|
||||||
|
return bytes;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a String of hexadecimal digits from a byte Array using Character conversion
|
||||||
|
* @param byteArray - The byte Array
|
||||||
|
* @return Desired String of hexadecimal digits in lower case
|
||||||
|
*/
|
||||||
|
public String encodeHexString(byte[] byteArray) {
|
||||||
|
StringBuffer hexStringBuffer = new StringBuffer();
|
||||||
|
for (int i = 0; i < byteArray.length; i++) {
|
||||||
|
hexStringBuffer.append(byteToHex(byteArray[i]));
|
||||||
|
}
|
||||||
|
return hexStringBuffer.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String byteToHex(byte num) {
|
||||||
|
char[] hexDigits = new char[2];
|
||||||
|
hexDigits[0] = Character.forDigit((num >> 4) & 0xF, 16);
|
||||||
|
hexDigits[1] = Character.forDigit((num & 0xF), 16);
|
||||||
|
return new String(hexDigits);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte hexToByte(String hexString) {
|
||||||
|
int firstDigit = toDigit(hexString.charAt(0));
|
||||||
|
int secondDigit = toDigit(hexString.charAt(1));
|
||||||
|
return (byte) ((firstDigit << 4) + secondDigit);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int toDigit(char hexChar) {
|
||||||
|
int digit = Character.digit(hexChar, 16);
|
||||||
|
if(digit == -1) {
|
||||||
|
throw new IllegalArgumentException("Invalid Hexadecimal Character: "+ hexChar);
|
||||||
|
}
|
||||||
|
return digit;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String encodeUsingBigIntegerToString(byte[] bytes) {
|
||||||
|
BigInteger bigInteger = new BigInteger(1, bytes);
|
||||||
|
return bigInteger.toString(16);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String encodeUsingBigIntegerStringFormat(byte[] bytes) {
|
||||||
|
BigInteger bigInteger = new BigInteger(1, bytes);
|
||||||
|
return String.format("%0" + (bytes.length << 1) + "x", bigInteger);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] decodeUsingBigInteger(String hexString) {
|
||||||
|
byte[] byteArray = new BigInteger(hexString, 16).toByteArray();
|
||||||
|
if (byteArray[0] == 0) {
|
||||||
|
byte[] output = new byte[byteArray.length - 1];
|
||||||
|
System.arraycopy(byteArray, 1, output, 0, output.length);
|
||||||
|
return output;
|
||||||
|
}
|
||||||
|
return byteArray;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String encodeUsingDataTypeConverter(byte[] bytes) {
|
||||||
|
return DatatypeConverter.printHexBinary(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] decodeUsingDataTypeConverter(String hexString) {
|
||||||
|
return DatatypeConverter.parseHexBinary(hexString);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String encodeUsingApacheCommons(byte[] bytes) throws DecoderException {
|
||||||
|
return Hex.encodeHexString(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] decodeUsingApacheCommons(String hexString) throws DecoderException {
|
||||||
|
return Hex.decodeHex(hexString);
|
||||||
|
}
|
||||||
|
|
||||||
|
public String encodeUsingGuava(byte[] bytes) {
|
||||||
|
return BaseEncoding.base16()
|
||||||
|
.encode(bytes);
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] decodeUsingGuava(String hexString) {
|
||||||
|
return BaseEncoding.base16()
|
||||||
|
.decode(hexString.toUpperCase());
|
||||||
|
}
|
||||||
|
}
|
||||||
+38
@@ -0,0 +1,38 @@
|
|||||||
|
package com.baeldung.algorithms.distancebetweenpoints;
|
||||||
|
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
|
||||||
|
public class DistanceBetweenPointsService {
|
||||||
|
|
||||||
|
public double calculateDistanceBetweenPoints(
|
||||||
|
double x1,
|
||||||
|
double y1,
|
||||||
|
double x2,
|
||||||
|
double y2) {
|
||||||
|
|
||||||
|
return Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public double calculateDistanceBetweenPointsWithHypot(
|
||||||
|
double x1,
|
||||||
|
double y1,
|
||||||
|
double x2,
|
||||||
|
double y2) {
|
||||||
|
|
||||||
|
double ac = Math.abs(y2 - y1);
|
||||||
|
double cb = Math.abs(x2 - x1);
|
||||||
|
|
||||||
|
return Math.hypot(ac, cb);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double calculateDistanceBetweenPointsWithPoint2D(
|
||||||
|
double x1,
|
||||||
|
double y1,
|
||||||
|
double x2,
|
||||||
|
double y2) {
|
||||||
|
|
||||||
|
return Point2D.distance(x1, y1, x2, y2);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,136 @@
|
|||||||
|
package com.baeldung.algorithms.heapsort;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class Heap<E extends Comparable<E>> {
|
||||||
|
|
||||||
|
private List<E> elements = new ArrayList<>();
|
||||||
|
|
||||||
|
public static <E extends Comparable<E>> List<E> sort(Iterable<E> elements) {
|
||||||
|
Heap<E> heap = of(elements);
|
||||||
|
|
||||||
|
List<E> result = new ArrayList<>();
|
||||||
|
|
||||||
|
while (!heap.isEmpty()) {
|
||||||
|
result.add(heap.pop());
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E extends Comparable<E>> Heap<E> of(E... elements) {
|
||||||
|
return of(Arrays.asList(elements));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static <E extends Comparable<E>> Heap<E> of(Iterable<E> elements) {
|
||||||
|
Heap<E> result = new Heap<>();
|
||||||
|
for (E element : elements) {
|
||||||
|
result.add(element);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void add(E e) {
|
||||||
|
elements.add(e);
|
||||||
|
int elementIndex = elements.size() - 1;
|
||||||
|
while (!isRoot(elementIndex) && !isCorrectChild(elementIndex)) {
|
||||||
|
int parentIndex = parentIndex(elementIndex);
|
||||||
|
swap(elementIndex, parentIndex);
|
||||||
|
elementIndex = parentIndex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public E pop() {
|
||||||
|
if (isEmpty()) {
|
||||||
|
throw new IllegalStateException("You cannot pop from an empty heap");
|
||||||
|
}
|
||||||
|
|
||||||
|
E result = elementAt(0);
|
||||||
|
|
||||||
|
int lasElementIndex = elements.size() - 1;
|
||||||
|
swap(0, lasElementIndex);
|
||||||
|
elements.remove(lasElementIndex);
|
||||||
|
|
||||||
|
int elementIndex = 0;
|
||||||
|
while (!isLeaf(elementIndex) && !isCorrectParent(elementIndex)) {
|
||||||
|
int smallerChildIndex = smallerChildIndex(elementIndex);
|
||||||
|
swap(elementIndex, smallerChildIndex);
|
||||||
|
elementIndex = smallerChildIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isEmpty() {
|
||||||
|
return elements.isEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isRoot(int index) {
|
||||||
|
return index == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int smallerChildIndex(int index) {
|
||||||
|
int leftChildIndex = leftChildIndex(index);
|
||||||
|
int rightChildIndex = rightChildIndex(index);
|
||||||
|
|
||||||
|
if (!isValidIndex(rightChildIndex)) {
|
||||||
|
return leftChildIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (elementAt(leftChildIndex).compareTo(elementAt(rightChildIndex)) < 0) {
|
||||||
|
return leftChildIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
return rightChildIndex;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isLeaf(int index) {
|
||||||
|
return !isValidIndex(leftChildIndex(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCorrectParent(int index) {
|
||||||
|
return isCorrect(index, leftChildIndex(index)) && isCorrect(index, rightChildIndex(index));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCorrectChild(int index) {
|
||||||
|
return isCorrect(parentIndex(index), index);
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isCorrect(int parentIndex, int childIndex) {
|
||||||
|
if (!isValidIndex(parentIndex) || !isValidIndex(childIndex)) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return elementAt(parentIndex).compareTo(elementAt(childIndex)) < 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean isValidIndex(int index) {
|
||||||
|
return index < elements.size();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void swap(int index1, int index2) {
|
||||||
|
E element1 = elementAt(index1);
|
||||||
|
E element2 = elementAt(index2);
|
||||||
|
elements.set(index1, element2);
|
||||||
|
elements.set(index2, element1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private E elementAt(int index) {
|
||||||
|
return elements.get(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int parentIndex(int index) {
|
||||||
|
return (index - 1) / 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int leftChildIndex(int index) {
|
||||||
|
return 2 * index + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
private int rightChildIndex(int index) {
|
||||||
|
return 2 * index + 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,41 @@
|
|||||||
|
package com.baeldung.algorithms.insertionsort;
|
||||||
|
|
||||||
|
public class InsertionSort {
|
||||||
|
|
||||||
|
public static void insertionSortImperative(int[] input) {
|
||||||
|
for (int i = 1; i < input.length; i++) {
|
||||||
|
int key = input[i];
|
||||||
|
int j = i - 1;
|
||||||
|
while (j >= 0 && input[j] > key) {
|
||||||
|
input[j + 1] = input[j];
|
||||||
|
j = j - 1;
|
||||||
|
}
|
||||||
|
input[j + 1] = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void insertionSortRecursive(int[] input) {
|
||||||
|
insertionSortRecursive(input, input.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void insertionSortRecursive(int[] input, int i) {
|
||||||
|
// base case
|
||||||
|
if (i <= 1) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// sort the first i - 1 elements of the array
|
||||||
|
insertionSortRecursive(input, i - 1);
|
||||||
|
|
||||||
|
// then find the correct position of the element at position i
|
||||||
|
int key = input[i - 1];
|
||||||
|
int j = i - 2;
|
||||||
|
// shifting the elements from their position by 1
|
||||||
|
while (j >= 0 && input[j] > key) {
|
||||||
|
input[j + 1] = input[j];
|
||||||
|
j = j - 1;
|
||||||
|
}
|
||||||
|
// inserting the key at the appropriate position
|
||||||
|
input[j + 1] = key;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -98,7 +98,7 @@ public class FindKthLargest {
|
|||||||
|
|
||||||
private int randomPartition(Integer arr[], int left, int right) {
|
private int randomPartition(Integer arr[], int left, int right) {
|
||||||
int n = right - left + 1;
|
int n = right - left + 1;
|
||||||
int pivot = (int) (Math.random()) % n;
|
int pivot = (int) (Math.random() * n);
|
||||||
swap(arr, left + pivot, right);
|
swap(arr, left + pivot, right);
|
||||||
return partition(arr, left, right);
|
return partition(arr, left, right);
|
||||||
}
|
}
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user