1 /*
2 * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package de.smartics.properties.api.config.domain;
17
18 import java.util.List;
19
20 /**
21 * A resolved property provides, in addition to a {@link Property} also the
22 * resolved and converted value.
23 */
24 public final class ResolvedProperty extends Property
25 {
26 // ********************************* Fields *********************************
27
28 // --- constants ------------------------------------------------------------
29
30 /**
31 * The class version identifier.
32 */
33 private static final long serialVersionUID = 1L;
34
35 // --- members --------------------------------------------------------------
36
37 /**
38 * The property expression. May be <code>null</code>. If the property value is
39 * <code>null</code>, the expression is checked for default values.
40 *
41 * @serial
42 */
43 private final String expression;
44
45 /**
46 * The resolved value. The property value is resolved and converted to this
47 * value. May be <code>null</code>.
48 *
49 * @serial
50 */
51 private final Object resolvedValue;
52
53 // ****************************** Initializer *******************************
54
55 // ****************************** Constructors ******************************
56
57 /**
58 * Default constructor.
59 *
60 * @param source the mandatory source where the property value has been
61 * defined.
62 * @param name the name of the property.
63 * @param value the value of the property.
64 * @param expression the property expression.
65 * @param resolvedValue the resolved value.
66 * @throws IllegalArgumentException if {@code name} is blank.
67 */
68 public ResolvedProperty(final PropertyLocation source, final String name,
69 final String value, final String expression, final Object resolvedValue)
70 throws IllegalArgumentException
71 {
72 super(source, name, value);
73
74 this.expression = expression;
75 this.resolvedValue = resolvedValue;
76 }
77
78 /**
79 * Copy constructor.
80 *
81 * @param plainProperty provides the information to be copied.
82 * @param expression the property expression.
83 * @param resolvedValue the resolved value.
84 */
85 public ResolvedProperty(final Property plainProperty,
86 final String expression, final Object resolvedValue)
87 {
88 this(plainProperty.getSource(), plainProperty.getName(), plainProperty
89 .getValue(), expression, resolvedValue);
90 }
91
92 // ****************************** Inner Classes *****************************
93
94 // ********************************* Methods ********************************
95
96 // --- init -----------------------------------------------------------------
97
98 // --- get&set --------------------------------------------------------------
99
100 /**
101 * Returns the resolved value. The property value is resolved and converted to
102 * this value.
103 *
104 * @return the resolved value. May be <code>null</code>.
105 */
106 public Object getResolvedValue()
107 {
108 return resolvedValue;
109 }
110
111 // --- business -------------------------------------------------------------
112
113 /**
114 * Returns the names of properties this property depends on. May be
115 * <code>null</code>, if this property does not refer to other properties.
116 *
117 * @return the names of properties this property depends on.
118 */
119 public List<String> getDependencies()
120 {
121 final DependenciesParser parser = new DependenciesParser();
122 final String value = getValue();
123 final String expression = value != null ? value : this.expression;
124 final List<String> dependencies = parser.getDependencies(expression);
125 return dependencies;
126 }
127
128 // --- object basics --------------------------------------------------------
129
130 @Override
131 public String toString()
132 {
133 return super.toString() + ": " + resolvedValue;
134 }
135 }