001
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
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 if (this == obj) {
046 return true;
047 }
048
049 if (!(obj instanceof Fields)) {
050 return false;
051 }
052
053 Fields fields = (Fields)obj;
054
055 if (Validator.equals(_fieldsMap, fields._fieldsMap)) {
056 return true;
057 }
058
059 return false;
060 }
061
062 public Field get(String name) {
063 return _fieldsMap.get(name);
064 }
065
066 public Set<Locale> getAvailableLocales() {
067 Set<Locale> availableLocales = new HashSet<Locale>();
068
069 for (Field field : _fieldsMap.values()) {
070 if (field.isPrivate()) {
071 continue;
072 }
073
074 for (Locale availableLocale : field.getAvailableLocales()) {
075 availableLocales.add(availableLocale);
076 }
077 }
078
079 return availableLocales;
080 }
081
082 public Locale getDefaultLocale() {
083 Locale defaultLocale = LocaleUtil.getSiteDefault();
084
085 Iterator<Field> itr = iterator();
086
087 if (itr.hasNext()) {
088 Field field = itr.next();
089
090 defaultLocale = field.getDefaultLocale();
091 }
092
093 return defaultLocale;
094 }
095
096 public Set<String> getNames() {
097 return _fieldsMap.keySet();
098 }
099
100 @Override
101 public Iterator<Field> iterator() {
102 return iterator(false);
103 }
104
105 public Iterator<Field> iterator(boolean includePrivateFields) {
106 return iterator(null, includePrivateFields);
107 }
108
109 public Iterator<Field> iterator(
110 Comparator<Field> comparator, boolean includePrivateFields) {
111
112 Collection<Field> fieldsCollection = _fieldsMap.values();
113
114 List<Field> fieldsList = new ArrayList<Field>();
115
116 Iterator<Field> itr = fieldsCollection.iterator();
117
118 while (itr.hasNext()) {
119 Field field = itr.next();
120
121 if (!includePrivateFields && field.isPrivate()) {
122 continue;
123 }
124
125 fieldsList.add(field);
126 }
127
128 if (comparator != null) {
129 Collections.sort(fieldsList, comparator);
130 }
131
132 return fieldsList.iterator();
133 }
134
135 public void put(Field field) {
136 _fieldsMap.put(field.getName(), field);
137 }
138
139 public Field remove(String name) {
140 return _fieldsMap.remove(name);
141 }
142
143 private Map<String, Field> _fieldsMap = new HashMap<String, Field>();
144
145 }