001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.plugin;
016    
017    import com.liferay.portal.kernel.plugin.Version;
018    import com.liferay.portal.kernel.util.StringPool;
019    
020    import java.io.Serializable;
021    
022    import java.util.Map;
023    import java.util.StringTokenizer;
024    import java.util.concurrent.ConcurrentHashMap;
025    
026    /**
027     * @author Jorge Ferrer
028     */
029    public class ModuleId implements Serializable {
030    
031            public static ModuleId getInstance(String moduleId) {
032                    ModuleId moduleIdObj = _moduleIds.get(moduleId);
033    
034                    if (moduleIdObj == null) {
035                            moduleIdObj =  new ModuleId(moduleId);
036    
037                            _moduleIds.put(moduleId, moduleIdObj);
038                    }
039    
040                    return moduleIdObj;
041            }
042    
043            public static String toString(
044                    String groupId, String artifactId, String version, String type) {
045    
046                    return groupId + StringPool.SLASH + artifactId + StringPool.SLASH +
047                            version + StringPool.SLASH + type;
048            }
049    
050            public String getGroupId() {
051                    return _groupId;
052            }
053    
054            public String getArtifactId() {
055                    return _artifactId;
056            }
057    
058            public String getPackageId() {
059                    return _groupId + StringPool.SLASH + _artifactId;
060            }
061    
062            public String getVersion() {
063                    return _pluginVersion.toString();
064            }
065    
066            public String getType() {
067                    return _type;
068            }
069    
070            public String getArtifactPath() {
071                    return StringPool.SLASH + _groupId + StringPool.SLASH + _artifactId +
072                            StringPool.SLASH + _pluginVersion + StringPool.SLASH +
073                                    getArtifactWARName();
074            }
075    
076            public String getArtifactWARName() {
077                    return _artifactId + StringPool.DASH + _pluginVersion +
078                            StringPool.PERIOD + _type;
079            }
080    
081            public boolean isLaterVersionThan(String version) {
082                    return _pluginVersion.isLaterVersionThan(version);
083            }
084    
085            public boolean isPreviousVersionThan(String version) {
086                    return _pluginVersion.isPreviousVersionThan(version);
087            }
088    
089            public boolean isSameVersionAs(String version) {
090                    return _pluginVersion.isSameVersionAs(version);
091            }
092    
093            @Override
094            public boolean equals(Object obj) {
095                    if (!(obj instanceof ModuleId)) {
096                            return false;
097                    }
098    
099                    ModuleId moduleId = (ModuleId)obj;
100    
101                    return toString().equals(moduleId.toString());
102            }
103    
104            @Override
105            public int hashCode() {
106                    return toString().hashCode();
107            }
108    
109            @Override
110            public String toString() {
111                    return toString(
112                            _groupId, _artifactId, _pluginVersion.toString(), _type);
113            }
114    
115            protected ModuleId(
116                    String groupId, String artifactId, Version pluginVersion, String type) {
117    
118                    _groupId = groupId;
119                    _artifactId = artifactId;
120                    _pluginVersion = pluginVersion;
121                    _type = type;
122            }
123    
124            protected ModuleId(String moduleId) {
125                    StringTokenizer st = new StringTokenizer(moduleId, StringPool.SLASH);
126    
127                    if (st.countTokens() < 4) {
128                            throw new RuntimeException(
129                                    "The moduleId " + moduleId + " is not correct");
130                    }
131    
132                    _groupId = st.nextToken();
133                    _artifactId = st.nextToken();
134                    _pluginVersion = Version.getInstance(st.nextToken());
135                    _type = st.nextToken();
136            }
137    
138            private static Map<String, ModuleId> _moduleIds =
139                    new ConcurrentHashMap<String, ModuleId>();
140    
141            private String _artifactId;
142            private String _groupId;
143            private Version _pluginVersion;
144            private String _type;
145    
146    }