1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.handler.media;
21
22 import java.awt.Rectangle;
23 import java.awt.geom.Rectangle2D;
24
25 import org.apache.commons.lang3.StringUtils;
26 import org.apache.commons.lang3.builder.EqualsBuilder;
27 import org.apache.commons.lang3.builder.HashCodeBuilder;
28 import org.apache.commons.lang3.builder.ToStringBuilder;
29 import org.apache.commons.lang3.builder.ToStringStyle;
30 import org.apache.commons.lang3.math.NumberUtils;
31 import org.jetbrains.annotations.NotNull;
32 import org.osgi.annotation.versioning.ProviderType;
33
34
35
36
37 @ProviderType
38 public final class CropDimension extends Dimension {
39
40 private final long left;
41 private final long top;
42 private final boolean autoCrop;
43
44
45
46
47
48
49
50 public CropDimension(long left, long top, long width, long height) {
51 this(left, top, width, height, false);
52 }
53
54
55
56
57
58
59
60
61 public CropDimension(long left, long top, long width, long height, boolean autoCrop) {
62 super(width, height);
63 this.left = left;
64 this.top = top;
65 this.autoCrop = autoCrop;
66 }
67
68
69
70
71 public long getLeft() {
72 return this.left;
73 }
74
75
76
77
78 public long getTop() {
79 return this.top;
80 }
81
82
83
84
85 public long getRight() {
86 return getLeft() + getWidth();
87 }
88
89
90
91
92 public long getBottom() {
93 return getTop() + getHeight();
94 }
95
96
97
98
99 public boolean isAutoCrop() {
100 return this.autoCrop;
101 }
102
103 @Override
104 public int hashCode() {
105 return HashCodeBuilder.reflectionHashCode(this);
106 }
107
108 @Override
109 public boolean equals(Object obj) {
110 return EqualsBuilder.reflectionEquals(this, obj);
111 }
112
113 @Override
114 public String toString() {
115 return new ToStringBuilder(this, ToStringStyle.NO_CLASS_NAME_STYLE)
116 .append("left", getLeft())
117 .append("top", getTop())
118 .append("width", getWidth())
119 .append("height", getHeight())
120 .build();
121 }
122
123
124
125
126 public @NotNull String getCropString() {
127 return getLeft() + "," + getTop() + "," + getRight() + "," + getBottom();
128 }
129
130
131
132
133 public @NotNull String getCropStringWidthHeight() {
134 return getLeft() + "," + getTop() + "," + getWidth() + "," + getHeight();
135 }
136
137
138
139
140 public @NotNull Rectangle2D getRectangle() {
141 return new Rectangle((int)getLeft(), (int)getTop(), (int)getWidth(), (int)getHeight());
142 }
143
144
145
146
147
148
149
150
151 @SuppressWarnings("java:S1192")
152 public static @NotNull CropDimension fromCropString(@NotNull String cropString) {
153 if (StringUtils.isEmpty(cropString)) {
154 throw new IllegalArgumentException("Invalid crop string: '" + cropString + "'.");
155 }
156
157
158 String crop = cropString;
159 if (StringUtils.contains(crop, "/")) {
160 crop = StringUtils.substringBefore(crop, "/");
161 }
162
163 String[] parts = StringUtils.split(crop, ",");
164 if (parts.length != 4) {
165 throw new IllegalArgumentException("Invalid crop string: '" + cropString + "'.");
166 }
167 long x1 = NumberUtils.toLong(parts[0]);
168 long y1 = NumberUtils.toLong(parts[1]);
169 long x2 = NumberUtils.toLong(parts[2]);
170 long y2 = NumberUtils.toLong(parts[3]);
171 long width = x2 - x1;
172 long height = y2 - y1;
173 if (x1 < 0 || y1 < 0 || width <= 0 || height <= 0) {
174 throw new IllegalArgumentException("Invalid crop string: '" + cropString + "'.");
175 }
176 return new CropDimension(x1, y1, width, height);
177 }
178
179 }