Skip to content

Commit 0702320

Browse files
Merge branch 'master' of https://github.com/eugenp/tutorials into BAEL-1525-Check-if-a-String-is-a-palindrome
2 parents 9d9045f + 0743dec commit 0702320

14 files changed

Lines changed: 663 additions & 42 deletions

File tree

‎algorithms/src/main/java/com/baeldung/algorithms/sudoku/BacktrackingAlgorithm.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,4 +101,4 @@ private boolean checkConstraint(int[][] board, int r, boolean[] constraint, int
101101
}
102102
return true;
103103
}
104-
}
104+
}

‎algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinks.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ private int[][] parseBoard(List<DancingNode> answer) {
120120
}
121121

122122
private static void printSolution(int[][] result) {
123-
int N = result.length;
123+
int size = result.length;
124124
for (int[] aResult : result) {
125125
StringBuilder ret = new StringBuilder();
126-
for (int j = 0; j < N; j++) {
126+
for (int j = 0; j < size; j++) {
127127
ret.append(aResult[j]).append(" ");
128128
}
129129
System.out.println(ret);

‎algorithms/src/main/java/com/baeldung/algorithms/sudoku/DancingLinksAlgorithm.java‎

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -39,70 +39,83 @@ private int getIndex(int row, int col, int num) {
3939
}
4040

4141
private boolean[][] createExactCoverBoard() {
42-
boolean[][] R = new boolean[BOARD_SIZE * BOARD_SIZE * MAX_VALUE][BOARD_SIZE * BOARD_SIZE * CONSTRAINTS];
42+
boolean[][] coverBoard = new boolean[BOARD_SIZE * BOARD_SIZE * MAX_VALUE][BOARD_SIZE * BOARD_SIZE * CONSTRAINTS];
4343

4444
int hBase = 0;
45+
hBase = checkCellConstraint(coverBoard, hBase);
46+
hBase = checkRowConstraint(coverBoard, hBase);
47+
hBase = checkColumnConstraint(coverBoard, hBase);
48+
checkSubsectionConstraint(coverBoard, hBase);
49+
50+
return coverBoard;
51+
}
4552

46-
// Cell constraint.
47-
for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
48-
for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) {
49-
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) {
50-
int index = getIndex(r, c, n);
51-
R[index][hBase] = true;
53+
private int checkSubsectionConstraint(boolean[][] coverBoard, int hBase) {
54+
for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) {
55+
for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) {
56+
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
57+
for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) {
58+
for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) {
59+
int index = getIndex(br + rDelta, bc + cDelta, n);
60+
coverBoard[index][hBase] = true;
61+
}
62+
}
5263
}
5364
}
5465
}
66+
return hBase;
67+
}
5568

56-
// Row constrain.
57-
for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
69+
private int checkColumnConstraint(boolean[][] coverBoard, int hBase) {
70+
for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) {
5871
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
59-
for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) {
60-
int index = getIndex(r, c1, n);
61-
R[index][hBase] = true;
72+
for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) {
73+
int index = getIndex(r1, c, n);
74+
coverBoard[index][hBase] = true;
6275
}
6376
}
6477
}
78+
return hBase;
79+
}
6580

66-
// Column constraint.
67-
for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++) {
81+
private int checkRowConstraint(boolean[][] coverBoard, int hBase) {
82+
for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
6883
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
69-
for (int r1 = COVER_START_INDEX; r1 <= BOARD_SIZE; r1++) {
70-
int index = getIndex(r1, c, n);
71-
R[index][hBase] = true;
84+
for (int c1 = COVER_START_INDEX; c1 <= BOARD_SIZE; c1++) {
85+
int index = getIndex(r, c1, n);
86+
coverBoard[index][hBase] = true;
7287
}
7388
}
7489
}
90+
return hBase;
91+
}
7592

