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  package org.apache.commons.jxpath.ri.model.dynabeans;
18  
19  import java.util.Locale;
20  
21  import org.apache.commons.beanutils.DynaBean;
22  import org.apache.commons.jxpath.ri.QName;
23  import org.apache.commons.jxpath.ri.model.NodePointer;
24  import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
25  import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
26  
27  
28  /**
29   * A Pointer that points to a {@link DynaBean}.  If the target DynaBean is Serializable,
30   * so should this instance be.
31   *
32   * @author Dmitri Plotnikov
33   * @version $Revision: 668329 $ $Date: 2008-06-16 23:59:48 +0200 (Mo, 16 Jun 2008) $
34   */
35  public class DynaBeanPointer extends PropertyOwnerPointer {
36      private static final long serialVersionUID = -9135052498044877965L;
37  
38      private QName name;
39      private DynaBean dynaBean;
40  
41      /**
42       * Create a new DynaBeanPointer.
43       * @param name is the name given to the first node
44       * @param dynaBean pointed
45       * @param locale Locale
46       */
47      public DynaBeanPointer(QName name, DynaBean dynaBean, Locale locale) {
48          super(null, locale);
49          this.name = name;
50          this.dynaBean = dynaBean;
51      }
52  
53      /**
54       * Create a new DynaBeanPointer.
55       * @param parent pointer
56       * @param name is the name given to the first node
57       * @param dynaBean pointed
58       */
59      public DynaBeanPointer(NodePointer parent, QName name, DynaBean dynaBean) {
60          super(parent);
61          this.name = name;
62          this.dynaBean = dynaBean;
63      }
64  
65      public PropertyPointer getPropertyPointer() {
66          return new DynaBeanPropertyPointer(this, dynaBean);
67      }
68  
69      public QName getName() {
70          return name;
71      }
72  
73      public Object getBaseValue() {
74          return dynaBean;
75      }
76  
77      public Object getImmediateNode() {
78          return dynaBean;
79      }
80  
81      public boolean isCollection() {
82          return false;
83      }
84  
85      public int getLength() {
86          return 1;
87      }
88  
89      public boolean isLeaf() {
90          return false;
91      }
92  
93      public int hashCode() {
94          return name == null ? 0 : name.hashCode();
95      }
96  
97      public boolean equals(Object object) {
98          if (object == this) {
99              return true;
100         }
101 
102         if (!(object instanceof DynaBeanPointer)) {
103             return false;
104         }
105 
106         DynaBeanPointer other = (DynaBeanPointer) object;
107         if (!(equalObjects(parent, other.parent) && equalObjects(name, other.name))) {
108             return false;
109         }
110 
111         int iThis = (index == WHOLE_COLLECTION ? 0 : index);
112         int iOther = (other.index == WHOLE_COLLECTION ? 0 : other.index);
113         return iThis == iOther && dynaBean == other.dynaBean;
114     }
115 
116     public String asPath() {
117         return parent == null ? "/" : super.asPath();
118     }
119 
120     /**
121      * Learn whether two objects are == || .equals().
122      * @param o1 first object
123      * @param o2 second object
124      * @return boolean
125      */
126     private static boolean equalObjects(Object o1, Object o2) {
127         return o1 == o2 || o1 != null && o1.equals(o2);
128     }
129 }