4723 |
13 Jan 09 |
nicklas |
1 |
/** |
4723 |
13 Jan 09 |
nicklas |
$Id$ |
4723 |
13 Jan 09 |
nicklas |
3 |
|
4723 |
13 Jan 09 |
nicklas |
Copyright (C) 2008 Nicklas Nordborg |
4723 |
13 Jan 09 |
nicklas |
5 |
|
4723 |
13 Jan 09 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
4723 |
13 Jan 09 |
nicklas |
Available at http://base.thep.lu.se/ |
4723 |
13 Jan 09 |
nicklas |
8 |
|
4723 |
13 Jan 09 |
nicklas |
BASE is free software; you can redistribute it and/or |
4723 |
13 Jan 09 |
nicklas |
modify it under the terms of the GNU General Public License |
4723 |
13 Jan 09 |
nicklas |
as published by the Free Software Foundation; either version 3 |
4723 |
13 Jan 09 |
nicklas |
of the License, or (at your option) any later version. |
4723 |
13 Jan 09 |
nicklas |
13 |
|
4723 |
13 Jan 09 |
nicklas |
BASE is distributed in the hope that it will be useful, |
4723 |
13 Jan 09 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
4723 |
13 Jan 09 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
4723 |
13 Jan 09 |
nicklas |
GNU General Public License for more details. |
4723 |
13 Jan 09 |
nicklas |
18 |
|
4723 |
13 Jan 09 |
nicklas |
You should have received a copy of the GNU General Public License |
4723 |
13 Jan 09 |
nicklas |
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 |
Collection transformer implementation that transforms a source |
4723 |
13 Jan 09 |
nicklas |
collection into the destination collection using multiple steps. |
4723 |
13 Jan 09 |
nicklas |
Each transformation step is delegated to a transformer that |
4723 |
13 Jan 09 |
nicklas |
is specified in the constructor. Source and destination collection |
4723 |
13 Jan 09 |
nicklas |
types must match between each step. Eg. the destination collection |
4723 |
13 Jan 09 |
nicklas |
from the first step is used as the source collection in the second |
4723 |
13 Jan 09 |
nicklas |
step, the destination from the second step is used as source in |
4723 |
13 Jan 09 |
nicklas |
the third step, and so on. |
4723 |
13 Jan 09 |
nicklas |
<p> |
4723 |
13 Jan 09 |
nicklas |
This class is abstract since it doesn't know which type of collection |
4723 |
13 Jan 09 |
nicklas |
to use in the intermediary steps. Subclasses need to implement {@link |
4723 |
13 Jan 09 |
nicklas |
#createTempCollection(CollectionTransformer)} that should create a new |
4723 |
13 Jan 09 |
nicklas |
collection in each step. |
4723 |
13 Jan 09 |
nicklas |
42 |
|
4723 |
13 Jan 09 |
nicklas |
@author Nicklas |
4723 |
13 Jan 09 |
nicklas |
@version 2.10 |
4723 |
13 Jan 09 |
nicklas |
@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 |
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 |
Transform the source collection into the destination collection |
4723 |
13 Jan 09 |
nicklas |
by applying all intermediate transformers specified in the |
4723 |
13 Jan 09 |
nicklas |
constructor. |
4723 |
13 Jan 09 |
nicklas |
@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 |
Create a temporary collection that is suitable for holding an intermediary |
4723 |
13 Jan 09 |
nicklas |
result. The returned collection will be used as the destination |
4723 |
13 Jan 09 |
nicklas |
collection when calling {@link CollectionTransformer#transform(Collection, Collection)}. |
4723 |
13 Jan 09 |
nicklas |
The default implementation always create {@link HashSet} collections. If |
4723 |
13 Jan 09 |
nicklas |
a different type of collection is needed, a subclass that overrides this method |
4723 |
13 Jan 09 |
nicklas |
should be used. |
4723 |
13 Jan 09 |
nicklas |
<p> |
4723 |
13 Jan 09 |
nicklas |
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 |
} |