
com.openpojo.random.collection.util.CollectionHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openpojo Show documentation
Show all versions of openpojo Show documentation
This project was born out of a need to validate all POJOs (Plain Old Java Object) are behaving correctly.
This project has two main aspects to it:
* Make Testing as easy as possible.
* Simplifying identity management (hashCode / equals) using annotation.
/*
* Copyright (c) 2010-2016 Osman Shoukry
*
* 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.
*/
package com.openpojo.random.collection.util;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.Date;
import java.util.Random;
import com.openpojo.random.RandomFactory;
import com.openpojo.reflection.impl.ParameterizableFactory;
/**
* This Helper class populates the randomly generated collection with some random elements.
* It is configured to generate anywhere between 0 - 10 elements in the collection.
*
* @author oshoukry
*/
public class CollectionHelper {
private static final Random RANDOM = new Random(new Date().getTime());
public static final int MAX_RANDOM_ELEMENTS = 5;
@SuppressWarnings("unchecked")
public static Collection buildCollections(Collection collection, Type type) {
if (type == null || collection == null)
return collection;
int counter = RANDOM.nextInt(MAX_RANDOM_ELEMENTS) + 1;
collection.clear();
while (counter-- > 0) {
Object nextEntry = RandomFactory.getRandomValue(ParameterizableFactory.getInstance(type));
collection.add(nextEntry);
}
return collection;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy