001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.dynamicdatamapping.storage;
016    
017    import java.io.Serializable;
018    
019    import java.util.ArrayList;
020    import java.util.Collection;
021    import java.util.Collections;
022    import java.util.Comparator;
023    import java.util.HashMap;
024    import java.util.Iterator;
025    import java.util.List;
026    import java.util.Map;
027    import java.util.Set;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     */
032    public class Fields implements Serializable {
033    
034            public boolean contains(String name) {
035                    return _fieldsMap.containsKey(name);
036            }
037    
038            public Field get(String name) {
039                    return _fieldsMap.get(name);
040            }
041    
042            public Set<String> getNames() {
043                    return _fieldsMap.keySet();
044            }
045    
046            public Iterator<Field> iterator() {
047                    return iterator(null);
048            }
049    
050            public Iterator<Field> iterator(Comparator<Field> comparator) {
051                    Collection<Field> fieldsCollection = _fieldsMap.values();
052    
053                    List<Field> fieldsList = new ArrayList<Field>(fieldsCollection);
054    
055                    if (comparator != null) {
056                            Collections.sort(fieldsList, comparator);
057                    }
058    
059                    return fieldsList.iterator();
060            }
061    
062            public void put(Field field) {
063                    _fieldsMap.put(field.getName(), field);
064            }
065    
066            public Field remove(String name) {
067                    return _fieldsMap.remove(name);
068            }
069    
070            private Map<String, Field> _fieldsMap = new HashMap<String, Field>();
071    
072    }