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