001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH (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, 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
030/**
031 * Describes an OpenCms module dependency.<p>
032 *
033 * Module dependencies are checked if a module is imported or deleted.
034 * If a module A requires certain resources (like Java classes)
035 * from another module B, a should be made dependend on B.<p>
036
037 *
038 * @since 6.0.0
039 */
040public class CmsModuleDependency implements Comparable<Object> {
041
042    /** The hash code of the module dependency. */
043    private int m_hashCode;
044
045    /** The name of the module dependency. */
046    private String m_name;
047
048    /** The (minimum) version of the module dependency. */
049    private CmsModuleVersion m_version;
050
051    /**
052     * Generates a new, empty module dependency.<p>
053     *
054     */
055    public CmsModuleDependency() {
056
057        super();
058        m_name = "";
059        m_version = new CmsModuleVersion("0");
060
061        // pre - calculate the hash code
062        m_hashCode = m_name.concat(m_version.toString()).hashCode();
063    }
064
065    /**
066     * Generates a new module dependency.<p>
067     *
068     * @param moduleName the name of the module dependency
069     * @param minVersion the minimum version of the dependency
070     */
071    public CmsModuleDependency(String moduleName, CmsModuleVersion minVersion) {
072
073        super();
074        m_name = moduleName;
075        m_version = minVersion;
076
077        // pre - calculate the hash code
078        m_hashCode = m_name.concat(m_version.toString()).hashCode();
079    }
080
081    /**
082     * @see java.lang.Object#clone()
083     */
084    @Override
085    public Object clone() {
086
087        return new CmsModuleDependency(m_name, new CmsModuleVersion(m_version.getVersion()));
088    }
089
090    /**
091     * @see java.lang.Comparable#compareTo(java.lang.Object)
092     */
093    public int compareTo(Object obj) {
094
095        if (obj == this) {
096            return 0;
097        }
098        if (obj instanceof CmsModuleDependency) {
099            CmsModuleDependency other = (CmsModuleDependency)obj;
100            if (!m_name.equals(other.m_name)) {
101                // not same name means no dependency
102                return 0;
103            }
104            // same name: result depends on version numbers
105            return m_version.compareTo(other.m_version);
106        }
107        return 0;
108    }
109
110    /**
111     * Checks if this module depedency depends on another given module dependency.<p>
112     *
113     * @param other the other dependency to check against
114     * @return true if this module depedency depends on the given module dependency
115     */
116    public boolean dependesOn(CmsModuleDependency other) {
117
118        if (!m_name.equals(other.m_name)) {
119            // not same name means no dependency
120            return false;
121        }
122
123        // same name: result depends on version numbers
124        return (m_version.compareTo(other.m_version) <= 0);
125    }
126
127    /**
128     * @see java.lang.Object#equals(java.lang.Object)
129     */
130    @Override
131    public boolean equals(Object obj) {
132
133        if (obj == this) {
134            return true;
135        }
136        if (obj instanceof CmsModuleDependency) {
137            CmsModuleDependency other = (CmsModuleDependency)obj;
138            return m_name.equals(other.m_name) && m_version.equals(other.m_version);
139        }
140        return false;
141    }
142
143    /**
144     * Returns the name of the module dependency.<p>
145     *
146     * @return the name of the module dependency
147     */
148    public String getName() {
149
150        return m_name;
151    }
152
153    /**
154     * Returns the minimum version of the dependency.<p>
155     *
156     * @return the minimum version of the dependency
157     */
158    public CmsModuleVersion getVersion() {
159
160        return m_version;
161    }
162
163    /**
164     * @see java.lang.Object#hashCode()
165     */
166    @Override
167    public int hashCode() {
168
169        return m_hashCode;
170    }
171
172    /** Sets the name of a module dependency.<p>
173     *
174     * @param value the name of a module dependency
175     */
176    public void setName(String value) {
177
178        m_name = value;
179    }
180
181    /** Sets the version of a module dependency.<p>
182     *
183     * @param value the version of a module dependency
184     */
185    public void setVersion(CmsModuleVersion value) {
186
187        m_version = value;
188    }
189
190    /**
191     * @see java.lang.Object#toString()
192     */
193    @Override
194    public String toString() {
195
196        return "[" + getClass().getName() + ", name: " + m_name + ", version: " + m_version + "]";
197    }
198}