001    /**
002     * Copyright (c) 2000-2012 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            @Override
051            public boolean equals(Object obj) {
052                    if (!(obj instanceof ModuleId)) {
053                            return false;
054                    }
055    
056                    ModuleId moduleId = (ModuleId)obj;
057    
058                    return toString().equals(moduleId.toString());
059            }
060    
061            public String getArtifactId() {
062                    return _artifactId;
063            }
064    
065            public String getArtifactPath() {
066                    return StringPool.SLASH + _groupId + StringPool.SLASH + _artifactId +
067                            StringPool.SLASH + _pluginVersion + StringPool.SLASH +
068                                    getArtifactWARName();
069            }
070    
071            public String getArtifactWARName() {
072                    return _artifactId + StringPool.DASH + _pluginVersion +
073                            StringPool.PERIOD + _type;
074            }
075    
076            public String getGroupId() {
077                    return _groupId;
078            }
079    
080            public String getPackageId() {
081                    return _groupId + StringPool.SLASH + _artifactId;
082            }
083    
084            public String getType() {
085                    return _type;
086            }
087    
088            public String getVersion() {
089                    return _pluginVersion.toString();
090            }
091    
092            @Override
093            public int hashCode() {
094                    return toString().hashCode();
095            }
096    
097            public boolean isLaterVersionThan(String version) {
098                    return _pluginVersion.isLaterVersionThan(version);
099            }
100    
101            public boolean isPreviousVersionThan(String version) {
102                    return _pluginVersion.isPreviousVersionThan(version);
103            }
104    
105            public boolean isSameVersionAs(String version) {
106                    return _pluginVersion.isSameVersionAs(version);
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    }