carfield.com.hkMain.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.iterator.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
import java.util.Iterator;
/**
* Implements the driver for the iterator design pattern example.<p>
*
* Intent: <i>Provide a way to access the elements of an aggregate object
* sequentially without exposing its underlying representation.</i><p>
*
* Participating objects are <code>SimpleList</code> as <i>Aggregate</i>,
* <code>OpenList</code> as <i>ConcreteAggregate</i>, <code>
* java.util.Iterator</code> as <i>Iterator</i>, and <code>ReverseIterator
* </code> as <i>ConcreteIterator</i>.
*
* In this example, the concrete aggregate is a list that gets filled with
* five integer objects (1 to 5). The, the <code>ReverseIterator</code> is
* created and used to print all elements in reverse order.
*
* <p><i>This is the Java version.</i><p>
*
* Aggregate and Iterator need to know about the pattern.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 07/13/02
*
* @see SimpleList
* @see OpenList
* @see java.util.Iterator
* @see ReverseIterator
*/
public class Main {
/**
* Prints all elements in the iterator to <code>System.out</code>.
*
* @param iter the iterator whose elements should be printed
*/
private static void print(Iterator iter) {
while(iter.hasNext()) {
System.out.println(iter.next());
}
}
/**
* Implements the driver for the iterator design pattern example.<p>
*
* In this example, the concrete aggregate is a list that gets filled with
* five integer objects (1 to 5). The, the <code>ReverseIterator</code> is
* created and used to print all elements in reverse order.
*
* @param args command line paramters, unused
*/
public static void main(String[] args) {
OpenList openList = new OpenList();
openList.append(new Integer(1));
openList.append(new Integer(2));
openList.append(new Integer(3));
openList.append(new Integer(4));
openList.append(new Integer(5));
System.out.println("List created, containing int objects 1, 2, 3, 4, 5.");
Iterator iter = openList.createReverseIterator();
System.out.println("Using ReverseIterator to print list elements in reverse order...");
print(iter);
System.out.println("done.");
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZOpenList.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.iterator.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
import java.util.Iterator;
/**
* Implements a basic open list. This implementation is based on
* <code>java.util.LinkedList</code>. In essence, this class acts as an
* adapter for the Java class.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 07/13/02
*
* @see SimpleList
*/
public class OpenList implements SimpleList {
java.util.LinkedList list = new java.util.LinkedList();
/**
* Returns the number of elements in the list
*
* @returns the number of elements in the list
*/
public int count() {
return list.size();
}
/**
* Appends an object to the list
*
* @param o the object to append
* @returns true if successful, false otherwise
*/
public boolean append(Object o) {
list.addLast(o);
return true;
}
/**
* Removes an object from the list
*
* @param o the object to remove
* @returns true if successful, false otherwise
*/
public boolean remove(Object o) {
return list.remove(o);
}
/**
* Returns an object from the list
*
* @param index the position of the object
* @returns the object at position index
*/
public Object get(int index) {
return list.get(index);
}
/**
* Returns a reverse iterator for this list.
*
* @returns the a reverse iterator for this list
*/
public Iterator createReverseIterator() {
return new ReverseIterator(this);
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZOpenListIteration.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.iterator.aspectj;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
import java.util.NoSuchElementException;
import java.util.Iterator;
/**
* Implements a concrete instance of the Iterator design pattern. This
* concrete aspect attaches a factory method to the <i>Aggregate</i> class
* and provides an implementation for the Iterator via an inner class.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 07/13/02
*
* @see SimpleList
* @see OpenList
*/
public aspect OpenListIteration {
/**
* Attaches the factory method to create the reverse iterator to the
* OpenList class using the open classes mechanism.
*/
Iterator OpenList.createReverseIterator() {
return new ReverseIterator(this);
}
/**
* Another possibility of where to place the factory method. If we
* have the factory method on the aspect, multiple instances of the
* same pattern will not be confused an can use the same aggregate
* (multiple factory methods won't conflict that way, even if they
* have the same signature). This is the preferrable implementation.
*/
Iterator createIteratorFor(OpenList list) {
return new ReverseIterator(list);
}
/**
* Provides the implementation of the reverse iterator. Instead
* of defining an inner class, one could also instantiate
* <code>java.util.Iterator</code> as an anonymous class and overwrite
* the appropriate methods.
*/
private static class ReverseIterator implements Iterator {
/**
* the positition of the current element
*/
protected int current;
/**
* the list this iterator operates on
*/
protected SimpleList list;
/**
* Returns true if the iteration has more elements.
*
* @returns true if the iteration has more elements
*/
public boolean hasNext() {
return (current > 0);
}
/**
* This opional method is not implemented for this iterator.
*/
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
/**
* Returns the next element in the iteration.
*
* @returns the next element in the iteration.
*/
public Object next() {
if (!hasNext()) {
throw new ArrayIndexOutOfBoundsException("Iterator out of Bounds");
} else {
return list.get(--current);
}
}
/**
* Creates a new ReverseIterator from the given list.
*
* @param list the list to generate an iterator from
*/
public ReverseIterator(SimpleList list) {
super();
this.list = list;
current = list.count();
}
}
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZReverseIterator.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.iterator.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
import java.util.Iterator;
/**
* Implements a reverse iterator. This means that it will return elements in
* reverse order. We chose not to define our own <i>Iterator</i>, but to use
* Java's <code>java.util.Iterator</code> interface.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 07/13/02
*
*/
public class ReverseIterator implements Iterator {
/**
* the positition of the current element
*/
protected int current;
/**
* the list this iterator operates on
*/
protected SimpleList list;
/**
* Returns true if the iteration has more elements.
*
* @returns true if the iteration has more elements
*/
public boolean hasNext() {
return (current > 0);
}
/**
* This opional method is not implemented for this iterator.
*/
public void remove() {
throw new UnsupportedOperationException("remove() not supported");
}
/**
* Returns the next element in the iteration.
*
* @returns the next element in the iteration.
*/
public Object next() {
if (!hasNext()) {
throw new ArrayIndexOutOfBoundsException("Iterator out of Bounds");
} else {
return list.get(--current);
}
}
/**
* Creates a new ReverseIterator from the given list.
*
* @param list the list to generate an iterator from
*/
public ReverseIterator(SimpleList list) {
super();
this.list = list;
current = list.count();
}
}</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00ZSimpleList.java2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><TEXTAREA name="code" class="java" rows="16" cols="100">package examples.iterator.java;
/* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* This file is part of the design patterns project at UBC
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is ca.ubc.cs.spl.patterns.
*
* Contributor(s):
*/
/**
* Defines an interface for a basic list.
*
* @author Jan Hannemann
* @author Gregor Kiczales
* @version 1.0, 07/13/02
*
* @see OpenList
*/
public interface SimpleList {
/**
* Returns the number of elements in the list
*
* @returns the number of elements in the list
*/
public int count();
/**
* Appends an object to the list
*
* @param o the object to append
* @returns true if successful, false otherwise
*/
public boolean append(Object o);
/**
* Removes an object from the list
*
* @param o the object to remove
* @returns true if successful, false otherwise
*/
public boolean remove(Object o);
/**
* Returns an object from the list
*
* @param index the position of the object
* @returns the object at position index
*/
public Object get(int index);
}
</TEXTAREA><br><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00Zfiles.lst2004-03-24T16:00:00Z2004-03-24T16:00:00Z<br/><br/><script type="text/javascript"><!--google_ad_client = "pub-9426659565807829";google_ad_slot = "9359905831";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>2004-03-24T16:00:00Z