001    /**
002     * Copyright (c) 2000-2013 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 com.liferay.portal.kernel.util.Validator;
018    
019    import java.io.Serializable;
020    
021    import java.util.ArrayList;
022    import java.util.Collection;
023    import java.util.Collections;
024    import java.util.Comparator;
025    import java.util.HashMap;
026    import java.util.Iterator;
027    import java.util.List;
028    import java.util.Map;
029    import java.util.Set;
030    
031    /**
032     * @author Brian Wing Shun Chan
033     */
034    public class Fields implements Serializable {
035    
036            public boolean contains(String name) {
037                    return _fieldsMap.containsKey(name);
038            }
039    
040            @Override
041            public boolean equals(Object obj) {
042                    if (this == obj) {
043                            return true;
044                    }
045    
046                    if (!(obj instanceof Fields)) {
047                            return false;
048                    }
049    
050                    Fields fields = (Fields)obj;
051    
052                    if (Validator.equals(_fieldsMap, fields._fieldsMap)) {
053                            return true;
054                    }
055    
056                    return false;
057            }
058    
059            public Field get(String name) {
060                    return _fieldsMap.get(name);
061            }
062    
063            public Set<String> getNames() {
064                    return _fieldsMap.keySet();
065            }
066    
067            public Iterator<Field> iterator() {
068                    return iterator(null);
069            }
070    
071            public Iterator<Field> iterator(Comparator<Field> comparator) {
072                    Collection<Field> fieldsCollection = _fieldsMap.values();
073    
074                    List<Field> fieldsList = new ArrayList<Field>(fieldsCollection);
075    
076                    if (comparator != null) {
077                            Collections.sort(fieldsList, comparator);
078                    }
079    
080                    return fieldsList.iterator();
081            }
082    
083            public void put(Field field) {
084                    _fieldsMap.put(field.getName(), field);
085            }
086    
087            public Field remove(String name) {
088                    return _fieldsMap.remove(name);
089            }
090    
091            private Map<String, Field> _fieldsMap = new HashMap<String, Field>();
092    
093    }