PageRenderTime 20ms CodeModel.GetById 12ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/tst/org/diffkit/util/tst/CSVDataGenerator.groovy

http://diffkit.googlecode.com/
Groovy | 122 lines | 87 code | 16 blank | 19 comment | 1 complexity | af6ce2c6ff36fdef84bb4ea8979ddff7 MD5 | raw file
  1/**
  2 * Copyright 2010-2011 Joseph Panico
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *   http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16package org.diffkit.util.tst
 17
 18import org.apache.commons.lang.RandomStringUtils;
 19
 20/**
 21 * @author jpanico
 22 */
 23public class CSVDataGenerator {
 24   enum FieldType {
 25      STRING, INTEGER, DECIMAL
 26   }
 27   
 28   private static CSVDataGenerator _instance = new CSVDataGenerator()
 29   
 30   public static void main(String[] args_){
 31      _instance.generate()
 32   }
 33   
 34   private void generate(){
 35      def rowSpecifier = [
 36            new FieldSpecifier(FieldType.INTEGER,10),
 37            new FieldSpecifier(FieldType.INTEGER,5),
 38            new FieldSpecifier(FieldType.INTEGER,5),
 39            new FieldSpecifier(FieldType.INTEGER,3),
 40            new FieldSpecifier(FieldType.INTEGER,1),
 41            new FieldSpecifier(FieldType.DECIMAL,6,2)
 42            ]
 43      def rows = this.writeRows( rowSpecifier, '|', 100, new File("data.csv").newWriter())
 44   }
 45
 46   private void generate1(){
 47      def rowSpecifier = [
 48            new FieldSpecifier(FieldType.STRING,10), 
 49            new FieldSpecifier(FieldType.INTEGER,10), 
 50            new FieldSpecifier(FieldType.DECIMAL,10,3), 
 51            new FieldSpecifier(FieldType.DECIMAL,10,3),
 52            new FieldSpecifier(FieldType.STRING,10), 
 53            new FieldSpecifier(FieldType.INTEGER,10), 
 54            new FieldSpecifier(FieldType.DECIMAL,10,3), 
 55            new FieldSpecifier(FieldType.DECIMAL,10,3),
 56            new FieldSpecifier(FieldType.STRING,10), 
 57            new FieldSpecifier(FieldType.INTEGER,10), 
 58            new FieldSpecifier(FieldType.DECIMAL,10,3), 
 59            new FieldSpecifier(FieldType.DECIMAL,10,3)
 60            ]
 61      def rows = this.writeRows( rowSpecifier, '|', 500000, new File("data.csv").newWriter())
 62   }
 63   
 64   private void writeRows(List rowSpecifier_, String delimeter_, int rowCount_, Writer out_){
 65      (0..rowCount_).each {
 66         out_ << this.generateRow(rowSpecifier_, delimeter_) + '\n'
 67      }
 68      out_.close()
 69   }
 70   
 71   private def generateRow(List rowSpecifier_, String delimeter_){
 72      def builder = new StringBuilder()
 73      rowSpecifier_.each {
 74         builder.append(this.generateFieldValue(it)+delimeter_)
 75      }
 76      // remove the last character
 77      return builder.toString()[0..-2]
 78   }
 79   
 80   private def generateFieldValue(FieldSpecifier fieldSpecifier_){
 81      switch (fieldSpecifier_.fieldType) {
 82         case FieldType.STRING:
 83            return generateStringFieldValue(fieldSpecifier_.length)
 84         case FieldType.INTEGER:
 85            return generateIntegerFieldValue(fieldSpecifier_.length)
 86         case FieldType.DECIMAL:
 87            return generateDecimalFieldValue(fieldSpecifier_.length,fieldSpecifier_.precision)
 88         
 89         default:
 90            break;
 91      }
 92   }
 93   
 94   private def generateStringFieldValue(int length_){
 95      return RandomStringUtils.random( length_, true, false)
 96   }
 97   
 98   private def generateIntegerFieldValue(int length_){
 99      return RandomStringUtils.random( length_, false, true)
100   }
101   
102   private def generateDecimalFieldValue(int length_, int precision_){
103      def integerString = RandomStringUtils.random( length_, false, true) << ''
104      return integerString.insert(integerString.size() - precision_ , ".")
105   }
106   
107   private class FieldSpecifier {
108      private final FieldType fieldType;
109      private final int length;
110      private final int precision;
111      
112      public FieldSpecifier(FieldType fieldType_, int length_){
113         this(fieldType_, length_, 0)
114      }
115      
116      public FieldSpecifier(FieldType fieldType_, int length_, int precision_){
117         fieldType = fieldType_
118         length = length_
119         precision = precision_
120      }
121   }
122}