Skip to content

Method: stringValue()

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