001// Copyright 2004, 2005 The Apache Software Foundation
002//
003// Licensed under the Apache License, Version 2.0 (the "License");
004// you may not use this file except in compliance with the License.
005// You may obtain a copy of the License at
006//
007//     http://www.apache.org/licenses/LICENSE-2.0
008//
009// Unless required by applicable law or agreed to in writing, software
010// distributed under the License is distributed on an "AS IS" BASIS,
011// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012// See the License for the specific language governing permissions and
013// limitations under the License.
014
015package org.apache.hivemind.util;
016
017import org.apache.hivemind.HiveMind;
018
019/**
020 * A collection of utilities for handling qualified and unqualified ids.
021 * 
022 * @author Howard Lewis Ship
023 */
024public class IdUtils
025{
026
027    /**
028     * Returns a fully qualfied id. If the id contains a '.', then it is returned unchanged.
029     * Otherwise, the module's id is prefixed (with a seperator '.') and returned;
030     */
031    public static String qualify(String moduleId, String id)
032    {
033        if (id.indexOf('.') > 0)
034            return id;
035
036        return moduleId + "." + id;
037    }
038
039    /**
040     * Qualifies a list of interceptor service ids provided for an interceptor contribution. The
041     * special value "*" is not qualified.
042     */
043    public static String qualifyList(String sourceModuleId, String list)
044    {
045        if (HiveMind.isBlank(list) || list.equals("*"))
046            return list;
047
048        String[] items = StringUtils.split(list);
049
050        for (int i = 0; i < items.length; i++)
051            items[i] = qualify(sourceModuleId, items[i]);
052
053        return StringUtils.join(items, ',');
054    }
055
056    /**
057     * Removes the module name from a fully qualified id
058     */
059    public static String stripModule(String id)
060    {
061        int lastPoint = id.lastIndexOf('.');
062        if (lastPoint > 0)
063            return id.substring(lastPoint + 1, id.length());
064
065        return id;
066    }
067
068    /**
069     * Extracts the module name from a fully qualified id Returns null if id contains no module
070     */
071    public static String extractModule(String id)
072    {
073        int lastPoint = id.lastIndexOf('.');
074        if (lastPoint > 0)
075            return id.substring(0, lastPoint);
076
077        return null;
078    }
079
080}