com.openpojo.random.service.impl.DefaultRandomGeneratorService 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-2013 Osman Shoukry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License or any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package com.openpojo.random.service.impl;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import com.openpojo.random.RandomGenerator;
import com.openpojo.random.service.RandomGeneratorService;
/**
* @author oshoukry
*/
public class DefaultRandomGeneratorService implements RandomGeneratorService {
private final Map, RandomGenerator> concreteRandomGenerator = new HashMap, RandomGenerator>();
private RandomGenerator defaultRandomGenerator;
private static final Random RANDOM = new Random(new Date().getTime());
public String getName() {
return this.getClass().getName();
}
public void registerRandomGenerator(final RandomGenerator randomGenerator) {
for (final Class> type : randomGenerator.getTypes()) {
concreteRandomGenerator.put(type, randomGenerator);
}
}
public void setDefaultRandomGenerator(final RandomGenerator randomGenerator) {
defaultRandomGenerator = randomGenerator;
}
public RandomGenerator getDefaultRandomGenerator() {
return defaultRandomGenerator;
}
public RandomGenerator getRandomGeneratorByType(final Class> type) {
RandomGenerator appropriateRandomGenerator = concreteRandomGenerator.get(type);
if (appropriateRandomGenerator == null) {
final List> assignableTypes = getAssignableTypesForType(type, concreteRandomGenerator.keySet());
if (assignableTypes.size() == 0) {
appropriateRandomGenerator = getDefaultRandomGenerator();
} else {
final Class> adaptToType = assignableTypes.get(RANDOM.nextInt(assignableTypes.size()));
appropriateRandomGenerator = new RandomGeneratorAdapter(type, adaptToType,
concreteRandomGenerator.get(adaptToType));
}
}
return appropriateRandomGenerator;
}
public Collection> getRegisteredTypes() {
return Collections.unmodifiableSet(concreteRandomGenerator.keySet());
}
private List> getAssignableTypesForType(final Class> type, final Set> registeredTypes) {
final List> assignableTypes = new LinkedList>();
for (final Class> registeredType : registeredTypes) {
if (type.isAssignableFrom(registeredType)) {
assignableTypes.add(registeredType);
}
}
return assignableTypes;
}
}