001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.core.schema;
016
017import java.io.Serializable;
018
019/**
020 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
021 */
022public class Namespace implements Serializable {
023
024    public static final Namespace DEFAULT_NS = new Namespace();
025
026    private static final long serialVersionUID = -3069469489908062592L;
027
028    private static final String DEFAULT_PREFIX = "";
029
030    private static final String DEFAULT_URI = "";
031
032    public final String uri;
033
034    public final String prefix;
035
036    public Namespace(String uri, String prefix) {
037        assert uri != null;
038        if (uri.length() == 0 && prefix.length() > 0) {
039            throw new IllegalArgumentException("prefix cannot be not empty if the uri is empty");
040        }
041        this.uri = uri;
042        this.prefix = prefix == null ? "" : prefix;
043    }
044
045    private Namespace() {
046        this(DEFAULT_URI, DEFAULT_PREFIX);
047    }
048
049    public boolean hasPrefix() {
050        return prefix.length() > 0;
051    }
052
053    @Override
054    public boolean equals(Object obj) {
055        if (obj == this) {
056            return true;
057        }
058        if (obj instanceof Namespace) {
059            Namespace ns = (Namespace) obj;
060            return ns.uri.equals(uri) && ns.prefix.equals(prefix);
061        }
062        return false;
063    }
064
065    @Override
066    public int hashCode() {
067        return uri.hashCode();
068    }
069
070    @Override
071    public String toString() {
072        return uri + " [" + prefix + ']';
073    }
074
075}