src/core/net/sf/basedb/util/collections/MultiStepCollectionTransformer.java

Code
Comments
Other
Rev Date Author Line
4723 13 Jan 09 nicklas 1 /**
4723 13 Jan 09 nicklas 2   $Id$
4723 13 Jan 09 nicklas 3
4723 13 Jan 09 nicklas 4   Copyright (C) 2008 Nicklas Nordborg
4723 13 Jan 09 nicklas 5
4723 13 Jan 09 nicklas 6   This file is part of BASE - BioArray Software Environment.
4723 13 Jan 09 nicklas 7   Available at http://base.thep.lu.se/
4723 13 Jan 09 nicklas 8
4723 13 Jan 09 nicklas 9   BASE is free software; you can redistribute it and/or
4723 13 Jan 09 nicklas 10   modify it under the terms of the GNU General Public License
4723 13 Jan 09 nicklas 11   as published by the Free Software Foundation; either version 3
4723 13 Jan 09 nicklas 12   of the License, or (at your option) any later version.
4723 13 Jan 09 nicklas 13
4723 13 Jan 09 nicklas 14   BASE is distributed in the hope that it will be useful,
4723 13 Jan 09 nicklas 15   but WITHOUT ANY WARRANTY; without even the implied warranty of
4723 13 Jan 09 nicklas 16   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4723 13 Jan 09 nicklas 17   GNU General Public License for more details.
4723 13 Jan 09 nicklas 18
4723 13 Jan 09 nicklas 19   You should have received a copy of the GNU General Public License
4723 13 Jan 09 nicklas 20   along with BASE. If not, see <http://www.gnu.org/licenses/>.
4723 13 Jan 09 nicklas 21 */
4723 13 Jan 09 nicklas 22 package net.sf.basedb.util.collections;
4723 13 Jan 09 nicklas 23
4723 13 Jan 09 nicklas 24 import java.util.Collection;
4723 13 Jan 09 nicklas 25 import java.util.HashSet;
4723 13 Jan 09 nicklas 26 import java.util.List;
4723 13 Jan 09 nicklas 27
4723 13 Jan 09 nicklas 28 /**
4723 13 Jan 09 nicklas 29   Collection transformer implementation that transforms a source
4723 13 Jan 09 nicklas 30   collection into the destination collection using multiple steps.
4723 13 Jan 09 nicklas 31   Each transformation step is delegated to a transformer that
4723 13 Jan 09 nicklas 32   is specified in the constructor. Source and destination collection
4723 13 Jan 09 nicklas 33   types must match between each step. Eg. the destination collection
4723 13 Jan 09 nicklas 34   from the first step is used as the source collection in the second 
4723 13 Jan 09 nicklas 35   step, the destination from the second step is used as source in
4723 13 Jan 09 nicklas 36   the third step, and so on.
4723 13 Jan 09 nicklas 37   <p>
4723 13 Jan 09 nicklas 38   This class is abstract since it doesn't know which type of collection
4723 13 Jan 09 nicklas 39   to use in the intermediary steps. Subclasses need to implement {@link
4723 13 Jan 09 nicklas 40   #createTempCollection(CollectionTransformer)} that should create a new 
4723 13 Jan 09 nicklas 41   collection in each step.
4723 13 Jan 09 nicklas 42   
4723 13 Jan 09 nicklas 43   @author Nicklas
4723 13 Jan 09 nicklas 44   @version 2.10
4723 13 Jan 09 nicklas 45   @base.modified $Date$
4723 13 Jan 09 nicklas 46 */
4723 13 Jan 09 nicklas 47 public class MultiStepCollectionTransformer<S, D>
4723 13 Jan 09 nicklas 48   implements CollectionTransformer<S, D>
4723 13 Jan 09 nicklas 49 {
4723 13 Jan 09 nicklas 50
6875 20 Apr 15 nicklas 51   private final List<CollectionTransformer<?, ?>> steps;
4723 13 Jan 09 nicklas 52   
6875 20 Apr 15 nicklas 53   public MultiStepCollectionTransformer(List<CollectionTransformer<?, ?>> steps)
4723 13 Jan 09 nicklas 54   {
4723 13 Jan 09 nicklas 55     this.steps = steps;
4723 13 Jan 09 nicklas 56   }
4723 13 Jan 09 nicklas 57   
4723 13 Jan 09 nicklas 58   /*
4723 13 Jan 09 nicklas 59     From the CollectionTransformer interface
4723 13 Jan 09 nicklas 60     ----------------------------------------
4723 13 Jan 09 nicklas 61   */
4723 13 Jan 09 nicklas 62   /**
4723 13 Jan 09 nicklas 63     Transform the source collection into the destination collection
4723 13 Jan 09 nicklas 64     by applying all intermediate transformers specified in the
4723 13 Jan 09 nicklas 65     constructor.
4723 13 Jan 09 nicklas 66     @return The result of the last transformation step
4723 13 Jan 09 nicklas 67   */
4723 13 Jan 09 nicklas 68   @Override
6875 20 Apr 15 nicklas 69   @SuppressWarnings({ "unchecked", "rawtypes" })
4723 13 Jan 09 nicklas 70   public int transform(Collection<? extends S> source, Collection<? super D> destination)
4723 13 Jan 09 nicklas 71   {
4723 13 Jan 09 nicklas 72     Collection<?> src = source;
4723 13 Jan 09 nicklas 73     Collection<?> dest = null;
4723 13 Jan 09 nicklas 74     int lastStep = steps.size() - 1;
4723 13 Jan 09 nicklas 75     int result = 0;
4723 13 Jan 09 nicklas 76     for (int i = 0; i <= lastStep; ++i)
4723 13 Jan 09 nicklas 77     {
4723 13 Jan 09 nicklas 78       CollectionTransformer transformer = steps.get(i);
4723 13 Jan 09 nicklas 79       dest = i == lastStep ? destination : createTempCollection(transformer);
4723 13 Jan 09 nicklas 80       result = transformer.transform(src, dest);
4723 13 Jan 09 nicklas 81       src = dest;
4723 13 Jan 09 nicklas 82     }
4723 13 Jan 09 nicklas 83     return result;
4723 13 Jan 09 nicklas 84   }
4723 13 Jan 09 nicklas 85   // ------------------------------------------
4723 13 Jan 09 nicklas 86
4723 13 Jan 09 nicklas 87   /**
4723 13 Jan 09 nicklas 88     Create a temporary collection that is suitable for holding an intermediary
4723 13 Jan 09 nicklas 89     result. The returned collection will be used as the destination
4723 13 Jan 09 nicklas 90     collection when calling {@link CollectionTransformer#transform(Collection, Collection)}.
4723 13 Jan 09 nicklas 91     The default implementation always create {@link HashSet} collections. If 
4723 13 Jan 09 nicklas 92     a different type of collection is needed, a subclass that overrides this method
4723 13 Jan 09 nicklas 93     should be used.
4723 13 Jan 09 nicklas 94     <p>
4723 13 Jan 09 nicklas 95     NOTE! Each call to this method should create a new collection.
4723 13 Jan 09 nicklas 96   */
6875 20 Apr 15 nicklas 97   protected Collection<?> createTempCollection(CollectionTransformer<?, ?> transformer)
4723 13 Jan 09 nicklas 98   {
6875 20 Apr 15 nicklas 99     return new HashSet<Object>();
4723 13 Jan 09 nicklas 100   }
4723 13 Jan 09 nicklas 101   
4723 13 Jan 09 nicklas 102   
6127 14 Sep 12 nicklas 103   @Override
4723 13 Jan 09 nicklas 104   public String toString()
4723 13 Jan 09 nicklas 105   {
4723 13 Jan 09 nicklas 106     StringBuilder sb = new StringBuilder();
4723 13 Jan 09 nicklas 107     sb.append("MultiStepCollectionTransformer: ");
4723 13 Jan 09 nicklas 108     boolean first = true;
6875 20 Apr 15 nicklas 109     for (CollectionTransformer<?, ?> ct : steps)
4723 13 Jan 09 nicklas 110     {
4723 13 Jan 09 nicklas 111       if (!first) sb.append("-->");
4723 13 Jan 09 nicklas 112       sb.append(ct.toString());
4723 13 Jan 09 nicklas 113       first = false;
4723 13 Jan 09 nicklas 114     }
4723 13 Jan 09 nicklas 115     return sb.toString();
4723 13 Jan 09 nicklas 116   }
4723 13 Jan 09 nicklas 117 }