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