1 package de.fhdw.wtf.context.model;
2
3 import de.fhdw.wtf.persistence.meta.StringValue;
4
5 /**
6 * A class to represent a sequence of Unicode-Characters with the maximum length of 4000 characters.
7 *
8 */
9 public final class Str extends AnyValue {
10
11 /**
12 * The representation of Str inside the JVM.
13 */
14 private final StringBuilder value;
15
16 /**
17 * Private standard constructor needing the value as a java.lang.StringBuilder.
18 *
19 * @param value
20 * The string value.
21 */
22 private Str(final StringBuilder value) {
23 this.value = value;
24 }
25
26 /**
27 * Creates a new Str object from a StringValue.
28 *
29 * @param value
30 * The string value.
31 */
32 public Str(final StringValue value) {
33 this(value.getValue());
34 }
35
36 /**
37 * Creates a new Str object from the java.lang.String representation.
38 *
39 * @param value
40 * The string value.
41 */
42 public Str(final String value) {
43 this(new StringBuilder(value));
44 }
45
46 /**
47 * Creates an empty Str object.
48 */
49 public Str() {
50 this.value = new StringBuilder();
51 }
52
53 /**
54 * Performs the concatenation of two Strs with each other.
55 *
56 * @param tail
57 * Another Str which will be prepended.
58 * @return Provides a new Str with the format (this)(tail).
59 */
60 public Str concat(final Str tail) {
61 final StringBuilder nw = new StringBuilder(this.value);
62 nw.append(tail.value.substring(0));
63 return new Str(nw);
64 }
65
66 /**
67 * Procides a sub sequence of this Str which starts at poistion from and end before position to. Please note, that
68 * an array out of bound exception might occur if to is larger than the length of this Str.
69 *
70 * @param from
71 * The starting position of the sub sequence.
72 * @param to
73 * The ending position of the sub sequence.
74 * @return The rquested sub sequence.
75 */
76 public Str substring(final int from, final int to) {
77 final StringBuilder res = new StringBuilder(this.value.substring(from, to));
78 return new Str(res);
79 }
80
81 /**
82 * Compares to this Str to another Str. An checks if this is lexigographical less or equal to another.
83 *
84 * @param other
85 * Another Str.
86 * @return Provides true if this is lexigographical less than the other.
87 */
88 public boolean lessEq(final Str other) {
89 return this.value.substring(0).compareTo(other.value.substring(0)) <= 0;
90 }
91
92 @Override
93 public boolean equals(final Object obj) {
94 if (obj instanceof Str) {
95 final Str other = (Str) obj;
96 return this.value.toString().equals(other.value.toString());
97 }
98 return false;
99 }
100
101 @Override
102 public int hashCode() {
103 return this.value.hashCode();
104 }
105
106 @Override
107 public String toString() {
108 return this.value.toString();
109 }
110
111 }