001 package org.hackystat.sensor.ant.perforce; 002 003 import java.text.SimpleDateFormat; 004 import java.util.ArrayList; 005 import java.util.Date; 006 import java.util.List; 007 import java.util.Locale; 008 009 /** 010 * Provides a data structure with information about each file affected by a ChangeList and 011 * the number of lines added, deleted, and modified. 012 * @author Philip Johnson 013 */ 014 public class PerforceChangeListData { 015 /** A list of instances indicating info about a single file. */ 016 private List<PerforceFileData> fileDataList = new ArrayList<PerforceFileData>(); 017 /** The perforce user who is the owner of this changelist. */ 018 private String owner; 019 /** The integer ID for this changelist. */ 020 private int id; 021 /** When this changelist was submitted. */ 022 private Date modTime; 023 /** Formatter for parsing the date string returned by perforce. */ 024 private SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd", Locale.US); 025 026 /** Disable public no-arg constructor. */ 027 @SuppressWarnings("unused") 028 private PerforceChangeListData() { 029 // Nothing. 030 } 031 032 033 /** 034 * Create a new instance with the specified owner. 035 * @param owner The owner who committed this changelist. 036 * @param id The id for this changelist. 037 * @param modTime The time this changelist was submitted. 038 */ 039 public PerforceChangeListData(String owner, int id, String modTime) { 040 this.owner = owner; 041 this.id = id; 042 try { 043 this.modTime = format.parse(modTime); 044 } 045 catch (Exception e) { 046 System.out.println("Could not parse date returned from Perforce: " + modTime); 047 System.out.println("Defaulting to today's date"); 048 this.modTime = new Date(); 049 } 050 } 051 052 053 /** 054 * Adds the Perforce file data information to this Changelist. 055 * @param fileName The file name. 056 * @param linesAdded The lines added. 057 * @param linesModified The lines modified. 058 * @param linesDeleted The lines deleted. 059 * @param totalLoc The total number of lines in the file. 060 */ 061 public void addFileData(String fileName, int linesAdded, int linesDeleted, int linesModified, 062 int totalLoc) { 063 this.fileDataList.add(new PerforceFileData(fileName, linesAdded, linesDeleted, linesModified, 064 totalLoc)); 065 } 066 067 /** 068 * Returns as list of PerforceFileData instances associated with this changelist. 069 * @return The list of PerforceFileData instances. 070 */ 071 public List<PerforceFileData> getFileData() { 072 return this.fileDataList; 073 } 074 075 /** 076 * Returns the owner of this Changelist. 077 * @return The owner. 078 */ 079 public String getOwner() { 080 return this.owner; 081 } 082 083 /** 084 * Returns the ID associated with this changelist. 085 * @return The id. 086 */ 087 public int getId() { 088 return this.id; 089 } 090 091 /** 092 * Returns the date on which this Changelist was submitted. 093 * @return The date on which this Changelist was submitted. 094 */ 095 public Date getModTime () { 096 return (Date)this.modTime.clone(); 097 } 098 099 /** 100 * Returns this changelist in a nicely formatted output for debugging purposes. 101 * @return The changelist as a string. 102 */ 103 @Override 104 public String toString() { 105 StringBuffer buff = new StringBuffer(); 106 String header = String.format("[ChangeList %d %s %s ", this.id, this.modTime, this.owner); 107 buff.append(header); 108 for (PerforceFileData fileData : getFileData()) { 109 String dataString = String.format("(%s %d %d %d %d) ", fileData.getFileName(), 110 fileData.getLinesAdded(), fileData.getLinesDeleted(), fileData.getLinesModified(), 111 fileData.getTotalLines()); 112 buff.append(dataString); 113 } 114 buff.append(']'); 115 return buff.toString(); 116 } 117 118 /** 119 * Inner class that provides information on a single file. 120 * @author Philip Johnson 121 */ 122 public static class PerforceFileData { 123 private String fileName; 124 private int linesAdded; 125 private int linesModified; 126 private int linesDeleted; 127 private int totalLoc; 128 129 /** 130 * Create a record with info about the given file. 131 * @param fileName The name. 132 * @param linesAdded Lines added. 133 * @param linesDeleted Lines deleted. 134 * @param linesModified Lines modified. 135 * @param totalLoc The total loc. 136 */ 137 public PerforceFileData(String fileName, int linesAdded, int linesDeleted, int linesModified, 138 int totalLoc) { 139 this.fileName = fileName; 140 this.linesAdded = linesAdded; 141 this.linesDeleted = linesDeleted; 142 this.linesModified = linesModified; 143 this.totalLoc = totalLoc; 144 } 145 146 /** 147 * Returns the file name. 148 * @return The file name. 149 */ 150 public String getFileName() { 151 return this.fileName; 152 } 153 154 /** 155 * Returns the lines added. 156 * @return The lines added. 157 */ 158 public int getLinesAdded() { 159 return this.linesAdded; 160 } 161 162 /** 163 * Returns the lines deleted. 164 * @return The lines deleted. 165 */ 166 public int getLinesDeleted() { 167 return this.linesDeleted; 168 } 169 170 /** 171 * Returns the lines modified. 172 * @return The lines modified. 173 */ 174 public int getLinesModified() { 175 return this.linesModified; 176 } 177 178 /** 179 * Returns the total number of lines. 180 * @return The total lines. 181 */ 182 public int getTotalLines() { 183 return this.totalLoc; 184 } 185 } 186 187 }