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.search.fields;
029
030import java.util.ArrayList;
031import java.util.LinkedHashMap;
032import java.util.List;
033import java.util.Map;
034import java.util.Objects;
035
036import org.apache.solr.uninverting.UninvertingReader.Type;
037
038/**
039 * Base class for a typical field configuration. Basically handles name and description
040 * and provides defaults for interface methods typically not of interest for most implementations.
041 */
042public abstract class A_CmsSearchFieldConfiguration implements I_CmsSearchFieldConfiguration {
043
044    /** The serial version id. */
045    private static final long serialVersionUID = 7948072454782743591L;
046
047    /** Description of the field configuration. */
048    private String m_description;
049    /** Name of the field configuration. */
050    private String m_name;
051
052    /** Map to lookup the configured {@link CmsSearchField} instances by name. */
053    private Map<String, CmsSearchField> m_fields;
054
055    /**
056     * Creates a new empty field configuration.
057     */
058    public A_CmsSearchFieldConfiguration() {
059
060        m_fields = new LinkedHashMap<String, CmsSearchField>();
061    }
062
063    /**
064     * Adds a field to this search field configuration.<p>
065     *
066     * @param field the field to add
067     */
068    public void addField(CmsSearchField field) {
069
070        if ((field != null) && (field.getName() != null)) {
071            m_fields.put(field.getName(), field);
072        }
073    }
074
075    /**
076     * @see org.opencms.search.fields.I_CmsSearchFieldConfiguration#addUninvertingMappings(java.util.Map)
077     */
078    public void addUninvertingMappings(Map<String, Type> uninvertingMap) {
079
080        // Do nothing by default
081    }
082
083    /**
084     * @see java.lang.Comparable#compareTo(java.lang.Object)
085     */
086    public int compareTo(I_CmsSearchFieldConfiguration o) {
087
088        return m_name.compareTo(o.getName());
089    }
090
091    /**
092     * @see java.lang.Object#equals(java.lang.Object)
093     */
094    @Override
095    public boolean equals(Object obj) {
096
097        if (obj == this) {
098            return true;
099        }
100        if (obj.getClass().getName().equals(obj.getClass().getName())) {
101            return Objects.equals(((CmsSearchFieldConfiguration)obj).getName(), getName());
102        }
103        return false;
104    }
105
106    /**
107     * @see org.opencms.search.fields.I_CmsSearchFieldConfiguration#getDescription()
108     */
109    public String getDescription() {
110
111        return m_description;
112    }
113
114    /**
115     * Returns the configured {@link CmsSearchField} instance with the given name.<p>
116     *
117     * @param name the search field name to look up
118     *
119     * @return the configured {@link CmsSearchField} instance with the given name
120     */
121    public CmsSearchField getField(String name) {
122
123        return m_fields.get(name);
124    }
125
126    /**
127     * Returns the list of configured field names (Strings).<p>
128     *
129     * @return the list of configured field names (Strings)
130     */
131    public List<String> getFieldNames() {
132
133        // create a copy of the list to prevent changes in other classes
134        return new ArrayList<String>(m_fields.keySet());
135    }
136
137    /**
138     * Returns the list of configured {@link CmsSearchField} instances.<p>
139     *
140     * @return the list of configured {@link CmsSearchField} instances
141     */
142    @Override
143    public List<CmsSearchField> getFields() {
144
145        return new ArrayList<>(m_fields.values());
146    }
147
148    /**
149     * @see org.opencms.search.fields.I_CmsSearchFieldConfiguration#getName()
150     */
151    public String getName() {
152
153        return m_name;
154    }
155
156    /**
157     * @see java.lang.Object#hashCode()
158     */
159    @Override
160    public int hashCode() {
161
162        return null == m_name ? 0 : m_name.hashCode();
163    }
164
165    /**
166     * @see org.opencms.search.fields.I_CmsSearchFieldConfiguration#init()
167     */
168    public void init() {
169
170        // By default do nothing
171
172    }
173
174    /**
175     * @see org.opencms.search.fields.I_CmsSearchFieldConfiguration#setDescription(java.lang.String)
176     */
177    public void setDescription(String description) {
178
179        m_description = description;
180
181    }
182
183    /**
184     * @see org.opencms.search.fields.I_CmsSearchFieldConfiguration#setName(java.lang.String)
185     */
186    public void setName(String name) {
187
188        m_name = name;
189
190    }
191}