001    /**
002     * Copyright (c) 2000-present 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.kernel.mobile.device;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.kernel.util.StringUtil;
021    import com.liferay.portal.kernel.util.Validator;
022    
023    import java.io.Serializable;
024    
025    import java.util.Collections;
026    import java.util.HashSet;
027    import java.util.Objects;
028    import java.util.Set;
029    import java.util.TreeSet;
030    
031    /**
032     * @author Milen Dyankov
033     * @author Michael C. Han
034     */
035    @ProviderType
036    public class VersionableName
037            implements Comparable<VersionableName>, Serializable {
038    
039            public static final VersionableName UNKNOWN = new VersionableName(
040                    "unknown", "unknown");
041    
042            public VersionableName(String name) {
043                    this(name, (Set<String>)null);
044            }
045    
046            public VersionableName(String name, Set<String> versions) {
047                    if (Validator.isNull(name)) {
048                            throw new IllegalArgumentException("Name is null");
049                    }
050    
051                    _name = name;
052                    _versions = versions;
053            }
054    
055            public VersionableName(String name, String version) {
056                    this(name, new HashSet<String>());
057    
058                    addVersion(version);
059            }
060    
061            public void addVersion(String version) {
062                    if (version == null) {
063                            return;
064                    }
065    
066                    if (_versions == null) {
067                            _versions = new TreeSet<>();
068                    }
069    
070                    _versions.add(version);
071            }
072    
073            @Override
074            public int compareTo(VersionableName versionableName) {
075                    return StringUtil.toUpperCase(_name).compareTo(
076                            StringUtil.toUpperCase(versionableName.getName()));
077            }
078    
079            @Override
080            public boolean equals(Object obj) {
081                    if (this == obj) {
082                            return true;
083                    }
084    
085                    if (!(obj instanceof VersionableName)) {
086                            return false;
087                    }
088    
089                    VersionableName versionableName = (VersionableName)obj;
090    
091                    if (Objects.equals(_name, versionableName._name)) {
092                            return true;
093                    }
094    
095                    return false;
096            }
097    
098            public String getName() {
099                    return _name;
100            }
101    
102            public Set<String> getVersions() {
103                    if (_versions == null) {
104                            return Collections.emptySet();
105                    }
106    
107                    return Collections.unmodifiableSet(_versions);
108            }
109    
110            @Override
111            public int hashCode() {
112                    if (_name != null) {
113                            return _name.hashCode();
114                    }
115                    else {
116                            return 0;
117                    }
118            }
119    
120            @Override
121            public String toString() {
122                    StringBundler sb = new StringBundler(5);
123    
124                    sb.append("{name=");
125                    sb.append(_name);
126                    sb.append(", versions=");
127                    sb.append(_versions);
128                    sb.append("}");
129    
130                    return sb.toString();
131            }
132    
133            private final String _name;
134            private Set<String> _versions;
135    
136    }