All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.fitbur.assertj.api.IterableAssert Maven / Gradle / Ivy

There is a newer version: 1.0.0
Show newest version
/**
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 * Copyright 2012-2016 the original author or authors.
 */
package com.fitbur.assertj.api;

import java.util.AbstractCollection;
import java.util.ArrayList;
import java.util.Iterator;

import com.fitbur.assertj.util.VisibleForTesting;

/**
 * Assertion methods for {@link Iterable}.
 * 

* To create an instance of this class, invoke {@link Assertions#assertThat(Iterable)}. *

* @param the type of elements of the "actual" value. * * @author Yvonne Wang * @author Alex Ruiz * @author Matthieu Baechler * @author Joel Costigliola * @author Mikhail Mazursky * @author Julien Meddah */ public class IterableAssert extends AbstractIterableAssert, Iterable, T> { protected IterableAssert(Iterable actual) { super(actual, IterableAssert.class); } protected IterableAssert(Iterator actual) { this(toLazyIterable(actual)); } private static Iterable toLazyIterable(Iterator actual) { if (actual == null) { return null; } return new LazyIterable<>(actual); } @Override public IterableAssert isInstanceOf(Class type) { if (actual instanceof LazyIterable) { objects.assertIsInstanceOf(info, asLazyIterable().iterator, type); return myself; } return super.isInstanceOf(type); } @Override public IterableAssert isInstanceOfAny(Class... types) { if (actual instanceof LazyIterable) { objects.assertIsInstanceOfAny(info, asLazyIterable().iterator, types); return myself; } return super.isInstanceOfAny(types); } @Override public IterableAssert isOfAnyClassIn(Class... types) { if (actual instanceof LazyIterable) { objects.assertIsOfAnyClassIn(info, asLazyIterable().iterator, types); return myself; } return super.isOfAnyClassIn(types); } @Override public IterableAssert isExactlyInstanceOf(Class type) { if (actual instanceof LazyIterable) { objects.assertIsExactlyInstanceOf(info, asLazyIterable().iterator, type); return myself; } return super.isExactlyInstanceOf(type); } @Override public IterableAssert isNotInstanceOf(Class type) { if (actual instanceof LazyIterable) { objects.assertIsNotInstanceOf(info, asLazyIterable().iterator, type); return myself; } return super.isNotInstanceOf(type); } @Override public IterableAssert isNotInstanceOfAny(Class... types) { if (actual instanceof LazyIterable) { objects.assertIsNotInstanceOfAny(info, asLazyIterable().iterator, types); return myself; } return super.isNotInstanceOfAny(types); } @Override public IterableAssert isNotOfAnyClassIn(Class... types) { if (actual instanceof LazyIterable) { objects.assertIsNotOfAnyClassIn(info, asLazyIterable().iterator, types); return myself; } return super.isNotOfAnyClassIn(types); } @Override public IterableAssert isNotExactlyInstanceOf(Class type) { if (actual instanceof LazyIterable) { objects.assertIsNotExactlyInstanceOf(info, asLazyIterable().iterator, type); return myself; } return super.isNotExactlyInstanceOf(type); } @SuppressWarnings("rawtypes") private LazyIterable asLazyIterable() { return (LazyIterable) actual; } // will only consume iterator when needed @VisibleForTesting static class LazyIterable extends AbstractCollection { private Iterator iterator; private Iterable iterable; public LazyIterable(Iterator iterator) { this.iterator = iterator; } @Override public Iterator iterator() { if (iterable == null) { iterable = toIterable(iterator); } return iterable.iterator(); } @Override public int size() { int size = 0; Iterator localIterator = iterator(); while (localIterator.hasNext()) { localIterator.next(); size++; } return size; } } private static Iterable toIterable(Iterator iterator) { ArrayList list = new ArrayList<>(); while (iterator.hasNext()) { list.add(iterator.next()); } return list; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy