List of usage examples for java.util Collection isEmpty
boolean isEmpty();
From source file:Main.java
private static SSLContext sslContextForTrustedCertificates(InputStream in) { try {//from w w w . j a v a 2 s . c o m CertificateFactory e = CertificateFactory.getInstance("X.509"); Collection certificates = e.generateCertificates(in); if (certificates.isEmpty()) { throw new IllegalArgumentException("expected non-empty set of trusted certificates"); } else { char[] password = "password".toCharArray(); KeyStore keyStore = newEmptyKeyStore(password); int index = 0; Iterator keyManagerFactory = certificates.iterator(); while (keyManagerFactory.hasNext()) { Certificate trustManagerFactory = (Certificate) keyManagerFactory.next(); String sslContext = Integer.toString(index++); keyStore.setCertificateEntry(sslContext, trustManagerFactory); } KeyManagerFactory var10 = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); var10.init(keyStore, password); TrustManagerFactory var11 = TrustManagerFactory .getInstance(TrustManagerFactory.getDefaultAlgorithm()); var11.init(keyStore); SSLContext var12 = SSLContext.getInstance("TLS"); var12.init(var10.getKeyManagers(), var11.getTrustManagers(), new SecureRandom()); return var12; } } catch (Exception var9) { var9.printStackTrace(); } return null; }
From source file:Main.java
public static <T> T single(Collection<T> cl, boolean force, Object msg) { String amsg = msg == null ? "" : "," + msg.toString(); if (cl.isEmpty()) { if (force) { throw new RuntimeException("force:" + cl + amsg); } else {/* www .ja va2 s . c om*/ return null; } } else if (cl.size() > 1) { throw new RuntimeException("tomuch:" + cl + amsg); } else { return cl.iterator().next(); } }
From source file:Main.java
public static <T> T getFirstElement(Collection<T> collection, T defaultValue) { T elem = defaultValue;/*from w w w .java 2 s. com*/ if (collection != null && !collection.isEmpty()) { if (List.class.isAssignableFrom(collection.getClass())) { elem = ((List<T>) collection).get(0); } else { for (T item : collection) { elem = item; break; } } } return elem; }
From source file:com.github.horrorho.inflatabledonkey.cloud.clients.SnapshotClient.java
public static List<Snapshot> snapshots(HttpClient httpClient, CloudKitty kitty, ProtectionZone zone, Collection<SnapshotID> snapshotIDs) throws IOException { if (snapshotIDs.isEmpty()) { return new ArrayList<>(); }/* w w w . ja v a 2 s . c o m*/ List<String> snapshots = snapshotIDs.stream().map(SnapshotID::id).collect(Collectors.toList()); List<CloudKit.RecordRetrieveResponse> responses = kitty.recordRetrieveRequest(httpClient, "mbksync", snapshots); logger.debug("-- manifests() - responses: {}", responses); return responses.stream().filter(CloudKit.RecordRetrieveResponse::hasRecord) .map(CloudKit.RecordRetrieveResponse::getRecord).map(r -> manifests(r, zone)) .filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList()); }
From source file:com.anhth12.lambda.common.math.VectorMath.java
public static RealMatrix transposeTimesSelf(Collection<float[]> M) { if (M == null || M.isEmpty()) { return null; }//from w ww .j av a2 s .com int features = 0; RealMatrix result = null; for (float[] vector : M) { if (result == null) { features = vector.length; result = new Array2DRowRealMatrix(features, features); } for (int row = 0; row < features; row++) { float rowValue = vector[row]; for (int col = 0; col < features; col++) { result.addToEntry(row, col, rowValue * vector[col]); } } } return result; }
From source file:Main.java
/** * Determine the index position of the first occurrence of the given object instance in the given collection. Thereby avoiding the use of * {@link Object#equals(Object) equals(Object)} and {@link Object#hashCode() hashCode()} in the {@link java.util.List#indexOf(Object) * indexOf(Object)} implementation of the collection/list itself. * * <p>/*from w ww . j a va2 s . c om*/ * This check is supposed to be faster and can be used with objects, that are missing proper implementations of those methods. Additionally, * objects that are deemed {@link Object#equals(Object) equal} but not identical are ignored. * </p> * * @param collection * collection to check for contained instance * @param instance * specific object instance to check for * @param <T> * type of the object to check for * @return position of the given instance in the collection (not just of an equal object) */ public static <T> int indexOfInstance(final Collection<? extends T> collection, final T instance) { if (collection != null && !collection.isEmpty()) { int index = 0; for (final T collectionElement : collection) { if (collectionElement == instance) { return index; } index++; } } return -1; }
From source file:Main.java
/** * Returns a String representation of the given collection by separating the * elements with the given separator.//from w ww .ja v a 2 s. co m * * @param collection * @param separator * @return */ public static String toString(Collection<? extends Object> collection, String separator) { if (collection == null || collection.isEmpty()) { return ""; } StringBuilder builder = new StringBuilder(); int count = collection.size(); for (Object obj : collection) { builder.append(obj); if (--count > 0) { builder.append(separator); } } return builder.toString(); }
From source file:Main.java
/** * Returns <code>true</code> iff all elements of {@code coll2} are also contained * in {@code coll1}. The cardinality of values in {@code coll2} is not taken into account, * which is the same behavior as {@link Collection#containsAll(Collection)}. * <p>/*from w w w. j a v a2 s . co m*/ * In other words, this method returns <code>true</code> iff the * {@link #intersection} of <i>coll1</i> and <i>coll2</i> has the same cardinality as * the set of unique values from {@code coll2}. In case {@code coll2} is empty, {@code true} * will be returned. * <p> * This method is intended as a replacement for {@link Collection#containsAll(Collection)} * with a guaranteed runtime complexity of {@code O(n + m)}. Depending on the type of * {@link Collection} provided, this method will be much faster than calling * {@link Collection#containsAll(Collection)} instead, though this will come at the * cost of an additional space complexity O(n). * * @param coll1 the first collection, must not be null * @param coll2 the second collection, must not be null * @return <code>true</code> iff the intersection of the collections has the same cardinality * as the set of unique elements from the second collection * @since 4.0 */ public static boolean containsAll(final Collection<?> coll1, final Collection<?> coll2) { if (coll2.isEmpty()) { return true; } else { final Iterator<?> it = coll1.iterator(); final Set<Object> elementsAlreadySeen = new HashSet<Object>(); for (final Object nextElement : coll2) { if (elementsAlreadySeen.contains(nextElement)) { continue; } boolean foundCurrentElement = false; while (it.hasNext()) { final Object p = it.next(); elementsAlreadySeen.add(p); if (nextElement == null ? p == null : nextElement.equals(p)) { foundCurrentElement = true; break; } } if (foundCurrentElement) { continue; } else { return false; } } return true; } }
From source file:net.lmxm.ute.UteTestAssert.java
/** * Assert empty./*from www .j av a2s .c o m*/ * * @param <E> the element type * @param message the message * @param collection the collection */ public static <E> void assertEmpty(final String message, final Collection<E> collection) { assertTrue(message, collection.isEmpty()); }
From source file:Main.java
/** * Return {@code true} if the supplied Collection is {@code null} or empty. * Otherwise, return {@code false}.//from w w w. ja va 2 s . c o m * * @param collection the Collection to check * @return whether the given Collection is empty */ public static boolean isEmpty(Collection<?> collection) { return (collection == null || collection.isEmpty()); }