Ejemplo n.º 1
0
  @SuppressWarnings("unchecked")
  @Override
  public void writeFile(File theFile, List<NemaData> data)
      throws IllegalArgumentException, FileNotFoundException, IOException {

    BufferedWriter writer = null;
    try {
      writer = new BufferedWriter(new FileWriter(theFile));
      NemaData obj;
      String identifier;
      String tag;
      HashMap<String, Double> tagAffinities = null;
      int noFileLocation = 0;
      for (Iterator<NemaData> it = data.iterator(); it.hasNext(); ) {
        obj = it.next();
        try {
          identifier = obj.getStringMetadata(NemaDataConstants.PROP_FILE_LOCATION);
        } catch (IllegalArgumentException e) {
          identifier = obj.getId();
          noFileLocation++;
        }
        tagAffinities =
            (HashMap<String, Double>) obj.getMetadata(NemaDataConstants.TAG_AFFINITY_MAP);
        // write a line per tag
        for (Iterator<String> tagIt = tagAffinities.keySet().iterator(); tagIt.hasNext(); ) {
          tag = tagIt.next();
          writer.write(
              identifier + WRITE_DELIMITER + tag + WRITE_DELIMITER + tagAffinities.get(tag));
          writer.newLine();
        }
      }

      if (noFileLocation == 0) {
        getLogger()
            .info(
                data.size()
                    + " examples with "
                    + NemaDataConstants.TAG_AFFINITY_MAP
                    + " metadata written to file: "
                    + theFile.getAbsolutePath());
      } else {
        getLogger()
            .warning(
                noFileLocation
                    + " of "
                    + data.size()
                    + " examples (with "
                    + NemaDataConstants.TAG_AFFINITY_MAP
                    + " metadata) did not have file locations, hence they will have be written out with the trackID only.\nFile written: "
                    + theFile.getAbsolutePath());
      }
    } finally {
      if (writer != null) {
        try {
          writer.flush();
          writer.close();
        } catch (IOException ex) {
          getLogger().log(Level.SEVERE, null, ex);
        }
      }
    }
  }