Skip to content

Commit 348edfb

Browse files
committed
Merge branch 'master' into stream-foreach-ifelse-logic
2 parents c1fce0b + 738d65b commit 348edfb

25 files changed

Lines changed: 761 additions & 24 deletions

File tree

‎core-java-collections/src/main/java/com/baeldung/performance/Employee.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,12 @@ public int hashCode() {
4444
result = 31 * result + name.hashCode();
4545
return result;
4646
}
47+
48+
@Override
49+
public String toString() {
50+
return "Employee{" +
51+
"id=" + id +
52+
", name='" + name + '\'' +
53+
'}';
54+
}
4755
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.sort;
2+
3+
public class Employee implements Comparable<Employee> {
4+
5+
private Long id;
6+
private String name;
7+
8+
public Employee(Long id, String name) {
9+
this.name = name;
10+
this.id = id;
11+
}
12+
13+
public Long getId() {
14+
return id;
15+
}
16+
17+
public void setId(Long id) {
18+
this.id = id;
19+
}
20+
21+
public String getName() {
22+
return name;
23+
}
24+
25+
public void setName(String name) {
26+
this.name = name;
27+
}
28+
29+
@Override
30+
public boolean equals(Object o) {
31+
if (this == o) return true;
32+
if (o == null || getClass() != o.getClass()) return false;
33+
34+
Employee employee = (Employee) o;
35+
36+
if (!id.equals(employee.id)) return false;
37+
return name.equals(employee.name);
38+
39+
}
40+
41+
@Override
42+
public int hashCode() {
43+
int result = id.hashCode();
44+
result = 31 * result + name.hashCode();
45+
return result;
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return "Employee{" +
51+
"id=" + id +
52+
", name='" + name + '\'' +
53+
'}';
54+
}
55+
56+
@Override
57+
public int compareTo(Employee employee) {
58+
return (int)(this.id - employee.getId());
59+
}
60+
}
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
package com.baeldung.sort;
2+
3+
import com.google.common.base.Functions;
4+
import com.google.common.collect.ImmutableSortedMap;
5+
import com.google.common.collect.Lists;
6+
import com.google.common.collect.Ordering;
7+
8+
import java.util.*;
9+
import java.util.stream.Collectors;
10+
11+
public class SortHashMap {
12+
13+
private static Map<String, Employee> map = new HashMap<>();
14+
15+
public static void main(String[] args) {
16+
17+
initialize();
18+
19+
treeMapSortByKey();
20+
21+
arrayListSortByValue();
22+
arrayListSortByKey();
23+
24+
sortStream();
25+
26+
sortGuava();
27+
28+
addDuplicates();
29+
30+
treeSetByKey();
31+
treeSetByValue();
32+
33+
}
34+
35+
private static void sortGuava() {
36+
final Ordering naturalOrdering =
37+
Ordering.natural().onResultOf(Functions.forMap(map, null));
38+
39+
System.out.println(ImmutableSortedMap.copyOf(map, naturalOrdering));
40+
}
41+
42+
private static void sortStream() {
43+
map.entrySet().stream()
44+
.sorted(Map.Entry.<String, Employee>comparingByKey().reversed())
45+
.forEach(System.out::println);
46+
47+
Map<String, Employee> result = map.entrySet().stream()
48+
.sorted(Map.Entry.comparingByValue())
49+
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue,
50+
(oldValue, newValue) -> oldValue, LinkedHashMap::new));
51+
52+
result.entrySet().forEach(System.out::println);
53+
}
54+
55+
private static void treeSetByValue() {
56+
SortedSet<Employee> values = new TreeSet<>(map.values());
57+
System.out.println(values);
58+
}
59+
60+
private static void treeSetByKey() {
61+
SortedSet<String> keysSet = new TreeSet<>(map.keySet());
62+
System.out.println(keysSet);
63+
}
64+
65+
private static void treeMapSortByKey() {
66+
TreeMap<String, Employee> sorted = new TreeMap<>(map);
67+
sorted.putAll(map);
68+
69+
sorted.entrySet().forEach(System.out::println);
70+
71+
}
72+
73+
private static void arrayListSortByValue() {
74+
List<Employee> employeeById = new ArrayList<>(map.values());
75+
76+
Collections.sort(employeeById);
77+
78+
System.out.println(employeeById);
79+
}
80+
81+
private static void arrayListSortByKey() {
82+
List<String> employeeByKey = new ArrayList<>(map.keySet());
83+
Collections.sort(employeeByKey);
84+
System.out.println(employeeByKey);
85+
}
86+
87+
private static void initialize() {
88+
Employee employee1 = new Employee(1L, "Mher");
89+
map.put(employee1.getName(), employee1);
90+
Employee employee2 = new Employee(22L, "Annie");
91+
map.put(employee2.getName(), employee2);
92+
Employee employee3 = new Employee(8L, "John");
93+
map.put(employee3.getName(), employee3);
94+
Employee employee4 = new Employee(2L, "George");
95+
map.put(employee4.getName(), employee4);
96+
}
97+
98+
private static void addDuplicates() {
99+
Employee employee5 = new Employee(1L, "Mher");
100+
map.put(employee5.getName(), employee5);
101+
Employee employee6 = new Employee(22L, "Annie");
102+
map.put(employee6.getName(), employee6);
103+
}
104+
}

‎libraries-security/pom.xml‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,30 @@
88

