Skip to content

Method: Value.NullValue()

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.ontodriver.model;
16:
17: import java.util.Objects;
18:
19: /**
20: * Represents assertion value.
21: * <p>
22: * This class is a base for both property and class assertion values.
23: */
24: public class Value<T> {
25:
26: /**
27: * Represents a null value - empty value
28: */
29: private static final Value<?> NULL_VALUE = new NullValue();
30:
31: private final T value;
32:
33: private Value() {
34: this.value = null;
35: }
36:
37: public Value(T value) {
38: Objects.requireNonNull(value);
39: this.value = value;
40: }
41:
42: /**
43: * Gets this value.
44: *
45: * @return Value of the appropriate type
46: */
47: public T getValue() {
48: return value;
49: }
50:
51: /**
52: * Gets this value as string.
53: *
54: * @return Value as string
55: */
56: public String stringValue() {
57: return value != null ? value.toString() : null;
58: }
59:
60: /**
61: * Returns a Null object for Value.
62: * <p>
63: * Since Value requires a non-null value, this method returns a predefined object which represents a null (empty)
64: * value.
65: *
66: * @return Null value
67: * @see <a href="http://en.wikipedia.org/wiki/Null_Object_pattern">http://en.wikipedia.org/wiki/Null_Object_pattern</a>
68: */
69: @SuppressWarnings("unchecked")
70: public static <T> Value<T> nullValue() {
71: return (Value<T>) NULL_VALUE;
72: }
73:
74: @Override
75: public int hashCode() {
76: final int prime = 31;
77: int result = 1;
78: result = prime * result + ((value == null) ? 0 : value.hashCode());
79: return result;
80: }
81:
82: @Override
83: public boolean equals(Object obj) {
84: if (this == obj)
85: return true;
86: if (obj == null)
87: return false;
88: if (getClass() != obj.getClass())
89: return false;
90: Value<?> other = (Value<?>) obj;
91: if (value == null) {
92: return other.value == null;
93: } else return value.equals(other.value);
94: }
95:
96: @Override
97: public String toString() {
98: return value != null ? value.toString() : "";
99: }
100:
101: private static final class NullValue extends Value<Void> {
102:
103: private NullValue() {
104: super();
105: }
106:
107: @Override
108: public String stringValue() {
109: return "null";
110: }
111: }
112: }