List of usage examples for java.util Collection getClass
@HotSpotIntrinsicCandidate public final native Class<?> getClass();
From source file:com.shenit.commons.utils.CollectionUtils.java
/** * Found out the item that col1 contains but col2 not contains. * @param col1 Collection 1/*from w ww . j a v a2 s . c om*/ * @param col2 Collection 2 * @return The items only in col1 but not col2 */ @SuppressWarnings("unchecked") public static <T> Collection<T> diff(Collection<T> col1, Collection<T> col2) { if (ValidationUtils.isEmpty(col1)) return col2; else if (ValidationUtils.isEmpty(col2)) return col1; try { Collection<T> diff = col1.getClass().newInstance(); col1.stream().forEach((item1) -> { if (col2.parallelStream().noneMatch((item2) -> ValidationUtils.eq(item1, item2))) { diff.add(item1); } }); return diff; } catch (InstantiationException | IllegalAccessException e) { LOG.warn("[diff] Could not create instance for {} with empty parameter constructor", col1.getClass()); } return null; }
From source file:ome.tools.hibernate.HibernateUtils.java
@SuppressWarnings("unchecked") protected static Collection copy(Collection c) { if (c instanceof Set) { return new HashSet(c); }/*from w w w .ja v a2 s. c o m*/ else if (c instanceof List) { return new ArrayList(c); } else { throw new InternalException("Unsupported collection type:" + c.getClass().getName()); } }
From source file:com.igormaznitsa.upom.UPomModel.java
private static Collection cloneCollection(final Collection collection) throws Exception { final Class collectionClass = collection.getClass(); final Constructor constructor = collectionClass.getConstructor(Collection.class); return (Collection) constructor.newInstance(collection); }
From source file:org.apache.cassandra.cql.jdbc.HandleObjects.java
private static final Class<?> getCollectionElementType(Object maybeCollection) { Collection trial = (Collection) maybeCollection; if (trial.isEmpty()) return trial.getClass(); else/*from ww w .j a v a2 s .co m*/ return trial.iterator().next().getClass(); }
From source file:org.apache.pig.impl.util.Utils.java
@SuppressWarnings("unchecked") public static <O> Collection<O> mergeCollection(Collection<O> a, Collection<O> b) { if (a == null && b == null) return null; Collection<O> result = null; try {/* w w w . j a v a 2 s .co m*/ if (a != null) result = a.getClass().newInstance(); else result = b.getClass().newInstance(); } catch (Exception e) { // Shall not happen } if (a == null) { result.addAll(b); } else if (b == null) { result.addAll(a); } else { result.addAll(a); for (O o : b) { if (!result.contains(o)) { result.add(o); } } } return result; }
From source file:br.msf.commons.util.CollectionUtils.java
public static <T extends Object> Collection<T> getCompatibleInstance(final Collection<T> collection) { Collection<T> instance = null; if (collection != null) { try {/*from ww w .j a v a 2s . co m*/ // try to use the same type of collection of the source instance = collection.getClass().newInstance(); } catch (InstantiationException | IllegalAccessException ex) { /** * Could not instantiate via reflection. probably is a * Unmodifiable Collection (not meant to be instantiated * directly) Fallback to common implementations... */ if (ObjectUtils.isSortedSet(collection)) { instance = new TreeSet<>(); } else if (ObjectUtils.isSet(collection)) { instance = new LinkedHashSet<>(collection.size()); } else if (ObjectUtils.isQueue(collection)) { instance = new LinkedList<>(); // implements List, Queue and Deque (deque is a queue) } else { instance = new ArrayList<>(collection.size()); } } } return instance; }
From source file:br.ojimarcius.commons.util.CollectionUtils.java
public static <T extends Object> Collection<T> getCompatibleInstance(final Collection<T> collection) { Collection<T> instance = null; if (collection != null) { try {// w w w . j a va 2 s. c o m // try to use the same type of collection of the source instance = collection.getClass().newInstance(); } catch (Exception ex) { /** * Could not instantiate via reflection. probably is a * Unmodifiable Collection (not meant to be instantiated * directly) Fallback to common implementations... */ if (ObjectUtils.isSortedSet(collection)) { instance = new TreeSet<T>(); } else if (ObjectUtils.isSet(collection)) { instance = new LinkedHashSet<T>(collection.size()); } else if (ObjectUtils.isQueue(collection)) { instance = new LinkedList<T>(); // implements List, Queue and Deque (deque is a queue) } else { instance = new ArrayList<T>(collection.size()); } } } return instance; }
From source file:jef.tools.reflect.ClassEx.java
@SuppressWarnings({ "rawtypes", "unchecked" }) private static Object checkAndConvertCollection(Collection rawValue, ClassEx container) { Type type = GenericUtils.getCollectionType(container.genericType); if (type == Object.class) return rawValue; ClassEx t = new ClassEx(type); Collection result;/*w ww. j a v a2 s . co m*/ try { result = rawValue.getClass().newInstance(); } catch (InstantiationException e1) { throw new IllegalArgumentException(e1); } catch (IllegalAccessException e1) { throw new IllegalArgumentException(e1); } for (Object e : rawValue) { e = toProperType(e, t, null); result.add(e); } return result; }
From source file:org.jets3t.service.utils.ServiceUtils.java
/** * From a map of metadata returned from a REST GET or HEAD request, returns a map * of metadata with the HTTP-connection-specific metadata items removed. * * @param metadata//from w w w . j av a 2 s . co m * metadata map to be cleaned * @param headerPrefix * prefix denoting service-specific "header" HTTP header values (case insensitive) * @param metadataPrefix * prefix denoting service-specific "metadata" HTTP header values (case insensitive) * @return * metadata map with HTTP-connection-specific items removed. */ public static Map<String, Object> cleanRestMetadataMap(Map<String, Object> metadata, String headerPrefix, String metadataPrefix) { if (log.isDebugEnabled()) { log.debug("Cleaning up REST metadata items"); } Map<String, Object> cleanMap = new HashMap<String, Object>(); if (metadata != null) { for (Map.Entry<String, Object> entry : metadata.entrySet()) { String key = entry.getKey(); Object value = entry.getValue(); // Trim prefixes from keys. String keyStr = (key != null ? key.toString() : ""); if (keyStr.toLowerCase().startsWith(metadataPrefix)) { key = keyStr.substring(metadataPrefix.length(), keyStr.length()); if (log.isDebugEnabled()) { log.debug("Removed meatadata header prefix " + metadataPrefix + " from key: " + keyStr + "=>" + key); } } else if (keyStr.toLowerCase().startsWith(headerPrefix)) { key = keyStr.substring(headerPrefix.length(), keyStr.length()); if (log.isDebugEnabled()) { log.debug("Removed header prefix " + headerPrefix + " from key: " + keyStr + "=>" + key); } } else if (RestUtils.HTTP_HEADER_METADATA_NAMES.contains(keyStr.toLowerCase(Locale.getDefault()))) { key = keyStr; if (log.isDebugEnabled()) { log.debug("Leaving HTTP header item unchanged: " + key + "=" + value); } } else if ("ETag".equalsIgnoreCase(keyStr) || "Date".equalsIgnoreCase(keyStr) || "Last-Modified".equalsIgnoreCase(keyStr) || "Content-Range".equalsIgnoreCase(keyStr)) { key = keyStr; if (log.isDebugEnabled()) { log.debug("Leaving header item unchanged: " + key + "=" + value); } } else { if (log.isDebugEnabled()) { log.debug("Ignoring metadata item: " + keyStr + "=" + value); } continue; } // Convert connection header string Collections into simple strings (where // appropriate) if (value instanceof Collection) { Collection<?> coll = (Collection<?>) value; if (coll.size() == 1) { if (log.isDebugEnabled()) { log.debug("Converted metadata single-item Collection " + coll.getClass() + " " + coll + " for key: " + key); } value = coll.iterator().next(); } else { if (log.isWarnEnabled()) { log.warn("Collection " + coll + " has too many items to convert to a single string"); } } } // Parse date strings into Date objects, if necessary. if ("Date".equals(key) || "Last-Modified".equals(key)) { if (!(value instanceof Date)) { if (log.isDebugEnabled()) { log.debug("Parsing date string '" + value + "' into Date object for key: " + key); } try { value = ServiceUtils.parseRfc822Date(value.toString()); } catch (ParseException pe) { // Try ISO-8601 date format, just in case try { value = ServiceUtils.parseIso8601Date(value.toString()); } catch (ParseException pe2) { // Log original exception if the work-around fails. if (log.isWarnEnabled()) { log.warn("Date string is not RFC 822 compliant for metadata field " + key, pe); } } } } } cleanMap.put(key, value); } } return cleanMap; }
From source file:org.marketcetera.util.ws.types.TypeTest.java
private static void assertColEquals(Collection<?> expected, Collection<?> actual) { assertEquals(expected, actual);/* www. j av a2 s . c om*/ /* * LIMITATION: the unmarshalled collection is an ArrayList * regardless of the actual type of the original collection. */ assertEquals(ArrayList.class, actual.getClass()); }