-
Notifications
You must be signed in to change notification settings - Fork 307
Expand file tree
/
Copy pathImage.java
More file actions
61 lines (52 loc) · 2.13 KB
/
Image.java
File metadata and controls
61 lines (52 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package nextstep.sessions.domain;
public class Image {
private static final Long MAX_SIZE_BYTES = 1024 * 1024L;
private static final Float MIN_SIZE_WIDTH = 300F;
private static final Float MIN_SIZE_HEIGHT = 200F;
private static final Float ASPECT_RATIO_TOLERANCE = 0.01f;
private Long id;
private Long sizeInBytes;
private String title;
private ImageType type;
private Float width;
private Float height;
public Image(Long sizeInBytes, String title, ImageType type, Float width, Float height) {
this(sizeInBytes, 0L, title, type, width, height);
}
public Image(Long sizeInBytes, Long id, String title, ImageType type, Float width, Float height) {
validate(sizeInBytes, title, type, width, height);
this.sizeInBytes = sizeInBytes;
this.id = id;
this.title = title;
this.type = type;
this.width = width;
this.height = height;
}
private void validate(Long sizeInBytes, String title, ImageType type, Float width, Float height) {
validateSize(sizeInBytes);
validateWidth(width);
validateHeight(height);
validateAspectRatio(width, height);
}
private void validateSize(Long sizeInBytes) {
if (sizeInBytes > MAX_SIZE_BYTES) {
throw new IllegalArgumentException("이미지 크기는 최대 1MB 이하여야 합니다.");
}
}
private void validateWidth(Float width) {
if (width < MIN_SIZE_WIDTH) {
throw new IllegalArgumentException("이미지 가로 길이는 최소 300픽셀 이상이어야 합니다.");
}
}
private void validateHeight(Float height) {
if (height < MIN_SIZE_HEIGHT) {
throw new IllegalArgumentException("이미지 세로 길이는 최소 200픽셀 이상이어야 합니다.");
}
}
private void validateAspectRatio(float width, float height) {
float ratio = width / height;
if (Math.abs(ratio - (MIN_SIZE_WIDTH / MIN_SIZE_HEIGHT)) > ASPECT_RATIO_TOLERANCE) {
throw new IllegalArgumentException("이미지 비율은 3:2이어야 합니다.");
}
}
}