1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.helger.schematron.pure.binding.xpath;
18
19 import java.util.Map;
20
21 import javax.annotation.Nonnull;
22 import javax.annotation.Nullable;
23 import javax.annotation.concurrent.NotThreadSafe;
24
25 import com.helger.commons.ValueEnforcer;
26 import com.helger.commons.annotation.Nonempty;
27 import com.helger.commons.annotation.ReturnsMutableCopy;
28 import com.helger.commons.collection.impl.CommonsTreeMap;
29 import com.helger.commons.collection.impl.ICommonsNavigableMap;
30 import com.helger.commons.compare.IComparator;
31 import com.helger.commons.state.EChange;
32 import com.helger.commons.string.StringHelper;
33 import com.helger.commons.string.ToStringGenerator;
34
35
36
37
38
39
40 @NotThreadSafe
41 public class PSXPathVariables implements IPSXPathVariables
42 {
43 @Nonnull
44 @ReturnsMutableCopy
45 private static ICommonsNavigableMap <String, String> _createMap ()
46 {
47 return new CommonsTreeMap <> (IComparator.getComparatorStringLongestFirst ());
48 }
49
50 private final ICommonsNavigableMap <String, String> m_aMap;
51
52 public PSXPathVariables ()
53 {
54 m_aMap = _createMap ();
55 }
56
57 public PSXPathVariables (@Nonnull final IPSXPathVariables aOther)
58 {
59 m_aMap = aOther.getAll ();
60 }
61
62
63
64
65
66
67
68
69
70
71 @Nonnull
72 public EChange add (@Nonnull final Map.Entry <String, String> aEntry)
73 {
74 return add (aEntry.getKey (), aEntry.getValue ());
75 }
76
77
78
79
80
81
82
83
84
85
86
87
88
89 @Nonnull
90 public EChange add (@Nonnull @Nonempty final String sName, @Nonnull @Nonempty final String sValue)
91 {
92 ValueEnforcer.notEmpty (sName, "Name");
93 ValueEnforcer.notEmpty (sValue, "Value");
94
95
96 final String sRealName = PSXPathQueryBinding.PARAM_VARIABLE_PREFIX + sName;
97 if (m_aMap.containsKey (sRealName))
98 return EChange.UNCHANGED;
99
100
101
102
103
104
105 final String sRealValue = getAppliedReplacement (sValue);
106 m_aMap.put (sRealName, sRealValue);
107 return EChange.CHANGED;
108 }
109
110 @Nullable
111 public String getAppliedReplacement (@Nullable final String sText)
112 {
113 return PSXPathQueryBinding.getWithParamTextsReplacedStatic (sText, m_aMap);
114 }
115
116
117
118
119
120
121
122
123
124 @Nonnull
125 public EChange remove (@Nullable final String sVarName)
126 {
127 if (StringHelper.hasText (sVarName))
128 if (m_aMap.remove (PSXPathQueryBinding.PARAM_VARIABLE_PREFIX + sVarName) == null)
129 return EChange.CHANGED;
130 return EChange.UNCHANGED;
131 }
132
133
134
135
136
137
138
139
140
141 @Nonnull
142 public EChange removeAll (@Nullable final Iterable <String> aVars)
143 {
144 EChange eChange = EChange.UNCHANGED;
145 if (aVars != null)
146 for (final String sName : aVars)
147 eChange = eChange.or (remove (sName));
148 return eChange;
149 }
150
151 @Nonnull
152 @ReturnsMutableCopy
153 public ICommonsNavigableMap <String, String> getAll ()
154 {
155 return m_aMap.getClone ();
156 }
157
158 public boolean contains (@Nullable final String sName)
159 {
160 if (StringHelper.hasNoText (sName))
161 return false;
162 return m_aMap.containsKey (sName);
163 }
164
165 @Nullable
166 public String get (@Nullable final String sName)
167 {
168 if (StringHelper.hasNoText (sName))
169 return null;
170 return m_aMap.get (sName);
171 }
172
173 @Nonnull
174 @ReturnsMutableCopy
175 public PSXPathVariables getClone ()
176 {
177 return new PSXPathVariables (this);
178 }
179
180 @Override
181 public String toString ()
182 {
183 return new ToStringGenerator (this).append ("map", m_aMap).getToString ();
184 }
185 }