1 package de.fhdw.wtf.persistence.meta;
2
3 import de.fhdw.wtf.persistence.exception.PersistenceException;
4 import de.fhdw.wtf.persistence.facade.ObjectFacade;
5
6 /**
7 * A class to represent any String Value. A String Value is an object which type is always of type String.
8 *
9 */
10 public class StringValue extends Object { // NOPMD: Object != java.lang.Object
11
12 /**
13 * The String Value will use lazy load to fetch unset data from the database. Therefore an access to the Object
14 * Facade is needed.
15 */
16 private static ObjectFacade facade;
17
18 /**
19 * set the ObjectFacade, because the String Value will use lazy load to fetch unset data from the database.
20 * Therefore an access to the Object Facade is needed.
21 *
22 * @param objectFacade
23 */
24 public static void setObjectFacade(final ObjectFacade objectFacade) {
25 facade = objectFacade;
26 }
27
28 /**
29 * The value of this Object.
30 */
31 private String value;
32
33 /**
34 * The unique identity of the string in the database.
35 */
36 private Long id;
37
38 /**
39 * Creates a new String Value if the id is known. The value will be fetched from the database if needed. Note that
40 * the id has to be a valid Id of a String Value Object in the database.
41 *
42 * @param id
43 * The existing Id of a String Value in the database.
44 */
45 public StringValue(final long id) {
46 super(StringType.getInstance());
47 this.id = id;
48 }
49
50 /**
51 * A constructor for a new String value if only the value is known. The Id will be determined by the database later.
52 *
53 * @param value
54 * Any String value.
55 */
56 public StringValue(final String value) {
57 super(StringType.getInstance());
58 this.value = value;
59 }
60
61 /**
62 * Constructor for a String value if both Id and value are known. The tuple if and value has to exist in the
63 * database otherwise the consistency will be violated.
64 *
65 * @param id
66 * The Id of this String Value in the database.
67 * @param value
68 * The value of this String value in the database.
69 */
70 public StringValue(final Long id, final String value) {
71 super(StringType.getInstance());
72 this.id = id;
73 this.value = value;
74 }
75
76 /**
77 * Getter for the value. Lazy Loads the Value from the database if unset.
78 *
79 * @return Provides the value of this String Value Object.
80 */
81 public String getValue() {
82 if (this.value == null) {
83 try {
84 this.value = facade.getStringForId(this.getId());
85 } catch (final PersistenceException e) {
86 throw new Error(e);
87 }
88 }
89 return this.value;
90 }
91
92 /**
93 * Getter for the Id. Lazy Loads the Value from the database if unset.
94 *
95 * @return Provides the id of this String Value Object.
96 */
97 @Override
98 public long getId() {
99 if (this.id == null) {
100 try {
101 this.id = facade.getIdForString(this.getValue());
102 } catch (final PersistenceException e) {
103 throw new Error(e);
104 }
105 }
106 return this.id;
107 }
108
109 @Override
110 public boolean isTheSameAs(final java.lang.Object other) {
111 if (!(other instanceof StringValue)) {
112 return false;
113 }
114
115 return this.getId() == ((StringValue) other).getId();
116 }
117
118 }