001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.module;
029
030import java.io.Serializable;
031
032/**
033 * Describes an OpenCms module dependency.<p>
034 *
035 * Module dependencies are checked if a module is imported or deleted.
036 * If a module A requires certain resources (like Java classes)
037 * from another module B, a should be made dependend on B.<p>
038
039 *
040 * @since 6.0.0
041 */
042public class CmsModuleDependency implements Comparable<Object>, Serializable {
043
044    /** The serial version id. */
045    private static final long serialVersionUID = -464911120361037953L;
046
047    /** The hash code of the module dependency. */
048    private int m_hashCode;
049
050    /** The name of the module dependency. */
051    private String m_name;
052
053    /** The (minimum) version of the module dependency. */
054    private CmsModuleVersion m_version;
055
056    /**
057     * Generates a new, empty module dependency.<p>
058     *
059     */
060    public CmsModuleDependency() {
061
062        super();
063        m_name = "";
064        m_version = new CmsModuleVersion("0");
065
066        // pre - calculate the hash code
067        m_hashCode = m_name.concat(m_version.toString()).hashCode();
068    }
069
070    /**
071     * Generates a new module dependency.<p>
072     *
073     * @param moduleName the name of the module dependency
074     * @param minVersion the minimum version of the dependency
075     */
076    public CmsModuleDependency(String moduleName, CmsModuleVersion minVersion) {
077
078        super();
079        m_name = moduleName;
080        m_version = minVersion;
081
082        // pre - calculate the hash code
083        m_hashCode = m_name.concat(m_version.toString()).hashCode();
084    }
085
086    /**
087     * @see java.lang.Object#clone()
088     */
089    @Override
090    public Object clone() {
091
092        return new CmsModuleDependency(m_name, new CmsModuleVersion(m_version.getVersion()));
093    }
094
095    /**
096     * @see java.lang.Comparable#compareTo(java.lang.Object)
097     */
098    public int compareTo(Object obj) {
099
100        if (obj == this) {
101            return 0;
102        }
103        if (obj instanceof CmsModuleDependency) {
104            CmsModuleDependency other = (CmsModuleDependency)obj;
105            if (!m_name.equals(other.m_name)) {
106                // not same name means no dependency
107                return 0;
108            }
109            // same name: result depends on version numbers
110            return m_version.compareTo(other.m_version);
111        }
112        return 0;
113    }
114
115    /**
116     * Checks if this module depedency depends on another given module dependency.<p>
117     *
118     * @param other the other dependency to check against
119     * @return true if this module depedency depends on the given module dependency
120     */
121    public boolean dependesOn(CmsModuleDependency other) {
122
123        if (!m_name.equals(other.m_name)) {
124            // not same name means no dependency
125            return false;
126        }
127
128        // same name: result depends on version numbers
129        return (m_version.compareTo(other.m_version) <= 0);
130    }
131
132    /**
133     * @see java.lang.Object#equals(java.lang.Object)
134     */
135    @Override
136    public boolean equals(Object obj) {
137
138        if (obj == this) {
139            return true;
140        }
141        if (obj instanceof CmsModuleDependency) {
142            CmsModuleDependency other = (CmsModuleDependency)obj;
143            return m_name.equals(other.m_name) && m_version.equals(other.m_version);
144        }
145        return false;
146    }
147
148    /**
149     * Returns the name of the module dependency.<p>
150     *
151     * @return the name of the module dependency
152     */
153    public String getName() {
154
155        return m_name;
156    }
157
158    /**
159     * Returns the minimum version of the dependency.<p>
160     *
161     * @return the minimum version of the dependency
162     */
163    public CmsModuleVersion getVersion() {
164
165        return m_version;
166    }
167
168    /**
169     * @see java.lang.Object#hashCode()
170     */
171    @Override
172    public int hashCode() {
173
174        return m_hashCode;
175    }
176
177    /** Sets the name of a module dependency.<p>
178     *
179     * @param value the name of a module dependency
180     */
181    public void setName(String value) {
182
183        m_name = value;
184    }
185
186    /** Sets the version of a module dependency.<p>
187     *
188     * @param value the version of a module dependency
189     */
190    public void setVersion(CmsModuleVersion value) {
191
192        m_version = value;
193    }
194
195    /**
196     * @see java.lang.Object#toString()
197     */
198    @Override
199    public String toString() {
200
201        return "[" + getClass().getName() + ", name: " + m_name + ", version: " + m_version + "]";
202    }
203}