DZone
Java Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Java Zone > Making Mock Data Circular

Making Mock Data Circular

Rob Williams user avatar by
Rob Williams
·
Jun. 25, 09 · Java Zone · Interview
Like (0)
Save
Tweet
5.30K Views

Join the DZone community and get the full member experience.

Join For Free

Was writing a test today where I was doing a Command Pattern that would allow me to contain a set of repeated invocations in a timebox. In other words: call me as many times as possible in x milliseconds. So for the test, I was using random #s, but that kept going to zero after a ton of invocations. Got out my Distribution class that allows me to say 'give me random #s with x% in range between y and z.' Once I do that, though, I have to know how many #s I am going to need, and I don't really.

First, I did the usual TDD thing: write it and let it fail. Sure enough, it fails on .next() after the iterator is exhausted. Did some searches on whether there is a reset method on some specialized iterator. But then, I decided it would just make more sense to make a simple CircularIterator, like so:

/**
* Provides the ability to give a fixed sample, and this class will provide an
* {@link Iterator} that will just repeatedly loop through the collection.
*
* @author Rob
*
* @param
* @Pattern Decorator
*
*/
public class CircularIterator implements Iterator {

/**
* Just using a real iterator here, after running through the set each time.
*/
private Iterator i;

/**
* The sample we want to cycle through.
*/
private Collection collection;

public CircularIterator(Collection collection) {
this.collection = collection;
this.i = collection.iterator();
}

/**
* Will always return true, since we are just looping.
*/
public boolean hasNext() {
return i.hasNext();
}

/**
* Gets the next element. If there are no more, the iterator {@link #i} is
* recreated.
*/
public T next() {
if (!i.hasNext())
i = collection.iterator();
return i.next();
}

/**
* Just proxies call so should work as usual.
*/
public void remove() {
i.remove();
}

}

 

Of course, this means that you are going to just get the same #s over and over again, but in a lot of cases (like mine), that doesn't really matter: scores, confidence levels, donations, whatever.

This is a good example of the Decorator pattern: it implements the interface, holds an instance of the type it's extending, and proxies on the methods it doesn't need to change. The nice part is that I can just create this, using the same declarations and then call next() for as long as I like.

From http://www.jroller.com/robwilliams

Data (computing) Command pattern Testing Reset (computing) IT Distribution (differential geometry) Interface (computing) Command (computing)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Which JVM Version Is the Fastest?
  • DZone's Article Submission Guidelines
  • Datafaker: An Alternative to Using Production Data
  • How to Determine if Microservices Architecture Is Right for Your Business

Comments

Java Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo