View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.jxpath.ri.model.dynabeans;
19  
20  import java.util.Locale;
21  import java.util.Objects;
22  
23  import org.apache.commons.beanutils.DynaBean;
24  import org.apache.commons.jxpath.ri.QName;
25  import org.apache.commons.jxpath.ri.model.NodePointer;
26  import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
27  import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
28  
29  /**
30   * A Pointer that points to a {@link DynaBean}. If the target DynaBean is Serializable, so should this instance be.
31   */
32  public class DynaBeanPointer extends PropertyOwnerPointer {
33  
34      private static final long serialVersionUID = -9135052498044877965L;
35  
36      /**
37       * Qualified name.
38       */
39      private final QName qName;
40  
41      /**
42       * DynaBean.
43       */
44      private final DynaBean dynaBean;
45  
46      /**
47       * Constructs a new DynaBeanPointer.
48       *
49       * @param parent   pointer
50       * @param qName     is the name given to the first node
51       * @param dynaBean pointed
52       */
53      public DynaBeanPointer(final NodePointer parent, final QName qName, final DynaBean dynaBean) {
54          super(parent);
55          this.qName = qName;
56          this.dynaBean = dynaBean;
57      }
58  
59      /**
60       * Constructs a new DynaBeanPointer.
61       *
62       * @param qName     is the name given to the first node
63       * @param dynaBean pointed
64       * @param locale   Locale
65       */
66      public DynaBeanPointer(final QName qName, final DynaBean dynaBean, final Locale locale) {
67          super(null, locale);
68          this.qName = qName;
69          this.dynaBean = dynaBean;
70      }
71  
72      @Override
73      public String asPath() {
74          return parent == null ? "/" : super.asPath();
75      }
76  
77      @Override
78      public boolean equals(final Object object) {
79          if (object == this) {
80              return true;
81          }
82          if (!(object instanceof DynaBeanPointer)) {
83              return false;
84          }
85          final DynaBeanPointer other = (DynaBeanPointer) object;
86          if (!(Objects.equals(parent, other.parent) && Objects.equals(qName, other.qName))) {
87              return false;
88          }
89          final int iThis = index == WHOLE_COLLECTION ? 0 : index;
90          final int iOther = other.index == WHOLE_COLLECTION ? 0 : other.index;
91          return iThis == iOther && dynaBean == other.dynaBean;
92      }
93  
94      @Override
95      public Object getBaseValue() {
96          return dynaBean;
97      }
98  
99      @Override
100     public Object getImmediateNode() {
101         return dynaBean;
102     }
103 
104     @Override
105     public int getLength() {
106         return 1;
107     }
108 
109     @Override
110     public QName getName() {
111         return qName;
112     }
113 
114     @Override
115     public PropertyPointer getPropertyPointer() {
116         return new DynaBeanPropertyPointer(this, dynaBean);
117     }
118 
119     @Override
120     public int hashCode() {
121         return qName == null ? 0 : qName.hashCode();
122     }
123 
124     @Override
125     public boolean isCollection() {
126         return false;
127     }
128 
129     @Override
130     public boolean isLeaf() {
131         return false;
132     }
133 }