All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sun.xml.ws.api.pipe.TubeCloner Maven / Gradle / Ivy

There is a newer version: 4.0.2
Show newest version
package com.sun.xml.ws.api.pipe;

import java.util.HashMap;
import java.util.Map;

/**
 * Clones the whole pipeline.
 *
 * 

* Since {@link Tube}s may form an arbitrary directed graph, someone needs * to keep track of isomorphism for a clone to happen correctly. This class * serves that role. * * @author Kohsuke Kawaguchi */ public class TubeCloner { // Pipe to pipe, or tube to tube protected final Map master2copy = new HashMap(); /*package*/ TubeCloner() { } /** * Invoked by a client of a tube to clone the whole pipeline. * *

* {@link Tube}s implementing the {@link Tube#copy(TubeCloner)} method * shall use {@link #copy(Tube)} method. * * @param p * The entry point of a pipeline to be copied. must not be null. * @return * The cloned pipeline. Always non-null. */ public static Tube clone(Tube p) { // we often want to downcast TubeCloner to PipeCloner, // so let's create PipeCloner to make that possible return new PipeCloner().copy(p); } /** * Invoked by a {@link Tube#copy(TubeCloner)} implementation * to copy a reference to another pipe. * *

* This method is for {@link Tube} implementations, not for users. * *

* If the given tube is already copied for this cloning episode, * this method simply returns that reference. Otherwise it copies * a tube, make a note, and returns a copied tube. This additional * step ensures that a graph is cloned isomorphically correctly. * *

* (Think about what happens when a graph is A->B, A->C, B->D, and C->D * if you don't have this step.) * * @param t * The tube to be copied. * @return * The cloned tube. Always non-null. */ public T copy(T t) { Tube r = (Tube)master2copy.get(t); if(r==null) { r = t.copy(this); // the pipe must puts its copy to the map by itself assert master2copy.get(t)==r : "the tube must call the add(...) method to register itself before start copying other pipes, but "+t +" hasn't done so"; } return (T)r; } /** * This method must be called from within the copy constructor * to notify that the copy was created. * *

* When your pipe has references to other pipes, * it's particularly important to call this method * before you start copying the pipes you refer to, * or else there's a chance of inifinite loop. */ public void add(Tube original, Tube copy) { assert !master2copy.containsKey(original); assert original!=null && copy!=null; master2copy.put(original,copy); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy