import java.util.*; import java.text.SimpleDateFormat; public class Repository { private String name; private LinkedList commits; public Repository(String name) { if (name.isEmpty() || name == null) { throw new IllegalArgumentException("Repository must have a name!"); } this.name = name; this.commits = new LinkedList<>(); } public String getRepoHead() { return commits.peek().id; } public int getRepoSize() { return commits.size(); } public String toString() { return name + " - Current head: " + commits.peek().toString(); } public boolean contains(String targetId) { for (Commit commit : commits) { if (commit.id == targetId) { return true; } } return false; } public String getHistory(int n) { String output = ""; for (int i = 0; i < commits.size() - 1; i++) { output += commits.get(i).toString() + "\n"; } return output; } public String commit(String message) { commits.add(new Commit(message)); return commits.peek().id; } public boolean drop(String targetId) { for (int n = 0; n < commits.size() - 1; n ++) { if (commits.get(n).id == targetId) { commits.remove(n); return true; } } return false; } public void synchronize(Repository other) { } /** * DO NOT MODIFY * A class that represents a single commit in the repository. * Commits are characterized by an identifier, a commit message, * and the time that the commit was made. A commit also stores * a reference to the immediately previous commit if it exists. * * Staff Note: You may notice that the comments in this * class openly mention the fields of the class. This is fine * because the fields of the Commit class are public. In general, * be careful about revealing implementation details! */ public class Commit { private static int currentCommitID; /** * The time, in milliseconds, at which this commit was created. */ public final long timeStamp; /** * A unique identifier for this commit. */ public final String id; /** * A message describing the changes made in this commit. */ public final String message; /** * A reference to the previous commit, if it exists. Otherwise, null. */ public Commit past; /** * Constructs a commit object. The unique identifier and timestamp * are automatically generated. * @param message A message describing the changes made in this commit. * @param past A reference to the commit made immediately before this * commit. */ public Commit(String message, Commit past) { this.id = "" + currentCommitID++; this.message = message; this.timeStamp = System.currentTimeMillis(); this.past = past; } /** * Constructs a commit object with no previous commit. The unique * identifier and timestamp are automatically generated. * @param message A message describing the changes made in this commit. */ public Commit(String message) { this(message, null); } /** * Returns a string representation of this commit. The string * representation consists of this commit's unique identifier, * timestamp, and message, in the following form: * "[identifier] at [timestamp]: [message]" * @return The string representation of this collection. */ @Override public String toString() { SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z"); Date date = new Date(timeStamp); return id + " at " + formatter.format(date) + ": " + message; } /** * Resets the IDs of the commit nodes such that they reset to 0. * Primarily for testing purposes. */ public static void resetIds() { Commit.currentCommitID = 0; } } }