Merge branch 'master' of https://github.com/eugenp/tutorials
This commit is contained in:
commit
595f6e3de0
@ -0,0 +1,22 @@
|
||||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
public class EditDistanceBase {
|
||||
|
||||
public static int costOfSubstitution(char a, char b) {
|
||||
if (a == b) {
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
public static int min(int... numbers) {
|
||||
int min = Integer.MAX_VALUE;
|
||||
|
||||
for (int x : numbers) {
|
||||
if (x < min)
|
||||
min = x;
|
||||
}
|
||||
|
||||
return min;
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
public class EditDistanceDynamicProgramming extends EditDistanceBase {
|
||||
|
||||
public static int calculate(String x, String y) {
|
||||
int[][] dp = new int[x.length() + 1][y.length() + 1];
|
||||
|
||||
for (int i = 0; i <= x.length(); i++) {
|
||||
for (int j = 0; j <= y.length(); j++) {
|
||||
if (i == 0)
|
||||
dp[i][j] = j;
|
||||
|
||||
else if (j == 0)
|
||||
dp[i][j] = i;
|
||||
|
||||
else {
|
||||
dp[i][j] = min(dp[i - 1][j - 1] + costOfSubstitution(x.charAt(i - 1), y.charAt(j - 1)), dp[i - 1][j] + 1, dp[i][j - 1] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return dp[x.length()][y.length()];
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,20 @@
|
||||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
public class EditDistanceRecursive extends EditDistanceBase {
|
||||
|
||||
public static int calculate(String x, String y) {
|
||||
|
||||
if (x.isEmpty())
|
||||
return y.length();
|
||||
|
||||
if (y.isEmpty())
|
||||
return x.length();
|
||||
|
||||
int substitution = calculate(x.substring(1), y.substring(1)) + costOfSubstitution(x.charAt(0), y.charAt(0));
|
||||
int insertion = calculate(x, y.substring(1)) + 1;
|
||||
int deletion = calculate(x.substring(1), y) + 1;
|
||||
|
||||
return min(substitution, insertion, deletion);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
||||
public class EditDistanceDataProvider {
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> getLists() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ "", "", 0 },
|
||||
{ "ago", "", 3 },
|
||||
{ "", "do", 2 },
|
||||
{ "abc", "adc", 1 },
|
||||
{ "peek", "pesek", 1 },
|
||||
{ "sunday", "saturday", 3 }
|
||||
});
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package com.baeldung.algorithms.editdistance;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class EditDistanceTest extends EditDistanceDataProvider {
|
||||
|
||||
String x;
|
||||
String y;
|
||||
int result;
|
||||
|
||||
public EditDistanceTest(String a, String b, int res) {
|
||||
super();
|
||||
x = a;
|
||||
y = b;
|
||||
result = res;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEditDistance_RecursiveImplementation() {
|
||||
Assert.assertEquals(result, EditDistanceRecursive.calculate(x, y));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEditDistance_givenDynamicProgrammingImplementation() {
|
||||
Assert.assertEquals(result, EditDistanceDynamicProgramming.calculate(x, y));
|
||||
}
|
||||
}
|
@ -1 +1,3 @@
|
||||
## Relevant articles:
|
||||
### Relevant Articles:
|
||||
|
||||
- [A Guide to Java Bytecode Manipulation with ASM](http://www.baeldung.com/java-asm)
|
||||
|
@ -15,7 +15,7 @@ public class DeserializeWithNestedPropertiesUnitTest {
|
||||
private String SOURCE_JSON = "{\"id\":\"957c43f2-fa2e-42f9-bf75-6e3d5bb6960a\",\"name\":\"The Best Product\",\"brand\":{\"id\":\"9bcd817d-0141-42e6-8f04-e5aaab0980b6\",\"name\":\"ACME Products\",\"owner\":{\"id\":\"b21a80b1-0c09-4be3-9ebd-ea3653511c13\",\"name\":\"Ultimate Corp, Inc.\"}}}";
|
||||
|
||||
@Test
|
||||
public void whenUsingJacksonAnnotations_thenOk() throws IOException {
|
||||
public void whenUsingAnnotations_thenOk() throws IOException {
|
||||
Product product = new ObjectMapper().readerFor(Product.class)
|
||||
.readValue(SOURCE_JSON);
|
||||
|
||||
@ -25,7 +25,7 @@ public class DeserializeWithNestedPropertiesUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJacksonJsonNode_thenOk() throws IOException {
|
||||
public void whenUsingJsonNode_thenOk() throws IOException {
|
||||
JsonNode productNode = new ObjectMapper().readTree(SOURCE_JSON);
|
||||
|
||||
Product product = new Product();
|
||||
@ -47,7 +47,7 @@ public class DeserializeWithNestedPropertiesUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJacksonDeserializerManuallyRegistered_thenOk() throws IOException {
|
||||
public void whenUsingDeserializerManuallyRegistered_thenOk() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
SimpleModule module = new SimpleModule();
|
||||
module.addDeserializer(Product.class, new ProductDeserializer());
|
||||
@ -60,7 +60,7 @@ public class DeserializeWithNestedPropertiesUnitTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUsingJacksonDeserializerAutoRegistered_thenOk() throws IOException {
|
||||
public void whenUsingDeserializerAutoRegistered_thenOk() throws IOException {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
Product product = mapper.readValue(SOURCE_JSON, Product.class);
|
||||
assertEquals(product.getName(), "The Best Product");
|
||||
|
@ -113,6 +113,11 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
@ -180,6 +185,7 @@
|
||||
<!-- util -->
|
||||
<guava.version>21.0</guava.version>
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<assertj.version>3.8.0</assertj.version>
|
||||
|
||||
<httpcore.version>4.4.5</httpcore.version>
|
||||
<httpclient.version>4.5.2</httpclient.version>
|
||||
|
@ -0,0 +1,69 @@
|
||||
package org.baeldung.config;
|
||||
|
||||
import java.util.Properties;
|
||||
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.baeldung.extended.persistence.dao.ExtendedRepositoryImpl;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.orm.jpa.JpaTransactionManager;
|
||||
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
|
||||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.extended.persistence.dao", repositoryBaseClass = ExtendedRepositoryImpl.class)
|
||||
@PropertySource("persistence-student-h2.properties")
|
||||
@EnableTransactionManagement
|
||||
public class StudentJPAH2Config {
|
||||
|
||||
@Autowired
|
||||
private Environment env;
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
final DriverManagerDataSource dataSource = new DriverManagerDataSource();
|
||||
dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
|
||||
dataSource.setUrl(env.getProperty("jdbc.url"));
|
||||
dataSource.setUsername(env.getProperty("jdbc.user"));
|
||||
dataSource.setPassword(env.getProperty("jdbc.pass"));
|
||||
|
||||
return dataSource;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.inmemory.persistence.model" });
|
||||
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||
em.setJpaProperties(additionalProperties());
|
||||
return em;
|
||||
}
|
||||
|
||||
@Bean
|
||||
JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
|
||||
JpaTransactionManager transactionManager = new JpaTransactionManager();
|
||||
transactionManager.setEntityManagerFactory(entityManagerFactory);
|
||||
return transactionManager;
|
||||
}
|
||||
|
||||
final Properties additionalProperties() {
|
||||
final Properties hibernateProperties = new Properties();
|
||||
|
||||
hibernateProperties.setProperty("hibernate.hbm2ddl.auto", env.getProperty("hibernate.hbm2ddl.auto"));
|
||||
hibernateProperties.setProperty("hibernate.dialect", env.getProperty("hibernate.dialect"));
|
||||
hibernateProperties.setProperty("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
|
||||
hibernateProperties.setProperty("hibernate.cache.use_second_level_cache", env.getProperty("hibernate.cache.use_second_level_cache"));
|
||||
hibernateProperties.setProperty("hibernate.cache.use_query_cache", env.getProperty("hibernate.cache.use_query_cache"));
|
||||
|
||||
return hibernateProperties;
|
||||
}
|
||||
}
|
@ -18,7 +18,7 @@ import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.persistence.dao")
|
||||
@EnableJpaRepositories(basePackages = "org.baeldung.inmemory.persistence.dao")
|
||||
@PropertySource("persistence-student.properties")
|
||||
@EnableTransactionManagement
|
||||
public class StudentJpaConfig {
|
||||
@ -41,7 +41,7 @@ public class StudentJpaConfig {
|
||||
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
|
||||
final LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
|
||||
em.setDataSource(dataSource());
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });
|
||||
em.setPackagesToScan(new String[] { "org.baeldung.inmemory.persistence.model" });
|
||||
em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
|
||||
em.setJpaProperties(additionalProperties());
|
||||
return em;
|
||||
|
@ -0,0 +1,12 @@
|
||||
package org.baeldung.extended.persistence.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.repository.NoRepositoryBean;
|
||||
|
||||
@NoRepositoryBean
|
||||
public interface ExtendedRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
|
||||
public List<T> findByAttributeContainsText(String attributeName, String text);
|
||||
}
|
@ -0,0 +1,36 @@
|
||||
package org.baeldung.extended.persistence.dao;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.List;
|
||||
|
||||
import javax.persistence.EntityManager;
|
||||
import javax.persistence.TypedQuery;
|
||||
import javax.persistence.criteria.CriteriaBuilder;
|
||||
import javax.persistence.criteria.CriteriaQuery;
|
||||
import javax.persistence.criteria.Root;
|
||||
import javax.transaction.Transactional;
|
||||
|
||||
import org.springframework.data.jpa.repository.support.JpaEntityInformation;
|
||||
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
|
||||
|
||||
public class ExtendedRepositoryImpl<T, ID extends Serializable> extends SimpleJpaRepository<T, ID> implements ExtendedRepository<T, ID> {
|
||||
|
||||
private EntityManager entityManager;
|
||||
|
||||
public ExtendedRepositoryImpl(JpaEntityInformation<T, ?> entityInformation, EntityManager entityManager) {
|
||||
super(entityInformation, entityManager);
|
||||
this.entityManager = entityManager;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<T> findByAttributeContainsText(String attributeName, String text) {
|
||||
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
|
||||
CriteriaQuery<T> query = builder.createQuery(getDomainClass());
|
||||
Root<T> root = query.from(getDomainClass());
|
||||
query.select(root)
|
||||
.where(builder.like(root.<String> get(attributeName), "%" + text + "%"));
|
||||
TypedQuery<T> q = entityManager.createQuery(query);
|
||||
return q.getResultList();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,6 @@
|
||||
package org.baeldung.extended.persistence.dao;
|
||||
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
|
||||
public interface ExtendedStudentRepository extends ExtendedRepository<Student, Long> {
|
||||
}
|
@ -1,8 +1,7 @@
|
||||
package org.baeldung.persistence.dao;
|
||||
package org.baeldung.inmemory.persistence.dao;
|
||||
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
import org.baeldung.persistence.model.Student;
|
||||
|
||||
public interface StudentRepository extends JpaRepository<Student, Long> {
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package org.baeldung.persistence.model;
|
||||
package org.baeldung.inmemory.persistence.model;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.Id;
|
@ -0,0 +1,12 @@
|
||||
# jdbc.X
|
||||
jdbc.driverClassName=org.h2.Driver
|
||||
jdbc.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1
|
||||
jdbc.user=sa
|
||||
# jdbc.pass=
|
||||
|
||||
# hibernate.X
|
||||
hibernate.dialect=org.hibernate.dialect.H2Dialect
|
||||
hibernate.show_sql=true
|
||||
hibernate.hbm2ddl.auto=create-drop
|
||||
hibernate.cache.use_second_level_cache=false
|
||||
hibernate.cache.use_query_cache=false
|
@ -0,0 +1,39 @@
|
||||
package org.baeldung.persistence.repository;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
|
||||
import org.baeldung.config.StudentJPAH2Config;
|
||||
import org.baeldung.extended.persistence.dao.ExtendedStudentRepository;
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { StudentJPAH2Config.class})
|
||||
public class ExtendedStudentRepositoryIntegrationTest {
|
||||
@Resource
|
||||
private ExtendedStudentRepository extendedStudentRepository;
|
||||
|
||||
@Before
|
||||
public void setup(){
|
||||
Student student = new Student(1, "john");
|
||||
extendedStudentRepository.save(student);
|
||||
Student student2 = new Student(2, "johnson");
|
||||
extendedStudentRepository.save(student2);
|
||||
Student student3 = new Student(3, "tom");
|
||||
extendedStudentRepository.save(student3);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenStudents_whenFindByName_thenGetOk(){
|
||||
List<Student> students = extendedStudentRepository.findByAttributeContainsText("name", "john");
|
||||
assertThat(students.size()).isEqualTo(2);
|
||||
}
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
package org.baeldung.persistence.repository;
|
||||
|
||||
import org.baeldung.config.StudentJpaConfig;
|
||||
import org.baeldung.persistence.dao.StudentRepository;
|
||||
import org.baeldung.persistence.model.Student;
|
||||
import org.baeldung.inmemory.persistence.dao.StudentRepository;
|
||||
import org.baeldung.inmemory.persistence.model.Student;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
Loading…
x
Reference in New Issue
Block a user