001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
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            public boolean equals(Object obj) {
094                    if (!(obj instanceof ModuleId)) {
095                            return false;
096                    }
097    
098                    ModuleId moduleId = (ModuleId)obj;
099    
100                    return toString().equals(moduleId.toString());
101            }
102    
103            public int hashCode() {
104                    return toString().hashCode();
105            }
106    
107            public String toString() {
108                    return toString(
109                            _groupId, _artifactId, _pluginVersion.toString(), _type);
110            }
111    
112            protected ModuleId(
113                    String groupId, String artifactId, Version pluginVersion, String type) {
114    
115                    _groupId = groupId;
116                    _artifactId = artifactId;
117                    _pluginVersion = pluginVersion;
118                    _type = type;
119            }
120    
121            protected ModuleId(String moduleId) {
122                    StringTokenizer st = new StringTokenizer(moduleId, StringPool.SLASH);
123    
124                    if (st.countTokens() < 4) {
125                            throw new RuntimeException(
126                                    "The moduleId " + moduleId + " is not correct");
127                    }
128    
129                    _groupId = st.nextToken();
130                    _artifactId = st.nextToken();
131                    _pluginVersion = Version.getInstance(st.nextToken());
132                    _type = st.nextToken();
133            }
134    
135            private static Map<String, ModuleId> _moduleIds =
136                    new ConcurrentHashMap<String, ModuleId>();
137    
138            private String _artifactId;
139            private String _groupId;
140            private Version _pluginVersion;
141            private String _type;
142    
143    }