com.arakelian.fake.model.RandomPerson Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.arakelian.fake.model;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.google.common.base.Charsets;
import com.google.common.collect.Lists;
public class RandomPerson {
public static List LAST_NAMES = readNames("/dist.all.last");
public static List MALE_NAMES = readNames("/dist.male.first");
public static List FEMALE_NAMES = readNames("/dist.female.first");
public static Iterator iteratorOf(final int count) {
return new Iterator() {
private int index;
@Override
public boolean hasNext() {
if (index < count) {
return true;
}
return false;
}
@Override
public Person next() {
index++;
return nextPerson();
}
};
}
public static List listOf(final int count) {
return Lists.newArrayList(iteratorOf(count));
}
public static Person nextPerson() {
final ThreadLocalRandom random = ThreadLocalRandom.current();
final String firstName = MALE_NAMES.get(random.nextInt(0, MALE_NAMES.size()));
final String spouseName = FEMALE_NAMES.get(random.nextInt(0, FEMALE_NAMES.size()));
final String lastName = LAST_NAMES.get(random.nextInt(0, LAST_NAMES.size()));
final Person person = Person.create().withFirstName(firstName).withLastName(lastName)
.withComments(firstName + " is married to " + spouseName);
return person;
}
private static List readNames(final String resourceName) {
final URL resource = RandomPerson.class.getResource(resourceName);
if (resource == null) {
throw new IllegalArgumentException("Resource " + resourceName + " not found");
}
try (BufferedReader reader = new BufferedReader(
new InputStreamReader(resource.openConnection().getInputStream(), Charsets.UTF_8))) {
final Stream stream = reader.lines();
final List list = stream.map(s -> s.substring(0, 15).trim()).collect(Collectors.toList());
System.out.println("There are " + list.size() + " names in " + resourceName);
return list;
} catch (final IOException e) {
throw new RuntimeException("Unable to load resource: " + resourceName, e);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy