1 /*
2 * Copyright (C) 2020-2023 Dipl.-Inform. Kai Hofmann. All rights reserved!
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 * Address Lines.
15 *
16 * Not DSGVO relevant.
17 */
18 public final class Lines implements Comparable<Lines>, IValueObject
19 {
20 /* *
21 * Cache for singletons.
22 */
23 // private static final Map<String, Lines> CACHE = new WeakHashMap<>();
24
25 /**
26 * Lines fregexp.
27 */
28 @SuppressWarnings("java:S6035")
29 private static final Pattern LINES_REGEXP = Pattern.compile("^([\\p{L}\\p{Digit},.& -]|\\R)*+$"); //$NON-NLS-1$
30
31 /**
32 * Lines.
33 */
34 private final String lines;
35
36
37 /**
38 * Constructor.
39 *
40 * @param lines Lines (1-5)
41 * @throws NullPointerException if lines is null
42 * @throws IllegalArgumentException if lines is not a correct Lines
43 */
44 private Lines(final String lines)
45 {
46 super();
47 Objects.requireNonNull(lines, "lines"); //$NON-NLS-1$
48 if ((lines.length() < 1) || (lines.length() > 200))
49 {
50 throw new IllegalArgumentException("Lines with wrong length"); //$NON-NLS-1$
51 }
52 if (!Lines.LINES_REGEXP.matcher(lines).matches())
53 {
54 throw new IllegalArgumentException("Lines with wrong format"); //$NON-NLS-1$
55 }
56 if (lines.lines().count() > 5)
57 {
58 throw new IllegalArgumentException("Do not use more than 5 lines"); //$NON-NLS-1$
59 }
60 this.lines = lines;
61 }
62
63
64 /**
65 * Lines factory.
66 *
67 * @param lines Lines
68 * @return Lines object
69 */
70 public static Lines of(final String lines)
71 {
72 /*
73 synchronized (Lines.class)
74 {
75 Lines obj = Lines.CACHE.get(lines);
76 if (obj != null)
77 {
78 return obj;
79 }
80 obj = new Lines(lines);
81 Lines.CACHE.put(lines, obj);
82 return obj;
83 }
84 */
85 return new Lines(lines);
86 }
87
88
89 /**
90 * Returns the value of this Lines as a string.
91 *
92 * @return The text value represented by this object after conversion to type string.
93 */
94 @Override
95 public String stringValue()
96 {
97 return this.lines;
98 }
99
100
101 /**
102 * Calculate hash code.
103 *
104 * @return Hash
105 * @see java.lang.Object#hashCode()
106 */
107 @Override
108 public int hashCode()
109 {
110 return this.lines.hashCode();
111 }
112
113
114 /**
115 * Is equal with another object.
116 *
117 * @param obj Object
118 * @return true when equal, false otherwise
119 * @see java.lang.Object#equals(java.lang.Object)
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 * Returns the string representation of this Lines.
139 *
140 * The exact details of this representation are unspecified and subject to change, but the following may be regarded as typical:
141 *
142 * "Lines[lines=Example1]"
143 *
144 * @return String representation of this Lines
145 * @see java.lang.Object#toString()
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(']'); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$
152 return builder.toString();
153 }
154
155
156 /**
157 * Compare with another object.
158 *
159 * @param obj Object to compare with
160 * @return 0: equal; 1: greater; -1: smaller
161 * @see java.lang.Comparable#compareTo(java.lang.Object)
162 */
163 @Override
164 public int compareTo(final Lines obj)
165 {
166 Objects.requireNonNull(obj, "obj"); //$NON-NLS-1$
167 return this.lines.compareTo(obj.lines);
168 }
169
170 }
171
172