View Javadoc

1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.fatwire.gst.foundation.facade.assetapi.asset;
17  
18  import java.util.Date;
19  import java.util.Iterator;
20  import java.util.LinkedList;
21  import java.util.List;
22  
23  import com.fatwire.assetapi.data.AssetData;
24  import com.fatwire.assetapi.data.AssetId;
25  import com.fatwire.gst.foundation.facade.assetapi.AssetAccessTemplate;
26  import com.fatwire.gst.foundation.facade.assetapi.AssetClosure;
27  
28  /**
29   * Iterable that filters a list of assetids based on a future date. It makes use
30   * of a DateFilterClosure.
31   * 
32   * @author Dolf.Dijkstra
33   * @since Apr 20, 2011
34   * @see DateFilterClosure
35   */
36  public class AssetFilterIterator implements Iterable<AssetId> {
37  
38      final Iterable<AssetId> i;
39  
40      /**
41       * @param aat
42       * @param assetIds
43       */
44      public AssetFilterIterator(final AssetAccessTemplate aat, final Iterable<AssetId> assetIds) {
45          this(aat, new Date(), assetIds);
46  
47      }
48  
49      /**
50       * @param aat
51       * @param date
52       * @param assetIds
53       */
54  
55      public AssetFilterIterator(final AssetAccessTemplate aat, final Date date, final Iterable<AssetId> assetIds) {
56          super();
57  
58          i = date == null ? assetIds : toIterable(aat, date, assetIds);
59      }
60  
61      private Iterable<AssetId> toIterable(final AssetAccessTemplate aat, final Date date,
62              final Iterable<AssetId> assetIds) {
63          final List<AssetId> list = new LinkedList<AssetId>();
64          final AssetClosure target = new AssetClosure() {
65              public boolean work(final AssetData asset) {
66                  list.add(asset.getAssetId());
67                  return true;
68              }
69          };
70          final AssetClosure closure = new DateFilterClosure(date, target);
71  
72          aat.readAsset(assetIds, closure, "startdate", "enddate");
73          return list;
74      }
75  
76      public Iterator<AssetId> iterator() {
77          return i.iterator();
78      }
79  }