99
<parent>
1010
<groupId>com.baeldung</groupId>
11-
<artifactId>parent-modules</artifactId>
12-
<version>1.0.0-SNAPSHOT</version>
11+
<artifactId>parent-boot-1</artifactId>
12+
<version>0.0.1-SNAPSHOT</version>
13+
<relativePath>../parent-boot-1</relativePath>
1314
</parent>
1415

1516
<dependencies>
1617

18+
<dependency>
19+
<groupId>org.springframework.boot</groupId>
20+
<artifactId>spring-boot-starter-web</artifactId>
21+
</dependency>
22+
23+
<dependency>
24+
<groupId>org.springframework.security.oauth</groupId>
25+
<artifactId>spring-security-oauth2</artifactId>
26+
<version>2.3.3.RELEASE</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>com.github.scribejava</groupId>
31+
<artifactId>scribejava-apis</artifactId>
32+
<version>${scribejava.version}</version>
33+
</dependency>
34+
1735
<dependency>
1836
<groupId>junit</groupId>
1937
<artifactId>junit</artifactId>
@@ -25,6 +43,9 @@
2543

2644
<properties>
2745
<junit.version>4.12</junit.version>
46+
<spring-boot-maven-plugin.version>2.0.4.RELEASE</spring-boot-maven-plugin.version>
47+
<scribejava.version>5.6.0</scribejava.version>
2848
</properties>
2949

50+
3051
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.scribejava;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
7+
@SpringBootApplication
8+
public class ScribejavaApplication {
9+
10+
public static void main(String[] args) {
11+
SpringApplication.run(ScribejavaApplication.class, args);
12+
}
13+
14+
15+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.baeldung.scribejava.api;
2+
3+
import com.github.scribejava.core.builder.api.DefaultApi20;
4+
5+
public class MyApi extends DefaultApi20 {
6+
7+
private MyApi() {
8+
}
9+
10+
private static class InstanceHolder {
11+
private static final MyApi INSTANCE = new MyApi();
12+
}
13+
14+
public static MyApi instance() {
15+
return InstanceHolder.INSTANCE;
16+
}
17+
18+
@Override
19+
public String getAccessTokenEndpoint() {
20+
return "http://localhost:8080/oauth/token";
21+
}
22+
23+
@Override
24+
protected String getAuthorizationBaseUrl() {
25+
return null;
26+
}
27+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.scribejava.controller;
2+
3+
import com.baeldung.scribejava.service.GoogleService;
4+
import com.github.scribejava.core.model.OAuth2AccessToken;
5+
import com.github.scribejava.core.model.OAuthRequest;
6+
import com.github.scribejava.core.model.Response;
7+
import com.github.scribejava.core.model.Verb;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.RequestParam;
11+
import org.springframework.web.bind.annotation.RestController;
12+
13+
import javax.servlet.http.HttpServletResponse;
14+
15+
@RestController
16+
public class GoogleController {
17+
18+
@Autowired
19+
private GoogleService service;
20+
21+
22+
@GetMapping(value ="/me/google")
23+
public void me(HttpServletResponse response){
24+
String auth = service.getService().getAuthorizationUrl();
25+
26+
response.setHeader("Location", auth);
27+
response.setStatus(302);
28+
29+
}
30+
31+
@GetMapping(value = "/auth/google")
32+
public String google(@RequestParam String code, HttpServletResponse servletResponse){
33+
34+
try {
35+
OAuth2AccessToken token = service.getService().getAccessToken(code);
36+
37+
OAuthRequest request = new OAuthRequest(Verb.GET, "https://www.googleapis.com/oauth2/v1/userinfo?alt=json");
38+
service.getService().signRequest(token, request);
39+
Response response = service.getService().execute(request);
40+
return response.getBody();
41+
42+
}catch (Exception e){
43+
servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
44+
}
45+
46+
return null;
47+
}
48+
49+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.baeldung.scribejava.controller;
2+
3+
import com.baeldung.scribejava.service.TwitterService;
4+
import com.github.scribejava.core.model.*;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
import javax.servlet.http.HttpServletResponse;
10+
import java.io.IOException;
11+
import java.util.Scanner;
12+
import java.util.concurrent.ExecutionException;
13+
14+
@RestController
15+
public class TwitterController {
16+
17+
@Autowired
18+
private TwitterService service;
19+
20+
21+
@GetMapping(value ="/me/twitter")
22+
public String me(HttpServletResponse servletResponse){
23+
try {
24+
OAuth1RequestToken requestToken = service.getService().getRequestToken();
25+
26+
String auth = service.getService().getAuthorizationUrl(requestToken);
27+
28+
Runtime runtime = Runtime.getRuntime();
29+
try {
30+
runtime.exec("rundll32 url.dll,FileProtocolHandler " + auth);
31+
} catch (IOException e) {
32+
servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
33+
return null;
34+
}
35+
36+
System.out.println("Insert twitter code:");
37+
Scanner in = new Scanner(System.in);
38+
39+
String oauthverifier = in.nextLine();
40+
41+
final OAuth1AccessToken accessToken = service.getService().getAccessToken(requestToken,oauthverifier);
42+
43+
OAuthRequest request = new OAuthRequest(Verb.GET, "https://api.twitter.com/1.1/account/verify_credentials.json");
44+
service.getService().signRequest(accessToken, request);
45+
Response response = service.getService().execute(request);
46+
return response.getBody();
47+
48+
} catch (IOException | InterruptedException | ExecutionException e) {
49+
servletResponse.setStatus(HttpServletResponse.SC_BAD_REQUEST);
50+
}
51+
52+
return null;
53+
}
54+
55+
56+
57+
}

0 commit comments

Comments
 (0)