76-
// Subsection constraint
77-
for (int br = COVER_START_INDEX; br <= BOARD_SIZE; br += SUBSECTION_SIZE) {
78-
for (int bc = COVER_START_INDEX; bc <= BOARD_SIZE; bc += SUBSECTION_SIZE) {
79-
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++, hBase++) {
80-
for (int rDelta = 0; rDelta < SUBSECTION_SIZE; rDelta++) {
81-
for (int cDelta = 0; cDelta < SUBSECTION_SIZE; cDelta++) {
82-
int index = getIndex(br + rDelta, bc + cDelta, n);
83-
R[index][hBase] = true;
84-
}
85-
}
93+
private int checkCellConstraint(boolean[][] coverBoard, int hBase) {
94+
for (int r = COVER_START_INDEX; r <= BOARD_SIZE; r++) {
95+
for (int c = COVER_START_INDEX; c <= BOARD_SIZE; c++, hBase++) {
96+
for (int n = COVER_START_INDEX; n <= BOARD_SIZE; n++) {
97+
int index = getIndex(r, c, n);
98+
coverBoard[index][hBase] = true;
8699
}
87100
}
88101
}
89-
return R;
102+
return hBase;
90103
}
91104

92105
private boolean[][] initializeExactCoverBoard(int[][] board) {
93-
boolean[][] R = createExactCoverBoard();
106+
boolean[][] coverBoard = createExactCoverBoard();
94107
for (int i = COVER_START_INDEX; i <= BOARD_SIZE; i++) {
95108
for (int j = COVER_START_INDEX; j <= BOARD_SIZE; j++) {
96109
int n = board[i - 1][j - 1];
97110
if (n != NO_VALUE) {
98111
for (int num = MIN_VALUE; num <= MAX_VALUE; num++) {
99112
if (num != n) {
100-
Arrays.fill(R[getIndex(i, j, num)], false);
113+
Arrays.fill(coverBoard[getIndex(i, j, num)], false);
101114
}
102115
}
103116
}
104117
}
105118
}
106-
return R;
119+
return coverBoard;
107120
}
108121
}

‎javaxval/pom.xml‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
<version>0.1-SNAPSHOT</version>
77

88
<properties>
9-
<validation-api.version>2.0.0.Final</validation-api.version>
10-
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
9+
<validation-api.version>2.0.1.Final</validation-api.version>
10+
<hibernate-validator.version>6.0.7.Final</hibernate-validator.version>
1111
<javax.el-api.version>3.0.0</javax.el-api.version>
1212
<javax.el.version>2.2.6</javax.el.version>
13+
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
1314
</properties>
1415

1516

@@ -21,12 +22,6 @@
2122

2223
<dependencies>
2324

24-
<dependency>
25-
<groupId>javax.validation</groupId>
26-
<artifactId>validation-api</artifactId>
27-
<version>${validation-api.version}</version>
28-
</dependency>
29-
3025
<dependency>
3126
<groupId>org.hibernate</groupId>
3227
<artifactId>hibernate-validator</artifactId>
@@ -50,6 +45,16 @@
5045
<artifactId>javax.el</artifactId>
5146
<version>${javax.el.version}</version>
5247
</dependency>
48+
<dependency>
49+
<groupId>org.springframework</groupId>
50+
<artifactId>spring-context</artifactId>
51+
<version>${org.springframework.version}</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.springframework</groupId>
55+
<artifactId>spring-test</artifactId>
56+
<version>${org.springframework.version}</version>
57+
</dependency>
5358

5459
</dependencies>
5560

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.baeldung.javaxval.methodvalidation;
2+
3+
import org.baeldung.javaxval.methodvalidation.model.Customer;
4+
import org.baeldung.javaxval.methodvalidation.model.Reservation;
5+
import org.springframework.beans.factory.config.BeanDefinition;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.ComponentScan;
8+
import org.springframework.context.annotation.Configuration;
9+
import org.springframework.context.annotation.Scope;
10+
import org.springframework.validation.beanvalidation.MethodValidationPostProcessor;
11+
12+
import java.time.LocalDate;
13+
14+
@Configuration
15+
@ComponentScan({ "org.baeldung.javaxval.methodvalidation.model" })
16+
public class MethodValidationConfig {
17+
18+
@Bean
19+
public MethodValidationPostProcessor methodValidationPostProcessor() {
20+
return new MethodValidationPostProcessor();
21+
}
22+
23+
@Bean("customer")
24+
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
25+
public Customer customer(String firstName, String lastName) {
26+
27+
Customer customer = new Customer(firstName, lastName);
28+
return customer;
29+
}
30+
31+
@Bean("reservation")
32+
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
33+
public Reservation reservation(LocalDate begin, LocalDate end, Customer customer, int room) {
34+
35+
Reservation reservation = new Reservation(begin, end, customer, room);
36+
return reservation;
37+
}
38+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.baeldung.javaxval.methodvalidation.constraints;
2+
3+
import javax.validation.ConstraintValidator;
4+
import javax.validation.ConstraintValidatorContext;
5+
import javax.validation.constraintvalidation.SupportedValidationTarget;
6+
import javax.validation.constraintvalidation.ValidationTarget;
7+
import java.time.LocalDate;
8+
9+
@SupportedValidationTarget(ValidationTarget.PARAMETERS)
10+
public class ConsistentDateParameterValidator implements ConstraintValidator<ConsistentDateParameters, Object[]> {
11+
12+
@Override
13+
public boolean isValid(Object[] value, ConstraintValidatorContext context) {
14+
15+
if (value[0] == null || value[1] == null) {
16+
return true;
17+
}
18+
19+
if (!(value[0] instanceof LocalDate) || !(value[1] instanceof LocalDate)) {
20+
throw new IllegalArgumentException("Illegal method signature, expected two parameters of type LocalDate.");
21+
}
22+
23+
return ((LocalDate) value[0]).isAfter(LocalDate.now()) && ((LocalDate) value[0]).isBefore((LocalDate) value[1]);
24+
}
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.baeldung.javaxval.methodvalidation.constraints;
2+
3+
import javax.validation.Constraint;
4+
import javax.validation.Payload;
5+
import java.lang.annotation.Documented;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
9+
import static java.lang.annotation.ElementType.*;
10+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
11+
12+
@Constraint(validatedBy = ConsistentDateParameterValidator.class)
13+
@Target({ METHOD, CONSTRUCTOR })
14+
@Retention(RUNTIME)
15+
@Documented
16+
public @interface ConsistentDateParameters {
17+
18+
String message() default "End date must be after begin date and both must be in the future";
19+
20+
Class<?>[] groups() default {};
21+
22+
Class<? extends Payload>[] payload() default {};
23+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.baeldung.javaxval.methodvalidation.constraints;
2+
3+
import javax.validation.Constraint;
4+
import javax.validation.Payload;
5+
import java.lang.annotation.Documented;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.Target;
8+
9+
import static java.lang.annotation.ElementType.CONSTRUCTOR;
10+
import static java.lang.annotation.ElementType.METHOD;
11+
import static java.lang.annotation.RetentionPolicy.RUNTIME;
12+
13+
@Constraint(validatedBy = ValidReservationValidator.class)
14+
@Target({ METHOD, CONSTRUCTOR })
15+
@Retention(RUNTIME)
16+
@Documented
17+
public @interface ValidReservation {
18+
19+
String message() default "End date must be after begin date and both must be in the future, room number must be bigger than 0";
20+
21+
Class<?>[] groups() default {};
22+
23+
Class<? extends Payload>[] payload() default {};
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung.javaxval.methodvalidation.constraints;
2+
3+
import org.baeldung.javaxval.methodvalidation.model.Reservation;
4+
5+
import javax.validation.ConstraintValidator;
6+
import javax.validation.ConstraintValidatorContext;
7+
import java.time.LocalDate;
8+
9+
public class ValidReservationValidator implements ConstraintValidator<ValidReservation, Reservation> {
10+
11+
@Override
12+
public boolean isValid(Reservation reservation, ConstraintValidatorContext context) {
13+
14+
if (reservation == null) {
15+
return true;
16+
}
17+
18+
if (!(reservation instanceof Reservation)) {
19+
throw new IllegalArgumentException("Illegal method signature, expected parameter of type Reservation.");
20+
}
21+
22+
if (reservation.getBegin() == null || reservation.getEnd() == null || reservation.getCustomer() == null) {
23+
return false;
24+
}
25+
26+
return (reservation.getBegin()
27+
.isAfter(LocalDate.now())
28+
&& reservation.getBegin()
29+
.isBefore(reservation.getEnd())
30+
&& reservation.getRoom() > 0);
31+
}
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package org.baeldung.javaxval.methodvalidation.model;
2+
3+
import org.springframework.validation.annotation.Validated;
4+
5+
import javax.validation.constraints.NotNull;
6+
import javax.validation.constraints.Size;
7+
8+
@Validated
9+
public class Customer {
10+
11+
@Size(min = 5, max = 200)
12+
private String firstName;
13+
14+
@Size(min = 5, max = 200)
15+
private String lastName;
16+
17+
public Customer(@Size(min = 5, max = 200) @NotNull String firstName, @Size(min = 5, max = 200) @NotNull String lastName) {
18+
this.firstName = firstName;
19+
this.lastName = lastName;
20+
}
21+
22+
public Customer() {
23+
24+
}
25+
26+
public String getFirstName() {
27+
return firstName;
28+
}
29+
30+
public void setFirstName(String firstName) {
31+
this.firstName = firstName;
32+
}
33+
34+
public String getLastName() {
35+
return lastName;
36+
}
37+
38+
public void setLastName(String lastName) {
39+
this.lastName = lastName;
40+
}
41+
}

0 commit comments

Comments
 (0)