001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.jxpath.ri.model.dynabeans; 018 019import java.util.Locale; 020 021import org.apache.commons.beanutils.DynaBean; 022import org.apache.commons.jxpath.ri.QName; 023import org.apache.commons.jxpath.ri.model.NodePointer; 024import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer; 025import org.apache.commons.jxpath.ri.model.beans.PropertyPointer; 026 027 028/** 029 * A Pointer that points to a {@link DynaBean}. If the target DynaBean is Serializable, 030 * so should this instance be. 031 * 032 * @author Dmitri Plotnikov 033 * @version $Revision: 668329 $ $Date: 2008-06-16 23:59:48 +0200 (Mo, 16 Jun 2008) $ 034 */ 035public class DynaBeanPointer extends PropertyOwnerPointer { 036 private static final long serialVersionUID = -9135052498044877965L; 037 038 private QName name; 039 private DynaBean dynaBean; 040 041 /** 042 * Create a new DynaBeanPointer. 043 * @param name is the name given to the first node 044 * @param dynaBean pointed 045 * @param locale Locale 046 */ 047 public DynaBeanPointer(QName name, DynaBean dynaBean, Locale locale) { 048 super(null, locale); 049 this.name = name; 050 this.dynaBean = dynaBean; 051 } 052 053 /** 054 * Create a new DynaBeanPointer. 055 * @param parent pointer 056 * @param name is the name given to the first node 057 * @param dynaBean pointed 058 */ 059 public DynaBeanPointer(NodePointer parent, QName name, DynaBean dynaBean) { 060 super(parent); 061 this.name = name; 062 this.dynaBean = dynaBean; 063 } 064 065 public PropertyPointer getPropertyPointer() { 066 return new DynaBeanPropertyPointer(this, dynaBean); 067 } 068 069 public QName getName() { 070 return name; 071 } 072 073 public Object getBaseValue() { 074 return dynaBean; 075 } 076 077 public Object getImmediateNode() { 078 return dynaBean; 079 } 080 081 public boolean isCollection() { 082 return false; 083 } 084 085 public int getLength() { 086 return 1; 087 } 088 089 public boolean isLeaf() { 090 return false; 091 } 092 093 public int hashCode() { 094 return name == null ? 0 : name.hashCode(); 095 } 096 097 public boolean equals(Object object) { 098 if (object == this) { 099 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}