public class User { private final Long id; private final String name; private final String email; private final String phone; private final String department; private final Set roles; private User(Builder builder) { this.id = builder.id; this.name = builder.name; this.email = builder.email; this.phone = builder.phone; this.department = builder.department; this.roles = Collections.unmodifiableSet(builder.roles); } // Getters public Long getId() { return id; } public String getName() { return name; } public String getEmail() { return email; } public String getPhone() { return phone; } public String getDepartment() { return department; } public Set getRoles() { return roles; } public static Builder builder() { return new Builder(); } public static class Builder { private Long id; private String name; private String email; private String phone; private String department; private Set roles = new HashSet<>(); public Builder id(Long id) { this.id = id; return this; } public Builder name(String name) { this.name = Objects.requireNonNull(name, "Name cannot be null"); return this; } public Builder email(String email) { this.email = Objects.requireNonNull(email, "Email cannot be null"); return this; } public Builder phone(String phone) { this.phone = phone; return this; } public Builder department(String department) { this.department = department; return this; } public Builder role(String role) { this.roles.add(role); return this; } public Builder roles(Set roles) { this.roles.addAll(roles); return this; } public User build() { // Validation if (name == null || name.isBlank()) { throw new IllegalStateException("Name is required"); } if (email == null || !email.contains("@")) { throw new IllegalStateException("Valid email is required"); } return new User(this); } } @Override public String toString() { return "User{id=%d, name='%s', email='%s', roles=%s}" .formatted(id, name, email, roles); } // Kullanım: // User user = User.builder() // .id(1L) // .name("Tolgahan Gencer") // .email("tolgahan@example.com") // .department("Engineering") // .role("ADMIN") // .role("USER") // .build(); }