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.runtime.model;
016
017import java.io.Serializable;
018
019/**
020 * A component name.
021 * <p>
022 * Component names are strings of the form <code>type:name</code> The type part is optional - when missing the type is
023 * assumed to be "service".
024 * <p>
025 * Example of valid component names:
026 * <ul>
027 * <li>repository:my.repo
028 * <li>service:my.service
029 * <li>my.component
030 * </ul>
031 *
032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
033 */
034public class ComponentName implements Serializable {
035
036    public static final String DEFAULT_TYPE = "service";
037
038    private static final long serialVersionUID = -7686792831111487156L;
039
040    private final String type;
041
042    private final String name;
043
044    private final String rawName;
045
046    /**
047     * Constructs a component name from its string representation.
048     *
049     * @param rawName the string representation of this name
050     */
051    public ComponentName(String rawName) {
052        int p = rawName.indexOf(':');
053        if (p > -1) {
054            type = rawName.substring(0, p).intern();
055            name = rawName.substring(p + 1);
056            this.rawName = rawName.intern();
057        } else {
058            type = DEFAULT_TYPE;
059            name = rawName;
060            this.rawName = (type + ':' + name).intern();
061        }
062    }
063
064    /**
065     * Constructs a component name from its two parts: type and name.
066     *
067     * @param type the type part of the component name
068     * @param name the name part of the component name
069     */
070    public ComponentName(String type, String name) {
071        this.type = type.intern();
072        this.name = name;
073        rawName = (type + ':' + name).intern();
074    }
075
076    /**
077     * Gets the type part of the component name.
078     *
079     * @return the type part
080     */
081    public final String getType() {
082        return type;
083    }
084
085    /**
086     * Gets the name part of the component name.
087     *
088     * @return the name part
089     */
090    public final String getName() {
091        return name;
092    }
093
094    /**
095     * Gets the qualified component name.
096     *
097     * @return the qualified component name
098     */
099    public final String getRawName() {
100        return rawName;
101    }
102
103    @Override
104    public boolean equals(Object obj) {
105        if (obj == this) {
106            return true;
107        }
108        if (obj instanceof ComponentName) {
109            return rawName.equals(((ComponentName) obj).rawName);
110        }
111        return false;
112    }
113
114    @Override
115    public int hashCode() {
116        return rawName.hashCode();
117    }
118
119    @Override
120    public String toString() {
121        return rawName;
122    }
123
124}