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