型
eunm
例えばこんなものをモデルクラスに用意
eunmは選択肢のリストを作成している。
変数に入れる値を厳格にしたいときに便利。
public enum Type {
ALL, PERSONAL, CUSTOM
}
private final Type type; //finalで不変に。データを保存する変数。
変数と引数
引数と変数で渡す違い
変数を使う場合は変数がこの処理のあとも使われる場合は定義しても良さげ。
一度しか使わないなら直接引数で渡したほうがすっきりする
# 引数で渡す
return transactionManager.executeInTransaction(conn -> {
if (communityRepository.existsByName(conn, name)) {
throw new DomainException("Community name already exists");
}else{
UUID communityId = UUID.randomUUID();
LocalDateTime now = LocalDateTime.now();
Community community = new Community(communityId, name, Community.Type.CUSTOM, allowFreeEdit, now);
communityRepository.save(conn, community);
CommunityMember adminMember = new CommunityMember(
communityId,
creatorUserId,
CommunityMember.Role.ADMIN,
CommunityMember.Status.ACTIVE
now
);
communityRepository.addMember(conn, adminMember);
return community;
}
});
# 変数を定義して渡す
CommunityMember.Status status = CommunityMember.Status.ACTIVE;
return transactionManager.executeInTransaction(conn -> {
if (communityRepository.existsByName(conn, name)) {
throw new DomainException("Community name already exists");
}else{
UUID communityId = UUID.randomUUID();
LocalDateTime now = LocalDateTime.now();
Community community = new Community(communityId, name, Community.Type.CUSTOM, allowFreeEdit, now);
communityRepository.save(conn, community);
CommunityMember adminMember = new CommunityMember(
communityId,
creatorUserId,
CommunityMember.Role.ADMIN,
status
now
);
communityRepository.addMember(conn, adminMember);
return community;
}
});
レコードとインターフェイスの違い
レコードを使える場面
public class CommunityMember {
private final UUID communityId;
private final UUID userId;
public CommunityMember(UUID communityId, UUID userId) {
this.communityId = communityId;
this.userId = userId;
}
public UUID getCommunityId() { return communityId; }
public UUID getUserId() { return userId; }
}
レコードを使うことで..短くできる
public record CommunityMember(
UUID communityId,
UUID userId
)
呼出側のメソッドを修正する必要がある
@Override
public void addMember(CommunityMember member) {
String sql = "INSERT INTO community_members (community_id, user_id) VALUES (?, ?, ?, ?, ?)";
jdbcTemplate.update(
sql,
member.getCommunityId(),
member.getUserId(),
);
}
修正後
@Override
public void addMember(CommunityMember member) {
String sql = "INSERT INTO community_members (community_id, user_id) VALUES (?, ?, ?, ?, ?)";
jdbcTemplate.update(
sql,
member.communityId(),
member.userId(),
);
}
root@backend_container:/workspaces# cloc app/src/main/java/com/example/app
26 text files.
26 unique files.
0 files ignored.
github.com/AlDanial/cloc v 2.06 T=0.08 s (324.3 files/s, 24485.5 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
Java 26 104 213 1646
-------------------------------------------------------------------------------
SUM: 26 104 213 1646
-------------------------------------------------------------------------------
Java 19 93 84 730
Kotlin 2 16 11 221
-------------------------------------------------------------------------------
SUM: 21 109 95 951
-------------------------------------------------------------------------------
The method save(Article) of type JdbcArticleRepository must override or implement a supertype method
Null type safety: parameter 'this' provided via method descriptor Function<Tag,UUID>.apply(Tag) needs unchecked conversion to conform to '@NonNull Tag'
List<UUID> tagIds = tags.stream()
.map(Tag::id)
.toList();
tagRepository.unlinkArticleTags(articleId);
tagRepository.linkArticleTags(articleId, tagIds);
return tags;
# 修正後
List<UUID> tagIds = tags.stream()
.filter(Objects::nonNull) // null要素の除外
.map(tag -> tag.id()) // メソッド参照からラムダ式へ変更
.filter(Objects::nonNull) // 万が一IDがnullの場合の除外
.toList();
tagRepository.unlinkArticleTags(articleId);
tagRepository.linkArticleTags(articleId, tagIds);
return tags;