001package com.hfg.util.collection;
002
003import java.util.ArrayList;
004import java.util.Collection;
005import java.util.Comparator;
006import java.util.LinkedHashMap;
007import java.util.List;
008import java.util.Map;
009import java.util.Random;
010import java.util.stream.Stream;
011import javax.swing.*;
012
013
014//------------------------------------------------------------------------------
015/**
016 Collection utility functions.
017 @author J. Alex Taylor, hairyfatguy.com
018 */
019//------------------------------------------------------------------------------
020// com.hfg XML/HTML Coding Library
021//
022// This library is free software; you can redistribute it and/or
023// modify it under the terms of the GNU Lesser General Public
024// License as published by the Free Software Foundation; either
025// version 2.1 of the License, or (at your option) any later version.
026//
027// This library is distributed in the hope that it will be useful,
028// but WITHOUT ANY WARRANTY; without even the implied warranty of
029// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
030// Lesser General Public License for more details.
031//
032// You should have received a copy of the GNU Lesser General Public
033// License along with this library; if not, write to the Free Software
034// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
035//
036// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
037// jataylor@hairyfatguy.com
038//------------------------------------------------------------------------------
039
040public class CollectionUtil
041{
042   private static Random sRandom;
043
044        //##########################################################################
045        // PUBLIC METHODS
046        //##########################################################################
047
048   //--------------------------------------------------------------------------
049   /**
050    Returns whether or not the specified Collection contains values.
051    @param inCollection the Collection to evaluate
052    @return whether or not the specified collection contains values
053    */
054   public static boolean hasValues(Collection inCollection)
055   {
056      return (inCollection != null && inCollection.size() > 0);
057   }
058
059   //--------------------------------------------------------------------------
060   /**
061    Returns whether or not the specified Map contains values.
062    @param inMap the Map to evaluate
063    @return whether or not the specified Map contains values
064    */
065   public static boolean hasValues(Map inMap)
066   {
067      return (inMap != null && inMap.size() > 0);
068   }
069
070   //--------------------------------------------------------------------------
071   /**
072    Returns whether or not the specified Collection contains a single value.
073    @param inCollection the Collection to evaluate
074    @return whether or not the specified collection has a single value
075    */
076   public static boolean hasSingleValue(Collection inCollection)
077   {
078      return (inCollection != null && 1 == inCollection.size());
079   }
080
081   //--------------------------------------------------------------------------
082   /**
083    Returns the sum of the specified Collection.
084    @param inCollection the Collection of numbers to sum
085    @return the sum of the collection
086    */
087   public static float sum(Collection<? extends Number> inCollection)
088   {
089      float sum = 0;
090      if (hasValues(inCollection))
091      {
092         for (Number num : inCollection)
093         {
094            if (num != null)
095            {
096               sum += num.floatValue();
097            }
098         }
099      }
100
101      return sum;
102   }
103
104   //--------------------------------------------------------------------------
105   public static <T> List<List<T>> chunk(Collection<T> inCollection, int inChunkSize)
106   {
107      List<List<T>> chunks = new ArrayList<>();
108
109      List<T> currentSublist = null;
110      int i = 0;
111      for (T obj : inCollection)
112      {
113         i++;
114         if (null == currentSublist
115              || i > inChunkSize)
116         {
117            currentSublist = new ArrayList<>(inChunkSize);
118            chunks.add(currentSublist);
119            i = 1;
120         }
121
122         currentSublist.add(obj);
123      }
124
125      return chunks;
126   }
127
128   //--------------------------------------------------------------------------
129   public static <T> List<List<T>> chunkList(List<T> inList, int inChunkSize)
130   {
131      List<List<T>> chunks = new ArrayList<>();
132
133      int startIdx = 0;
134      int endIdx   = inChunkSize;
135
136      while (startIdx < inList.size())
137      {
138         if (endIdx > inList.size())
139         {
140            endIdx = inList.size();
141         }
142
143         chunks.add(new ArrayList<>(inList.subList(startIdx, endIdx)));
144
145         startIdx = endIdx;
146         endIdx   = startIdx + inChunkSize;
147      }
148
149      return chunks;
150   }
151
152   //--------------------------------------------------------------------------
153   public static <K, V extends Comparable<? super V>> Map<K, V> sortMapByValue(Map<K, V> inMap)
154   {
155      return sortMapByValue(inMap, SortOrder.ASCENDING);
156   }
157
158   //--------------------------------------------------------------------------
159   public static <K, V extends Comparable<? super V>> Map<K, V> sortMapByValue(Map<K, V> inMap, SortOrder inSortOrder)
160   {
161      Map<K,V> result = new LinkedHashMap<>();
162
163      Stream<Map.Entry<K,V>> entrySetStream = inMap.entrySet().stream();
164
165      if (null == inSortOrder
166          || inSortOrder.equals(SortOrder.ASCENDING))
167      {
168         entrySetStream = entrySetStream.sorted(Comparator.comparing(e -> e.getValue()));
169      }
170      else
171      {
172         entrySetStream = entrySetStream.sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()));
173      }
174
175      entrySetStream.forEach(e ->result.put(e.getKey(),e.getValue()));
176
177      return result;
178   }
179
180
181   //--------------------------------------------------------------------------
182   public static <T> T getRandomListItem(List<T> inCollection)
183   {
184      return inCollection.get(getRandom().nextInt((inCollection.size())));
185   }
186
187   //--------------------------------------------------------------------------
188   private static synchronized Random getRandom()
189   {
190      if (null == sRandom)
191      {
192         sRandom = new Random();
193      }
194
195      return sRandom;
196   }
197}