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, 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.relations;
029
030import org.opencms.util.CmsUUID;
031
032import org.apache.commons.lang3.builder.HashCodeBuilder;
033import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
034import org.apache.commons.lang3.builder.ToStringStyle;
035
036/**
037 * Immutable bean representing most of the information in a CmsLink.
038 *
039 */
040public class CmsLinkInfo {
041
042    /** Empty link. */
043    public static final CmsLinkInfo EMPTY = new CmsLinkInfo(CmsUUID.getNullUUID(), null, null, null, null, true);
044
045    /** The anchor. */
046    private String m_anchor;
047
048    /** Cached hash code. */
049    private transient int m_hashCode;
050
051    /** Indicates whether the link is internal or not. */
052    private boolean m_internal;
053
054    /** The query. */
055    private String m_query;
056
057    /** The structure id. */
058    private CmsUUID m_structureId;
059
060    /** The link target. */
061    private String m_target;
062
063    /** Cached toString() result. */
064    private transient String m_toStringRepr;
065
066    /** The relation type. */
067    private CmsRelationType m_type;
068
069    /**
070     * Creates a new instance.
071     *
072     * @param structureId the structure id
073     * @param target the link target
074     * @param query the query
075     * @param anchor the anchor
076     * @param type the type
077     * @param internal true if the link is internal
078     */
079    public CmsLinkInfo(
080        CmsUUID structureId,
081        String target,
082        String query,
083        String anchor,
084        CmsRelationType type,
085        boolean internal) {
086
087        m_structureId = structureId;
088        m_target = target;
089        m_query = query;
090        m_anchor = anchor;
091        m_type = type;
092        m_internal = internal;
093        HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
094        // don't use the type in the hash code
095        m_hashCode = hashCodeBuilder.append(m_structureId).append(m_target).append(m_query).append(m_anchor).append(
096            m_internal).toHashCode();
097    }
098
099    /**
100     * @see java.lang.Object#equals(java.lang.Object)
101     */
102    @Override
103    public boolean equals(Object obj) {
104
105        // equals() method auto-generated by Eclipse. Does *not* compare the type.
106
107        if (this == obj) {
108            return true;
109        }
110        if (obj == null) {
111            return false;
112        }
113        if (getClass() != obj.getClass()) {
114            return false;
115        }
116        CmsLinkInfo other = (CmsLinkInfo)obj;
117        if (m_anchor == null) {
118            if (other.m_anchor != null) {
119                return false;
120            }
121        } else if (!m_anchor.equals(other.m_anchor)) {
122            return false;
123        }
124        if (m_internal != other.m_internal) {
125            return false;
126        }
127        if (m_query == null) {
128            if (other.m_query != null) {
129                return false;
130            }
131        } else if (!m_query.equals(other.m_query)) {
132            return false;
133        }
134        if (m_structureId == null) {
135            if (other.m_structureId != null) {
136                return false;
137            }
138        } else if (!m_structureId.equals(other.m_structureId)) {
139            return false;
140        }
141        if (m_target == null) {
142            if (other.m_target != null) {
143                return false;
144            }
145        } else if (!m_target.equals(other.m_target)) {
146            return false;
147        }
148        return true;
149    }
150
151    /**
152     * Gets the anchor.
153     *
154     * @return the anchor
155     */
156    public String getAnchor() {
157
158        return m_anchor;
159    }
160
161    /**
162     * Gets the query
163     *
164     * @return the query
165     */
166    public String getQuery() {
167
168        return m_query;
169    }
170
171    /**
172     * Gets the structure id.
173     *
174     * @return the structure id
175     */
176    public CmsUUID getStructureId() {
177
178        return m_structureId;
179    }
180
181    /**
182     * Gets the target.
183     *
184     * @return the target
185     */
186    public String getTarget() {
187
188        return m_target;
189    }
190
191    /**
192     * Gets the relation type.
193     *
194     * @return the type
195     */
196    public CmsRelationType getType() {
197
198        return m_type;
199    }
200
201    /**
202     * @see java.lang.Object#hashCode()
203     */
204    @Override
205    public int hashCode() {
206
207        return m_hashCode;
208    }
209
210    /**
211     * Checks whether the link is internal.
212     *
213     * @return true if this is an internal
214     */
215    public boolean isInternal() {
216
217        return m_internal;
218    }
219
220    /**
221     * Converts this to a CmsLink.
222     *
223     * @return a new CmsLink instance with the information from this bean
224     */
225    public CmsLink toLink() {
226
227        if (this == EMPTY) {
228            return null;
229        }
230        return new CmsLink(this);
231
232    }
233
234    /**
235     * @see java.lang.Object#toString()
236     */
237    @Override
238    public String toString() {
239
240        if (m_toStringRepr == null) {
241            m_toStringRepr = ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
242        }
243        return m_toStringRepr;
244    }
245
246}