001    /**
002     * Copyright (c) 2000-2013 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 (this == obj) {
053                            return true;
054                    }
055    
056                    if (!(obj instanceof ModuleId)) {
057                            return false;
058                    }
059    
060                    ModuleId moduleId = (ModuleId)obj;
061    
062                    return toString().equals(moduleId.toString());
063            }
064    
065            public String getArtifactId() {
066                    return _artifactId;
067            }
068    
069            public String getArtifactPath() {
070                    return StringPool.SLASH + _groupId + StringPool.SLASH + _artifactId +
071                            StringPool.SLASH + _pluginVersion + StringPool.SLASH +
072                                    getArtifactWARName();
073            }
074    
075            public String getArtifactWARName() {
076                    return _artifactId + StringPool.DASH + _pluginVersion +
077                            StringPool.PERIOD + _type;
078            }
079    
080            public String getGroupId() {
081                    return _groupId;
082            }
083    
084            public String getPackageId() {
085                    return _groupId + StringPool.SLASH + _artifactId;
086            }
087    
088            public String getType() {
089                    return _type;
090            }
091    
092            public String getVersion() {
093                    return _pluginVersion.toString();
094            }
095    
096            @Override
097            public int hashCode() {
098                    return toString().hashCode();
099            }
100    
101            public boolean isLaterVersionThan(String version) {
102                    return _pluginVersion.isLaterVersionThan(version);
103            }
104    
105            public boolean isPreviousVersionThan(String version) {
106                    return _pluginVersion.isPreviousVersionThan(version);
107            }
108    
109            public boolean isSameVersionAs(String version) {
110                    return _pluginVersion.isSameVersionAs(version);
111            }
112    
113            @Override
114            public String toString() {
115                    return toString(
116                            _groupId, _artifactId, _pluginVersion.toString(), _type);
117            }
118    
119            protected ModuleId(String moduleId) {
120                    StringTokenizer st = new StringTokenizer(moduleId, StringPool.SLASH);
121    
122                    if (st.countTokens() < 4) {
123                            throw new RuntimeException(
124                                    "The moduleId " + moduleId + " is not correct");
125                    }
126    
127                    _groupId = st.nextToken();
128                    _artifactId = st.nextToken();
129                    _pluginVersion = Version.getInstance(st.nextToken());
130                    _type = st.nextToken();
131            }
132    
133            protected ModuleId(
134                    String groupId, String artifactId, Version pluginVersion, String type) {
135    
136                    _groupId = groupId;
137                    _artifactId = artifactId;
138                    _pluginVersion = pluginVersion;
139                    _type = type;
140            }
141    
142            private static Map<String, ModuleId> _moduleIds =
143                    new ConcurrentHashMap<String, ModuleId>();
144    
145            private String _artifactId;
146            private String _groupId;
147            private Version _pluginVersion;
148            private String _type;
149    
150    }