Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
KeyAppender |
|
| 3.1666666666666665;3.167 |
1 | package org.apache.onami.configuration.variables; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.util.Map; | |
23 | ||
24 | /** | |
25 | * Appender which relies on another appender to provides a configuration key, and a fallback appender in case no | |
26 | * configuration value is found. | |
27 | * | |
28 | * @since 6.0 | |
29 | */ | |
30 | final class KeyAppender | |
31 | extends AbstractAppender | |
32 | { | |
33 | ||
34 | /** Appender which will resolve key (add the possibility for dynamic variable) */ | |
35 | private final Appender key; | |
36 | ||
37 | /** Appender which will resolve default value */ | |
38 | private Appender defaultValue; | |
39 | ||
40 | /** Parser to use if dynamic resolution is needed */ | |
41 | private Parser parser; | |
42 | ||
43 | /** | |
44 | * Constructor for key without default value. | |
45 | * | |
46 | * @param parser The parser from which this appender has been created. | |
47 | * @param chunk | |
48 | * @param key Appender to resolve configuration key. | |
49 | */ | |
50 | public KeyAppender( final Parser parser, final String chunk, final Appender key ) | |
51 | { | |
52 | 0 | this( parser, chunk, key, null ); |
53 | 0 | } |
54 | ||
55 | /** | |
56 | * Constructor for key without default value. | |
57 | * | |
58 | * @param parser The parser from which this appender has been created. | |
59 | * @param chunk | |
60 | * @param key Appender to resolve configuration key. | |
61 | * @param defaultValue Appender to resolve default value, may be null. | |
62 | */ | |
63 | public KeyAppender( final Parser parser, final String chunk, final Appender key, final Appender defaultValue ) | |
64 | { | |
65 | 2482 | super( chunk ); |
66 | 2482 | this.parser = parser; |
67 | 2482 | this.key = key; |
68 | 2482 | this.defaultValue = defaultValue; |
69 | 2482 | } |
70 | ||
71 | /** | |
72 | * {@inheritDoc} | |
73 | */ | |
74 | @Override | |
75 | public void doAppend( StringBuilder buffer, Map<String, String> configuration, Tree<Appender> context ) | |
76 | { | |
77 | // Resolve key eventually | |
78 | 35364 | StringBuilder keyBuffer = new StringBuilder(); |
79 | 35364 | key.append( keyBuffer, configuration, context ); |
80 | 35364 | String resolvedKey = keyBuffer.toString(); |
81 | ||
82 | 35364 | String resolvedValue = configuration.get( resolvedKey ); |
83 | 35364 | if ( resolvedValue != null ) |
84 | { | |
85 | // Resolved value from the configuration may have variable | |
86 | // unresolved | |
87 | 19718 | Resolver value = parser.parse( resolvedValue ); |
88 | 19718 | if ( !value.needsResolving() ) |
89 | { | |
90 | 19690 | buffer.append( resolvedValue ); |
91 | ||
92 | } | |
93 | // Process value | |
94 | else | |
95 | { | |
96 | 28 | if ( !( value instanceof Appender ) ) |
97 | { | |
98 | 0 | resolvedValue = value.resolve( configuration ); |
99 | } | |
100 | else | |
101 | { | |
102 | 28 | StringBuilder resolvedValueBuffer = new StringBuilder(); |
103 | 28 | ( (Appender) value ).append( resolvedValueBuffer, configuration, context ); |
104 | 28 | resolvedValue = resolvedValueBuffer.toString(); |
105 | } | |
106 | ||
107 | 28 | buffer.append( resolvedValue ); |
108 | // Update the configuration | |
109 | 28 | configuration.put( resolvedKey, resolvedValue ); |
110 | } | |
111 | 19718 | } |
112 | // No value found from configuration, take default one | |
113 | 15646 | else if ( defaultValue != null ) |
114 | { | |
115 | 14896 | defaultValue.append( buffer, configuration, context ); |
116 | } | |
117 | // Fallback, print original chunk, will let the possibility to resolve | |
118 | // it later | |
119 | else | |
120 | { | |
121 | 750 | buffer.append( chunk ); |
122 | } | |
123 | 35364 | } |
124 | ||
125 | @Override | |
126 | public boolean equals( Object obj ) | |
127 | { | |
128 | 97796 | if ( obj == this ) |
129 | { | |
130 | 0 | return true; |
131 | } | |
132 | 97796 | if ( obj instanceof KeyAppender ) |
133 | { | |
134 | 20824 | KeyAppender other = (KeyAppender) obj; |
135 | 20824 | return ( key != null ? key.equals( other.key ) : other.key == null ) |
136 | && ( defaultValue != null ? defaultValue.equals( other.defaultValue ) : other.defaultValue == null ); | |
137 | } | |
138 | 76972 | return false; |
139 | } | |
140 | ||
141 | @Override | |
142 | public int hashCode() | |
143 | { | |
144 | 0 | return ( key != null ? key.hashCode() : 0 ) + ( defaultValue != null ? defaultValue.hashCode() * 31 : 0 ); |
145 | } | |
146 | ||
147 | /** | |
148 | * @return Always true | |
149 | */ | |
150 | public boolean needsResolving() | |
151 | { | |
152 | 1164 | return true; |
153 | } | |
154 | ||
155 | } |