    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;
    019
    020import java.io.Serializable;
    021
    022/**
    023 * A qualified name: a combination of an optional namespace prefix and an local name.
    024 */
    025public class QName implements Serializable {
    026
    027    private static final long serialVersionUID = 7616199282015091496L;
    028
    029    /**
    030     * Prefix.
    031     */
    032    private final String prefix;
    033
    034    /**
    035     * Name.
    036     */
    037    private final String name;
    038
    039    /**
    040     * Qualified name.
    041     */
    042    private final String qualifiedName;
    043
    044    /**
    045     * Constructs a new QName.
    046     *
    047     * @param qualifiedName value
    048     */
    049    public QName(final String qualifiedName) {
    050        this.qualifiedName = qualifiedName;
    051        final int index = qualifiedName.indexOf(':');
    052        prefix = index < 0 ? null : qualifiedName.substring(0, index);
    053        name = index < 0 ? qualifiedName : qualifiedName.substring(index + 1);
    054    }
    055
    056    /**
    057     * Constructs a new QName.
    058     *
    059     * @param prefix    ns
    060     * @param localName String
    061     */
    062    public QName(final String prefix, final String localName) {
    063        this.prefix = prefix;
    064        this.name = localName;
    065        this.qualifiedName = prefix == null ? localName : prefix + ':' + localName;
    066    }
    067
    068    @Override
    069    public boolean equals(final Object object) {
    070        if (this == object) {
    071            return true;
    072        }
    073        if (!(object instanceof QName)) {
    074            return false;
    075        }
    076        return qualifiedName.equals(((QName) object).qualifiedName);
    077    }
    078
    079    /**
    080     * Gets the local name.
    081     *
    082     * @return String
    083     */
    084    public String getName() {
    085        return name;
    086    }
    087
    088    /**
    089     * Gets the prefix of this QName.
    090     *
    091     * @return String
    092     */
    093    public String getPrefix() {
    094        return prefix;
    095    }
    096
    097    @Override
    098    public int hashCode() {
    099        return name.hashCode();
    100    }
    101
    102    @Override
    103    public String toString() {
    104        return qualifiedName;
    105    }
    106}