001    /**
002     * Copyright (c) 2000-2011 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.util.ArrayList;
018    import java.util.Collection;
019    import java.util.Collections;
020    import java.util.Comparator;
021    import java.util.HashMap;
022    import java.util.Iterator;
023    import java.util.List;
024    import java.util.Map;
025    import java.util.Set;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class Fields {
031    
032            public boolean contains(String name) {
033                    return _fieldsMap.containsKey(name);
034            }
035    
036            public Field get(String name) {
037                    return _fieldsMap.get(name);
038            }
039    
040            public Set<String> getNames() {
041                    return _fieldsMap.keySet();
042            }
043    
044            public Iterator<Field> iterator() {
045                    return iterator(null);
046            }
047    
048            public Iterator<Field> iterator(Comparator<Field> comparator) {
049                    Collection<Field> fieldsCollection = _fieldsMap.values();
050    
051                    List<Field> fieldsList = new ArrayList<Field>(fieldsCollection);
052    
053                    if (comparator != null) {
054                            Collections.sort(fieldsList, comparator);
055                    }
056    
057                    return fieldsList.iterator();
058            }
059    
060            public void put(Field field) {
061                    _fieldsMap.put(field.getName(), field);
062            }
063    
064            public Field remove(String name) {
065                    return _fieldsMap.remove(name);
066            }
067    
068            private Map<String, Field> _fieldsMap = new HashMap<String, Field>();
069    
070    }