Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.baeldung.adapters;

import com.baeldung.domain.Friend;
import com.baeldung.ports.FriendDAOPort;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FriendDAOAdapter implements FriendDAOPort {
@Override
public List<Friend> list(Long id) {
// Call external component to query friends data and convert the entities to friends domain
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.baeldung.adapters;

import com.baeldung.domain.Friend;
import com.baeldung.ports.FriendUIPort;
import com.baeldung.service.FriendService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class FriendUIAdapter implements FriendUIPort {
@Resource
private FriendService friendService;

@Override
public Map<String, List<Friend>> getFriends(Long id) {
return friendService.friends(id);
}
}
49 changes: 49 additions & 0 deletions spring-boot-data/src/main/java/com/baeldung/domain/Friend.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.baeldung.domain;

public class Friend {
private Long id;
private String name;
private Integer age;
private Long updateTime;
private String group;

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;
}

public Integer getAge() {
return age;
}

public void setAge(Integer age) {
this.age = age;
}

public Long getUpdateTime() {
return updateTime;
}

public void setUpdateTime(Long updateTime) {
this.updateTime = updateTime;
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.ports;

import com.baeldung.domain.Friend;

import java.util.List;

public interface FriendDAOPort {
List<Friend> list(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baeldung.ports;

import com.baeldung.domain.Friend;

import java.util.List;
import java.util.Map;

public interface FriendUIPort {
Map<String, List<Friend>> getFriends(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.baeldung.service;

import com.baeldung.domain.Friend;
import com.baeldung.ports.FriendDAOPort;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

import static java.util.Comparator.comparingLong;
import static java.util.stream.Collectors.groupingBy;

@Service
public class FriendService {
@Resource
private FriendDAOPort friendDAOPort;

public Map<String, List<Friend>> friends(Long id) {
List<Friend> friends = friendDAOPort.list(id);
return friends.stream()
.sorted(comparingLong(Friend::getUpdateTime).reversed())
.collect(groupingBy(Friend::getGroup));
}
}