1
2
3
4 package de.powerstat.validation.values;
5
6
7 import java.util.Objects;
8 import java.util.regex.Pattern;
9
10 import de.powerstat.validation.interfaces.IValueObject;
11
12
13
14
15
16
17
18 public final class Lines implements Comparable<Lines>, IValueObject
19 {
20
21
22
23
24
25
26
27
28 @SuppressWarnings("java:S6035")
29 private static final Pattern LINES_REGEXP = Pattern.compile("^([\\p{L}\\p{Digit},.& -]|\\R)*+$");
30
31
32
33
34 private final String lines;
35
36
37
38
39
40
41
42
43
44 private Lines(final String lines)
45 {
46 super();
47 Objects.requireNonNull(lines, "lines");
48 if ((lines.length() < 1) || (lines.length() > 200))
49 {
50 throw new IllegalArgumentException("Lines with wrong length");
51 }
52 if (!Lines.LINES_REGEXP.matcher(lines).matches())
53 {
54 throw new IllegalArgumentException("Lines with wrong format");
55 }
56 if (lines.lines().count() > 5)
57 {
58 throw new IllegalArgumentException("Do not use more than 5 lines");
59 }
60 this.lines = lines;
61 }
62
63
64
65
66
67
68
69
70 public static Lines of(final String lines)
71 {
72
73
74
75
76
77
78
79
80
81
82
83
84
85 return new Lines(lines);
86 }
87
88
89
90
91
92
93
94 @Override
95 public String stringValue()
96 {
97 return this.lines;
98 }
99
100
101
102
103
104
105
106
107 @Override
108 public int hashCode()
109 {
110 return this.lines.hashCode();
111 }
112
113
114
115
116
117
118
119
120
121 @Override
122 public boolean equals(final Object obj)
123 {
124 if (this == obj)
125 {
126 return true;
127 }
128 if (!(obj instanceof Lines))
129 {
130 return false;
131 }
132 final Lines other = (Lines)obj;
133 return this.lines.equals(other.lines);
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147 @Override
148 public String toString()
149 {
150 final var builder = new StringBuilder();
151 builder.append("Lines[lines=").append(this.lines.replace("\n", "\\n").replace("\r", "\\r")).append(']');
152 return builder.toString();
153 }
154
155
156
157
158
159
160
161
162
163 @Override
164 public int compareTo(final Lines obj)
165 {
166 Objects.requireNonNull(obj, "obj");
167 return this.lines.compareTo(obj.lines);
168 }
169
170 }
171
172