001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.core.schema;
023
024import java.io.Serializable;
025
026/**
027 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
028 */
029public class Namespace implements Serializable {
030
031    public static final Namespace DEFAULT_NS = new Namespace();
032
033    private static final long serialVersionUID = -3069469489908062592L;
034
035    private static final String DEFAULT_PREFIX = "";
036
037    private static final String DEFAULT_URI = "";
038
039    public final String uri;
040
041    public final String prefix;
042
043    public Namespace(String uri, String prefix) {
044        assert uri != null;
045        if (uri.length() == 0 && prefix.length() > 0) {
046            throw new IllegalArgumentException("prefix cannot be not empty if the uri is empty");
047        }
048        this.uri = uri;
049        this.prefix = prefix == null ? "" : prefix;
050    }
051
052    private Namespace() {
053        this(DEFAULT_URI, DEFAULT_PREFIX);
054    }
055
056    public boolean hasPrefix() {
057        return prefix.length() > 0;
058    }
059
060    @Override
061    public boolean equals(Object obj) {
062        if (obj == this) {
063            return true;
064        }
065        if (obj instanceof Namespace) {
066            Namespace ns = (Namespace) obj;
067            return ns.uri.equals(uri) && ns.prefix.equals(prefix);
068        }
069        return false;
070    }
071
072    @Override
073    public int hashCode() {
074        return uri.hashCode();
075    }
076
077    @Override
078    public String toString() {
079        return uri + " [" + prefix + ']';
080    }
081
082}