A cyclic iterator

Sometimes, a cyclic iterator can be useful to supply an endless supply of deterministic data. Here’s an simple implementation that I wrote today.

import java.util.Iterator;

 * This class iterates across an iterable object with the option to recycle amongst the elements infinitely.
 * In case the recycle option is not set, the original data is iterated upon once, and all subsequent calls to
 * next() will return the defaultDataValue
 * @param <T>
public class CyclicIterator<T>
 implements Iterator<T> {
 final private Iterable<T> data;
 final private boolean recycleData;
 final private T defaultDataValue;
 private Iterator<T> dataIterator;

public CyclicIterator(Iterable<T> data, boolean recycle, T defaultDataValue) {
 this.data = data;
 this.recycleData = recycle;
 this.defaultDataValue = defaultDataValue;

private void resetDataIterator() {
 this.dataIterator = this.data.iterator();

 public boolean hasNext() {
 return true;

 public T next() {
 //if the iterator is done, check whether to reset or not
 if (!dataIterator.hasNext()) {
 if (recycleData) {
 } else {
 return defaultDataValue;

//at this point, next should be valid
 return dataIterator.next();

 public void remove() {

Testing for Equality using reflection

This utility tests for equality using reflection. Saves you from getting carpal tunnel by  writing multiple lines of the form

assertThat(result.getFoo(), is(equalTo(item.getFoo())));

Here’s the source. Modify as needed

public class MappingTestUtility {

public static void assertEquals(Object input, Object result) throws IllegalAccessException {
 assertThat(result, is(equalTo(input)));
 final Field[] fields = input.getClass().getDeclaredFields();
 for (final Field field : fields) {
 assertThat(field.get(input), is(equalTo(field.get(result))));

Implementing PUT REST requests when using Spring RestTemplate

Spring’s RestTemplate.put(…) method returns a void. Why is this a bother? One reason is to capture PUT response information and make logical decisions based on that data.

As it turns out, The RestTemplate’s exchange method can be used to implement the PUT request using the following snippet. It’s not complete code but enough to the point where it should be self explanatory

public boolean putIndex(String id) throws Exception {
 final String endpoint = endpoint("index/{id}");

HttpHeaders requestHeaders = new HttpHeaders();
 List <MediaType> mediaTypeList = new ArrayList<MediaType>();
 HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);

 // Create the HTTP PUT request,
 ResponseEntity<Object> response = operations.exchange(endpoint, HttpMethod.PUT, requestEntity, null, id);
 if (response == null) {
 return false;
 return HttpStatus.CREATED.equals(response.getStatusCode());