gt:                         x = Double.parseDouble(record.originalValue(xVar));
context: package com.itemanalysis.jmetrik.graph.density;
  
  
  import com.itemanalysis.jmetrik.file.JmetrikCSVRecord;
  import com.itemanalysis.jmetrik.file.JmetrikZipFileReader;
  import com.itemanalysis.jmetrik.graph.ChartDisplayPanel;
  import com.itemanalysis.jmetrik.manager.VariableChangeEvent;
  import com.itemanalysis.jmetrik.manager.VariableChangeListener;
  import com.itemanalysis.psychometrics.data.DataType;
  import com.itemanalysis.psychometrics.data.VariableName;
  import com.itemanalysis.psychometrics.distribution.KernelDensity;
  import com.itemanalysis.psychometrics.distribution.KernelType;
  import com.itemanalysis.psychometrics.tools.StopWatch;
  import org.apache.commons.io.FilenameUtils;
  import org.apache.log4j.Logger;
  import org.jfree.data.UnknownKeyException;
  import org.jfree.data.xy.XYSeries;
  import org.jfree.data.xy.XYSeriesCollection;
  
  import javax.swing.*;
  import java.io.File;
  import java.io.IOException;
  import java.util.ArrayList;
  import java.util.TreeMap;
  
  public class DensityAnalysis extends SwingWorker<XYSeriesCollection, Void> {
  
      private DensityCommand command = null;
      private ArrayList<VariableName> variables = null;
      private ChartDisplayPanel chartDisplayPanel = null;
      private Throwable theException = null;
      private boolean hasGroupingVariable = false;
      private boolean saveChart = false;
      private boolean saveAsJPG = false;
  
      private File file = null;
      private File outputFile = null;
      private VariableName xVar = null;
      private VariableName weightVar = null;
      private VariableName groupVar = null;
      private int chartWidth = 450;
      private int chartHeight = 400;
  
      private KernelType kernelType = KernelType.GAUSSIAN;
      private KernelDensity.BandwidthType bandwidthType = KernelDensity.BandwidthType.BW_NRD0;
      private double adjust = 1;
      private int numberOfPoints = 512;
      private int maxEval = 500000;
  
      private StopWatch sw = null;
      private ArrayList<VariableChangeListener> variableChangeListeners = null;
      static Logger logger = Logger.getLogger("jmetrik-logger");
      static Logger scriptLogger = Logger.getLogger("jmetrik-script-logger");
  
  
      public DensityAnalysis(DensityCommand command, ChartDisplayPanel chartDisplayPanel){
          this.command = command;
          this.chartDisplayPanel = chartDisplayPanel;
      }
  
      private void processCommand(){
          variables = command.getVariables();
          file = command.getDataFile();
          outputFile = command.getOutputFile();
          xVar = command.getXVariable();
          weightVar = command.getWeightVariable();
          kernelType = command.getKernelType();
          bandwidthType = command.getBandwidthType();
          adjust = command.getBandwidthAdjustment();
          numberOfPoints = command.getNumberOfPoints();
          maxEval = command.getMaxNumberOfEvaluations();
          groupVar = command.getByVariable();
          hasGroupingVariable = null!=groupVar;
          saveAsJPG = command.saveAsJPG();
          chartWidth = command.getChartWidth();
          chartHeight = command.getChartHeight();
          outputFile = command.getOutputFile();
          outputFile = checkOutputFile(outputFile, saveAsJPG);
          if(null!=outputFile) saveChart = true;
      }
  
      /**
       * Ensures the correct file suffix is used.
       *
       * @param f a file
       * @param asJPG true if the file is to be a jpeg file. False for a png file.
       * @return
       */
      private File checkOutputFile(File f, boolean asJPG){
          String fname = "";
          if(null!=f){
              String suffix =  FilenameUtils.getExtension(f.getAbsolutePath());
              if(asJPG && !"jpg".equals(suffix)) {
                  fname = f.getAbsolutePath() + ".jpg";
              }else if(!asJPG && !"png".equals(suffix)){
                  fname = f.getAbsolutePath() + ".png";
              }
              return new File(fname);
          }
          return null;
      }
  
  
      private XYSeriesCollection readGroupedData()throws IOException{
          ArrayList<Double> data = null;
          double x = 0;
          TreeMap<String, ArrayList<Double>> dataMap = new TreeMap<String, ArrayList<Double>>();
  
          try(JmetrikZipFileReader reader = new JmetrikZipFileReader(file)){
              reader.openConnection();
  
              String groupName = "";
  
              while(reader.hasNext()){
                  JmetrikCSVRecord record = reader.next();
  
                  groupName = record.originalValue(groupVar);
                  data = dataMap.get(groupName);
                  if(null==data){
                      data = new ArrayList<Double>();
                      dataMap.put(groupName, data);
                  }
  
                  //x-value
                  if(reader.getVariableAttributesAt(xVar).getDataType()!= DataType.STRING){
                      if(record.isPresent(xVar)){
                          x = Double.parseDouble(record.originalValue(xVar));
                          data.add(x);
                      }
                  }
  
              }//end loop over data file
  
              //Compute density for each group and add to collection
              XYSeriesCollection seriesCollection = new XYSeriesCollection();
              for(String s : dataMap.keySet()){
                  data = dataMap.get(s);
                  double[] xData = new double[data.size()];
                  for(int i=0;i<data.size();i++){
                      xData[i] = data.get(i).doubleValue();
                  }
  
                  KernelDensity kernelDensity = new KernelDensity(xData, kernelType, bandwidthType, adjust, numberOfPoints, maxEval);
                  double[] p = kernelDensity.getPoints();
                  double[] d = kernelDensity.getDensity();
  
                  XYSeries series = null;
                  try{
                      series = seriesCollection.getSeries(s);
                  }catch(UnknownKeyException ex){
                      series = new XYSeries(s);
                      seriesCollection.addSeries(series);
                  }finally{
                      for(int i=0;i<p.length;i++){
                          series.add(p[i], d[i]);
                      }
                  }
  
              }
  
              return seriesCollection;
  
          }catch(IOException ex){
              throw ex;
          }
  
      }
  
      private XYSeriesCollection readGroupedDataWithWeights()throws IOException{
          ArrayList<Double> data = null;
          ArrayList<Double> weight = null;
          double x = 0;
          double w = 0;
          TreeMap<String, ArrayList<Double>> dataMap = new TreeMap<String, ArrayList<Double>>();
          TreeMap<String, ArrayList<Double>> weightMap = new TreeMap<String, ArrayList<Double>>();
  
          try(JmetrikZipFileReader reader = new JmetrikZipFileReader(file)){
              reader.openConnection();
              String groupName = "";
  
              while(reader.hasNext()){
                  JmetrikCSVRecord record = reader.next();
  
                  groupName = record.originalValue(groupVar);
                  data = dataMap.get(groupName);
                  weight = weightMap.get(groupName);
                  if(null==data){
                      data = new ArrayList<Double>();
                      dataMap.put(groupName, data);
  
                      weight = new ArrayList<Double>();
                      weightMap.put(groupName, weight);
                  }
  
                  //x-value
                  if(reader.getVariableAttributesAt(xVar).getDataType()!= DataType.STRING){
                      if(record.isPresent(xVar)){
                          x = Double.parseDouble(record.originalValue(xVar));
                          data.add(x);
                          w = Double.parseDouble(record.originalValue(weightVar));
                          weight.add(w);
                      }
                  }
              }//end loop over data file
  
              //Compute density for each group and add to collection
              XYSeriesCollection seriesCollection = new XYSeriesCollection();
              for(String s : dataMap.keySet()){
                  data = dataMap.get(s);
                  double[] xData = new double[data.size()];
                  double[] wData = new double[data.size()];
                  for(int i=0;i<data.size();i++){
                      xData[i] = data.get(i).doubleValue();
                      wData[i] = weight.get(i).doubleValue();
                  }
  
                  KernelDensity kernelDensity = new KernelDensity(xData, wData, kernelType, bandwidthType, adjust, numberOfPoints, maxEval);
                  double[] p = kernelDensity.getPoints();
                  double[] d = kernelDensity.getDensity();
  
                  XYSeries series = null;
                  try{
                      series = seriesCollection.getSeries(s);
                  }catch(UnknownKeyException ex){
                      series = new XYSeries(s);
                      seriesCollection.addSeries(series);
                  }finally{
                      for(int i=0;i<p.length;i++){
                          series.add(p[i], d[i]);
                      }
                  }
  
              }
  
  
  
              return seriesCollection;
  
          }catch(IOException ex){
              throw ex;
          }
  
      }
  
      private XYSeriesCollection readDataWithWeights()throws IOException{
          ArrayList<Double> data = new ArrayList<Double>();
          ArrayList<Double> weight = new ArrayList<Double>();
          double x = 0;
          double w = 0;
  
          try(JmetrikZipFileReader reader = new JmetrikZipFileReader(file)){
              reader.openConnection();
              String groupName = "Series 1";
  
              while(reader.hasNext()){
                  JmetrikCSVRecord record = reader.next();
  
                  //x-value
                  if(reader.getVariableAttributesAt(xVar).getDataType()!= DataType.STRING){
                      if(record.isPresent(xVar)){
                          x = Double.parseDouble(record.originalValue(xVar));
                          data.add(x);
                          w = Double.parseDouble(record.originalValue(weightVar));
                          weight.add(w);
                      }
                  }
              }
  
              double[] xData = new double[data.size()];
              double[] wData = new double[data.size()];
              for(int i=0;i<data.size();i++){
                  xData[i] = data.get(i).doubleValue();
                  wData[i] = weight.get(i).doubleValue();
              }
              KernelDensity kernelDensity = new KernelDensity(xData, wData, kernelType, bandwidthType, adjust, numberOfPoints, maxEval);
              double[] p = kernelDensity.getPoints();
              double[] d = kernelDensity.getDensity();
  
              XYSeries series1 = new XYSeries("Series 1");
              for(int i=0;i<p.length;i++){
                  series1.add(p[i], d[i]);
              }
  
              XYSeriesCollection seriesCollection = new XYSeriesCollection();
              seriesCollection.addSeries(series1);
  
              return seriesCollection;
  
          }catch(IOException ex){
              throw ex;
          }
  
      }
  
      private XYSeriesCollection readData()throws IOException{
          ArrayList<Double> data = new ArrayList<Double>();
          double x = 0;
  
          try(JmetrikZipFileReader reader = new JmetrikZipFileReader(file)){
              reader.openConnection();
              String groupName = "Series 1";
  
              while(reader.hasNext()){
                  JmetrikCSVRecord record = reader.next();
  
                  //x-value
                  if(reader.getVariableAttributesAt(xVar).getDataType()!= DataType.STRING){
                      if(record.isPresent(xVar)){
  