<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FCollections_Data_Structure%2FHistory</id>
		<title>Java/Collections Data Structure/History - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java%2FCollections_Data_Structure%2FHistory"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Collections_Data_Structure/History&amp;action=history"/>
		<updated>2026-04-19T04:45:24Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Collections_Data_Structure/History&amp;diff=9111&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Collections_Data_Structure/History&amp;diff=9111&amp;oldid=prev"/>
				<updated>2010-06-01T07:25:25Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 07:25, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java/Collections_Data_Structure/History&amp;diff=9110&amp;oldid=prev</id>
		<title> в 18:01, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java/Collections_Data_Structure/History&amp;diff=9110&amp;oldid=prev"/>
				<updated>2010-05-31T18:01:48Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== History management ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
  &lt;br /&gt;
/*&lt;br /&gt;
 * Copyright (c) 1998-2002 Carnegie Mellon University.  All rights&lt;br /&gt;
 * reserved.&lt;br /&gt;
 *&lt;br /&gt;
 * Redistribution and use in source and binary forms, with or without&lt;br /&gt;
 * modification, are permitted provided that the following conditions&lt;br /&gt;
 * are met:&lt;br /&gt;
 *&lt;br /&gt;
 * 1. Redistributions of source code must retain the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer.&lt;br /&gt;
 *&lt;br /&gt;
 * 2. Redistributions in binary form must reproduce the above copyright&lt;br /&gt;
 *    notice, this list of conditions and the following disclaimer in&lt;br /&gt;
 *    the documentation and/or other materials provided with the&lt;br /&gt;
 *    distribution.&lt;br /&gt;
 *&lt;br /&gt;
 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS&amp;quot;&amp;quot; AND&lt;br /&gt;
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,&lt;br /&gt;
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR&lt;br /&gt;
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY&lt;br /&gt;
 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,&lt;br /&gt;
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT&lt;br /&gt;
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,&lt;br /&gt;
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY&lt;br /&gt;
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT&lt;br /&gt;
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE&lt;br /&gt;
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.&lt;br /&gt;
 *&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
import java.util.Enumeration;&lt;br /&gt;
public class History {&lt;br /&gt;
    &lt;br /&gt;
    protected Object[] history;   // array of history objects (arranged as&lt;br /&gt;
                        // a circular queue)&lt;br /&gt;
    protected int start;      // points to oldest object in history&lt;br /&gt;
    protected int end;        // points after newest object in history&lt;br /&gt;
    protected int curr;       // points to current object;&lt;br /&gt;
                    // either start &amp;lt;= curr &amp;lt; end (modulo history.length)&lt;br /&gt;
                    // or start == curr == end&lt;br /&gt;
    /**&lt;br /&gt;
     * Make a History.&lt;br /&gt;
     * @param   max     Maximum length of history&lt;br /&gt;
     */&lt;br /&gt;
    public History (int max) {&lt;br /&gt;
        history = new Object[max+1];&lt;br /&gt;
        start = end = curr = 0;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Make a duplicate of another History.&lt;br /&gt;
     * @param   h History to copy&lt;br /&gt;
     */&lt;br /&gt;
    public History (History h) {&lt;br /&gt;
        this.history = new Object[h.history.length];&lt;br /&gt;
        System.arraycopy (h.history, 0, this.history, 0, h.history.length);&lt;br /&gt;
        this.start = h.start;&lt;br /&gt;
        this.end = h.end;&lt;br /&gt;
        this.curr = h.curr;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Clear the history.&lt;br /&gt;
     */&lt;br /&gt;
    public void clear () {&lt;br /&gt;
        for (int i = 0; i &amp;lt; history.length; ++i)&lt;br /&gt;
            history[i] = null;&lt;br /&gt;
        int s = start;&lt;br /&gt;
        int e = end;&lt;br /&gt;
        start = end = curr = 0;&lt;br /&gt;
        if (s != e)&lt;br /&gt;
            fireRemoved (s, (e &amp;gt; 0) ? e-1 : history.length-1);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Double the capacity of the history.&lt;br /&gt;
     */&lt;br /&gt;
    public void expand () {&lt;br /&gt;
        Object[] newHistory = new Object[(history.length * 2) - 1];&lt;br /&gt;
        int i = 0;&lt;br /&gt;
        int newCurr = 0;&lt;br /&gt;
        for (int j = start; j != end; j = (j+1) % history.length, ++i) {&lt;br /&gt;
            newHistory[i] = history[j];&lt;br /&gt;
            if (j == curr)&lt;br /&gt;
                newCurr = i;&lt;br /&gt;
        }&lt;br /&gt;
        history = newHistory;&lt;br /&gt;
        start = 0;&lt;br /&gt;
        end = i;&lt;br /&gt;
        curr = newCurr;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Add an object to the history after the current point (deleting all&lt;br /&gt;
     * objects forward of this point).  If history overflows, the oldest&lt;br /&gt;
     * object is thrown away.&lt;br /&gt;
     * @param obj   Object to add to history&lt;br /&gt;
     */&lt;br /&gt;
    public void put (Object obj) {&lt;br /&gt;
        if (!isEmpty ()) {&lt;br /&gt;
            // throw away all objects after curr&lt;br /&gt;
            int newEnd = (curr+1) % history.length;&lt;br /&gt;
            if (newEnd != end) {&lt;br /&gt;
                int e = end;&lt;br /&gt;
                end = newEnd;&lt;br /&gt;
                fireRemoved (newEnd, (e &amp;gt; 0) ? e-1 : history.length-1);&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        add (obj);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Add an object to the end of the history, moving the current point to it.&lt;br /&gt;
     * If history overflows, the oldest object is thrown away.&lt;br /&gt;
     * @param obj   Object to add to history&lt;br /&gt;
     */&lt;br /&gt;
    public void add (Object obj) {&lt;br /&gt;
        if (isFull ()) {&lt;br /&gt;
            // throw away oldest object to make room&lt;br /&gt;
            start = (start+1) % history.length;&lt;br /&gt;
            fireRemoved (start, start);&lt;br /&gt;
        }&lt;br /&gt;
        // insert the new object at end&lt;br /&gt;
        history[end] = obj;&lt;br /&gt;
        curr = end;&lt;br /&gt;
        end = (end+1) % history.length;&lt;br /&gt;
        fireAdded (curr, curr);&lt;br /&gt;
        System.out.println(&amp;quot;after put: start=&amp;quot; + start + &amp;quot;, end=&amp;quot; + end + &amp;quot;, curr=&amp;quot; + curr);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the current object of the history.&lt;br /&gt;
     * @return current object of history, or null if history is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public Object get () {&lt;br /&gt;
        return !isEmpty () ? history[curr] : null;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the object that would be returned by back(), without actually&lt;br /&gt;
     * changing the current object.&lt;br /&gt;
     * @return object before current object, or null if at beginning of &lt;br /&gt;
     * history or history is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public Object peekBack () {&lt;br /&gt;
        if (curr == start)&lt;br /&gt;
            return null;&lt;br /&gt;
        else {&lt;br /&gt;
            int bk = (curr &amp;gt; 0) ? curr-1 : history.length-1;&lt;br /&gt;
            return history[bk];&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the object that would be returned by forward(), without actually&lt;br /&gt;
     * changing the current object.&lt;br /&gt;
     * @return object after current object, or null if at end of &lt;br /&gt;
     * history or history is empty.&lt;br /&gt;
     */&lt;br /&gt;
    public Object peekForward () {&lt;br /&gt;
        if (start == end)&lt;br /&gt;
            return null;&lt;br /&gt;
        int fw = (curr+1) % history.length;&lt;br /&gt;
        if (fw == end)&lt;br /&gt;
            return null;&lt;br /&gt;
        else &lt;br /&gt;
            return history[fw];&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Replace the current object of the history. The rest of the history&lt;br /&gt;
     * is unaffected, and the current pointer stays where it is.&lt;br /&gt;
     * &amp;lt;P&amp;gt; If the history is empty,&lt;br /&gt;
     * then this call is equivalent to put(obj).&lt;br /&gt;
     * @param obj object to substitute&lt;br /&gt;
     */&lt;br /&gt;
    public void replace (Object obj) {&lt;br /&gt;
        if (isEmpty ())&lt;br /&gt;
            put (obj);&lt;br /&gt;
        else {&lt;br /&gt;
            history[curr] = obj;&lt;br /&gt;
            fireChanged (curr, curr);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Move back one object in the history, if possible.&lt;br /&gt;
     * @return previous object in the history, or null if at start.&lt;br /&gt;
     */&lt;br /&gt;
    public Object back () {&lt;br /&gt;
        if (curr == start)&lt;br /&gt;
            return null;&lt;br /&gt;
        else {&lt;br /&gt;
            curr = (curr &amp;gt; 0) ? curr-1 : history.length-1;&lt;br /&gt;
            fireChanged (curr, curr);&lt;br /&gt;
            return history[curr];&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Move forward one object in the history, if possible.&lt;br /&gt;
     * @return next object in the history, or null if at end of history.&lt;br /&gt;
     */&lt;br /&gt;
    public Object forward () {&lt;br /&gt;
        if (start == end)&lt;br /&gt;
            return null;&lt;br /&gt;
        int newCurr = (curr+1) % history.length;&lt;br /&gt;
        if (newCurr == end)&lt;br /&gt;
            return null;&lt;br /&gt;
        else {&lt;br /&gt;
            curr = newCurr;&lt;br /&gt;
            fireChanged (curr, curr);&lt;br /&gt;
            return history[curr];&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Move to first (oldest) object in the history, if possible.&lt;br /&gt;
     * @return first object in the history, or null if history empty.&lt;br /&gt;
     */&lt;br /&gt;
    public Object toStart () {&lt;br /&gt;
        if (curr != start) {&lt;br /&gt;
            curr = start;&lt;br /&gt;
            fireChanged (curr, curr);&lt;br /&gt;
        }&lt;br /&gt;
        return history[curr];&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Move to last (newest) object in the history, if possible.&lt;br /&gt;
     * @return last object in the history, or null if history empty.&lt;br /&gt;
     */&lt;br /&gt;
    public Object toEnd () {&lt;br /&gt;
        if (start == end)&lt;br /&gt;
            return null;&lt;br /&gt;
        int newCurr = (end &amp;gt; 0) ? end-1 : history.length-1;&lt;br /&gt;
        if (curr != newCurr) {&lt;br /&gt;
            curr = newCurr;&lt;br /&gt;
            fireChanged (curr, curr);&lt;br /&gt;
        }&lt;br /&gt;
        return history[curr];&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Test whether back() will succeed.&lt;br /&gt;
     * @return true if and only if there are objects before the current object&lt;br /&gt;
     */&lt;br /&gt;
    public boolean canBack () {&lt;br /&gt;
        return (curr != start);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Test whether forward() will succeed.&lt;br /&gt;
     * @return true if and only if there are objects after the current object&lt;br /&gt;
     */&lt;br /&gt;
    public boolean canForward () {&lt;br /&gt;
        return ((curr+1) % history.length != end);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Test whether history is empty.&lt;br /&gt;
     * @return true if and only if history contains no objects&lt;br /&gt;
     */&lt;br /&gt;
    public boolean isEmpty () {&lt;br /&gt;
        return start == end;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Test whether history is full.&lt;br /&gt;
     * @return true if and only if history contains max objects&lt;br /&gt;
     */&lt;br /&gt;
    public boolean isFull () {&lt;br /&gt;
        return start == (end+1) % history.length;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Test whether history already contains an object.&lt;br /&gt;
     * @param obj   Object to search for&lt;br /&gt;
     * @return true if and only if history contains an object that equals() obj&lt;br /&gt;
     */&lt;br /&gt;
    public boolean contains (Object obj) {&lt;br /&gt;
        for (int i = start; i != end; i = (i+1) % history.length)&lt;br /&gt;
            if (history[i].equals (obj))&lt;br /&gt;
                return true;&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the objects in the history.&lt;br /&gt;
     * @returns enumeration yielding the history objects in order from&lt;br /&gt;
     * oldest to newest.&lt;br /&gt;
     */&lt;br /&gt;
    public Enumeration elements () {&lt;br /&gt;
        return new HistoryEnumeration (start, end);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the objects AFTER the current object.&lt;br /&gt;
     * @returns enumeration yielding the history objects after current,&lt;br /&gt;
     * in order from oldest to newest.&lt;br /&gt;
     */&lt;br /&gt;
    public Enumeration forwardElements () {&lt;br /&gt;
        return &lt;br /&gt;
            (curr == end)&lt;br /&gt;
            ? new HistoryEnumeration (curr, end)&lt;br /&gt;
            : new HistoryEnumeration ((curr + 1) % history.length, end);&lt;br /&gt;
    }&lt;br /&gt;
    /**&lt;br /&gt;
     * Get the objects BEFORE the current object.&lt;br /&gt;
     * @returns enumeration yielding the history objects before current,&lt;br /&gt;
     * in order from oldest to newest.&lt;br /&gt;
     */&lt;br /&gt;
    public Enumeration backElements () {&lt;br /&gt;
        return new HistoryEnumeration (start, curr);&lt;br /&gt;
    }&lt;br /&gt;
    class HistoryEnumeration implements Enumeration {&lt;br /&gt;
        int p;&lt;br /&gt;
        int e;&lt;br /&gt;
        public HistoryEnumeration (int start, int end) {&lt;br /&gt;
            p = start;&lt;br /&gt;
            e = end;&lt;br /&gt;
        }&lt;br /&gt;
        public boolean hasMoreElements () {&lt;br /&gt;
            return p != e;&lt;br /&gt;
        }&lt;br /&gt;
        public Object nextElement () {&lt;br /&gt;
            Object obj = history[p];&lt;br /&gt;
            p = (p+1) % history.length;&lt;br /&gt;
            return obj;&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
    protected void fireRemoved (int i, int j) {&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void fireAdded (int i, int j) {&lt;br /&gt;
    }&lt;br /&gt;
    &lt;br /&gt;
    protected void fireChanged (int i, int j) {&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
   &lt;br /&gt;
    &lt;br /&gt;
  &amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>