001package jmri.util.docbook;
002
003import java.util.ArrayList;
004
005/**
006 * Memo class to remember a revision history.
007 * <p>
008 * These can be nested: A revision can come with a history.
009 *
010 * @author Bob Jacobsen Copyright (c) 2010
011 */
012public class RevHistory {
013
014    ArrayList<Revision> list = new ArrayList<>();
015
016    /**
017     * Used to add a revision form complete information created elsewhere
018     *
019     * @param revnumber      the revision number
020     * @param date           the revision date
021     * @param authorinitials the author's initials
022     * @param revremark      the revision
023     */
024    public void addRevision(int revnumber, String date, String authorinitials, String revremark) {
025        Revision r = new Revision();
026        r.revnumber = revnumber;
027        r.date = date;
028        r.authorinitials = authorinitials;
029        r.revremark = revremark;
030
031        list.add(r);
032    }
033
034    public void addRevision(Revision r) {
035        list.add(r);
036    }
037
038    /**
039     * Usual form.
040     *
041     * Dated now, with the next number.
042     *
043     * @param authorinitials the author's initials
044     * @param revremark      the revision
045     */
046    public void addRevision(String authorinitials, String revremark) {
047        addRevision(maxNumber() + 1, (new java.util.Date()).toString(), authorinitials, revremark);
048    }
049
050    public int maxNumber() {
051        int retval = 0;  // zero if none yet
052        for (int i = 0; i < list.size(); i++) {
053            retval = (list.get(i).revnumber > retval) ? list.get(i).revnumber : retval;
054        }
055        return retval;
056    }
057
058    /**
059     * Add a revision, credited to the current user
060     *
061     * @param revremark the revision
062     */
063    public void addRevision(String revremark) {
064        addRevision(System.getProperty("user.name"), revremark);
065    }
066
067    public String toString(String prefix) {
068        StringBuilder retval = new StringBuilder();
069        for (int i = 0; i < list.size(); i++) {
070            Revision r = list.get(i);
071            retval.append(prefix)
072                    .append(r.revnumber)
073                    .append(", ")
074                    .append(r.date)
075                    .append(", ")
076                    .append(r.authorinitials)
077                    .append(", ")
078                    .append(r.revremark)
079                    .append("\n");
080        }
081        return retval.toString();
082    }
083
084    @Override
085    public String toString() {
086        return toString("");
087    }
088
089    public ArrayList<Revision> getList() {
090        return list;
091    }
092
093}