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.ade.containerpage.shared;
029
030import java.util.ArrayList;
031import java.util.Iterator;
032
033import com.google.gwt.user.client.rpc.IsSerializable;
034
035/**
036 * Bean which holds a list of formatter configurations (client-side) which can be retrieved by key or id.
037 */
038public class CmsFormatterConfigCollection implements Iterable<CmsFormatterConfig>, IsSerializable {
039
040    /** The list of formatter beans. */
041    private ArrayList<CmsFormatterConfig> m_formatters = new ArrayList<>();
042
043    /**
044     * Adds another formatter configuration.
045     *
046     * @param config the formatter configuration bean
047     */
048    public void add(CmsFormatterConfig config) {
049
050        m_formatters.add(config);
051    }
052
053    /**
054     * Gets the formatter configuration for the given key or id.
055     *
056     * @param keyOrId the formatter key or id
057     * @return the first formatter configuration with that key or id, or null if none were found
058     */
059    public CmsFormatterConfig get(String keyOrId) {
060        
061        for (CmsFormatterConfig config : this) {
062            if (keyOrId.equals(config.getKey()) || keyOrId.equals(config.getId())) {
063                return config;
064            }
065        }
066        return null;
067    }
068
069    /**
070     * Gets the first formatter configuration bean.
071     *
072     * @return the first formatter configuration bean
073     */
074    public CmsFormatterConfig getFirstFormatter() {
075
076        return m_formatters.iterator().next();
077    }
078
079    /**
080     * Gets an iterator over the formatter beans.
081     *
082     * @see java.lang.Iterable#iterator()
083     */
084    public Iterator<CmsFormatterConfig> iterator() {
085
086        return m_formatters.iterator();
087    }
088
089    /**
090     * Returns the number of formatters.
091     *
092     * @return the number of formatters
093     */
094    public int size() {
095
096        return m_formatters.size();
097    }
098
099    /**
100     * @see java.lang.Object#toString()
101     */
102    @Override
103    public String toString() {
104
105        StringBuilder result = new StringBuilder();
106        for (CmsFormatterConfig config : this) {
107            result.append("[" + config.getKey() + "/" + config.getId() + "]\n");
108        }
109        return result.toString();
110    }
111
112}