Skip to content

Commit

Permalink
refactor(follow): primitive long을 걷어낸다 (#11)
Browse files Browse the repository at this point in the history
* test: long의 기본값은 0이 될 수 있으므로 위험하다

* refactor(follow): primitive long을 걷어낸다
  • Loading branch information
0chil authored Nov 13, 2024
1 parent acc6f0b commit bb1c838
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

import com.demo.feedsystemdesign.common.BaseTimeEntity;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Getter
@NoArgsConstructor
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Follow extends BaseTimeEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "follow_id")
private Long id;

private long sourceId;
private long targetId;
private Long sourceId;
private Long targetId;

public Follow(long sourceId, long targetId) {
public Follow(Long sourceId, Long targetId) {
this.sourceId = sourceId;
this.targetId = targetId;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,28 @@ public class FollowService {
private final FollowRepository followRepository;

@Transactional
public void follow(long sourceId, long targetId) {
public void follow(Long sourceId, Long targetId) {
validateExists(sourceId);
validateExists(targetId);

followRepository.save(new Follow(sourceId, targetId));
}

public List<Long> getFollowers(long userId) {
public List<Long> getFollowers(Long userId) {
return followRepository.findAllByTargetId(userId)
.stream()
.map(Follow::getSourceId)
.toList();
}

public List<Long> getFollowings(long userId) {
public List<Long> getFollowings(Long userId) {
return followRepository.findAllBySourceId(userId)
.stream()
.map(Follow::getTargetId)
.toList();
}

private void validateExists(long userId) {
private void validateExists(Long userId) {
userRepository.findById(userId)
.orElseThrow(() -> new NotFoundException(USER_NOT_FOUND));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.demo.feedsystemdesign.study;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
public class PrimitiveTypeTest {

@Test
void long은_지역_내에서_값을_할당하지_않으면_컴파일_에러가_발생한다() {
long a;

// System.out.println("a = " + a); // 에러 발생
}

@Test
void 그러나_객체의_필드로_long을_사용하면_0으로_초기화된다() {
class PrimitiveLongClass {
long a;
}

PrimitiveLongClass testInstance = new PrimitiveLongClass();

assertThat(testInstance.a).isZero();
// 0으로 초기화되는 것은 의도와 다르다.
// 더 정확한 제어를 위해서는 null이 들어가서 오류가 발생하는 편이 낫다.
}

@Test
void 따라서_0을_기본값으로_사용하고_싶은_것이_아니라면_Long이_더_안전할수도_있다() {
class ReferenceLongClass {
Long a;
}

ReferenceLongClass testInstance = new ReferenceLongClass();

assertThat(testInstance.a).isNull();
}
}

0 comments on commit bb1c838

Please sign in to comment.