001    /**
002     * Copyright (c) 2000-present 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.HashUtil;
018    import com.liferay.portal.kernel.util.LocaleUtil;
019    import com.liferay.portal.kernel.util.Validator;
020    
021    import java.io.Serializable;
022    
023    import java.util.ArrayList;
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 Iterable<Field>, Serializable {
038    
039            public boolean contains(String name) {
040                    return _fieldsMap.containsKey(name);
041            }
042    
043            @Override
044            public boolean equals(Object obj) {
045                    return equals(obj, true);
046            }
047    
048            public boolean equals(Object obj, boolean includePrivateFields) {
049                    if (this == obj) {
050                            return true;
051                    }
052    
053                    if (!(obj instanceof Fields)) {
054                            return false;
055                    }
056    
057                    Fields fields = (Fields)obj;
058    
059                    if (includePrivateFields) {
060                            return Validator.equals(_fieldsMap, fields._fieldsMap);
061                    }
062    
063                    List<Field> fieldList1 = getFieldsList(includePrivateFields);
064                    List<Field> fieldList2 = fields.getFieldsList(includePrivateFields);
065    
066                    if (fieldList1.size() != fieldList2.size()) {
067                            return false;
068                    }
069    
070                    if (fieldList1.containsAll(fieldList2)) {
071                            return true;
072                    }
073    
074                    return false;
075            }
076    
077            public Field get(String name) {
078                    return _fieldsMap.get(name);
079            }
080    
081            public Set<Locale> getAvailableLocales() {
082                    Set<Locale> availableLocales = new HashSet<>();
083    
084                    for (Field field : _fieldsMap.values()) {
085                            if (field.isPrivate()) {
086                                    continue;
087                            }
088    
089                            for (Locale availableLocale : field.getAvailableLocales()) {
090                                    availableLocales.add(availableLocale);
091                            }
092                    }
093    
094                    return availableLocales;
095            }
096    
097            public long getDDMStructureId() {
098                    long ddmStructureId = 0;
099    
100                    Iterator<Field> itr = iterator();
101    
102                    if (itr.hasNext()) {
103                            Field field = itr.next();
104    
105                            ddmStructureId = field.getDDMStructureId();
106                    }
107    
108                    return ddmStructureId;
109            }
110    
111            public Locale getDefaultLocale() {
112                    Locale defaultLocale = LocaleUtil.getSiteDefault();
113    
114                    Iterator<Field> itr = iterator();
115    
116                    if (itr.hasNext()) {
117                            Field field = itr.next();
118    
119                            defaultLocale = field.getDefaultLocale();
120                    }
121    
122                    return defaultLocale;
123            }
124    
125            public Set<String> getNames() {
126                    return _fieldsMap.keySet();
127            }
128    
129            @Override
130            public int hashCode() {
131                    int hash = HashUtil.hash(0, _fieldsMap);
132    
133                    return HashUtil.hash(hash, getFieldsList(true));
134            }
135    
136            @Override
137            public Iterator<Field> iterator() {
138                    return iterator(false);
139            }
140    
141            public Iterator<Field> iterator(boolean includePrivateFields) {
142                    return iterator(null, includePrivateFields);
143            }
144    
145            public Iterator<Field> iterator(
146                    Comparator<Field> comparator, boolean includePrivateFields) {
147    
148                    List<Field> fieldsList = getFieldsList(includePrivateFields);
149    
150                    if (comparator != null) {
151                            Collections.sort(fieldsList, comparator);
152                    }
153    
154                    return fieldsList.iterator();
155            }
156    
157            public void put(Field field) {
158                    _fieldsMap.put(field.getName(), field);
159            }
160    
161            public Field remove(String name) {
162                    return _fieldsMap.remove(name);
163            }
164    
165            protected List<Field> getFieldsList(boolean includePrivateFields) {
166                    List<Field> fieldsList = new ArrayList<>();
167    
168                    for (Field field : _fieldsMap.values()) {
169                            if (!includePrivateFields && field.isPrivate()) {
170                                    continue;
171                            }
172    
173                            fieldsList.add(field);
174                    }
175    
176                    return fieldsList;
177            }
178    
179            private final Map<String, Field> _fieldsMap = new HashMap<>();
180    
181    }