    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 */
    017
    018package org.apache.commons.jxpath.ri.model.dynabeans;
    019
    020import java.util.Locale;
    021import java.util.Objects;
    022
    023import org.apache.commons.beanutils.DynaBean;
    024import org.apache.commons.jxpath.ri.QName;
    025import org.apache.commons.jxpath.ri.model.NodePointer;
    026import org.apache.commons.jxpath.ri.model.beans.PropertyOwnerPointer;
    027import org.apache.commons.jxpath.ri.model.beans.PropertyPointer;
    028
    029/**
    030 * A Pointer that points to a {@link DynaBean}. If the target DynaBean is Serializable, so should this instance be.
    031 */
    032public class DynaBeanPointer extends PropertyOwnerPointer {
    033
    034    private static final long serialVersionUID = -9135052498044877965L;
    035
    036    /**
    037     * Qualified name.
    038     */
    039    private final QName qName;
    040
    041    /**
    042     * DynaBean.
    043     */
    044    private final DynaBean dynaBean;
    045
    046    /**
    047     * Constructs a new DynaBeanPointer.
    048     *
    049     * @param parent   pointer
    050     * @param qName     is the name given to the first node
    051     * @param dynaBean pointed
    052     */
    053    public DynaBeanPointer(final NodePointer parent, final QName qName, final DynaBean dynaBean) {
    054        super(parent);
    055        this.qName = qName;
    056        this.dynaBean = dynaBean;
    057    }
    058
    059    /**
    060     * Constructs a new DynaBeanPointer.
    061     *
    062     * @param qName     is the name given to the first node
    063     * @param dynaBean pointed
    064     * @param locale   Locale
    065     */
    066    public DynaBeanPointer(final QName qName, final DynaBean dynaBean, final Locale locale) {
    067        super(null, locale);
    068        this.qName = qName;
    069        this.dynaBean = dynaBean;
    070    }
    071
    072    @Override
    073    public String asPath() {
    074        return parent == null ? "/" : super.asPath();
    075    }
    076
    077    @Override
    078    public boolean equals(final Object object) {
    079        if (object == this) {
    080            return true;
    081        }
    082        if (!(object instanceof DynaBeanPointer)) {
    083            return false;
    084        }
    085        final DynaBeanPointer other = (DynaBeanPointer) object;
    086        if (!(Objects.equals(parent, other.parent) && Objects.equals(qName, other.qName))) {
    087            return false;
    088        }
    089        final int iThis = index == WHOLE_COLLECTION ? 0 : index;
    090        final int iOther = other.index == WHOLE_COLLECTION ? 0 : other.index;
    091        return iThis == iOther && dynaBean == other.dynaBean;
    092    }
    093
    094    @Override
    095    public Object getBaseValue() {
    096        return dynaBean;
    097    }
    098
    099    @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}