📄 compositepattern.java
字号:
/* @<#>CompositePattern.java version 0.0.1, 1/1/2000
*
* THIS PROGRAM IS FREE SOFTWARE; YOU CAN DISTRIBUTE IT AND/OR
* MODIFY IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE
* AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.
*
* THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL,
* BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF
* MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE
* GNU GENERAL PUBLIC LICENSE FOR MORE DETAILS.
*
* Copyright (c) 2000 Wayne State University. All Rights Reserved.
*/
package naplet.itinerary;
import java.util.*;
import naplet.*;
/**
* The <code>CompositePattern</code> class defines a base of
* composite itineraries, in contrast to singleton itineraries.
*
* @version 0.0.1, 1/1/2000
* @author C. Xu
*/
public abstract class CompositePattern implements ItineraryPattern {
/*
* ArrayList is defined over a transient data array, which is support
* not to be part of the persistent state of its object by the transient
* definition in Java specification. However, the data array is serialized
* by ArrayList.writeObject (see Source Code JDK1.2.2).
* Its clone returns a shallow copy. So is LinkList.
*/
private ArrayList path;
/*
* Build-in classes Iterator and ListIterator are non-serializable.
* A custom iterator is implemented for Serializable and Clonable.
*/
protected ItineraryIterator iter;
protected Checkable guard;
protected Operable action;
public abstract void go(Naplet nap)
throws java.rmi.RemoteException,UnableDispatchException;
protected int itinerarySize() { return path.size(); }
public CompositePattern() {
path = new ArrayList();
iter = new IteratorImpl();
guard = null;
action = null;
}
public CompositePattern(String[] hosts )
throws InvalidItineraryException {
this(null, hosts, null);
}
public CompositePattern( String[] hosts, Operable act)
throws InvalidItineraryException {
this(null, hosts, act);
}
public CompositePattern( Checkable guard, String[] hosts)
throws InvalidItineraryException {
this(guard, hosts, null);
}
public CompositePattern( Checkable guard, String[] hosts, Operable act)
throws InvalidItineraryException {
SingletonPattern[] si = new SingletonPattern[hosts.length];
for (int i=0; i<hosts.length; i++)
si[i] = new SingletonPattern( hosts[i] );
path = new ArrayList( hosts.length );
path.addAll( Arrays.asList( si ) );
iter = new IteratorImpl();
action = act;
this.guard = guard;
}
public CompositePattern( ItineraryPattern[] itin ) {
this(null, itin, null);
}
public CompositePattern( ItineraryPattern[] itin, Operable act ) {
this(null, itin, act);
}
public CompositePattern( Checkable guard, ItineraryPattern[] itin ) {
this(guard, itin, null);
}
public CompositePattern( Checkable guard, ItineraryPattern[] itin, Operable act ) {
path = new ArrayList( itin.length );
path.addAll( Arrays.asList( itin ) );
iter = new IteratorImpl();
action = act;
this.guard = guard;
}
/**
* Get the first naplet server URN
*/
public URN first() {
ItineraryPattern itin = (ItineraryPattern)path.get(0);
return itin.first();
}
public void reset() {
iter.reset();
}
/**
* Generat an itinerary in string like SEQ(S0, S1, S2)
*/
public String toString()
{
StringBuffer out = new StringBuffer();
if (this instanceof SeqPattern)
out.append( "SEQ(" );
else if (this instanceof ParPattern)
out.append("PAR(" );
else {
out.append("???(" );
}
out.append( ItineraryToString(path).toString() + ")" );
return out.toString();
}
private StringBuffer ItineraryToString( ArrayList path ) {
StringBuffer out = new StringBuffer();
for (int i=0; i<path.size(); i++) {
ItineraryPattern itin = (ItineraryPattern) path.get(i);
if ( itin instanceof SingletonPattern ) {
out.append( itin.toString() + " " );
} else {
if (itin instanceof SeqPattern)
out.append( "SEQ(" );
else if (itin instanceof ParPattern)
out.append("PAR(" );
else {
out.append("???(");
}
out.append( ItineraryToString( ((CompositePattern)itin).path ));
out.append(")");
}
}
return out;
}
// ArrayList.clone() retruns a shallow copy.
public synchronized Object clone() {
return deepClone( this );
}
public CompositePattern deepClone( CompositePattern itin ) {
try {
CompositePattern ci = (CompositePattern)super.clone();
ci.iter = (ItineraryIterator)iter.clone();
ci.action = action;
ci.guard = guard;
List list = itin.path;
ci.path = new ArrayList( list.size() );
for (int i=0; i<list.size(); i++) {
ItineraryPattern orgItin = (ItineraryPattern) list.get(i);
if ( orgItin instanceof SingletonPattern ) {
ci.path.add((SingletonPattern) orgItin.clone());
} else {
ci.path.add(deepClone( (CompositePattern) orgItin ));
}
}
return ci;
} catch (CloneNotSupportedException e) {
throw new NapletInternalError();
}
}
/**
* The <code>IteratorImpl</code> inner object defines a custom iterator
* over <code>CompositePattern</code> objects.
*/
class IteratorImpl implements ItineraryIterator {
Integer cursor;
public IteratorImpl() {
cursor = new Integer(0);
}
public synchronized Object clone() {
try {
IteratorImpl ii = (IteratorImpl)super.clone();
ii.cursor = new Integer( cursor.intValue() );
return ii;
} catch (CloneNotSupportedException e) {
throw new NapletInternalError();
}
}
public boolean hasNext() {
return cursor.intValue() < path.size();
}
public ItineraryPattern next() {
try {
int cur = cursor.intValue();
ItineraryPattern next = (ItineraryPattern) path.get(cur);
cursor = new Integer( cur+1 );
return next;
} catch (IndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
public void reset() {
cursor = new Integer(0);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -