6774 |
17 Mar 15 |
nicklas |
1 |
/** |
6774 |
17 Mar 15 |
nicklas |
$Id$ |
6774 |
17 Mar 15 |
nicklas |
3 |
|
6774 |
17 Mar 15 |
nicklas |
Copyright (C) 2015 Nicklas Nordborg |
6774 |
17 Mar 15 |
nicklas |
5 |
|
6774 |
17 Mar 15 |
nicklas |
This file is part of BASE - BioArray Software Environment. |
6774 |
17 Mar 15 |
nicklas |
Available at http://base.thep.lu.se/ |
6774 |
17 Mar 15 |
nicklas |
8 |
|
6774 |
17 Mar 15 |
nicklas |
BASE is free software; you can redistribute it and/or |
6774 |
17 Mar 15 |
nicklas |
modify it under the terms of the GNU General Public License |
6774 |
17 Mar 15 |
nicklas |
as published by the Free Software Foundation; either version 3 |
6774 |
17 Mar 15 |
nicklas |
of the License, or (at your option) any later version. |
6774 |
17 Mar 15 |
nicklas |
13 |
|
6774 |
17 Mar 15 |
nicklas |
BASE is distributed in the hope that it will be useful, |
6774 |
17 Mar 15 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
6774 |
17 Mar 15 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
6774 |
17 Mar 15 |
nicklas |
GNU General Public License for more details. |
6774 |
17 Mar 15 |
nicklas |
18 |
|
6774 |
17 Mar 15 |
nicklas |
You should have received a copy of the GNU General Public License |
6774 |
17 Mar 15 |
nicklas |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
6774 |
17 Mar 15 |
nicklas |
21 |
*/ |
6774 |
17 Mar 15 |
nicklas |
22 |
package net.sf.basedb.util.listable; |
6774 |
17 Mar 15 |
nicklas |
23 |
|
6796 |
24 Mar 15 |
nicklas |
24 |
import java.util.HashSet; |
6774 |
17 Mar 15 |
nicklas |
25 |
import java.util.List; |
6774 |
17 Mar 15 |
nicklas |
26 |
import java.util.Set; |
6774 |
17 Mar 15 |
nicklas |
27 |
|
6774 |
17 Mar 15 |
nicklas |
28 |
import net.sf.basedb.core.Item; |
6774 |
17 Mar 15 |
nicklas |
29 |
|
6774 |
17 Mar 15 |
nicklas |
30 |
/** |
6774 |
17 Mar 15 |
nicklas |
Chains together one or more source item transformers. Each step in the |
6774 |
17 Mar 15 |
nicklas |
chain should take over from the previous in the sense that the |
6774 |
17 Mar 15 |
nicklas |
{@link SourceItemTransformer#getSourceItemType()} of step N+1 |
6774 |
17 Mar 15 |
nicklas |
must match the {@link SourceItemTransformer#getTargetItemType()} |
6774 |
17 Mar 15 |
nicklas |
of step N. |
6774 |
17 Mar 15 |
nicklas |
36 |
|
6774 |
17 Mar 15 |
nicklas |
@author Nicklas |
6774 |
17 Mar 15 |
nicklas |
@since 3.5 |
6774 |
17 Mar 15 |
nicklas |
39 |
*/ |
6774 |
17 Mar 15 |
nicklas |
40 |
public class SourceItemTransformerChain |
6774 |
17 Mar 15 |
nicklas |
41 |
implements SourceItemTransformer |
6774 |
17 Mar 15 |
nicklas |
42 |
{ |
6774 |
17 Mar 15 |
nicklas |
43 |
|
6774 |
17 Mar 15 |
nicklas |
44 |
private final List<SourceItemTransformer> chain; |
6774 |
17 Mar 15 |
nicklas |
45 |
|
6774 |
17 Mar 15 |
nicklas |
46 |
private final Item source; |
6774 |
17 Mar 15 |
nicklas |
47 |
private final Item target; |
6774 |
17 Mar 15 |
nicklas |
48 |
|
6774 |
17 Mar 15 |
nicklas |
49 |
/** |
6774 |
17 Mar 15 |
nicklas |
Create a new source item transformer chain. The chain |
6774 |
17 Mar 15 |
nicklas |
must contain at least one element and all steps must have |
6774 |
17 Mar 15 |
nicklas |
matching source and target item types. |
6774 |
17 Mar 15 |
nicklas |
@throws IllegalArgumentException If the chain is null or empty or have non-matching |
6774 |
17 Mar 15 |
nicklas |
transformation steps |
6774 |
17 Mar 15 |
nicklas |
55 |
*/ |
6774 |
17 Mar 15 |
nicklas |
56 |
public SourceItemTransformerChain(List<SourceItemTransformer> chain) |
6774 |
17 Mar 15 |
nicklas |
57 |
{ |
6774 |
17 Mar 15 |
nicklas |
58 |
if (chain == null || chain.size() == 0) |
6774 |
17 Mar 15 |
nicklas |
59 |
{ |
6774 |
17 Mar 15 |
nicklas |
60 |
throw new IllegalArgumentException("Chain is null or empty"); |
6774 |
17 Mar 15 |
nicklas |
61 |
} |
6774 |
17 Mar 15 |
nicklas |
62 |
for (int i = 1; i < chain.size(); i++) |
6774 |
17 Mar 15 |
nicklas |
63 |
{ |
6774 |
17 Mar 15 |
nicklas |
64 |
Item prevTarget = chain.get(i-1).getTargetItemType(); |
6774 |
17 Mar 15 |
nicklas |
65 |
Item nextSrc = chain.get(i).getSourceItemType(); |
6774 |
17 Mar 15 |
nicklas |
66 |
if (prevTarget != nextSrc) |
6774 |
17 Mar 15 |
nicklas |
67 |
{ |
6774 |
17 Mar 15 |
nicklas |
68 |
throw new IllegalArgumentException("Source and target mismatch in chain["+i+"]: " + |
6774 |
17 Mar 15 |
nicklas |
69 |
nextSrc.name() + " != " + prevTarget.name()); |
6774 |
17 Mar 15 |
nicklas |
70 |
} |
6774 |
17 Mar 15 |
nicklas |
71 |
} |
6774 |
17 Mar 15 |
nicklas |
72 |
this.chain = chain; |
6774 |
17 Mar 15 |
nicklas |
73 |
this.source = chain.get(0).getSourceItemType(); |
6774 |
17 Mar 15 |
nicklas |
74 |
this.target = chain.get(chain.size()-1).getTargetItemType(); |
6774 |
17 Mar 15 |
nicklas |
75 |
} |
6774 |
17 Mar 15 |
nicklas |
76 |
|
6774 |
17 Mar 15 |
nicklas |
77 |
/** |
6774 |
17 Mar 15 |
nicklas |
Get the source item type of the first step in the chain. |
6774 |
17 Mar 15 |
nicklas |
79 |
*/ |
6774 |
17 Mar 15 |
nicklas |
80 |
@Override |
6774 |
17 Mar 15 |
nicklas |
81 |
public Item getSourceItemType() |
6774 |
17 Mar 15 |
nicklas |
82 |
{ |
6774 |
17 Mar 15 |
nicklas |
83 |
return source; |
6774 |
17 Mar 15 |
nicklas |
84 |
} |
6774 |
17 Mar 15 |
nicklas |
85 |
|
6774 |
17 Mar 15 |
nicklas |
86 |
/** |
6774 |
17 Mar 15 |
nicklas |
Get the target item type of the last step in the chain. |
6774 |
17 Mar 15 |
nicklas |
88 |
*/ |
6774 |
17 Mar 15 |
nicklas |
89 |
@Override |
6774 |
17 Mar 15 |
nicklas |
90 |
public Item getTargetItemType() |
6774 |
17 Mar 15 |
nicklas |
91 |
{ |
6774 |
17 Mar 15 |
nicklas |
92 |
return target; |
6774 |
17 Mar 15 |
nicklas |
93 |
} |
6774 |
17 Mar 15 |
nicklas |
94 |
|
6774 |
17 Mar 15 |
nicklas |
95 |
/** |
6774 |
17 Mar 15 |
nicklas |
Transform the source items into target items by invoking each |
6774 |
17 Mar 15 |
nicklas |
step in the transformer chain. |
6774 |
17 Mar 15 |
nicklas |
98 |
*/ |
6774 |
17 Mar 15 |
nicklas |
99 |
@Override |
6774 |
17 Mar 15 |
nicklas |
100 |
public Set<Integer> transform(TransformContext context, Set<Integer> source) |
6774 |
17 Mar 15 |
nicklas |
101 |
{ |
6774 |
17 Mar 15 |
nicklas |
102 |
for (SourceItemTransformer sit : chain) |
6774 |
17 Mar 15 |
nicklas |
103 |
{ |
6774 |
17 Mar 15 |
nicklas |
104 |
source = sit.transform(context, source); |
6774 |
17 Mar 15 |
nicklas |
105 |
} |
6774 |
17 Mar 15 |
nicklas |
106 |
return source; |
6774 |
17 Mar 15 |
nicklas |
107 |
} |
6774 |
17 Mar 15 |
nicklas |
108 |
|
6796 |
24 Mar 15 |
nicklas |
// To test how filters behave when input source set is large |
6796 |
24 Mar 15 |
nicklas |
110 |
private Set<Integer> appendFakeSources(Set<Integer> existing) |
6796 |
24 Mar 15 |
nicklas |
111 |
{ |
6796 |
24 Mar 15 |
nicklas |
112 |
Set<Integer> fake = new HashSet<Integer>(existing); |
6796 |
24 Mar 15 |
nicklas |
113 |
while (fake.size() < 40000) |
6796 |
24 Mar 15 |
nicklas |
114 |
{ |
6796 |
24 Mar 15 |
nicklas |
115 |
fake.add(-fake.size()); |
6796 |
24 Mar 15 |
nicklas |
116 |
} |
6796 |
24 Mar 15 |
nicklas |
117 |
return fake; |
6796 |
24 Mar 15 |
nicklas |
118 |
} |
6774 |
17 Mar 15 |
nicklas |
119 |
} |