1 /* 2 * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, is permitted under the terms of the 6 * GNU General Public License. 7 * 8 * For redistributing this software or a derivative work under a license other 9 * than the GPL-compatible Free Software License as defined by the Free 10 * Software Foundation or approved by OSI, you must first obtain a commercial 11 * license to this software product from rapiddweller GmbH & Volker Bergmann. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS, 15 * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE 17 * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 18 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 package com.rapiddweller.domain.us; 28 29 import com.rapiddweller.benerator.NonNullGenerator; 30 import com.rapiddweller.benerator.distribution.sequence.RandomIntegerGenerator; 31 import com.rapiddweller.benerator.wrapper.CompositeGenerator; 32 import com.rapiddweller.benerator.wrapper.ProductWrapper; 33 import com.rapiddweller.common.StringUtil; 34 35 /** 36 * Generates US Social Security Numbers.<br/> 37 * <br/> 38 * Created at 17.11.2008 06:54:42 39 * 40 * @author Volker Bergmann 41 * @see "http://en.wikipedia.org/wiki/Social_security_number" 42 * @see "http://www.socialsecurity.gov/history/ssn/geocard.html" 43 * @see "http://www.socialsecurity.gov/employer/stateweb.htm" 44 * @see "http://www.socialsecurity.gov/employer/ssnvhighgroup.htm" 45 * @since 0.5.6 46 */ 47 public class SSNGenerator extends CompositeGenerator<String> 48 implements NonNullGenerator<String> { 49 50 private final RandomIntegerGenerator areaNumberGenerator; 51 private final RandomIntegerGenerator groupNumberGenerator; 52 private final RandomIntegerGenerator serialNumberGenerator; 53 54 /** 55 * Instantiates a new Ssn generator. 56 */ 57 public SSNGenerator() { 58 this(772); 59 } 60 61 /** 62 * Instantiates a new Ssn generator. 63 * 64 * @param maxAreaCode the max area code 65 */ 66 public SSNGenerator(int maxAreaCode) { 67 super(String.class); 68 areaNumberGenerator = 69 registerComponent(new RandomIntegerGenerator(1, maxAreaCode)); 70 groupNumberGenerator = 71 registerComponent(new RandomIntegerGenerator(1, 99)); 72 serialNumberGenerator = 73 registerComponent(new RandomIntegerGenerator(1, 9999)); 74 } 75 76 @Override 77 public ProductWrapper<String> generate(ProductWrapper<String> wrapper) { 78 return wrapper.wrap(generate()); 79 } 80 81 @Override 82 public String generate() { 83 Integer area; 84 do { 85 area = areaNumberGenerator.generate(); 86 } while (area == 666 || (area >= 734 && area <= 749)); 87 return StringUtil.padLeft(String.valueOf(area), 3, '0') + '-' + 88 StringUtil.padLeft( 89 String.valueOf(groupNumberGenerator.generate()), 2, 90 '0') + '-' + 91 StringUtil.padLeft( 92 String.valueOf(serialNumberGenerator.generate()), 4, 93 '0'); 94 } 95 96 /** 97 * Sets max area code. 98 * 99 * @param maxAreaCode the max area code 100 */ 101 public void setMaxAreaCode(int maxAreaCode) { 102 areaNumberGenerator.setMax(maxAreaCode); 103 } 104 105 @Override 106 public String toString() { 107 return getClass().getSimpleName(); 108 } 109 110 }