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 com.liferay.portal.kernel.util.LocaleUtil;
018    import com.liferay.portal.kernel.util.Validator;
019    
020    import java.io.Serializable;
021    
022    import java.util.ArrayList;
023    import java.util.Collection;
024    import java.util.Collections;
025    import java.util.Comparator;
026    import java.util.HashMap;
027    import java.util.HashSet;
028    import java.util.Iterator;
029    import java.util.List;
030    import java.util.Locale;
031    import java.util.Map;
032    import java.util.Set;
033    
034    /**
035     * @author Brian Wing Shun Chan
036     */
037    public class Fields implements Serializable {
038    
039            public boolean contains(String name) {
040                    return _fieldsMap.containsKey(name);
041            }
042    
043            @Override
044            public boolean equals(Object obj) {
045                    if (!(obj instanceof Fields)) {
046                            return false;
047                    }
048    
049                    Fields fields = (Fields)obj;
050    
051                    if (Validator.equals(_fieldsMap, fields._fieldsMap)) {
052                            return true;
053                    }
054    
055                    return false;
056            }
057    
058            public Field get(String name) {
059                    return _fieldsMap.get(name);
060            }
061    
062            public Set<Locale> getAvailableLocales() {
063                    Set<Locale> availableLocales = new HashSet<Locale>();
064    
065                    for (Field field : _fieldsMap.values()) {
066                            for (Locale availableLocale : field.getAvailableLocales()) {
067                                    availableLocales.add(availableLocale);
068                            }
069                    }
070    
071                    return availableLocales;
072            }
073    
074            public Locale getDefaultLocale() {
075                    Locale defaultLocale = LocaleUtil.getDefault();
076    
077                    Iterator<Field> itr = iterator();
078    
079                    if (itr.hasNext()) {
080                            Field field = itr.next();
081    
082                            defaultLocale = field.getDefaultLocale();
083                    }
084    
085                    return defaultLocale;
086            }
087    
088            public Set<String> getNames() {
089                    return _fieldsMap.keySet();
090            }
091    
092            public Iterator<Field> iterator() {
093                    return iterator(false);
094            }
095    
096            public Iterator<Field> iterator(boolean includePrivateFields) {
097                    return iterator(null, includePrivateFields);
098            }
099    
100            public Iterator<Field> iterator(
101                    Comparator<Field> comparator, boolean includePrivateFields) {
102    
103                    Collection<Field> fieldsCollection = _fieldsMap.values();
104    
105                    List<Field> fieldsList = new ArrayList<Field>();
106    
107                    Iterator<Field> itr = fieldsCollection.iterator();
108    
109                    while (itr.hasNext()) {
110                            Field field = itr.next();
111    
112                            if (!includePrivateFields && field.isPrivate()) {
113                                    continue;
114                            }
115    
116                            fieldsList.add(field);
117                    }
118    
119                    if (comparator != null) {
120                            Collections.sort(fieldsList, comparator);
121                    }
122    
123                    return fieldsList.iterator();
124            }
125    
126            public void put(Field field) {
127                    _fieldsMap.put(field.getName(), field);
128            }
129    
130            public Field remove(String name) {
131                    return _fieldsMap.remove(name);
132            }
133    
134            private Map<String, Field> _fieldsMap = new HashMap<String, Field>();
135    
136    }