1 package org.apache.commons.digester3.binder;
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 org.apache.commons.digester3.ObjectParamRule;
23
24 /**
25 * Builder chained when invoking {@link LinkedRuleBuilder#objectParam(Object)}.
26 *
27 * @param <T> The object type represented by this builder
28 * @since 3.0
29 */
30 public final class ObjectParamBuilder<T>
31 extends AbstractBackToLinkedRuleBuilder<ObjectParamRule>
32 {
33
34 private final T paramObj;
35
36 private int paramIndex = 0;
37
38 private String attributeName;
39
40 ObjectParamBuilder( String keyPattern, String namespaceURI, RulesBinder mainBinder, LinkedRuleBuilder mainBuilder,
41 /* @Nullable */T paramObj )
42 {
43 super( keyPattern, namespaceURI, mainBinder, mainBuilder );
44 this.paramObj = paramObj;
45 }
46
47 /**
48 * The zero-relative index of the parameter we are saving.
49 *
50 * @param paramIndex The zero-relative index of the parameter we are saving
51 * @return this builder instance
52 */
53 public ObjectParamBuilder<T> ofIndex( int paramIndex )
54 {
55 if ( paramIndex < 0 )
56 {
57 this.reportError( "objectParam( %s ).ofIndex( int )", "negative index argument not allowed" );
58 }
59
60 this.paramIndex = paramIndex;
61 return this;
62 }
63
64 /**
65 * The attribute which we are attempting to match.
66 *
67 * @param attributeName The attribute which we are attempting to match
68 * @return this builder instance
69 */
70 public ObjectParamBuilder<T> matchingAttribute( /* @Nullable */String attributeName )
71 {
72 this.attributeName = attributeName;
73 return this;
74 }
75
76 /**
77 * {@inheritDoc}
78 */
79 @Override
80 protected ObjectParamRule createRule()
81 {
82 return new ObjectParamRule( paramIndex, attributeName, paramObj );
83 }
84
85 }