Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||
<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>
|
||||
@@ -23,27 +19,6 @@
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>${project.artifactId}</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/ApplicationTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<twitter4j-stream.version>4.0.6</twitter4j-stream.version>
|
||||
</properties>
|
||||
|
||||
+1
-46
@@ -1,7 +1,6 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>activejdbc</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
@@ -79,55 +78,11 @@
|
||||
</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>
|
||||
<activejdbc.version>2.0</activejdbc.version>
|
||||
<environments>development.test,development</environments>
|
||||
<mysql.connector.version>5.1.34</mysql.connector.version>
|
||||
</properties>
|
||||
|
||||
@@ -16,4 +16,8 @@ public class Employee extends Model {
|
||||
set("created_by",createdBy);
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return getString("last_name");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,4 +15,8 @@ public class Role extends Model {
|
||||
set("role_name",role);
|
||||
set("created_by",createdBy);
|
||||
}
|
||||
|
||||
public String getRoleName() {
|
||||
return getString("role_name");
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -7,7 +7,7 @@ import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class ActiveJDBCAppTest extends DBSpec
|
||||
public class ActiveJDBCAppManualTest extends DBSpec
|
||||
{
|
||||
@Test
|
||||
public void ifEmployeeCreated_thenIsValid() {
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,6 @@
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>apache-shiro</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
@@ -36,13 +35,11 @@
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>jcl-over-slf4j</artifactId>
|
||||
<version>${slf4j-version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-log4j12</artifactId>
|
||||
<version>${slf4j-version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -53,26 +50,9 @@
|
||||
</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>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<apache-shiro-core-version>1.4.0</apache-shiro-core-version>
|
||||
<maven-compiler-plugin.version>3.7.0</maven-compiler-plugin.version>
|
||||
<java.version>1.8</java.version>
|
||||
<log4j-version>1.2.17</log4j-version>
|
||||
<slf4j-version>1.7.25</slf4j-version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -42,14 +42,6 @@
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<argLine>-javaagent:"C:\asm-1.0.jar"</argLine>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
|
||||
@@ -2,12 +2,10 @@
|
||||
<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>cas-secured-app</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<name>cas-secured-app</name>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
<description>Demo project for CAS</description>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
@@ -60,10 +58,4 @@
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
|
||||
+10
-40
@@ -2,11 +2,17 @@
|
||||
<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>cas-server</artifactId>
|
||||
<packaging>war</packaging>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<artifactId>parent-boot-1</artifactId>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-1</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apereo.cas</groupId>
|
||||
@@ -39,7 +45,6 @@
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${springboot.version}</version>
|
||||
<configuration>
|
||||
<mainClass>${mainClassName}</mainClass>
|
||||
<addResources>true</addResources>
|
||||
@@ -74,42 +79,10 @@
|
||||
</overlays>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
</plugin>
|
||||
</plugins>
|
||||
<finalName>cas</finalName>
|
||||
</build>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>sonatype-releases</id>
|
||||
<url>http://oss.sonatype.org/content/repositories/releases/</url>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>true</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>sonatype-snapshots</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
<releases>
|
||||
<enabled>false</enabled>
|
||||
</releases>
|
||||
</repository>
|
||||
<repository>
|
||||
<id>shibboleth-releases</id>
|
||||
<url>https://build.shibboleth.net/nexus/content/repositories/releases</url>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<activation>
|
||||
@@ -214,8 +187,7 @@
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
<cas.version>5.3.0-SNAPSHOT</cas.version>
|
||||
<springboot.version>1.5.13.RELEASE</springboot.version>
|
||||
<cas.version>5.3.3</cas.version>
|
||||
<!-- app.server could be -jetty, -undertow, -tomcat, or blank if you plan to provide appserver -->
|
||||
<app.server>-tomcat</app.server>
|
||||
|
||||
@@ -223,14 +195,12 @@
|
||||
<isExecutable>false</isExecutable>
|
||||
<manifestFileToUse>${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp${app.server}/META-INF/MANIFEST.MF</manifestFileToUse>
|
||||
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<wrapper-maven-plugin.version>0.0.4</wrapper-maven-plugin.version>
|
||||
<maven-war-plugin.version>2.6</maven-war-plugin.version>
|
||||
<maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
|
||||
|
||||
<echo-maven-plugin.version>0.3.0</echo-maven-plugin.version>
|
||||
<pgpverify-maven-plugin.version>1.1.0</pgpverify-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
||||
Executable
+15
@@ -0,0 +1,15 @@
|
||||
# compile logging module
|
||||
# javac --module-path mods -d mods/com.baeldung.logging src/modules/com.baeldung.logging/module-info.java src/modules/com.baeldung.logging/com/baeldung/logging/*.java
|
||||
|
||||
# compile logging slf4j module
|
||||
javac --module-path mods -d mods/com.baeldung.logging.slf4j src/modules/com.baeldung.logging.slf4j/module-info.java src/modules/com.baeldung.logging.slf4j/com/baeldung/logging/slf4j/*.java
|
||||
|
||||
|
||||
# compile logging main app module
|
||||
javac --module-path mods -d mods/com.baeldung.logging.app src/modules/com.baeldung.logging.app/module-info.java src/modules/com.baeldung.logging.app/com/baeldung/logging/app/*.java
|
||||
|
||||
# run logging main app
|
||||
# java --module-path mods -m com.baeldung.logging.app/com.baeldung.logging.app.MainApp
|
||||
|
||||
# run looging main app using logback
|
||||
java --module-path mods -Dlogback.configurationFile=mods/logback.xml -m com.baeldung.logging.app/com.baeldung.logging.app.MainApp
|
||||
@@ -0,0 +1,16 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
|
||||
<pattern>
|
||||
%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} -- %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root>
|
||||
<appender-ref ref="STDOUT"/>
|
||||
</root>
|
||||
|
||||
</configuration>
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
package com.baeldung.logging.app;
|
||||
|
||||
import static java.lang.System.Logger.*;
|
||||
|
||||
public class MainApp {
|
||||
|
||||
private static System.Logger LOGGER = System.getLogger("MainApp");
|
||||
|
||||
public static void main(String[] args) {
|
||||
LOGGER.log(Level.ERROR, "error test");
|
||||
LOGGER.log(Level.INFO, "info test");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
module com.baeldung.logging.app {
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
package com.baeldung.logging.slf4j;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class Slf4jLogger implements System.Logger {
|
||||
|
||||
private final String name;
|
||||
private final Logger logger;
|
||||
|
||||
public Slf4jLogger(String name) {
|
||||
this.name = name;
|
||||
logger = LoggerFactory.getLogger(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLoggable(Level level) {
|
||||
switch (level) {
|
||||
case OFF:
|
||||
return false;
|
||||
case TRACE:
|
||||
return logger.isTraceEnabled();
|
||||
case DEBUG:
|
||||
return logger.isDebugEnabled();
|
||||
case INFO:
|
||||
return logger.isInfoEnabled();
|
||||
case WARNING:
|
||||
return logger.isWarnEnabled();
|
||||
case ERROR:
|
||||
return logger.isErrorEnabled();
|
||||
case ALL:
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) {
|
||||
if (!isLoggable(level)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (level) {
|
||||
case TRACE:
|
||||
logger.trace(msg, thrown);
|
||||
break;
|
||||
case DEBUG:
|
||||
logger.debug(msg, thrown);
|
||||
break;
|
||||
case INFO:
|
||||
logger.info(msg, thrown);
|
||||
break;
|
||||
case WARNING:
|
||||
logger.warn(msg, thrown);
|
||||
break;
|
||||
case ERROR:
|
||||
logger.error(msg, thrown);
|
||||
break;
|
||||
case ALL:
|
||||
default:
|
||||
logger.info(msg, thrown);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(Level level, ResourceBundle bundle, String format, Object... params) {
|
||||
if (!isLoggable(level)) {
|
||||
return;
|
||||
}
|
||||
|
||||
switch (level) {
|
||||
case TRACE:
|
||||
logger.trace(format, params);
|
||||
break;
|
||||
case DEBUG:
|
||||
logger.debug(format, params);
|
||||
break;
|
||||
case INFO:
|
||||
logger.info(format, params);
|
||||
break;
|
||||
case WARNING:
|
||||
logger.warn(format, params);
|
||||
break;
|
||||
case ERROR:
|
||||
logger.error(format, params);
|
||||
break;
|
||||
case ALL:
|
||||
default:
|
||||
logger.info(format, params);
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.logging.slf4j;
|
||||
|
||||
public class Slf4jLoggerFinder extends System.LoggerFinder {
|
||||
@Override
|
||||
public System.Logger getLogger(String name, Module module) {
|
||||
return new Slf4jLogger(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
module com.baeldung.logging.slf4j {
|
||||
requires org.slf4j;
|
||||
provides java.lang.System.LoggerFinder
|
||||
with com.baeldung.logging.slf4j.Slf4jLoggerFinder;
|
||||
exports com.baeldung.logging.slf4j;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.logging;
|
||||
|
||||
import java.text.MessageFormat;
|
||||
import java.util.ResourceBundle;
|
||||
|
||||
public class ConsoleLogger implements System.Logger {
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "ConsoleLogger";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isLoggable(Level level) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(Level level, ResourceBundle bundle, String msg, Throwable thrown) {
|
||||
System.out.printf("ConsoleLogger [%s]: %s - %s%n", level, msg, thrown);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void log(Level level, ResourceBundle bundle, String format, Object... params) {
|
||||
System.out.printf("ConsoleLogger [%s]: %s%n", level, MessageFormat.format(format, params));
|
||||
}
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
package com.baeldung.logging;
|
||||
|
||||
public class CustomLoggerFinder extends System.LoggerFinder {
|
||||
|
||||
@Override
|
||||
public System.Logger getLogger(String name, Module module) {
|
||||
return new ConsoleLogger();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
module com.baeldung.logging {
|
||||
provides java.lang.System.LoggerFinder
|
||||
with com.baeldung.logging.CustomLoggerFinder;
|
||||
exports com.baeldung.logging;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.java.map;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
/**
|
||||
* @author swpraman
|
||||
*
|
||||
*/
|
||||
public class MapUtil {
|
||||
|
||||
public static <K, V> Stream<K> keys(Map<K, V> map, V value) {
|
||||
return map.entrySet()
|
||||
.stream()
|
||||
.filter(entry -> value.equals(entry.getValue()))
|
||||
.map(Map.Entry::getKey);
|
||||
}
|
||||
|
||||
public static <K, V> Set<K> getKeys(Map<K, V> map, V value) {
|
||||
Set<K> keys = new HashSet<>();
|
||||
for (Entry<K, V> entry : map.entrySet()) {
|
||||
if (entry.getValue().equals(value)) {
|
||||
keys.add(entry.getKey());
|
||||
}
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
public static <K, V> K getKey(Map<K, V> map, V value) {
|
||||
for (Entry<K, V> entry : map.entrySet()) {
|
||||
if (entry.getValue().equals(value)) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -44,4 +44,12 @@ public class Employee {
|
||||
result = 31 * result + name.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.sort;
|
||||
|
||||
public class Employee implements Comparable<Employee> {
|
||||
|
||||
private Long id;
|
||||
private String name;
|
||||
|
||||
public Employee(Long id, String name) {
|
||||
this.name = name;
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
|
||||
Employee employee = (Employee) o;
|
||||
|
||||
if (!id.equals(employee.id)) return false;
|
||||
return name.equals(employee.name);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
int result = id.hashCode();
|
||||
result = 31 * result + name.hashCode();
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Employee{" +
|
||||
"id=" + id +
|
||||
", name='" + name + '\'' +
|
||||
'}';
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Employee employee) {
|
||||
return (int)(this.id - employee.getId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.baeldung.sort;
|
||||
|
||||
import com.google.common.base.Functions;
|
||||
import com.google.common.collect.ImmutableSortedMap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Ordering;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class SortHashMap {
|
||||
|
||||
private static Map<String, Employee> map = new HashMap<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
initialize();
|
||||
|
||||
treeMapSortByKey();
|
||||
|
||||
arrayListSortByValue();
|
||||
arrayListSortByKey();
|
||||
|
||||
sortStream();
|
||||
|
||||
sortGuava();
|
||||
|
||||
addDuplicates();
|
||||
|
||||
treeSetByKey();
|
||||
treeSetByValue();
|
||||
|
||||
}
|
||||
|
||||
private static void sortGuava() {
|
||||
final Ordering naturalOrdering =
|
||||
Ordering.natural().onResultOf(Functions.forMap(map, null));
|
||||
|
||||
System.out.println(ImmutableSortedMap.copyOf(map, naturalOrdering));
|
||||
}
|
||||
|
||||
private static void sortStream() {
|
||||
map.entrySet().stream()
|
||||
.sorted(Map.Entry.<String, Employee>comparingByKey().reversed())
|
||||
.forEach(System.out::println);
|
||||
|
||||
Map<String, Employee> result = map.entrySet().stream()
|
||||
.sorted(Map.Entry.comparingByValue())
|
||||
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
|
||||
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
|
||||
|
||||
result.entrySet().forEach(System.out::println);
|
||||
}
|
||||
|
||||
private static void treeSetByValue() {
|
||||
SortedSet<Employee> values = new TreeSet<>(map.values());
|
||||
System.out.println(values);
|
||||
}
|
||||
|
||||
private static void treeSetByKey() {
|
||||
SortedSet<String> keysSet = new TreeSet<>(map.keySet());
|
||||
System.out.println(keysSet);
|
||||
}
|
||||
|
||||
private static void treeMapSortByKey() {
|
||||
TreeMap<String, Employee> sorted = new TreeMap<>(map);
|
||||
sorted.putAll(map);
|
||||
|
||||
sorted.entrySet().forEach(System.out::println);
|
||||
|
||||
}
|
||||
|
||||
private static void arrayListSortByValue() {
|
||||
List<Employee> employeeById = new ArrayList<>(map.values());
|
||||
|
||||
Collections.sort(employeeById);
|
||||
|
||||
System.out.println(employeeById);
|
||||
}
|
||||
|
||||
private static void arrayListSortByKey() {
|
||||
List<String> employeeByKey = new ArrayList<>(map.keySet());
|
||||
Collections.sort(employeeByKey);
|
||||
System.out.println(employeeByKey);
|
||||
}
|
||||
|
||||
private static void initialize() {
|
||||
Employee employee1 = new Employee(1L, "Mher");
|
||||
map.put(employee1.getName(), employee1);
|
||||
Employee employee2 = new Employee(22L, "Annie");
|
||||
map.put(employee2.getName(), employee2);
|
||||
Employee employee3 = new Employee(8L, "John");
|
||||
map.put(employee3.getName(), employee3);
|
||||
Employee employee4 = new Employee(2L, "George");
|
||||
map.put(employee4.getName(), employee4);
|
||||
}
|
||||
|
||||
private static void addDuplicates() {
|
||||
Employee employee5 = new Employee(1L, "Mher");
|
||||
map.put(employee5.getName(), employee5);
|
||||
Employee employee6 = new Employee(22L, "Annie");
|
||||
map.put(employee6.getName(), employee6);
|
||||
}
|
||||
}
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
package com.baeldung.collection;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Unit tests demonstrating differences between ArrayList#clear() and ArrayList#removeAll()
|
||||
*/
|
||||
class ClearVsRemoveAllUnitTest {
|
||||
|
||||
/*
|
||||
* Tests
|
||||
*/
|
||||
@Test
|
||||
void givenArrayListWithElements_whenClear_thenListBecomesEmpty() {
|
||||
Collection<Integer> collection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
|
||||
|
||||
collection.clear();
|
||||
|
||||
assertTrue(collection.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenTwoArrayListsWithCommonElements_whenRemoveAll_thenFirstListMissElementsFromSecondList() {
|
||||
Collection<Integer> firstCollection = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
|
||||
Collection<Integer> secondCollection = new ArrayList<>(Arrays.asList(3, 4, 5, 6, 7));
|
||||
|
||||
firstCollection.removeAll(secondCollection);
|
||||
|
||||
assertEquals(Arrays.asList(1, 2), firstCollection);
|
||||
assertEquals(Arrays.asList(3, 4, 5, 6, 7), secondCollection);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
*
|
||||
*/
|
||||
package com.baeldung.java.map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.collections4.BidiMap;
|
||||
import org.apache.commons.collections4.bidimap.DualHashBidiMap;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.google.common.collect.HashBiMap;
|
||||
|
||||
/**
|
||||
* @author swpraman
|
||||
*
|
||||
*/
|
||||
public class MapUtilUnitTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void whenUsingImperativeWayForSingleKey_shouldReturnSingleKey() {
|
||||
Map<String, String> capitalCountryMap = new HashMap<>();
|
||||
capitalCountryMap.put("Tokyo", "Japan");
|
||||
capitalCountryMap.put("New Delhi", "India");
|
||||
assertEquals("New Delhi", MapUtil.getKey(capitalCountryMap, "India"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingImperativeWayForAllKeys_shouldReturnAllKeys() {
|
||||
Map<String, String> capitalCountryMap = new HashMap<>();
|
||||
capitalCountryMap.put("Tokyo", "Japan");
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
capitalCountryMap.put("Pretoria", "South Africa");
|
||||
capitalCountryMap.put("Bloemfontein", "South Africa");
|
||||
|
||||
assertEquals(new HashSet<String>(Arrays.asList(
|
||||
new String[] {"Cape Town", "Pretoria", "Bloemfontein"})),
|
||||
MapUtil.getKeys(capitalCountryMap, "South Africa"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFunctionalWayForSingleKey_shouldReturnSingleKey() {
|
||||
Map<String, String> capitalCountryMap = new HashMap<>();
|
||||
capitalCountryMap.put("Tokyo", "Japan");
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
assertEquals("Berlin", MapUtil.keys(capitalCountryMap, "Germany").findFirst().get());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingFunctionalWayForAllKeys_shouldReturnAllKeys() {
|
||||
Map<String, String> capitalCountryMap = new HashMap<>();
|
||||
capitalCountryMap.put("Tokyo", "Japan");
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
capitalCountryMap.put("Pretoria", "South Africa");
|
||||
capitalCountryMap.put("Bloemfontein", "South Africa");
|
||||
assertEquals(new HashSet<String>(Arrays.asList(
|
||||
new String[] {"Cape Town", "Pretoria", "Bloemfontein"})),
|
||||
MapUtil.keys(capitalCountryMap, "South Africa").collect(Collectors.toSet()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingBidiMap_shouldReturnKey() {
|
||||
BidiMap<String, String> capitalCountryMap = new DualHashBidiMap<String, String>();
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
assertEquals("Berlin", capitalCountryMap.getKey("Germany"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingBidiMapAddDuplicateValue_shouldRemoveOldEntry() {
|
||||
BidiMap<String, String> capitalCountryMap = new DualHashBidiMap<String, String>();
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
capitalCountryMap.put("Pretoria", "South Africa");
|
||||
assertEquals("Pretoria", capitalCountryMap.getKey("South Africa"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingBiMap_shouldReturnKey() {
|
||||
HashBiMap<String, String> capitalCountryMap = HashBiMap.create();
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
assertEquals("Berlin", capitalCountryMap.inverse().get("Germany"));
|
||||
}
|
||||
|
||||
@Test(expected=IllegalArgumentException.class)
|
||||
public void whenUsingBiMapAddDuplicateValue_shouldThrowException() {
|
||||
HashBiMap<String, String> capitalCountryMap = HashBiMap.create();
|
||||
capitalCountryMap.put("Berlin", "Germany");
|
||||
capitalCountryMap.put("Cape Town", "South Africa");
|
||||
capitalCountryMap.put("Pretoria", "South Africa");
|
||||
assertEquals("Berlin", capitalCountryMap.inverse().get("Germany"));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.concurrent.yield;
|
||||
|
||||
public class ThreadYield {
|
||||
public static void main(String[] args) {
|
||||
Runnable r = () -> {
|
||||
int counter = 0;
|
||||
while (counter < 2) {
|
||||
System.out.println(Thread.currentThread()
|
||||
.getName());
|
||||
counter++;
|
||||
Thread.yield();
|
||||
}
|
||||
};
|
||||
new Thread(r).start();
|
||||
new Thread(r).start();
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>core-java-io</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
@@ -166,36 +165,6 @@
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*IntTest.java</exclude>
|
||||
<exclude>**/*LongRunningUnitTest.java</exclude>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
<testFailureIgnore>true</testFailureIgnore>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring-boot-maven-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>repackage</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classifier>spring-boot</classifier>
|
||||
<mainClass>org.baeldung.executable.ExecutableMavenJar</mainClass>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
@@ -229,32 +198,6 @@
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*ManualTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/*IntegrationTest.java</include>
|
||||
<include>**/*IntTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
@@ -310,14 +253,11 @@
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven.compiler.source>1.8</maven.compiler.source>
|
||||
<maven.compiler.target>1.8</maven.compiler.target>
|
||||
<maven-javadoc-plugin.version>3.0.0-M1</maven-javadoc-plugin.version>
|
||||
<hsqldb.version>2.4.0</hsqldb.version>
|
||||
<esapi.version>2.1.0.1</esapi.version>
|
||||
<jmh-generator-annprocess.version>1.19</jmh-generator-annprocess.version>
|
||||
<async-http-client.version>2.4.5</async-http-client.version>
|
||||
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user