View Javadoc

1   package com.atlassian.jira.rest.client;
2   
3   import com.sun.istack.Nullable;
4   
5   import java.util.Collections;
6   import java.util.Iterator;
7   
8   /**
9    * Represent iterable which is optional (for example due to lack of field in old REST API version).<br/>
10   * This iterable is intended to be not null, so you can always get items by using iterator. If you need
11   * to check if value was actually provided then use {@link com.atlassian.jira.rest.client.OptionalIterable#isSupported}.
12   *
13   * @since v1.0
14   */
15  public class OptionalIterable<T> implements Iterable<T> {
16  
17  	@SuppressWarnings("unchecked")
18  	private static final OptionalIterable absentInstance = new OptionalIterable(null);
19  
20  	@Nullable
21  	private final Iterable<T> iterable;
22  
23  	@SuppressWarnings("unchecked")
24  	public static <T> OptionalIterable<T> absent() {
25  		return absentInstance;
26  	}
27  
28  	public OptionalIterable(@Nullable Iterable<T> iterable) {
29  		this.iterable = iterable;
30  	}
31  
32  	/**
33  	 * @return iterator for original iterable if {@link OptionalIterable#isSupported} is true,
34  	 *         or empty iterator in other case.
35  	 */
36  	@Override
37  	public Iterator<T> iterator() {
38  		return isSupported()
39  				? iterable.iterator()
40  				: Collections.<T>emptyList().iterator();
41  	}
42  
43  	/**
44  	 * @return true if server supports this field
45  	 */
46  	public boolean isSupported() {
47  		return iterable != null;
48  	}
49  }