/** * Checks if the given collection is not null or empty. * * @param <T> the generic type of the elements in the given collection * @param <C> the generic type of the given collection * @param collection the collection to check * @param name the name of the given collection * @return if the check is successful a reference to this object. This case is if the given * collection is not null or empty. * @throws IllegalArgumentException when the given {@code collection} is null or empty. */ public <T, C extends Collection<T>> Check notEmpty(final C collection, final String name) { nullCheck(collection, name); if (collection.isEmpty()) { throw new IllegalArgumentException("Given collection '" + name + "' may not be empty."); } return this; }
/** {@inheritDoc} */ @Override public boolean isEmpty() { return collection.isEmpty(); }
/** * Convert an Object to a Collection with elements of the desired type. If it is a Collection and * it is of the same collection and element type it is returned as is, otherwise a new Collection * is created with all elements converted to the desired type. If it is a String representation of * a Collection it is converted to a Collection. * * @param <E> The element type. * @param <C> The Collection type. * @param obj The Object to be converted. * @param collectionType The desired Collection type. * @param elementType The desired element type. * @param emptyIfNull Whether or not an empty Collection should be returned if the Collection * could not be converted. * @return The converted Collection. */ public <E, C extends Collection<E>> C convertToCollection( Object obj, Class<C> collectionType, Class<E> elementType, boolean emptyIfNull) { // Grab defaults for types if interfaces or non-datareference collections are provided Class<? extends C> constructionCollectionType; Class<? extends E> constructionElementType; if (collectionType.equals(List.class)) { if (DataReferencable.class.isAssignableFrom(elementType)) { constructionCollectionType = (Class) DataReferenceList.class; constructionElementType = (Class) DataReference.class; } else { constructionCollectionType = (Class) ArrayList.class; } } else if (collectionType.equals(Set.class)) { if (DataReferencable.class.isAssignableFrom(elementType)) { constructionElementType = (Class) DataReference.class; throw new IllegalArgumentException( "Set class is not supported for DataReferencable elements"); } constructionCollectionType = (Class) HashSet.class; } else { constructionCollectionType = collectionType; } if (obj != null) { if (obj instanceof Collection) { // If it's already a collection Collection collection = (Collection) obj; Class cachedElementType = getCachedElementType(collection); // If we have cached it's element type return if same if (cachedElementType != null && elementType.isAssignableFrom(cachedElementType) && collection.getClass().equals(constructionCollectionType)) { setCachedElementType(collection, elementType); return (C) collection; } else { return _convertContents(collection, constructionCollectionType, elementType); } } else { try { // If it's a single value convertable to E E e = convert(obj, elementType); C c = CollectionUtils.instanceOf(constructionCollectionType); c.add(e); return c; } catch (ConversionException ex) { } try { // If it's a string representation of one or many E's String string = convert(obj, String.class); String[] split = TextUtils.splitOnSpacesAndCommas(string); C c = CollectionUtils.instanceOf(constructionCollectionType); for (String str : split) { try { E e = convert(obj, elementType); c.add(e); } catch (ConversionException ex) { } } if (!c.isEmpty()) { return c; } } catch (ConversionException ex) { } } } // Fall through to here if a collection could not be converted for any reason if (emptyIfNull) { return CollectionUtils.instanceOf(constructionCollectionType); } else { return null; } }
@Override public void readMarshallable(WireIn wire) throws IllegalStateException { @SuppressWarnings("ConstantConditions") final Bytes<?> outBytes = outWire.bytes(); try { final StringBuilder eventName = Wires.acquireStringBuilder(); @SuppressWarnings("ConstantConditions") final ValueIn valueIn = inWire.readEventName(eventName); outWire.writeDocument( true, w -> w.writeEventName(CoreFields.tid).int64(CollectionWireHandler.this.tid)); outWire.writeDocument( false, out -> { // note : remove on the key-set returns a boolean and on the map returns the // old value if (EventId.remove.contentEquals(eventName)) { outWire .write(CoreFields.reply) .bool(underlyingCollection.remove(fromWire.apply(valueIn))); return; } // note : remove on the key-set returns a boolean and on the map returns the // old value if (EventId.iterator.contentEquals(eventName)) { final ValueOut valueOut = out.writeEventName(CoreFields.reply); valueOut.sequence(v -> underlyingCollection.forEach(e -> toWire.accept(v, e))); return; } if (EventId.numberOfSegments.contentEquals(eventName)) { outWire.write(CoreFields.reply).int32(1); return; } if (EventId.isEmpty.contentEquals(eventName)) { outWire.write(CoreFields.reply).bool(underlyingCollection.isEmpty()); return; } if (EventId.size.contentEquals(eventName)) { outWire.write(CoreFields.reply).int32(underlyingCollection.size()); return; } if (EventId.clear.contentEquals(eventName)) { underlyingCollection.clear(); return; } if (EventId.contains.contentEquals(eventName)) { outWire .write(CoreFields.reply) .bool(underlyingCollection.contains(fromWire.apply(valueIn))); return; } if (EventId.add.contentEquals(eventName)) { outWire .write(CoreFields.reply) .bool(underlyingCollection.add(fromWire.apply(valueIn))); return; } if (EventId.remove.contentEquals(eventName)) { outWire .write(CoreFields.reply) .bool(underlyingCollection.remove(fromWire.apply(valueIn))); return; } if (EventId.containsAll.contentEquals(eventName)) { outWire .write(CoreFields.reply) .bool(underlyingCollection.remove(collectionFromWire())); return; } if (EventId.addAll.contentEquals(eventName)) { outWire .write(CoreFields.reply) .bool(underlyingCollection.addAll(collectionFromWire())); return; } if (EventId.removeAll.contentEquals(eventName)) { outWire .write(CoreFields.reply) .bool(underlyingCollection.removeAll(collectionFromWire())); return; } if (EventId.retainAll.contentEquals(eventName)) { outWire .write(CoreFields.reply) .bool(underlyingCollection.retainAll(collectionFromWire())); return; } throw new IllegalStateException("unsupported event=" + eventName); }); } catch (Exception e) { LOG.error("", e); } finally { if (YamlLogging.showServerWrites) { long len = outBytes.writePosition(); if (len >= SIZE_OF_SIZE) { String s = Wires.fromSizePrefixedBlobs(outBytes); LOG.info("server writes:\n\n" + s); } } } }