View Javadoc
1   package fr.ifremer.quadrige2.core.dao.technical;
2   
3   /*-
4    * #%L
5    * Quadrige2 Core :: Quadrige2 Core Shared
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  
27  import com.google.common.base.Function;
28  import com.google.common.base.Preconditions;
29  import com.google.common.collect.Lists;
30  import com.google.common.collect.Maps;
31  import com.google.common.collect.Sets;
32  import fr.ifremer.quadrige2.core.exception.Quadrige2TechnicalException;
33  import org.apache.commons.beanutils.PropertyUtils;
34  import org.apache.commons.collections4.CollectionUtils;
35  import org.apache.commons.collections4.MapUtils;
36  import org.apache.commons.lang3.StringUtils;
37  
38  import java.lang.reflect.InvocationTargetException;
39  import java.util.*;
40  
41  /**
42   * helper class for beans (split by property, make sure list exists, ...)
43   * Created by blavenie on 13/10/15.
44   */
45  public class Beans {
46  
47      /**
48       * <p>getList.</p>
49       *
50       * @param list a {@link java.util.Collection} object.
51       * @param <E> a E object.
52       * @return a {@link java.util.List} object.
53       */
54      public static <E> List<E> getList(Collection<E> list) {
55          if (CollectionUtils.isEmpty(list)) {
56              return Lists.newArrayList();
57          } else {
58              return Lists.newArrayList(list);
59          }
60      }
61  
62      /**
63       * <p>getListWithoutNull.</p>
64       *
65       * @param list a {@link java.util.Collection} object.
66       * @param <E> a E object.
67       * @return a {@link java.util.List} object.
68       */
69      public static <E> List<E> getListWithoutNull(Collection<E> list) {
70          List<E> result = getList(list);
71          result.removeAll(Collections.singleton((E) null));
72          return result;
73      }
74  
75      /**
76       * <p>getSet.</p>
77       *
78       * @param list a {@link java.util.Collection} object.
79       * @param <E> a E object.
80       * @return a {@link java.util.Set} object.
81       */
82      public static <E> Set<E> getSet(Collection<E> list) {
83          if (CollectionUtils.isEmpty(list)) {
84              return Sets.newHashSet();
85          } else {
86              return Sets.newHashSet(list);
87          }
88      }
89  
90      /**
91       * <p>getSetWithoutNull.</p>
92       *
93       * @param list a {@link java.util.Collection} object.
94       * @param <E> a E object.
95       * @return a {@link java.util.Set} object.
96       */
97      public static <E> Set<E> getSetWithoutNull(Collection<E> list) {
98          Set<E> result = getSet(list);
99          result.removeAll(Collections.singleton((E) null));
100         return result;
101     }
102 
103     /**
104      * <p>getMap.</p>
105      *
106      * @param map a {@link java.util.Map} object.
107      * @param <K> a K object.
108      * @param <V> a V object.
109      * @return a {@link java.util.Map} object.
110      */
111     public static <K, V> Map<K, V> getMap(Map<K, V> map) {
112         if (MapUtils.isEmpty(map)) {
113             return Maps.newHashMap();
114         } else {
115             return Maps.newHashMap(map);
116         }
117     }
118 
119     /**
120      * <p>splitByProperty.</p>
121      *
122      * @param list a {@link java.lang.Iterable} object.
123      * @param propertyName a {@link java.lang.String} object.
124      * @param <K> a K object.
125      * @param <V> a V object.
126      * @return a {@link java.util.Map} object.
127      */
128     public static <K, V> Map<K, V> splitByProperty(Iterable<V> list, String propertyName) {
129         Preconditions.checkArgument(StringUtils.isNotBlank(propertyName));
130         Function<V, K> function = newPropertyFunction(propertyName);
131         return getMap(Maps.uniqueIndex(list, function));
132     }
133 
134     /**
135      * <p>collectProperties.</p>
136      *
137      * @param collection a {@link java.util.Collection} object.
138      * @param propertyName a {@link java.lang.String} object.
139      * @param <K> a K object.
140      * @param <V> a V object.
141      * @return a {@link java.util.List} object.
142      */
143     public static <K, V> List<K> collectProperties(Collection<V> collection, String propertyName) {
144         List<V> list;
145         if (collection != null && collection instanceof List) {
146             list = (List<V>) collection;
147         } else {
148             list = getList(collection);
149         }
150         Preconditions.checkArgument(StringUtils.isNotBlank(propertyName));
151         Function<V, K> function = newPropertyFunction(propertyName);
152         return Lists.transform(list, function);
153     }
154 
155     private static <K, V> Function<V, K> newPropertyFunction(final String propertyName) {
156         return input -> getProperty(input, propertyName);
157     }
158 
159     /**
160      * <p>getProperty.</p>
161      *
162      * @param object       a K object.
163      * @param propertyName a {@link java.lang.String} object.
164      * @param <K>          a K object.
165      * @param <V>          a V object.
166      * @return a V object.
167      */
168     @SuppressWarnings("unchecked")
169     public static <K, V> V getProperty(K object, String propertyName) {
170         try {
171             return (V) PropertyUtils.getProperty(object, propertyName);
172         } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
173             throw new Quadrige2TechnicalException(String.format("Could not get property %1s on object of type %2s", propertyName, object.getClass().getName()), e);
174         }
175     }
176 
177     /**
178      * <p>setProperty.</p>
179      *
180      * @param object       a K object.
181      * @param propertyName a {@link java.lang.String} object.
182      * @param value        a V object.
183      * @param <K>          a K object.
184      * @param <V>          a V object.
185      */
186     public static <K, V> void setProperty(K object, String propertyName, V value) {
187         try {
188             PropertyUtils.setProperty(object, propertyName, value);
189         } catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
190             throw new Quadrige2TechnicalException(String.format("Could not set property %1s not found on object of type %2s", propertyName, object.getClass().getName()), e);
191         }
192     }
193 
194     public static Integer[] asIntegerArray(Collection<Integer> values) {
195         if (CollectionUtils.isEmpty(values)) {
196             return null;
197         }
198         return values.toArray(new Integer[values.size()]);
199     }
200 
201     public static String[] asStringArray(Collection<String> values) {
202         if (CollectionUtils.isEmpty(values)) {
203             return null;
204         }
205         return values.toArray(new String[values.size()]);
206     }
207 
208     public static String[] asStringArray(String value, String delimiter) {
209         if (StringUtils.isBlank(value)) return new String[0];
210         StringTokenizer tokenizer = new StringTokenizer(value, delimiter);
211         String[] values = new String[tokenizer.countTokens()];
212         int i=0;
213         while (tokenizer.hasMoreTokens()) {
214             values[i] = tokenizer.nextToken();
215             i++;
216         }
217         return values;
218     }
219 }