Skip to content

Commit cbadb6a

Browse files
committed
add images as not required to tweet domain
1 parent d88f33c commit cbadb6a

10 files changed

Lines changed: 777 additions & 3 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.mastercloudapps.twitterscheduler.domain.tweet;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import java.util.Objects;
6+
7+
import com.mastercloudapps.twitterscheduler.domain.shared.ValueObject;
8+
9+
public class Height implements ValueObject {
10+
11+
private static final long serialVersionUID = -6422141827884275696L;
12+
13+
private final Integer height;
14+
15+
private Height(final Integer height) {
16+
this.height = requireNonNull(height, "Height cannot be null.");
17+
}
18+
19+
public static Height valueOf(final Integer height) {
20+
return new Height(height);
21+
}
22+
23+
public Integer height() {
24+
return height;
25+
}
26+
27+
@Override
28+
public boolean equals(Object o) {
29+
if (this == o) {
30+
return true;
31+
}
32+
if (o == null || getClass() != o.getClass()) {
33+
return false;
34+
}
35+
Height that = (Height) o;
36+
return Objects.equals(height, that.height);
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return Objects.hash(height);
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "Height{height='" + height + '\'' + '}';
47+
}
48+
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.mastercloudapps.twitterscheduler.domain.tweet;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import java.util.Objects;
6+
7+
import com.mastercloudapps.twitterscheduler.domain.shared.ValueObject;
8+
9+
public class Size implements ValueObject {
10+
11+
private static final long serialVersionUID = -6422141827884275696L;
12+
13+
private final Long size;
14+
15+
private Size(final Long size) {
16+
this.size = requireNonNull(size, "Size cannot be null.");
17+
}
18+
19+
public static Size valueOf(final Long size) {
20+
return new Size(size);
21+
}
22+
23+
public Long size() {
24+
return size;
25+
}
26+
27+
@Override
28+
public boolean equals(Object o) {
29+
if (this == o) {
30+
return true;
31+
}
32+
if (o == null || getClass() != o.getClass()) {
33+
return false;
34+
}
35+
Size that = (Size) o;
36+
return Objects.equals(size, that.size);
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return Objects.hash(size);
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "Size{size='" + size + '\'' + '}';
47+
}
48+
49+
}

src/main/java/com/mastercloudapps/twitterscheduler/domain/tweet/Tweet.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33
import static java.util.Objects.requireNonNull;
44

55
import java.time.Instant;
6+
import java.util.ArrayList;
7+
import java.util.Arrays;
8+
import java.util.Collection;
9+
import java.util.Collections;
10+
import java.util.List;
611
import java.util.Objects;
12+
import java.util.stream.Collectors;
713

814
import com.mastercloudapps.twitterscheduler.domain.shared.AggregateRoot;
915
import com.mastercloudapps.twitterscheduler.domain.shared.Message;
@@ -25,6 +31,8 @@ public class Tweet extends AggregateRoot<TweetId> {
2531
private NullableInstant createdAt;
2632

2733
private PublicationType publicationType;
34+
35+
private final List<TweetImage> images;
2836

2937
private Tweet(final Builder builder) {
3038
super(builder.tweetId);
@@ -34,6 +42,7 @@ private Tweet(final Builder builder) {
3442
this.publishedAt = builder.publishedAt;
3543
this.createdAt = builder.createdAt;
3644
this.publicationType = builder.publicationType;
45+
this.images = builder.images;
3746
}
3847

3948
public Message message() {
@@ -62,7 +71,7 @@ public NullableInstant createdAt() {
6271
}
6372

6473
public PublicationType publicationType() {
65-
74+
6675
return publicationType;
6776
}
6877

@@ -102,15 +111,41 @@ public interface CreatedAtStep {
102111
}
103112

104113
public interface PublicationTypeStep {
105-
114+
106115
Build publicationType(PublicationType publicationType);
107116
}
108117

109118
public interface Build {
110119

120+
Build images(List<TweetImage> images);
121+
111122
Tweet build();
112123
}
113124

125+
public void addImages(TweetImage... images) {
126+
this.images.addAll(Arrays.stream(images).filter(Objects::nonNull).collect(Collectors.toSet()));
127+
}
128+
129+
public void addImages(Collection<TweetImage> images) {
130+
this.images.addAll(images.stream().filter(Objects::nonNull).collect(Collectors.toSet()));
131+
}
132+
133+
public void deleteImages(TweetImage... images) {
134+
this.images.removeAll(Arrays.stream(images).filter(Objects::nonNull).collect(Collectors.toSet()));
135+
}
136+
137+
public void deleteImages(Collection<TweetImage> images) {
138+
this.images.removeAll(images.stream().filter(Objects::nonNull).collect(Collectors.toSet()));
139+
}
140+
141+
public void deleteImages() {
142+
this.images.clear();
143+
}
144+
145+
public List<TweetImage> getImages() {
146+
return Collections.unmodifiableList(images);
147+
}
148+
114149
public static class Builder implements IdStep, MessageStep, UrlStep,
115150
RequestedPublicationDateStep, PublishedAtStep, CreatedAtStep, PublicationTypeStep, Build {
116151

@@ -127,6 +162,8 @@ public static class Builder implements IdStep, MessageStep, UrlStep,
127162
private NullableInstant createdAt;
128163

129164
private PublicationType publicationType;
165+
166+
private List<TweetImage> images = new ArrayList<>();
130167

131168
@Override
132169
public MessageStep id(Long tweetId) {
@@ -178,6 +215,12 @@ public Build publicationType(PublicationType publicationType) {
178215
public Tweet build() {
179216
return new Tweet(this);
180217
}
218+
219+
@Override
220+
public Build images(List<TweetImage> images) {
221+
this.images = Objects.requireNonNull(images);
222+
return this;
223+
}
181224
}
182225

183226
}
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package com.mastercloudapps.twitterscheduler.domain.tweet;
2+
3+
import static java.util.Objects.requireNonNull;
4+
5+
import java.util.Objects;
6+
7+
import com.mastercloudapps.twitterscheduler.domain.shared.Entity;
8+
9+
public class TweetImage extends Entity<TweetImageId> {
10+
11+
private static final long serialVersionUID = -6578177746889786426L;
12+
13+
private Size size;
14+
15+
private Type type;
16+
17+
private Width width;
18+
19+
private Height height;
20+
21+
private TweetImage(Builder builder) {
22+
super(builder.tweetImageId);
23+
this.size = builder.size;
24+
this.type = builder.type;
25+
this.width = builder.width;
26+
this.height = builder.height;
27+
}
28+
29+
public Size size() {
30+
return size;
31+
}
32+
33+
public Type type() {
34+
return type;
35+
}
36+
37+
public Width width() {
38+
return width;
39+
}
40+
41+
public Height height() {
42+
return height;
43+
}
44+
45+
public static IdStep builder() {
46+
return new Builder();
47+
}
48+
49+
public interface IdStep {
50+
51+
SizeStep id(Long tweetImageId);
52+
}
53+
54+
public interface SizeStep {
55+
56+
TypeStep size(Long size);
57+
}
58+
59+
public interface TypeStep {
60+
61+
WidthStep type(String type);
62+
}
63+
64+
public interface WidthStep {
65+
66+
HeightStep width(Integer width);
67+
}
68+
69+
public interface HeightStep {
70+
71+
Build height(Integer height);
72+
}
73+
74+
public interface Build {
75+
76+
TweetImage build();
77+
}
78+
79+
public static class Builder implements IdStep, SizeStep, TypeStep, WidthStep, HeightStep, Build {
80+
81+
private TweetImageId tweetImageId;
82+
83+
private Size size;
84+
85+
private Type type;
86+
87+
private Width width;
88+
89+
private Height height;
90+
91+
@Override
92+
public SizeStep id(Long tweetImageId) {
93+
this.tweetImageId = TweetImageId.valueOf(requireNonNull(tweetImageId, "Tweet Image Id cannot be null."));
94+
return this;
95+
}
96+
97+
@Override
98+
public TypeStep size(Long size) {
99+
this.size = Size.valueOf(requireNonNull(size, "Size cannot be null."));
100+
return this;
101+
}
102+
103+
@Override
104+
public WidthStep type(String type) {
105+
this.type = Type.valueOf(requireNonNull(type, "Type cannot be null."));
106+
return this;
107+
}
108+
109+
@Override
110+
public HeightStep width(Integer width) {
111+
this.width = Width.valueOf(requireNonNull(width, "Width cannot be null."));
112+
return this;
113+
}
114+
115+
@Override
116+
public Build height(Integer height) {
117+
this.height = Height.valueOf(requireNonNull(height, "Height cannot be null."));
118+
return this;
119+
}
120+
121+
@Override
122+
public TweetImage build() {
123+
return new TweetImage(this);
124+
}
125+
}
126+
127+
@Override
128+
public boolean equals(Object o) {
129+
if (this == o) {
130+
return true;
131+
}
132+
if (o == null || getClass() != o.getClass()) {
133+
return false;
134+
}
135+
if (!super.equals(o)) {
136+
return false;
137+
}
138+
TweetImage that = (TweetImage) o;
139+
return Objects.equals(size, that.size)
140+
&& Objects.equals(type, that.type)
141+
&& Objects.equals(width, that.width)
142+
&& Objects.equals(height, that.height);
143+
}
144+
145+
@Override
146+
public int hashCode() {
147+
return Objects.hash(super.hashCode(), size, type, width, height);
148+
}
149+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.mastercloudapps.twitterscheduler.domain.tweet;
2+
3+
import java.util.Objects;
4+
5+
import com.mastercloudapps.twitterscheduler.domain.shared.id.DomainObjectId;
6+
7+
public class TweetImageId extends DomainObjectId<Long> {
8+
9+
private static final long serialVersionUID = -961510813076429288L;
10+
11+
public TweetImageId(Long id) {
12+
super(id);
13+
}
14+
15+
public static TweetImageId valueOf(final Long id) {
16+
return new TweetImageId(id);
17+
}
18+
19+
@Override
20+
public String toString() {
21+
return "TweetImageId{" + this.id + "}";
22+
}
23+
24+
@Override
25+
public boolean equals(final Object o) {
26+
if (this == o) {
27+
return true;
28+
}
29+
if (o == null || getClass() != o.getClass()) {
30+
return false;
31+
}
32+
TweetImageId that = (TweetImageId) o;
33+
return Objects.equals(id, that.id);
34+
}
35+
36+
@Override
37+
public int hashCode() {
38+
return Objects.hash(id);
39+
}
40+
41+
}

0 commit comments

Comments
 (0)